|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "collapsed": false |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Probability \n", |
| 10 | + "\n", |
| 11 | + "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." |
| 12 | + ] |
| 13 | + }, |
3 | 14 | {
|
4 | 15 | "cell_type": "code",
|
5 | 16 | "execution_count": null,
|
6 | 17 | "metadata": {
|
7 |
| - "collapsed": false |
| 18 | + "collapsed": true |
8 | 19 | },
|
9 | 20 | "outputs": [],
|
10 | 21 | "source": [
|
11 |
| - "import probability" |
| 22 | + "from probability import *" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": { |
| 28 | + "collapsed": true |
| 29 | + }, |
| 30 | + "source": [ |
| 31 | + "## Probability Distribution\n", |
| 32 | + "\n", |
| 33 | + "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." |
12 | 34 | ]
|
13 | 35 | },
|
14 | 36 | {
|
|
18 | 40 | "collapsed": true
|
19 | 41 | },
|
20 | 42 | "outputs": [],
|
21 |
| - "source": [] |
| 43 | + "source": [ |
| 44 | + "%psource ProbDist" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": { |
| 51 | + "collapsed": false |
| 52 | + }, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "p = ProbDist('Flip')\n", |
| 56 | + "p['H'], p['T'] = 0.25, 0.75\n", |
| 57 | + "p['T']" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "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." |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": null, |
| 70 | + "metadata": { |
| 71 | + "collapsed": false |
| 72 | + }, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "p = ProbDist(freqs={'low': 125, 'medium': 375, 'high': 500})\n", |
| 76 | + "p.varname\n" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": { |
| 83 | + "collapsed": false |
| 84 | + }, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "(p['low'], p['medium'], p['high'])" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "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." |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "metadata": { |
| 101 | + "collapsed": false |
| 102 | + }, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "p.values" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "The distribution by default is not normalized if values are added incremently. We can still force normalization by invoking the **normalize** method." |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": { |
| 119 | + "collapsed": false |
| 120 | + }, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "p = ProbDist('Y')\n", |
| 124 | + "p['Cat'] = 50\n", |
| 125 | + "p['Dog'] = 114\n", |
| 126 | + "p['Mice'] = 64\n", |
| 127 | + "(p['Cat'], p['Dog'], p['Mice'])" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": null, |
| 133 | + "metadata": { |
| 134 | + "collapsed": false |
| 135 | + }, |
| 136 | + "outputs": [], |
| 137 | + "source": [ |
| 138 | + "p.normalize()\n", |
| 139 | + "(p['Cat'], p['Dog'], p['Mice'])" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "It is also possible to display the approximate values upto decimals using the **show_approx** method." |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": null, |
| 152 | + "metadata": { |
| 153 | + "collapsed": false |
| 154 | + }, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "p.show_approx()" |
| 158 | + ] |
22 | 159 | }
|
23 | 160 | ],
|
24 | 161 | "metadata": {
|
|
37 | 174 | "name": "python",
|
38 | 175 | "nbconvert_exporter": "python",
|
39 | 176 | "pygments_lexer": "ipython3",
|
40 |
| - "version": "3.5.1" |
| 177 | + "version": "3.4.3" |
41 | 178 | }
|
42 | 179 | },
|
43 | 180 | "nbformat": 4,
|
|
0 commit comments