Skip to content

Commit

Permalink
Sample calculation for Simple Bayesian Network
Browse files Browse the repository at this point in the history
  • Loading branch information
Pim-Mostert committed Oct 4, 2024
1 parent 242caa3 commit d0adfda
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions docs/SimpleBayesianNetwork.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 37,
"metadata": {},
"outputs": [
{
Expand All @@ -72,7 +72,7 @@
" [0.33333333, 0.66666667]])"
]
},
"execution_count": 30,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -185,45 +185,51 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.11764706, 0.88235294])"
"array([[0.11764706, 0.88235294],\n",
" [0.8 , 0.2 ],\n",
" [0.33333333, 0.66666667]])"
]
},
"execution_count": 33,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y_hat = 0\n",
"P_QxY = np.zeros((3, 2))\n",
"\n",
"# Upwards\n",
"d3 = (np.arange(3) == Y_hat).astype(float)\n",
"a3 = d3\n",
"b2 = (P_YxQ * a3[None, :]).sum(axis=1)\n",
"a1 = b2\n",
"# For each possible input value\n",
"for Y_hat in [0, 1, 2]:\n",
" # Messages upwards\n",
" d3 = (np.arange(3) == Y_hat).astype(float)\n",
" a3 = d3\n",
" b2 = (P_YxQ * a3[None, :]).sum(axis=1)\n",
" a1 = b2\n",
"\n",
"# Downards\n",
"b1 = P_Q\n",
"a2 = b1\n",
"b3 = (P_YxQ * a2[:, None]).sum(axis=0)\n",
"\n",
"\n",
"# maak netjes\n",
"a1 * b1 / (a1 * b1).sum()"
" # Messages downwards\n",
" b1 = P_Q\n",
" a2 = b1\n",
" b3 = (P_YxQ * a2[:, None]).sum(axis=0)\n",
" \n",
" # Calculate P_Q\n",
" P_QxY[Y_hat, :] = a1 * b1 / (a1 * b1).sum()\n",
" \n",
"P_QxY\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
"source": [
"These values match with the ones calculated naively, showing the correctness of the algorithm."
]
}
],
"metadata": {
Expand Down

0 comments on commit d0adfda

Please sign in to comment.