Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/deprecated/scripts/antithetic_sampling.py
1192 views
1
# demo of antithetic sampling
2
# https://en.wikipedia.org/wiki/Antithetic_variates
3
4
5
import superimport
6
7
import numpy as np
8
np.random.seed(0)
9
10
N = 750 #1500
11
u1 = np.random.uniform(size=N)
12
u2 = np.random.uniform(size=N)
13
u = np.concatenate((u1,u2))
14
f = 1 / (1+u)
15
mu_naive = np.mean(f)
16
se_naive = np.sqrt(np.var(f)/(2*N))
17
print('naive {:0.4f}, se {:0.4f}'.format(mu_naive, se_naive))
18
19
# antithetic version
20
uprime = 1-u1
21
f1 = 1 / (1+u1)
22
fprime = 1 / (1+uprime)
23
f = (f1 + fprime) / 2.0 # paired samples!
24
mu_anti = np.mean(f)
25
se_anti = np.sqrt(np.var(f)/(2*N))
26
print('anti {:0.4f}, se {:0.4f}'.format(mu_anti, se_anti))
27