You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Notice that we specified `size=2`: we are modeling both $\\tau$s as a single PyMC variable. Note that is does not induce a necessary relationship between the two $\\tau$s, it is simply for succinctness.\n",
"PyMC has an MCMC class, `MCMC` in the main namespace of PyMC, that implements the MCMC exploring algorithm. We initialize it by passing in a `Model` instance:\n",
487
487
"\n",
488
-
" mcmc = mc.MCMC( model )\n",
488
+
" mcmc = pm.MCMC( model )\n",
489
489
"\n",
490
490
"The method for asking the `MCMC` to explore the space is `sample( iterations )`, where `iterations` is the number of steps you wish the algorithm to perform. We try 50000 steps below:"
491
491
]
@@ -494,7 +494,7 @@
494
494
"cell_type": "code",
495
495
"collapsed": false,
496
496
"input": [
497
-
"mcmc = mc.MCMC(model)\n",
497
+
"mcmc = pm.MCMC(model)\n",
498
498
"mcmc.sample(50000)"
499
499
],
500
500
"language": "python",
@@ -574,7 +574,7 @@
574
574
"3. The traces appear as a random \"walk\" around the space, that is, the paths exhibit correlation with previous positions. This is both good and bad. We will always have correlation between current positions and the previous positions, but too much of it means we are not exploring the space well. This will be detailed in the Diagnostics section later in this chapter.\n",
575
575
"\n",
576
576
"\n",
577
-
"To achieve further convergence, we will perform more MCMC steps. Starting the MCMC again after it has already been called does not mean starting the entire algorithm over. In the pseudo-code algorithm of MCMC above, the only position that matters is the current position (new positions are investigated near the current position), implicitly stored in PyMC variables' `value` attribute. Thus it is fine to halt an MCMC algorithm and inspect its progress, with the intention of starting it up again later. The `value' attributes are not overwritten. \n",
577
+
"To achieve further convergence, we will perform more MCMC steps. Starting the MCMC again after it has already been called does not mean starting the entire algorithm over. In the pseudo-code algorithm of MCMC above, the only position that matters is the current position (new positions are investigated near the current position), implicitly stored in PyMC variables' `value` attribute. Thus it is fine to halt an MCMC algorithm and inspect its progress, with the intention of starting it up again later. The `value` attributes are not overwritten. \n",
578
578
"\n",
579
579
"We will sample the MCMC one hundred thousand more times and visualize the progress below:"
580
580
]
@@ -807,12 +807,12 @@
807
807
"cell_type": "code",
808
808
"collapsed": false,
809
809
"input": [
810
-
"import pymc as mc\n",
810
+
"import pymc as pm\n",
811
811
"\n",
812
-
"x = mc.Normal(\"x\", 4, 10)\n",
813
-
"y = mc.Lambda(\"y\", lambda x=x: 10 - x, trace=True)\n",
812
+
"x = pm.Normal(\"x\", 4, 10)\n",
813
+
"y = pm.Lambda(\"y\", lambda x=x: 10 - x, trace=True)\n",
814
814
"\n",
815
-
"ex_mcmc = mc.MCMC(mc.Model([x, y]))\n",
815
+
"ex_mcmc = pm.MCMC(pm.Model([x, y]))\n",
816
816
"ex_mcmc.sample(500)\n",
817
817
"\n",
818
818
"plt.plot(ex_mcmc.trace(\"x\")[:])\n",
@@ -930,7 +930,7 @@
930
930
"\n",
931
931
"Of course, we do not know where the MAP is. PyMC provides an object that will approximate, if not find, the MAP location. In the PyMC main namespace is the `MAP` object that accepts a PyMC `Model` instance. Calling `.fit()` from the `MAP` instance sets the variables in the model to their MAP values.\n",
932
932
"\n",
933
-
" map_ = mc.MAP( model )\n",
933
+
" map_ = pm.MAP( model )\n",
934
934
" map_.fit()\n",
935
935
"\n",
936
936
"The `MAP.fit()` methods has the flexibility of allowing the user to choose which optimization algorithm to use (after all, this is a optimization problem: we are looking for the values that maximize our landscape), as not all optimization algorithms are created equal. The default optimization algorithm in the call to `fit` is scipy's `fmin` algorithm (which attempts to minimize the *negative of the landscape*). An alternative algorithm that is available is Powell's Method, a favourite of PyMC blogger [Abraham Flaxman](http://healthyalgorithms.com/) [1], by calling `fit(method='fmin_powell')`. From my experience, I use the default, but if my convergence is slow or not guaranteed, I experiment with Powell's method. \n",
@@ -943,12 +943,12 @@
943
943
"\n",
944
944
"It is still a good idea to provide a burn-in period, even if we are using `MAP` prior to calling `MCMC.sample`, just to be safe. We can have PyMC automatically discard the first $n$ samples by specifying the `burn` parameter in the call to `sample`. As one does not know when the chain has fully converged, I like to assign the first *half* of my samples to be discarded, sometimes up to 90% of my samples for longer runs. To continue the clustering example from above, my new code would look something like:\n",
945
945
"\n",
946
-
" model = mc.Model( [p, assignment, taus, centers ] )\n",
946
+
" model = pm.Model( [p, assignment, taus, centers ] )\n",
947
947
"\n",
948
-
" map_ = mc.MAP( model )\n",
948
+
" map_ = pm.MAP( model )\n",
949
949
" map_.fit() #stores the fitted variables' values in foo.value\n",
950
950
"\n",
951
-
" mcmc = mc.MCMC( model )\n",
951
+
" mcmc = pm.MCMC( model )\n",
952
952
" mcmc.sample( 100000, 50000 )\n"
953
953
]
954
954
},
@@ -978,12 +978,12 @@
978
978
"input": [
979
979
"figsize(12.5, 4)\n",
980
980
"\n",
981
-
"import pymc as mc\n",
982
-
"x_t = mc.rnormal(0, 1, 200)\n",
981
+
"import pymc as pm\n",
982
+
"x_t = pm.rnormal(0, 1, 200)\n",
983
983
"x_t[0] = 0\n",
984
984
"y_t = np.zeros(200)\n",
985
985
"for i in range(1, 200):\n",
986
-
" y_t[i] = mc.rnormal(y_t[i - 1], 1)\n",
986
+
" y_t[i] = pm.rnormal(y_t[i - 1], 1)\n",
987
987
"\n",
988
988
"plt.plot(y_t, label=\"$y_t$\", lw=3)\n",
989
989
"plt.plot(x_t, label=\"$x_t$\", lw=3)\n",
@@ -1055,7 +1055,7 @@
1055
1055
"\n",
1056
1056
"A chain that is [Isn't meandering exploring?] exploring the space well will exhibit very high autocorrelation. Visually, if the trace seems to meander like a river, and not settle down, the chain will have high autocorrelation.\n",
1057
1057
"\n",
1058
-
"This does not imply that a converged MCMC has low autocorrelation. Hence low autocorrelation is not necessary for convergence, but it is sufficient. PyMC has an built-in autocorrelation plotting function in the `Matplot` module. "
1058
+
"This does not imply that a converged MCMC has low autocorrelation. Hence low autocorrelation is not necessary for convergence, but it is sufficient. PyMC has a built-in autocorrelation plotting function in the `Matplot` module. "
1059
1059
]
1060
1060
},
1061
1061
{
@@ -1107,7 +1107,7 @@
1107
1107
"\n",
1108
1108
"What is a good amount of thinning? The returned samples will always exhibit some autocorrelation, regardless of how much thinning is done. So long as the autocorrelation tends to zero, you are probably ok. Typically thinning of more than 10 is not necessary.\n",
1109
1109
"\n",
1110
-
"PyMC exposes a `thinning` parameter in the call the `sample`, for example: `sample( 10000, burn = 5000, thinning = 5)`. "
1110
+
"PyMC exposes a `thinning` parameter in the call to `sample`, for example: `sample( 10000, burn = 5000, thinning = 5)`. "
1111
1111
]
1112
1112
},
1113
1113
{
@@ -1198,9 +1198,9 @@
1198
1198
"\n",
1199
1199
"### Intelligent starting values\n",
1200
1200
"\n",
1201
-
"It would be great to start the MCMC algorithm off near the posterior distribution, so that it will take little time to start sampling correctly. We can aid the algorithm by telling where we *think* the posterior distribution will be by specifying the `value` parameter in the `Stochastic` variable creation. In many cases we can produce a reasonable guess for the parameter. For example, if we have data from a Normal distribution, and we wish to estimate the $\\mu$ parameter, then a good starting value would the *mean* of the data. \n",
1201
+
"It would be great to start the MCMC algorithm off near the posterior distribution, so that it will take little time to start sampling correctly. We can aid the algorithm by telling where we *think* the posterior distribution will be by specifying the `value` parameter in the `Stochastic` variable creation. In many cases we can produce a reasonable guess for the parameter. For example, if we have data from a Normal distribution, and we wish to estimate the $\\mu$ parameter, then a good starting value would be the *mean* of the data. \n",
1202
1202
"\n",
1203
-
" mu = mc.Uniform( \"mu\", 0, 100, value = data.mean() )\n",
1203
+
" mu = pm.Uniform( \"mu\", 0, 100, value = data.mean() )\n",
1204
1204
"\n",
1205
1205
"For most parameters in models, there is a frequentist estimate of it. These estimates are a good starting value for our MCMC algorithms. Of course, this is not always possible for some variables, but including as many appropriate initial values is always a good idea. Even if your guesses are wrong, the MCMC will still converge to the proper distribution, so there is little to lose.\n",
0 commit comments