-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfit-numpy.py
executable file
·122 lines (98 loc) · 2.95 KB
/
fit-numpy.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
#!/usr/bin/env python3
# fit-numpy.py
# Basic Bayesian fit using MH with regular numpy
import os
import pandas as pd
import numpy as np
import scipy as sp
import scipy.stats
from scipy.optimize import minimize
df = pd.read_parquet(os.path.join("..", "pima.parquet"))
print(df)
n, p = df.shape
print(n, p)
y = pd.get_dummies(df["type"])["Yes"].to_numpy(dtype='float32')
X = df.drop(columns="type").to_numpy()
X = np.hstack((np.ones((n,1)), X))
print(X)
print(y)
def ll(beta):
return np.sum(-np.log(1 + np.exp(-(2*y - 1)*(X.dot(beta)))))
init = np.random.randn(p)*0.1
print(init)
print(ll(init))
print("First, MLE:")
res = minimize(lambda x: -ll(x), init, method='BFGS')
print(res.x)
print(ll(res.x))
print("MAP next:")
def lprior(beta):
return (sp.stats.norm.logpdf(beta[0], loc=0, scale=10) +
np.sum(sp.stats.norm.logpdf(beta[range(1,p)], loc=0, scale=1)))
print(lprior(init))
def lpost(beta):
return ll(beta) + lprior(beta)
print(lpost(init))
res = minimize(lambda x: -lpost(x), init, method='BFGS')
print(res.x)
print(ll(res.x))
print("Next, MH:")
def mhKernel(lpost, rprop, dprop = lambda new, old: 1.):
def kernel(x, ll):
prop = rprop(x)
lp = lpost(prop)
a = lp - ll + dprop(x, prop) - dprop(prop, x)
if (np.log(np.random.rand()) < a):
x = prop
ll = lp
return x, ll
return kernel
def mcmc(init, kernel, thin = 10, iters = 10000, verb = True):
p = len(init)
ll = -np.inf
mat = np.zeros((iters, p))
x = init
if (verb):
print(str(iters) + " iterations")
for i in range(iters):
if (verb):
print(str(i), end=" ", flush=True)
for j in range(thin):
x, ll = kernel(x, ll)
mat[i,:] = x
if (verb):
print("\nDone.", flush=True)
return mat
pre = np.array([10.,1.,1.,1.,1.,1.,5.,1.])
def rprop(beta):
return beta + 0.02*pre*np.random.randn(p)
out = mcmc(res.x, mhKernel(lpost, rprop), thin=1000)
print(out)
#np.savetxt("fit-numpy.tsv", out, delimiter='\t')
odf = pd.DataFrame(out, columns=["b0","b1","b2","b3","b4","b5","b6","b7"])
odf.to_parquet("fit-numpy.parquet")
print("Posterior summaries:")
summ = scipy.stats.describe(out)
print(summ)
print("\nMean: " + str(summ.mean))
print("Variance: " + str(summ.variance))
import matplotlib.pyplot as plt
figure, axis = plt.subplots(4, 2)
for i in range(8):
axis[i // 2, i % 2].plot(range(out.shape[0]), out[:,i])
axis[i // 2, i % 2].set_title(f'Trace plot for the variable {i}')
plt.savefig("np-trace.png")
#plt.show()
figure, axis = plt.subplots(4, 2)
for i in range(8):
axis[i // 2, i % 2].hist(out[:,i], 50)
axis[i // 2, i % 2].set_title(f'Histogram for variable {i}')
plt.savefig("np-hist.png")
#plt.show()
figure, axis = plt.subplots(4, 2)
for i in range(8):
axis[i // 2, i % 2].acorr(out[:,i] - np.mean(out[:,i]), maxlags=100)
axis[i // 2, i % 2].set_title(f'ACF for variable {i}')
plt.savefig("np-acf.png")
#plt.show()
# eof