forked from jasonmhite/PyMC-DRAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_model.py
68 lines (50 loc) · 1.11 KB
/
test_model.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
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
import seaborn as sb
import numpy as np
import pymc as P
from pymc_dram import DRAM
NS = int(1e5)
K = 20.5
C0 = 1.5
CMIN, CMAX = 0.5, 2.5
SIGMA = 0.1
NT = 500
TMIN, TMAX = 0, 5
T = np.linspace(TMIN, TMAX, NT)
def model(C):
return 2. * np.exp(-C * T / 2.) * np.cos(np.sqrt(K - (C ** 2) / 4.) * T)
nominal = model(C0)
data = nominal + np.random.normal(0, SIGMA, len(T))
plt.figure()
plt.plot(nominal, label="Nominal response")
plt.plot(data, '.', label="Simulated data")
plt.legend()
plt.show()
def model_factory():
C = P.Uniform("C", value=C0, lower=CMIN, upper=CMAX)
@P.deterministic(plot=False)
def response(C=C):
return model(C)
Y = P.MvNormalCov(
'Y',
response,
(SIGMA ** 2) * np.eye(NT),
observed=True,
plot=False,
value=data,
)
return locals()
mvars = model_factory()
M = P.MCMC(mvars)
M.use_step_method(
# P.AdaptiveMetropolis,
DRAM,
[mvars["C"]],
# verbose=3,
)
M.sample(NS, burn=NS / 2)
# plt.figure()
P.Matplot.plot(M)
plt.show()