Skip to content

Commit

Permalink
Finished q1
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasbrus committed May 20, 2016
1 parent dcb9800 commit 437413e
Showing 1 changed file with 57 additions and 13 deletions.
70 changes: 57 additions & 13 deletions advanced/practicum/week-3/lab-3.ipynb
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"metadata": {
"collapsed": false
},
Expand All @@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -167,7 +167,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 24,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -284,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 27,
"metadata": {
"collapsed": false
},
Expand All @@ -303,8 +303,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/thomasbrus/Code/machine-learning/lib/python2.7/site-packages/ipykernel/__main__.py:65: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n",
"/Users/thomasbrus/Code/machine-learning/lib/python2.7/site-packages/ipykernel/__main__.py:94: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n"
"/Users/thomasbrus/Code/machine-learning/lib/python2.7/site-packages/ipykernel/__main__.py:65: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n"
]
}
],
Expand Down Expand Up @@ -385,7 +384,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 28,
"metadata": {
"collapsed": false
},
Expand All @@ -405,18 +404,33 @@
"\n",
"Q1.4:\n",
"P(C|-A,-B) = (see Q1.3) = 0.152777777778\n",
"P(B|-A) = (see Q1.1) = 0.0694444444444\n",
"P(-B|-A) = (see Q1.1) = 0.025462962963\n",
"P(-A) = (see marginal probabilities of initial graph) = 0.22222222\n",
"P(-D|-A) = (given) = 0.25\n",
"P(-A,-B,C,-D) = P(C|-A,-B) * P(B|-A) * P(-A) * P(-D|-A) = 0.000589420433064\n"
"P(-A,-B,C,-D) = P(C|-A,-B) * P(B|-A) * P(-A) * P(-D|-A) = 0.000216120825457\n",
"\n",
"Q1.5:\n",
"p(B|A) = 0.0138888888889\n",
"p(C|A) = 0.152777777778\n",
"p(D|A) = 0.0\n",
"P(A) = 0.77777778\n",
"\n",
"P(B|-A) = 0.0694444444444\n",
"P(C|-A) = 0.180555555556\n",
"P(D|-A) = 0.166666666667\n",
"P(-A) = 0.22222222\n",
"\n",
"P(B,C,D) = P(B|A) * P(C|A) * P(D|A) * P(A) + P(B|-A) * P(C|-A) * P(D|-A) * P(-A) = 0.000464391856355\n",
"P(D) = (see marginial probabilities of initial graph) = 0.16666667\n",
"\n",
"P(B,C|D) = P(B,C,D) / P(D) = 0.0027863510824\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/thomasbrus/Code/machine-learning/lib/python2.7/site-packages/ipykernel/__main__.py:65: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n",
"/Users/thomasbrus/Code/machine-learning/lib/python2.7/site-packages/ipykernel/__main__.py:94: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n"
"/Users/thomasbrus/Code/machine-learning/lib/python2.7/site-packages/ipykernel/__main__.py:65: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n"
]
}
],
Expand All @@ -431,11 +445,14 @@
"# Answer Q1.1\n",
"print \"Q1.1:\"\n",
"print \"p(B|-A) = \", g.getMarginals(\"B\")[0]\n",
"# print \"p(-B|-A) = \", g.getMarginals(\"B\")[1]\n",
"\n",
"# Answer Q1.2\n",
"print \"\\nQ1,2:\"\n",
"print \"p(C|-A) = \", g.getMarginals(\"C\")[0]\n",
"\n",
"# print \"p(D|-A) = \", g.getMarginals(\"D\")[0]\n",
"\n",
"g.reset()\n",
"\n",
"g.addFactor(\"obs -B\", np.array([0.,1.]), \"B\")\n",
Expand All @@ -450,11 +467,38 @@
"# Answer Q1.4\n",
"print \"\\nQ1.4:\"\n",
"print \"P(C|-A,-B) = (see Q1.3) = 0.152777777778\"\n",
"print \"P(B|-A) = (see Q1.1) = 0.0694444444444\"\n",
"print \"P(-B|-A) = (see Q1.1) = 0.025462962963\"\n",
"print \"P(-A) = (see marginal probabilities of initial graph) = 0.22222222\"\n",
"print \"P(-D|-A) = (given) = 0.25\"\n",
"\n",
"print \"P(-A,-B,C,-D) = P(C|-A,-B) * P(B|-A) * P(-A) * P(-D|-A) = \", 0.152777777778 * 0.0694444444444 * 0.22222222 * 0.25"
"print \"P(-A,-B,C,-D) = P(C|-A,-B) * P(B|-A) * P(-A) * P(-D|-A) = \", 0.152777777778 * 0.025462962963 * 0.22222222 * 0.25\n",
"\n",
"# Answer Q1.5\n",
"g2 = Graph()\n",
"g2.addVariables([\"True\",\"False\"], \"A\",\"B\",\"C\",\"D\")\n",
"g2.addFactor(\"p(B)\", pB, \"B\")\n",
"g2.addFactor(\"p(C)\", pC, \"C\")\n",
"g2.addFactor(\"p(A|B,C)\", pA_BC, \"A\",\"B\",\"C\")\n",
"g2.addFactor(\"P(D|A)\",pD_A,\"D\",\"A\")\n",
"\n",
"g2.addFactor(\"obs A\", np.array([1.,0.]), \"A\")\n",
"g2.propagateMessages()\n",
"\n",
"print \"\\nQ1.5:\"\n",
"print \"p(B|A) = \", g2.getMarginals(\"B\")[0]\n",
"print \"p(C|A) = \", g2.getMarginals(\"C\")[0]\n",
"print \"p(D|A) = \", g2.getMarginals(\"D\")[0]\n",
"print \"P(A) = 0.77777778\"\n",
"\n",
"print \"\\nP(B|-A) = 0.0694444444444\"\n",
"print \"P(C|-A) = 0.180555555556\"\n",
"print \"P(D|-A) = 0.166666666667\"\n",
"print \"P(-A) = 0.22222222\"\n",
"\n",
"print \"\\nP(B,C,D) = P(B|A) * P(C|A) * P(D|A) * P(A) + P(B|-A) * P(C|-A) * P(D|-A) * P(-A) = \", 0.0138888888889 * 0.152777777778 * 0.0 * 0.77777778 + 0.0694444444444 * 0.180555555556 * 0.166666666667 * 0.22222222\n",
"print \"P(D) = (see marginial probabilities of initial graph) = 0.16666667\"\n",
"\n",
"print \"\\nP(B,C|D) = P(B,C,D) / P(D) = \", 0.000464391856355 / 0.16666667"
]
},
{
Expand Down

0 comments on commit 437413e

Please sign in to comment.