Last active
January 21, 2016 18:29
-
-
Save lotka/fab69bb9b952904c6c0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# IDAPI Tutorial 01\n", | |
"1. P(C) = (0.49418546 0.50581454)\n", | |
"2. P(C) = (0.52250216 0.47749784)\n", | |
"3. P(C) = (0.57124195 0.42875805)\n", | |
"4. See the final cell" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"S P(S|E) = [[ 0. 0.33 0.14]]\n", | |
"D P(D|E) = [[ 0.4 0.33 0.14]]\n", | |
"F P(F|C) = [[ 0.125 0.14 ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"verbose = False\n", | |
"# Link Matrices\n", | |
"P_DE = np.matrix('0.4, 0.33, 0.29; 0.4, 0.33, 0.14;0.2, 0.34, 0.14;0, 0, 0.43')\n", | |
"P_SE = np.matrix('0 0.33 0.14; 0.6 0 0.14; 0.4 0.34 0; 0 0.33 0.14; 0 0 0.14; 0 0 0.14; 0 0 0.28')\n", | |
"P_FC = np.matrix('0 0.3; 0.125 0; 0.125 0.14; 0.25 0.14; 0.125 0; 0.125 0; 0 0.14; 0.125 0.14; 0 0.14; 0.125 0')\n", | |
"P_EC = np.matrix('0.5 0.14; 0.25 0.14; 0.25 0.72')\n", | |
"P_C = np.matrix('0.5 0.5')\n", | |
"# Initial Conditions\n", | |
"S = np.ones(7)\n", | |
"D = np.ones(4)\n", | |
"F = np.ones(10)\n", | |
"Question = 1\n", | |
"if(Question == 1):\n", | |
" S = np.array([1.,0.,0.,0.,0.,0.,0.])\n", | |
" D = np.array([0., 1., 0., 0.])\n", | |
" F = np.array([0.,0,1.,0.,0.,0.,0.,0.,0.,0.])\n", | |
"elif(Question == 2):\n", | |
" S = np.array([1.,0.,0.,0.,0.,0.,0.])\n", | |
" D = np.array([0., 1., 0., 0.])\n", | |
" F = np.array([1.,1.,1.,1.,1.,1.,1.,1.,1.,1.])\n", | |
"elif(Question == 3):\n", | |
" S = np.array([0.8,0.2,0.,0.,0.,0.,0.])\n", | |
" D = np.array([0.3, 0.4, 0.3, 0.0])\n", | |
" F = np.array([1.,1.,1.,1.,1.,1.,1.,1.,1.,1.])\n", | |
" \n", | |
"if verbose:\n", | |
" print '\\nS=', S, ' D=', D, ' F=', F\n", | |
" print 'Sizes = ', S.size, D.size, F.size\n", | |
" print '\\nP(D|E)\\n', P_DE\n", | |
" print '\\nP(S|E)\\n', P_SE\n", | |
" print '\\nP(F|C)\\n', P_FC\n", | |
" print '\\nP(E|C)\\n', P_EC\n", | |
" print '\\nP(C)\\n', P_C\n", | |
" \n", | |
"def elementWiseVectorProduct(a,b):\n", | |
" if not (a.size > 0 and a.shape == b.shape):\n", | |
" print 'Cannot do element wise vector product on :\\n', a\n", | |
" print b\n", | |
" return\n", | |
" else:\n", | |
" res = np.zeros(a.shape)\n", | |
" for i in xrange(a.shape[1]):\n", | |
" res[0,i] = a[0,i]*b[0,i]\n", | |
" return res\n", | |
" \n", | |
"def normaliseMatrix(M):\n", | |
" N = 0\n", | |
" for i in xrange(M.shape[0]):\n", | |
" for j in xrange(M.shape[1]):\n", | |
" N += M[i,j]\n", | |
" if N == 0 :\n", | |
" print 'Can\\'t normalise', M\n", | |
" return M\n", | |
" return M/N\n", | |
"\n", | |
"print 'S P(S|E) = ', S*P_SE\n", | |
"print 'D P(D|E) = ', D*P_DE\n", | |
"print 'F P(F|C) = ', F*P_FC" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": false | |
}, | |
"source": [ | |
"$$\\lambda_{SD}(E) = (S\\cdot P(S|E)) \\otimes (D \\cdot P(D|E)) $$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0. 0.1089 0.0196]]\n" | |
] | |
} | |
], | |
"source": [ | |
"lambda_sd = elementWiseVectorProduct(S*P_SE,D*P_DE)\n", | |
"print lambda_sd" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": false | |
}, | |
"source": [ | |
"$$\\lambda_{E}(C) = \\lambda_{SD}(E) P(E|C)$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.032125 0.029358]]\n" | |
] | |
} | |
], | |
"source": [ | |
"lambda_ec = lambda_sd*P_EC\n", | |
"print lambda_ec" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": false | |
}, | |
"source": [ | |
"$$\\lambda_{F}(C) = \\lambda(F) P(F|C)$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.125 0.14 ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"lambda_fc = F*P_FC\n", | |
"print lambda_fc" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"$$ P'(C) = NP(C)\\lambda_{E}(C) \\lambda_{F}(C)$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.49418546 0.50581454]]\n" | |
] | |
} | |
], | |
"source": [ | |
"res = normaliseMatrix(elementWiseVectorProduct(lambda_fc,lambda_ec))\n", | |
"print res" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"$$ \\pi(F) = P(F|C)\\pi_F(C) $$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.15849057]\n", | |
" [ 0.05896226]\n", | |
" [ 0.13292453]\n", | |
" [ 0.19188679]\n", | |
" [ 0.05896226]\n", | |
" [ 0.05896226]\n", | |
" [ 0.07396226]\n", | |
" [ 0.13292453]\n", | |
" [ 0.07396226]\n", | |
" [ 0.05896226]]\n" | |
] | |
} | |
], | |
"source": [ | |
"pi_fc = elementWiseVectorProduct(lambda_fc,P_C)\n", | |
"print normaliseMatrix(P_FC*pi_fc.transpose())" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment