-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproject0.py
83 lines (60 loc) · 2.7 KB
/
project0.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
import numpy
from scipy.stats import laplace, norm, t
import math
import numpy as np
from scipy.special import logsumexp
VARIANCE = 2.0
normal_scale = math.sqrt(VARIANCE)
student_t_df = (2 * VARIANCE) / (VARIANCE - 1)
laplace_scale = VARIANCE / 2
HYPOTHESIS_SPACE = [norm(loc=0.0, scale=math.sqrt(VARIANCE)),
laplace(loc=0.0, scale=laplace_scale),
t(df=student_t_df)]
PRIOR_PROBS = np.array([0.35, 0.25, 0.4])
def generate_sample(n_samples, seed=None):
""" data generating process of the Bayesian model """
random_state = np.random.RandomState(seed)
hypothesis_idx = np.random.choice(3, p=PRIOR_PROBS)
dist = HYPOTHESIS_SPACE[hypothesis_idx]
return dist.rvs(n_samples, random_state=random_state)
def log_posterior_probs(x):
"""
Computes the log posterior probabilities for the three hypotheses, given the data x
Args:
x (np.ndarray): one-dimensional numpy array containing the training data
Returns:
log_posterior_probs (np.ndarray): a numpy array of size 3, containing the Bayesian log-posterior probabilities
corresponding to the three hypotheses
"""
assert x.ndim == 1
denom_norm = np.sum(np.log(norm.pdf(x, loc=0.0, scale=math.sqrt(VARIANCE)))) + np.log(PRIOR_PROBS[0])
denom_laplace = np.sum(np.log(laplace.pdf(x, loc=0.0, scale=laplace_scale))) + np.log(PRIOR_PROBS[1])
denom_t = np.sum(np.log(t.pdf(x, df=student_t_df))) + np.log(PRIOR_PROBS[2])
log_evidence = logsumexp(a=[denom_norm, denom_laplace, denom_t])
log_p_norm = denom_norm - log_evidence
log_p_laplace= denom_laplace - log_evidence
log_p_t = denom_t - log_evidence
log_p = np.array([log_p_norm, log_p_laplace, log_p_t])
assert log_p.shape == (3,)
return log_p
def posterior_probs(x):
return np.exp(log_posterior_probs(x))
def main():
""" Sample from Laplace dist """
dist = HYPOTHESIS_SPACE[1]
x = dist.rvs(1000, random_state=22)
print("Posterior probs for 1 sample from Laplacian")
p = posterior_probs(x[:1])
print("Normal: %.4f , Laplace: %.4f, Student-t: %.4f\n" % tuple(p))
print("Posterior probs for 100 samples from Laplacian")
p = posterior_probs(x[:100])
print("Normal: %.4f , Laplace: %.4f, Student-t: %.4f\n" % tuple(p))
print("Posterior probs for 1000 samples from Laplacian")
p = posterior_probs(x[:1000])
print("Normal: %.4f , Laplace: %.4f, Student-t: %.4f\n" % tuple(p))
print("Posterior for 100 samples from the Bayesian data generating process")
x = generate_sample(n_samples=100)
p = posterior_probs(x)
print("Normal: %.4f , Laplace: %.4f, Student-t: %.4f\n" % tuple(p))
if __name__ == "__main__":
main()