|
| 1 | +import numpy as np |
1 | 2 | import pymc as pm
|
2 | 3 |
|
3 | 4 |
|
4 |
| -data = np.loadtxt( "../../Chapter3_MCMC/data/mixture_data.csv", delimiter="," ) |
| 5 | +data = np.loadtxt("../../Chapter3_MCMC/data/mixture_data.csv", delimiter=",") |
5 | 6 |
|
6 | 7 |
|
7 |
| -p = pm.Uniform( "p", 0, 1) |
| 8 | +p = pm.Uniform("p", 0, 1) |
8 | 9 |
|
9 |
| -assignment = pm.Categorical("assignment", [p, 1-p], size = data.shape[0] ) |
| 10 | +assignment = pm.Categorical("assignment", [p, 1 - p], size=data.shape[0]) |
10 | 11 |
|
11 |
| -taus = 1.0/mc.Uniform( "stds", 0, 100, size= 2)**2 #notice the size! |
12 |
| -centers = pm.Normal( "centers", [150, 150], [0.001, 0.001], size =2 ) |
| 12 | +taus = 1.0 / pm.Uniform("stds", 0, 100, size=2) ** 2 # notice the size! |
| 13 | +centers = pm.Normal("centers", [150, 150], [0.001, 0.001], size=2) |
13 | 14 |
|
14 | 15 | """
|
15 | 16 | The below deterministic functions map a assingment, in this case 0 or 1,
|
16 | 17 | to a set of parameters, located in the (1,2) arrays `taus` and `centers.`
|
17 | 18 | """
|
18 | 19 |
|
19 |
| -@pm.deterministic |
20 |
| -def center_i( assignment = assignment, centers = centers ): |
21 |
| - return centers[ assignment] |
22 | 20 |
|
23 | 21 | @pm.deterministic
|
24 |
| -def tau_i( assignment = assignment, taus = taus ): |
25 |
| - return taus[ assignment] |
| 22 | +def center_i(assignment=assignment, centers=centers): |
| 23 | + return centers[assignment] |
26 | 24 |
|
27 |
| -#and to combine it with the observations: |
28 |
| -observations = pm.Normal( "obs", center_i, tau_i, value = data, observed = True ) |
29 | 25 |
|
30 |
| -#below we create a model class |
31 |
| -model = pm.Model( [p, assignment, taus, centers ] ) |
| 26 | +@pm.deterministic |
| 27 | +def tau_i(assignment=assignment, taus=taus): |
| 28 | + return taus[assignment] |
| 29 | + |
| 30 | +# and to combine it with the observations: |
| 31 | +observations = pm.Normal("obs", center_i, tau_i, |
| 32 | + value=data, observed=True) |
| 33 | + |
| 34 | +# below we create a model class |
| 35 | +model = pm.Model([p, assignment, taus, centers]) |
32 | 36 |
|
33 | 37 |
|
34 |
| -map_ = pm.MAP( model ) |
| 38 | +map_ = pm.MAP(model) |
35 | 39 | map_.fit()
|
36 |
| -mcmc = pm.MCMC( model ) |
37 |
| -mcmc.sample( 100000, 50000 ) |
| 40 | +mcmc = pm.MCMC(model) |
| 41 | +mcmc.sample(100000, 50000) |
0 commit comments