Problem: Monte Carlo Estimate of $\int_0^1 \frac{1}{1+x} \mathrm{d}x$

\begin{equation}
\begin{split}
\int_0^1 \frac{1}{1+x} \mathrm{d}x & = \ln(x+1)+C |_0^1 \\
 & = \ln(2) - \ln(1) \\
 & = 0.693147 \\
\end{split}
\end{equation}

Source: https://en.wikipedia.org/wiki/Antithetic_variates


```python
# Calculate the truth
import math
truth = math.log(2)-math.log(1)
```


```python
# Classical Estimate
import numpy as np
from sklearn.metrics import mean_squared_error

def f(x): return 1.0/(1+x)
def sampling(n): return np.random.uniform(0,1,n)

n = 1500
rand = sampling(2*n)
samples = map(f, rand)

print "==== Classical Estimate ===="
print "Estimate: ", np.mean(samples)
print "Variance: ", np.var(samples)
```

    ==== Classical Estimate ====
    Estimate:  0.6935116995264258
    Variance:  0.019320749240938653



```python
# Antithetic Variates
import numpy as np
from sklearn.metrics import mean_squared_error

def f(x): return 1.0/(1+x)
def sampling(n): return np.random.uniform(0,1,n)

n = 1500
rand = sampling(n)
rand_anti = 1-rand
samples = np.add(map(f, rand), map(f, rand_anti))/2.0

print "==== Antithetic Variates ===="
print "Estimate: ", np.mean(samples)
print "Variance: ", np.var(samples)
```

    ==== Antithetic Variates ====
    Estimate:  0.6925277753075945
    Variance:  0.0005805944133518291


4e9e371b4cfb98fc4ed8103523b81adce975cef1
ipynb
Jupyter Notebook
011519_antithetic_variates.ipynb
kinsumliu/notes
3601c50a11966bed84c5d792778f3b103ba801d2
MIT
011519_antithetic_variates.ipynb
kinsumliu/notes
3601c50a11966bed84c5d792778f3b103ba801d2
MIT
011519_antithetic_variates.ipynb
kinsumliu/notes
3601c50a11966bed84c5d792778f3b103ba801d2
MIT
Qwen/Qwen-72B
1. YES
2. YES