-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
158 lines (128 loc) · 4.75 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import random
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.stats import t
def log_pi_fun(mu, n, y_bar):
"""
For computational reasons we take the log-posterior of the target function g, log(g(mu)), as follows
:param y_hat:
:param n:
:param mu:
:return:
"""
log_g_mu = n*(y_bar*mu - .5*mu**2)-np.log(1+mu**2)
return log_g_mu
def metropolis_hastings(n, y_bar, n_iter, mu_init, cand_std):
"""
Metropolis hastings
:param n: sample size
:param y_bar: the sample mean of y the data.
:param n_iter: how many iterations
:param mu_init: initial valur for mu
:param cand_std: std deviation for the candidate generating normal distribution
:return:
"""
# Selecting an initial values and initializing the Random walk Metropolis sampling
mu_out = [] #numeric vector of mu accepted values
accept = 0 #how many times we accepted
mu_now = mu_init #updating our value for mu, intitalized as the mu_init, our param
lg_now = log_pi_fun(mu_now, n, y_bar) #evaluation of lg function
# MC iterations under accepting ratio
for i in range(0, n_iter):
mu_cand = np.random.normal(mu_now, cand_std, 1) #draw a candidate from the proposal distribution, normal with mean mu_now and std =cand_std
#by last candidate assumption, a symmetric one, we now turn into a Metropolis algorithm
lg_cand = log_pi_fun(mu_cand, n, y_bar) #the numerator of the acceptance ration, in a logarithm scale
# Acceptance ratio
lalpha = lg_cand - lg_now #this is the log ratio, for posterior on the candidate minus the current value lg_now
alpha = np.exp(lalpha[0]) #turned it back into a non log-scale
u = random.random()
# Acceptance condition to update the candidate
if u<alpha: #that gives us an event that happens with probability alpha
mu_now_ = mu_cand
try:
mu_now = mu_now_[0]
except:
mu_now = mu_now_
accept+=1
lg_now = lg_cand #updated the lg_now too as the one evaluated in the candidate
mu_out.append(mu_now)
#returning the mean estimated together with acceptance ratio
return [mu_out, accept/n_iter]
def trace_plot(samples, description):
"""
trace plot on MC
:param samples:
:param description:
:return:
"""
# Plot the trace plot
plt.figure(figsize=(10, 5))
plt.plot(samples)
plt.title(f"Trace Plot on {description}")
plt.xlabel("Iteration")
plt.ylabel("Value")
# Save the plot as a JPG image
plt.savefig("trace_plot.jpg", format="jpg", dpi=300)
def plot_t_density(df=1, lty='--', add=False):
"""
Plot the density of the t-distribution with specified degrees of freedom.
Parameters:
- df: degrees of freedom (default is 1).
- lty: line style (default is '--', which corresponds to lty=2 in R).
- add: whether to add to the existing plot or create a new one (default is False).
"""
x = np.linspace(-1, 3, 400)
y = t.pdf(x, df)
if not add:
plt.figure()
plt.plot(x, y, linestyle=lty, color='blue', label='Prior Distribution')
plt.legend()
plt.xlabel("Value")
plt.ylabel("Density")
def density_estimate_plot(samples, description, x_range, prior_mean):
"""
:param samples: list of posterior samples
:param description:
:param x_range:
:param prior_mean:
:return:
"""
plt.figure(figsize=(10, 5))
# Posterior
data = pd.DataFrame(samples, columns=['sampling'])
data.sampling.plot.density(color='green', label='Posterior Distribution')
plt.legend()
plt.title(description)
plt.xlim(x_range)
# Prior mean
plt.axvline(prior_mean, color='red', linestyle='-', label='y_bar on prior')
plt.xlabel("Value")
plt.ylabel("Density")
# Save the plot as a JPG image
plot_t_density(df=1, lty='--', add=True)
plt.title(f'Prior and Posterior')
filename = "posterior_density_plot.jpg"
plt.savefig(filename, format="jpg", dpi=300)
plt.show()
# set up
def main(y, mu, std):
"""
:param y:
:param mu:
:param std:
:return:
"""
random.seed(42)
ybar = np.mean(y)
n = len(y)
posterior_samples = metropolis_hastings(n, ybar, 1000, mu, std)
samples = [arr for arr in posterior_samples[0]]
trace_plot(samples, f"Mean {ybar} and Std {std} -- Acceptance ratio {posterior_samples[-1]}")
density_estimate_plot(samples, "Density estimate on posterior distribution ", (-1,3), ybar)
if __name__ == '__main__':
# Example
y = [1.2, 1.4, -.5, .3, .9, 2.3, 1, .1, 1.3, 1.9]
std = np.std(y)
mu = 30 # crazy intial value to test how many iterations are needed to get close to the true mean
main(y, mu, std)