Newer
Older
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# Probability \n",
"\n",
"This IPy notebook acts as supporting material for **Chapter 13 Quantifying Uncertainty**, **Chapter 14 Probabilistic Reasoning** and **Chapter 15 Probabilistic Reasoning over Time** of the book* Artificial Intelligence: A Modern Approach*. This notebook makes use of the implementations in probability.py module. Let us import everything from the probability module. It might be helpful to view the source of some of our implementations. Please refer to the Introductory IPy file for more details on how to do so."
]
},
"from probability import *"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Probability Distribution\n",
"\n",
"Let us begin by specifying discrete probability distributions. The class **ProbDist** defines a discrete probability distribution. We name our random variable and then assign probabilities to the different values of the random variable. Assigning probabilities to the values works similar to that of using a dictionary with keys being the Value and we assign to it the probability. This is possible because of the magic methods **_ _getitem_ _** and **_ _setitem_ _** which store the probabilities in the prob dict of the object. You can keep the source window open alongside while playing with the rest of the code to get a better understanding."
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
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
"source": [
"%psource ProbDist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = ProbDist('Flip')\n",
"p['H'], p['T'] = 0.25, 0.75\n",
"p['T']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first parameter of the constructor **varname** has a default value of '?'. So if the name is not passed it defaults to ?. The keyword argument **freqs** can be a dictionary of values of random variable:probability. These are then normalized such that the probability values sum upto 1 using the **normalize** method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = ProbDist(freqs={'low': 125, 'medium': 375, 'high': 500})\n",
"p.varname\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"(p['low'], p['medium'], p['high'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Besides the **prob** and **varname** the object also separately keeps track of all the values of the distribution in a list called **values**. Every time a new value is assigned a probability it is appended to this list, This is done inside the **_ _setitem_ _** method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p.values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The distribution by default is not normalized if values are added incremently. We can still force normalization by invoking the **normalize** method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = ProbDist('Y')\n",
"p['Cat'] = 50\n",
"p['Dog'] = 114\n",
"p['Mice'] = 64\n",
"(p['Cat'], p['Dog'], p['Mice'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p.normalize()\n",
"(p['Cat'], p['Dog'], p['Mice'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also possible to display the approximate values upto decimals using the **show_approx** method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p.show_approx()"
]
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Joint Probability Distribution\n",
"\n",
"The helper function **event_values** returns a tuple of the values of variables in event. An event is specified by a dict where the keys are the names of variables and the corresponding values are the value of the variable. Variables are specified with a list. The ordering of the returned tuple is same as those of the variables.\n",
"\n",
"\n",
"Alternatively if the event is specified by a list or tuple of equal length of the variables. Then the events tuple is returned as it is."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"event = {'A': 10, 'B': 9, 'C': 8}\n",
"variables = ['C', 'A']\n",
"event_values (event, variables)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"_A probability model is completely determined by the joint distribution for all of the random variables._ (**Section 13.3**) The probability module implements these as the class **JointProbDist** which inherits from the **ProbDist** class. This class specifies a discrete probability distribute over a set of variables. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%psource JointProbDist"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Values for a Joint Distribution is a an ordered tuple in which each item corresponds to the value associate with a particular variable. For Joint Distribution of X, Y where X, Y take integer values this can be something like (18, 19).\n",
"\n",
"To specify a Joint distribution we first need an ordered list of variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"variables = ['X', 'Y']\n",
"j = JointProbDist(variables)\n",
"j"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like the **ProbDist** class **JointProbDist** also employes magic methods to assign probability to different values.\n",
"The probability can be assigned in either of the two formats for all possible values of the distribution. The **event_values** call inside **_ _getitem_ _** and **_ _setitem_ _** does the required processing to make this work."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"j[1,1] = 0.2\n",
"j[dict(X=0, Y=1)] = 0.5\n",
"\n",
"(j[1,1], j[0,1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also possible to list all the values for a particular variable using the **values** method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"j.values('X')"
]
}
],
"metadata": {
"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",