Skip to content

Commit fc6a48d

Browse files
Merge pull request CamDavidsonPilon#58 from pablooliveira/Chapter3-fixtypos
Fix typos in Chapter 3
2 parents 79c5274 + e5a13aa commit fc6a48d

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Chapter3_MCMC/IntroMCMC.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"cell_type": "markdown",
2424
"metadata": {},
2525
"source": [
26-
"The previous two chapters hid the inner-mechanics of PyMC, and more generally Monte Carlo Markov Chains (MCMC), from the reader. The reason for including this chapter is three-fold. The first is that any book on Bayesian inference must discuss MCMC. I cannot fight this. Blame the statisticians. Secondly, knowing the process of MCMC gives you insight into whether your algorithm has converged. (Converged to what? We will get to that) Thirdly, we'll understand *why* we are returned thousands of samples from the positerior as a solution, which at first thought can be odd. "
26+
"The previous two chapters hid the inner-mechanics of PyMC, and more generally Monte Carlo Markov Chains (MCMC), from the reader. The reason for including this chapter is three-fold. The first is that any book on Bayesian inference must discuss MCMC. I cannot fight this. Blame the statisticians. Secondly, knowing the process of MCMC gives you insight into whether your algorithm has converged. (Converged to what? We will get to that) Thirdly, we'll understand *why* we are returned thousands of samples from the posterior as a solution, which at first thought can be odd. "
2727
]
2828
},
2929
{
@@ -32,7 +32,7 @@
3232
"source": [
3333
"### The Bayesian landscape\n",
3434
"\n",
35-
"When we setup a Bayesian inference problem with $N$ unknowns, we are implicitly creating an $N$ dimensional space for the prior distributions to exist in. Associated with the space is an additional dimension, which we can describe as the *surface*, or *curve*, that sits ontop of the space, that reflects the *prior probability* of a particular point. The surface on the space is defined by our prior distributions. For example, if we have two unknowns $p_1$ and $p_2$, and priors for both are $\\text{Uniform}(0,5)$, the space created is a square of length 5 and the surface is a flat plane that sits ontop of the square (representing that every point is equally likely). "
35+
"When we setup a Bayesian inference problem with $N$ unknowns, we are implicitly creating an $N$ dimensional space for the prior distributions to exist in. Associated with the space is an additional dimension, which we can describe as the *surface*, or *curve*, that sits on top of the space, that reflects the *prior probability* of a particular point. The surface on the space is defined by our prior distributions. For example, if we have two unknowns $p_1$ and $p_2$, and priors for both are $\\text{Uniform}(0,5)$, the space created is a square of length 5 and the surface is a flat plane that sits on top of the square (representing that every point is equally likely). "
3636
]
3737
},
3838
{
@@ -80,7 +80,7 @@
8080
"cell_type": "markdown",
8181
"metadata": {},
8282
"source": [
83-
"Alternatively, if the two priors are $\\text{Exp}(3)$ and $\\text{Exp}(10)$, then the space is all postive numbers on the 2-D plane, and the surface induced by the priors looks like a water fall that starts at the point (0,0) and flows over the positive numbers. \n",
83+
"Alternatively, if the two priors are $\\text{Exp}(3)$ and $\\text{Exp}(10)$, then the space is all positive numbers on the 2-D plane, and the surface induced by the priors looks like a water fall that starts at the point (0,0) and flows over the positive numbers. \n",
8484
"\n",
8585
"The plots below visualize this. The more dark red the color, the more prior probability is assigned to that location. Conversely, areas with darker blue represent that our priors assign very low probability to that location. "
8686
]
@@ -374,7 +374,7 @@
374374
"\n",
375375
" taus = 1.0/mc.Uniform( \"stds\", 0, 100, size= 2)**2 \n",
376376
"\n",
377-
"Notice that we specified `size=2`: we are modelling 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",
377+
"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",
378378
"\n",
379379
"We also need to specify priors on the centers of the clusters. The centers are really the $\\mu$ parameters in this Normal distributions. Their priors can be modeled by a Normal distribution. Looking at the data, I have an idea where the two centers might be — I would guess somewhere around 120 and 190 respectively, though I am not very confident in these eyeballed estimates. Hence I will set $\\mu_0 = 120, \\mu_1 = 190$ and $\\sigma_{0,1} = 10$ (recall we enter the $\\tau$ parameter, so enter $1/\\sigma^2 = 0.01$ in the PyMC variable.)"
380380
]
@@ -388,7 +388,7 @@
388388
"centers = mc.Normal( \"centers\", [120, 190], [0.01, 0.01], size =2 )\n",
389389
"\n",
390390
"\"\"\"\n",
391-
"The below determinsitic functions map a assingment, in this case 0 or 1,\n",
391+
"The below deterministic functions map a assignment, in this case 0 or 1,\n",
392392
"to a set of parameters, located in the (1,2) arrays `taus` and `centers.`\n",
393393
"\"\"\"\n",
394394
"\n",
@@ -834,9 +834,9 @@
834834
"#### Returning to Clustering: Prediction\n",
835835
"The above clustering can be generalized to $k$ clusters. Choosing $k=2$ allowed us to visualize the MCMC better, and examine some very interesting plots. \n",
836836
"\n",
837-
"What about prediction? Suppose we observe a new data point, say $x = 175$, and we wish to label it to a cluster. It is foolish to simply assign it to the *closer* cluster center, as this ignores the standard deviation of the clusters, and we have seen from the plots aboves that this consideration is very important. More formally: we are interested in the *probability* (as we cannot be certain about labels) of assigning $x=175$ to cluster 1. Denote the assignment of $x$ as $L_x$, which is equal to 0 or 1, and we are interested in $P(L_x = 1 \\;|\\; x = 175 )$. \n",
837+
"What about prediction? Suppose we observe a new data point, say $x = 175$, and we wish to label it to a cluster. It is foolish to simply assign it to the *closer* cluster center, as this ignores the standard deviation of the clusters, and we have seen from the plots above that this consideration is very important. More formally: we are interested in the *probability* (as we cannot be certain about labels) of assigning $x=175$ to cluster 1. Denote the assignment of $x$ as $L_x$, which is equal to 0 or 1, and we are interested in $P(L_x = 1 \\;|\\; x = 175 )$. \n",
838838
"\n",
839-
"A naive method to compute this is to re-reun the above MCMC with the additional data point appended. The disadvantage with this method is that it will be slow to infer for each novel data point. Alternatively, we can try a *less precise*, but much quicker method. \n",
839+
"A naive method to compute this is to re-run the above MCMC with the additional data point appended. The disadvantage with this method is that it will be slow to infer for each novel data point. Alternatively, we can try a *less precise*, but much quicker method. \n",
840840
"\n",
841841
"We will use Bayes Theorem for this. If you recall, Bayes Theorem looks like:\n",
842842
"\n",
@@ -909,15 +909,15 @@
909909
" map_ = mc.MAP( model )\n",
910910
" map.fit()\n",
911911
"\n",
912-
"The `MAP.fit()` methods has the flexibility of allowing the user to choose which opimization 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 attemps 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",
912+
"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",
913913
"\n",
914914
"The MAP can also be used as a solution to the inference problem, as mathematically it is the *most likely* value for the unknowns. But as mentioned earlier in this chapter, this location ignores the uncertainty and doesn't return a distribution.\n",
915915
"\n",
916916
"Typically, it is always a good idea, and rarely a bad idea, to prepend your call to `mcmc` with a call to `MAP(model).fit()`. The intermediate call to `fit` is hardly computationally intensive, and will save you time later due to a shorter burn-in period. \n",
917917
"\n",
918918
"#### Speaking of the burn-in period\n",
919919
"\n",
920-
"Tt 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",
920+
"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",
921921
"\n",
922922
" model = mc.Model( [p, assignment, taus, centers ] )\n",
923923
"\n",
@@ -1079,7 +1079,7 @@
10791079
"cell_type": "markdown",
10801080
"metadata": {},
10811081
"source": [
1082-
"With more thinning, the autocorrelation drops quicker. There is a tradeoff though: higher thinning requires more MCMC iterations to achieve the same number of retured samples. For example, 10 000 samples unthinned is 100 000 with a thinning of 10 (though the latter has less autocorrelation). \n",
1082+
"With more thinning, the autocorrelation drops quicker. There is a tradeoff though: higher thinning requires more MCMC iterations to achieve the same number of returned samples. For example, 10 000 samples unthinned is 100 000 with a thinning of 10 (though the latter has less autocorrelation). \n",
10831083
"\n",
10841084
"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",
10851085
"\n",
@@ -1174,13 +1174,13 @@
11741174
"\n",
11751175
"### Intelligent starting values\n",
11761176
"\n",
1177-
"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. Often we posses guess about this anyways. For example, if we have data from a Normal distribution, and we wish to estimate the $\\mu$ paramter, then a good starting value would the *mean* of the data. \n",
1177+
"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. Often we posses guess about this anyways. 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",
11781178
"\n",
11791179
" mu = mc.Uniform( \"mu\", 0, 100, value = data.mean() )\n",
11801180
"\n",
1181-
"For most parameters in models, there is a frequentist esimate of it. These estimates are a good starting value for our MCMC algoithms. 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",
1181+
"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",
11821182
"\n",
1183-
"This is what using `MAP` tries to do, by giving good initial values to the MCMC. So why bother specifing user-defined values? Well, even giving `MAP` good values will help it find the maximum a-posterior. \n",
1183+
"This is what using `MAP` tries to do, by giving good initial values to the MCMC. So why bother specifying user-defined values? Well, even giving `MAP` good values will help it find the maximum a-posterior. \n",
11841184
"\n",
11851185
"Also important, *bad initial values* are a source of major bugs in PyMC and can hurt convergence.\n",
11861186
"\n",

0 commit comments

Comments
 (0)