|
156 | 156 | "source": [
|
157 | 157 | "p.show_approx()"
|
158 | 158 | ]
|
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "markdown", |
| 162 | + "metadata": {}, |
| 163 | + "source": [ |
| 164 | + "## Joint Probability Distribution\n", |
| 165 | + "\n", |
| 166 | + "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", |
| 167 | + "\n", |
| 168 | + "\n", |
| 169 | + "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." |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": null, |
| 175 | + "metadata": { |
| 176 | + "collapsed": false |
| 177 | + }, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "event = {'A': 10, 'B': 9, 'C': 8}\n", |
| 181 | + "variables = ['C', 'A']\n", |
| 182 | + "event_values (event, variables)" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "markdown", |
| 187 | + "metadata": { |
| 188 | + "collapsed": true |
| 189 | + }, |
| 190 | + "source": [ |
| 191 | + "_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. " |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": null, |
| 197 | + "metadata": { |
| 198 | + "collapsed": true |
| 199 | + }, |
| 200 | + "outputs": [], |
| 201 | + "source": [ |
| 202 | + "%psource JointProbDist" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "markdown", |
| 207 | + "metadata": {}, |
| 208 | + "source": [ |
| 209 | + "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", |
| 210 | + "\n", |
| 211 | + "To specify a Joint distribution we first need an ordered list of variables." |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": null, |
| 217 | + "metadata": { |
| 218 | + "collapsed": false |
| 219 | + }, |
| 220 | + "outputs": [], |
| 221 | + "source": [ |
| 222 | + "variables = ['X', 'Y']\n", |
| 223 | + "j = JointProbDist(variables)\n", |
| 224 | + "j" |
| 225 | + ] |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "markdown", |
| 229 | + "metadata": {}, |
| 230 | + "source": [ |
| 231 | + "Like the **ProbDist** class **JointProbDist** also employes magic methods to assign probability to different values.\n", |
| 232 | + "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." |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": null, |
| 238 | + "metadata": { |
| 239 | + "collapsed": false |
| 240 | + }, |
| 241 | + "outputs": [], |
| 242 | + "source": [ |
| 243 | + "j[1,1] = 0.2\n", |
| 244 | + "j[dict(X=0, Y=1)] = 0.5\n", |
| 245 | + "\n", |
| 246 | + "(j[1,1], j[0,1])" |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "markdown", |
| 251 | + "metadata": {}, |
| 252 | + "source": [ |
| 253 | + "It is also possible to list all the values for a particular variable using the **values** method." |
| 254 | + ] |
| 255 | + }, |
| 256 | + { |
| 257 | + "cell_type": "code", |
| 258 | + "execution_count": null, |
| 259 | + "metadata": { |
| 260 | + "collapsed": false |
| 261 | + }, |
| 262 | + "outputs": [], |
| 263 | + "source": [ |
| 264 | + "j.values('X')" |
| 265 | + ] |
159 | 266 | }
|
160 | 267 | ],
|
161 | 268 | "metadata": {
|
|
0 commit comments