-
Notifications
You must be signed in to change notification settings - Fork 10
/
RejectSamplingMC.py
74 lines (38 loc) · 1.36 KB
/
RejectSamplingMC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.stats as stats
plt.style.use('ggplot')
x = np.linspace(-5, 5)
def f(x):
return np.exp(0.4*(x-0.4)**2-0.08*x**4)
df = 10
upper = f(-1.75233) # maximum of f(x), found from differentiation.
plt.figure(figsize=(12,4))
plt.subplot(121)
plt.plot(x, f(x))
plt.axhline(upper, color='grey')
px = -1.0
plt.arrow(px,0,0,f(px)-0.01, linewidth=1,
head_width=0.2, head_length=0.01, fc='g', ec='g')
plt.arrow(px,upper,0,-(upper-f(px)-0.01), linewidth=1,
head_width=0.3, head_length=0.01, fc='r', ec='r')
plt.text(px+.25, 2, 'Reject', fontsize=16)
plt.text(px+.25, 0.5, 'Accept', fontsize=16)
plt.axis([-5,5,0,4])
plt.title('Rejection sampling concepts', fontsize=20)
plt.subplot(122)
n = 100000
# generate from sampling distribution randomly and uniformly.
u = np.random.uniform(-5, 5, n)
r = np.random.uniform(0, upper, n)
v = u[r < f(u)] # accepting procedure is in it.
plt.plot(x, f(x), linewidth=2)
# Plot scaled histogram
factor = 7.85218 # Normalizing constant. Integrated value of f(x) from -5 to 5.
hist, bin_edges = np.histogram(v, bins=100, normed=True)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.
plt.step(bin_centers, factor*hist, linewidth=2)
plt.axis([-5,5,0,4])
plt.title('Histogram of accepted samples', fontsize=20);
plt.show()