Created
June 1, 2017 08:22
-
-
Save kgourgou/f62a5ee84209bc989af8f26c39db05e0 to your computer and use it in GitHub Desktop.
Quick example demonstrating the importance sampling idea.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
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