| 
45 | 45 |       "\n",  | 
46 | 46 |       "*  *child variable* are variables that are affected by other variables, i.e. are the subject of parent variables. \n",  | 
47 | 47 |       "\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."  | 
49 | 49 |      ]  | 
50 | 50 |     },  | 
51 | 51 |     {  | 
 | 
63 | 63 |      "language": "python",  | 
64 | 64 |      "metadata": {},  | 
65 | 65 |      "outputs": [],  | 
66 |  | -     "prompt_number": 9  | 
 | 66 | +     "prompt_number": 4  | 
67 | 67 |     },  | 
68 | 68 |     {  | 
69 | 69 |      "cell_type": "markdown",  | 
 | 
73 | 73 |       "\n",  | 
74 | 74 |       "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",  | 
75 | 75 |       "\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."  | 
77 | 77 |      ]  | 
78 | 78 |     },  | 
79 | 79 |     {  | 
 | 
119 | 119 |      "cell_type": "markdown",  | 
120 | 120 |      "metadata": {},  | 
121 | 121 |      "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."  | 
123 | 123 |      ]  | 
124 | 124 |     },  | 
125 | 125 |     {  | 
 | 
1149 | 1149 |       "plt.subplot(212)\n",  | 
1150 | 1150 |       "plt.hist( alpha_samples, histtype='stepfilled', bins = 45, alpha = 0.85, \\\n",  | 
1151 | 1151 |       "        label = r\"positerior of $\\alpha$\", color = \"#A60628\",normed = True )\n",  | 
1152 |  | -      "plt.legend()\n",  | 
1153 |  | -      "\n",  | 
1154 |  | -      "\n"  | 
 | 1152 | +      "plt.legend()\n"  | 
1155 | 1153 |      ],  | 
1156 | 1154 |      "language": "python",  | 
1157 | 1155 |      "metadata": {},  | 
 | 
1335 | 1333 |      "cell_type": "markdown",  | 
1336 | 1334 |      "metadata": {},  | 
1337 | 1335 |      "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",  | 
1339 | 1345 |       "\n",  | 
1340 | 1346 |       "\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"  | 
1345 | 1351 |      ]  | 
1346 | 1352 |     },  | 
 | 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 | +    },  | 
1347 | 1367 |     {  | 
1348 | 1368 |      "cell_type": "markdown",  | 
1349 | 1369 |      "metadata": {},  | 
1350 | 1370 |      "source": [  | 
1351 |  | -      "### Returning to the Challenger data\n",  | 
 | 1371 | +      "### Bonus: Returning to the Challenger data\n",  | 
1352 | 1372 |       "#####Is there really a relationship between failure and temperature?\n",  | 
1353 | 1373 |       "\n",  | 
1354 | 1374 |       "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",  | 
1355 | 1375 |       "\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."  | 
1357 | 1377 |      ]  | 
1358 | 1378 |     },  | 
1359 | 1379 |     {  | 
 | 
1505 | 1525 |      "cell_type": "code",  | 
1506 | 1526 |      "collapsed": false,  | 
1507 | 1527 |      "input": [  | 
1508 |  | -      "\n",  | 
1509 | 1528 |       "beta = 0\n",  | 
1510 | 1529 |       "alpha = mc.Normal( \"alpha\", 0, 0.001, value = 0 )\n",  | 
1511 | 1530 |       "\n",  | 
 | 
1609 | 1628 |      "source": [  | 
1610 | 1629 |       "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",  | 
1611 | 1630 |       "\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><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>  > 100:1</td><td> Decisive</td></tr></tbody></table>\n",  | 
1618 | 1632 |       "\n",  | 
1619 | 1633 |       "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:"  | 
1620 | 1634 |      ]  | 
 | 
1623 | 1637 |      "cell_type": "code",  | 
1624 | 1638 |      "collapsed": false,  | 
1625 | 1639 |      "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",  | 
1637 | 1640 |       "p = logistic( temperature[None,:], beta_samples, alpha_samples )\n",  | 
1638 | 1641 |       "_product = p**( D )*(1-p)**(1-D)\n",  | 
1639 | 1642 |       "prob_given_model_1 = _product.prod(axis=1).mean()\n",  | 
 | 
1750 | 1753 |         "    h1 {\n",  | 
1751 | 1754 |         "        font-family: \"Charis SIL\", Palatino, serif;\n",  | 
1752 | 1755 |         "    }\n",  | 
 | 1756 | +        "    h4{\n",  | 
 | 1757 | +        "        margin-top:12px;\n",  | 
 | 1758 | +        "        margin-bottom: 3px;\n",  | 
 | 1759 | +        "       }\n",  | 
1753 | 1760 |         "    div.text_cell_render{\n",  | 
1754 | 1761 |         "        font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",  | 
1755 | 1762 |         "        line-height: 145%;\n",  | 
 | 
1797 | 1804 |        "output_type": "pyout",  | 
1798 | 1805 |        "prompt_number": 1,  | 
1799 | 1806 |        "text": [  | 
1800 |  | -        "<IPython.core.display.HTML at 0x8293dd8>"  | 
 | 1807 | +        "<IPython.core.display.HTML at 0x57da050>"  | 
1801 | 1808 |        ]  | 
1802 | 1809 |       }  | 
1803 | 1810 |      ],  | 
 | 
0 commit comments