File size: 5,465 Bytes
93d7919 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Extending pgmpy"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"It's really easy to extend pgmpy to quickly prototype your ideas. pgmpy has a base abstract class for most of main functionalities like: `BaseInference` for inference, `BaseFactor` for model parameters, `BaseEstimators` for parameter and model learning. For adding a new feature to pgmpy we just need to implement a new class inheriting one of these base classes and then we can use the new class with other functionality of pgmpy.\n",
"\n",
"In this example we will see how to write a new inference algorithm. We will take the example of a very simple algorithm in which we will multiply all the factors/CPD of the network and marginalize over variable to get the desired query."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# A simple Exact inference algorithm\n",
"\n",
"import itertools\n",
"\n",
"from pgmpy.inference.base import Inference\n",
"from pgmpy.factors import factor_product\n",
"\n",
"\n",
"class SimpleInference(Inference):\n",
" def __init__(self,model):\n",
" super(SimpleInference, self).__init__(model)\n",
" self._initialize_structures()\n",
" \n",
" # By inheriting Inference we can use self.model, self.factors and self.cardinality in our class\n",
" def query(self, var, evidence):\n",
" # self.factors is a dict of the form of {node: [factors_involving_node]}\n",
" factors_list = set(itertools.chain(*self.factors.values()))\n",
" product = factor_product(*factors_list)\n",
" reduced_prod = product.reduce(evidence, inplace=False)\n",
" reduced_prod.normalize()\n",
" var_to_marg = (\n",
" set(self.model.nodes()) - set(var) - set([state[0] for state in evidence])\n",
" )\n",
" marg_prod = reduced_prod.marginalize(var_to_marg, inplace=False)\n",
" return marg_prod"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Defining a model\n",
"\n",
"from pgmpy.models import BayesianModel\n",
"from pgmpy.factors.discrete import TabularCPD\n",
"\n",
"model = BayesianModel([(\"A\", \"J\"), (\"R\", \"J\"), (\"J\", \"Q\"), (\"J\", \"L\"), (\"G\", \"L\")])\n",
"cpd_a = TabularCPD(\"A\", 2, values=[[0.2], [0.8]])\n",
"cpd_r = TabularCPD(\"R\", 2, values=[[0.4], [0.6]])\n",
"cpd_j = TabularCPD(\n",
" \"J\",\n",
" 2,\n",
" values=[[0.9, 0.6, 0.7, 0.1], [0.1, 0.4, 0.3, 0.9]],\n",
" evidence=[\"A\", \"R\"],\n",
" evidence_card=[2, 2],\n",
")\n",
"cpd_q = TabularCPD(\n",
" \"Q\", 2, values=[[0.9, 0.2], [0.1, 0.8]], evidence=[\"J\"], evidence_card=[2]\n",
")\n",
"cpd_l = TabularCPD(\n",
" \"L\",\n",
" 2,\n",
" values=[[0.9, 0.45, 0.8, 0.1], [0.1, 0.55, 0.2, 0.9]],\n",
" evidence=[\"J\", \"G\"],\n",
" evidence_card=[2, 2],\n",
")\n",
"cpd_g = TabularCPD(\"G\", 2, values=[[0.6], [0.4]])\n",
"\n",
"model.add_cpds(cpd_a, cpd_r, cpd_j, cpd_q, cpd_l, cpd_g)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Doing inference with our SimpleInference\n",
"\n",
"infer = SimpleInference(model)\n",
"a = infer.query(var=[\"A\"], evidence=[(\"J\", 0), (\"R\", 1)])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+-----+----------+\n",
"| A | phi(A) |\n",
"|-----+----------|\n",
"| A_0 | 0.6000 |\n",
"| A_1 | 0.4000 |\n",
"+-----+----------+\n"
]
}
],
"source": [
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+-----+----------+\n",
"| A | phi(A) |\n",
"|-----+----------|\n",
"| A_0 | 0.6000 |\n",
"| A_1 | 0.4000 |\n",
"+-----+----------+\n"
]
}
],
"source": [
"# Comparing the results with Variable Elimination Algorithm\n",
"\n",
"from pgmpy.inference import VariableElimination\n",
"\n",
"infer = VariableElimination(model)\n",
"result = infer.query([\"A\"], evidence={\"J\": 0, \"R\": 1})\n",
"print(result[\"A\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly we can also create new classes for Factor or CPDs and add them to networks and do inference over it or can write a new estimator class."
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
|