Skip to content

Commit c08a634

Browse files
ch2 edits + 1 style change to h4
1 parent aeb5d06 commit c08a634

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

Chapter2_MorePyMC/MorePyMC.ipynb

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"\n",
4646
"* *child variable* are variables that are affected by other variables, i.e. are the subject of parent variables. \n",
4747
"\n",
48-
"Variables can be both parents and children. For example, consider the PyMC code below"
48+
"Variables can be both parents and children. For example, consider the PyMC code below."
4949
]
5050
},
5151
{
@@ -63,7 +63,7 @@
6363
"language": "python",
6464
"metadata": {},
6565
"outputs": [],
66-
"prompt_number": 9
66+
"prompt_number": 4
6767
},
6868
{
6969
"cell_type": "markdown",
@@ -73,7 +73,7 @@
7373
"\n",
7474
"Likewise, `data_generator` is a parent to the variable `output` (hence making `data_generator` both a parent and child variable). Although it does not look like one, `output` should be treated as a PyMC variable as it is a function of another PyMC variable. \n",
7575
"\n",
76-
"This nomenclature is introduced to help us describe relationships in PyMC modeling. You can access a variables children and parent variables using the `children` and `parents` methods attached to variables."
76+
"This nomenclature is introduced to help us describe relationships in PyMC modeling. You can access a variables children and parent variables using the `children` and `parents` attributes attached to variables."
7777
]
7878
},
7979
{
@@ -119,7 +119,7 @@
119119
"cell_type": "markdown",
120120
"metadata": {},
121121
"source": [
122-
"Of course a child can have more than one parent, and parent can have many children."
122+
"Of course a child can have more than one parent, and a parent can have many children."
123123
]
124124
},
125125
{
@@ -1149,9 +1149,7 @@
11491149
"plt.subplot(212)\n",
11501150
"plt.hist( alpha_samples, histtype='stepfilled', bins = 45, alpha = 0.85, \\\n",
11511151
" label = r\"positerior of $\\alpha$\", color = \"#A60628\",normed = True )\n",
1152-
"plt.legend()\n",
1153-
"\n",
1154-
"\n"
1152+
"plt.legend()\n"
11551153
],
11561154
"language": "python",
11571155
"metadata": {},
@@ -1335,25 +1333,47 @@
13351333
"cell_type": "markdown",
13361334
"metadata": {},
13371335
"source": [
1338-
"### More PyMC tricks: \n",
1336+
"### More PyMC Tricks\n",
1337+
"\n",
1338+
"#### Protip: *Lighter* deterministic variables with `Lambda` class\n",
1339+
"\n",
1340+
"Sometimes writing a deterministic function using the `@mc.deterministic` dectorator can seem like a chore, especially for small function. I have already mentioned that simple elementary math operations *can* produce deterministic variables implicitly, but what about operations like indexing or slicing? Built-in `Lambda` functions can achieve the elegance desired plus that simplicity required. For example, \n",
1341+
"\n",
1342+
" beta = mc.Normal( \"coefficients\", 0, size=(N,1) )\n",
1343+
" x = np.random.randn( (N,1) )\n",
1344+
" linear_combination = mc.Lambda( lambda x=x, beta = beta, np.dot( x.T, beta ) )\n",
13391345
"\n",
13401346
"\n",
1341-
"##TO DO\n",
1342-
"- lambda function\n",
1343-
"- potential functions\n",
1344-
"- containers?"
1347+
"#### Protip: Arrays of PyMC variables\n",
1348+
"There is no reason why we cannot store multiple heterogeneous PyMC variables in a Numpy array. Just remember to set the `dtype` of the array to `object` upon initialization. For example:\n",
1349+
"\n",
1350+
"\n"
13451351
]
13461352
},
1353+
{
1354+
"cell_type": "code",
1355+
"collapsed": false,
1356+
"input": [
1357+
"N = 10\n",
1358+
"x = np.empty( N , dtype=object )\n",
1359+
"for i in range(0, N):\n",
1360+
" x[i] = mc.Exponential('x_%i' % i, (i+1)**2)"
1361+
],
1362+
"language": "python",
1363+
"metadata": {},
1364+
"outputs": [],
1365+
"prompt_number": 13
1366+
},
13471367
{
13481368
"cell_type": "markdown",
13491369
"metadata": {},
13501370
"source": [
1351-
"### Returning to the Challenger data\n",
1371+
"### Bonus: Returning to the Challenger data\n",
13521372
"#####Is there really a relationship between failure and temperature?\n",
13531373
"\n",
13541374
"An critism of our above analysis is that *assumed* that the relationship followed a logistic model, this we implictly assumed that the probabilities change over temperature. Let's look at the data again. (Top figure)\n",
13551375
"\n",
1356-
"Could it be that infact this pattern occured by chance? This might explain the large overlap in temperatures. After all, I can produce similar plots. (Bottom figure)"
1376+
"Could it be that infact this particular sequence of defects occured by chance? After all, I can produce similar plots. (Bottom figure) This might explain the large overlap in temperatures."
13571377
]
13581378
},
13591379
{
@@ -1505,7 +1525,6 @@
15051525
"cell_type": "code",
15061526
"collapsed": false,
15071527
"input": [
1508-
"\n",
15091528
"beta = 0\n",
15101529
"alpha = mc.Normal( \"alpha\", 0, 0.001, value = 0 )\n",
15111530
"\n",
@@ -1609,12 +1628,7 @@
16091628
"source": [
16101629
"Is this good? Below is a chart that can be used to compare the computed Bayes factors to a degree of confidence in M1. \n",
16111630
"\n",
1612-
"- <1:1 Negative (supports M2)\n",
1613-
"- 1:1 to 3:1 Barely worth mentioning\n",
1614-
"- 3:1 to 10:1 Substantial\n",
1615-
"- 10:1 to 30:1 Strong\n",
1616-
"- 30:1 to 100:1 Very strong\n",
1617-
"- \\> 100:1\t Decisive\n",
1631+
"<table><tbody><tr><th>Bayes factor</th><th>Supporting M1</th></tr><tr><td>&lt;1:1 </td><td>Negative (supports M2)</td></tr><tr><td> 1:1 to 3:1 </td><td>Barely worth mentioning</td></tr><tr><td> 3:1 to 10:1 </td><td>Substantial</td></tr><tr><td> 10:1 to 30:1 </td><td>Strong</td></tr><tr><td> 30:1 to 100:1</td><td> Very strong</td></tr><tr><td> &gt; 100:1</td><td> Decisive</td></tr></tbody></table>\n",
16181632
"\n",
16191633
"We are not done yet. If you recall, we selected only one model, but recall we actually have a distribution of possible models (sequential pairs of ($\\beta, \\alpha$) from the posterior disitributions). So to be correct we need to average over samples from the posterior:"
16201634
]
@@ -1623,17 +1637,6 @@
16231637
"cell_type": "code",
16241638
"collapsed": false,
16251639
"input": [
1626-
"\"\"\"\n",
1627-
"linear_combination= np.dot( beta_samples[:,None], \n",
1628-
" observed_temperatures[None,:]) \\\n",
1629-
" + alpha_samples[:,None]\n",
1630-
" \n",
1631-
"p_model_1 = 1.0/(1.0 + np.exp( linear_combination ) )\n",
1632-
"product = p_model_1**( D )*(1-p_model_1)**(1-D)\n",
1633-
"model_1_product = product.prod(axis=1).mean()\n",
1634-
"print model_1_product\n",
1635-
"\"\"\"\n",
1636-
"\n",
16371640
"p = logistic( temperature[None,:], beta_samples, alpha_samples )\n",
16381641
"_product = p**( D )*(1-p)**(1-D)\n",
16391642
"prob_given_model_1 = _product.prod(axis=1).mean()\n",
@@ -1750,6 +1753,10 @@
17501753
" h1 {\n",
17511754
" font-family: \"Charis SIL\", Palatino, serif;\n",
17521755
" }\n",
1756+
" h4{\n",
1757+
" margin-top:12px;\n",
1758+
" margin-bottom: 3px;\n",
1759+
" }\n",
17531760
" div.text_cell_render{\n",
17541761
" font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
17551762
" line-height: 145%;\n",
@@ -1797,7 +1804,7 @@
17971804
"output_type": "pyout",
17981805
"prompt_number": 1,
17991806
"text": [
1800-
"<IPython.core.display.HTML at 0x8293dd8>"
1807+
"<IPython.core.display.HTML at 0x57da050>"
18011808
]
18021809
}
18031810
],

styles/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
h1 {
1212
font-family: "Charis SIL", Palatino, serif;
1313
}
14+
h4{
15+
margin-top:12px;
16+
margin-bottom: 3px;
17+
}
1418
div.text_cell_render{
1519
font-family: Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
1620
line-height: 145%;

0 commit comments

Comments
 (0)