Skip to content

Instantly share code, notes, and snippets.

@kgourgou
Created June 1, 2017 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgourgou/f62a5ee84209bc989af8f26c39db05e0 to your computer and use it in GitHub Desktop.
Save kgourgou/f62a5ee84209bc989af8f26c39db05e0 to your computer and use it in GitHub Desktop.
Quick example demonstrating the importance sampling idea.
#
import scipy as sc
from scipy.stats import norm
N=100
M = 1000
nsamp = sc.zeros(M)
for i in range(M):
data=sc.randn(N)*0.1
nsamp[i] = sc.mean(data<-0.5)
print "Direct estimation:",sc.mean(nsamp)
# Importance sampling
p = lambda x: norm.pdf(x,scale=0.1) # original distribution
q = lambda x: norm.pdf(x,loc=-0.5) # proposal distribution --- focuses on the regime of interest!
# new quantity of interest
f = lambda x: (x<-0.5)*p(x)/q(x)
for i in range(M):
data = -0.5+sc.randn(N)
nsamp[i] = sc.mean(f(data))
print "Importance sampling:", sc.mean(nsamp)
print "Exact value:", norm.cdf(-0.5,scale=0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment