Skip to content

Commit 1086d26

Browse files
author
kirankarkera
committed
initial checkin
0 parents  commit 1086d26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+53659
-0
lines changed
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
{
2+
"metadata": {
3+
"name": ""
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0,
7+
"worksheets": [
8+
{
9+
"cells": [
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"In this section we shall look at different kinds of reasoning used in a Bayes Net. We shall use the libpgm library to create a Bayes net. Libpgm uses a JSON-formatted file with a specific format where the edges, vertices and the CPDs at each vertice are annotated. This json file is read into the NodeData and GraphSkeleton objects to create a DiscreteBayesianNetwork (which, as the name suggests is a Bayes net where the CPDs take on discrete values). The TableCPDFactorization is an object that wraps the DiscreteBayesianNetwork and allows us to query the CPDs in the network. Please copy the json file for this example, 'job_interview.txt' to a local folder and change the relevant line in the getTableCPD() function to refer your local path to job_interview.txt. \n",
15+
"\n",
16+
"The following discussion uses integers 0, 1, 2 for discrete outcomes of each random variable, where 0 is the worst outcome. For e.g, Interview=0 indicates the worst outcome of the interview and Interview=2 is the best outcome. \n",
17+
"\n",
18+
"The first kind of reasoning we shall explore is called 'Causal Reasoning'. Initially we observe the prior probability of an event unconditioned by any evidence(for this example, we shall focus on the 'Offer' random variable). We then introduce observations of (one of) the parent variables. Consistent with our logical reasoning, we note that if one of the parents (equivalent to causes) of an event are observed, then we have stronger beliefs about the Offer random variable.\n",
19+
"\n",
20+
"We start off by defining a function that reads the JSON data file and creating an object we can use to run probability queries on."
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"collapsed": false,
26+
"input": [
27+
"from libpgm.graphskeleton import GraphSkeleton\n",
28+
"from libpgm.nodedata import NodeData\n",
29+
"from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork\n",
30+
"from libpgm.tablecpdfactorization import TableCPDFactorization\n",
31+
"\n",
32+
"def getTableCPD():\n",
33+
" nd = NodeData()\n",
34+
" skel = GraphSkeleton()\n",
35+
" jsonpath=\"job_interview.txt\"\n",
36+
" nd.load(jsonpath)\n",
37+
" skel.load(jsonpath)\n",
38+
" bn = DiscreteBayesianNetwork(skel, nd)\n",
39+
" tablecpd=TableCPDFactorization(bn)\n",
40+
" return tablecpd\n"
41+
],
42+
"language": "python",
43+
"metadata": {},
44+
"outputs": [],
45+
"prompt_number": 1
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"What is prior probability of getting a job offer $ P(Offer=1) $? Note that the probability query takes 2 dictionary arguments, the first one being the query and the second being the evidence set, which is empty right now."
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"collapsed": false,
57+
"input": [
58+
"tcpd=getTableCPD()\n",
59+
"tcpd.specificquery(dict(Offer='1'),{})"
60+
],
61+
"language": "python",
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"metadata": {},
66+
"output_type": "pyout",
67+
"prompt_number": 13,
68+
"text": [
69+
"0.432816"
70+
]
71+
}
72+
],
73+
"prompt_number": 13
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"It is about 43%, and if we now introduce evidence that the candidate has poor grades, how does it change the probability of getting an offer? Evaluating for $ P(Offer=1 | Grades=0) $"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"collapsed": false,
85+
"input": [
86+
"tcpd=getTableCPD()\n",
87+
"tcpd.specificquery(dict(Offer='1'),dict(Grades='0'))"
88+
],
89+
"language": "python",
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"metadata": {},
94+
"output_type": "pyout",
95+
"prompt_number": 11,
96+
"text": [
97+
"0.35148"
98+
]
99+
}
100+
],
101+
"prompt_number": 11
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"As expected, it decreases the probability of getting an offer. Adding further evidence that the candidate's experience is low as well, we evaluate $ P(Offer=1 | Grades=0, Experience=0) $"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"collapsed": false,
113+
"input": [
114+
"tcpd=getTableCPD()\n",
115+
"tcpd.specificquery(dict(Offer='1'),dict(Grades='0',Experience='0'))"
116+
],
117+
"language": "python",
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"metadata": {},
122+
"output_type": "pyout",
123+
"prompt_number": 12,
124+
"text": [
125+
"0.2078"
126+
]
127+
}
128+
],
129+
"prompt_number": 12
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"As expected, it drops even lower to 20%.\n",
136+
"\n",
137+
"What we have seen is the introduction of observed parent random variable strenthens our beliefs leading to the name 'Causal Reasoning'. \n",
138+
"\n",
139+
"## Evidential Reasoning\n",
140+
"\n"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"The second kind of reasoning is when we observe the value of a child variable, and we wish to reason about how it strengthens our beliefs about its parents. Evaluating for the prior probability of high Experience $ P(Experience=1) $"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"collapsed": false,
153+
"input": [
154+
"tcpd=getTableCPD()\n",
155+
"tcpd.specificquery(dict(Experience='1'),{})"
156+
],
157+
"language": "python",
158+
"metadata": {},
159+
"outputs": [
160+
{
161+
"output_type": "stream",
162+
"stream": "stdout",
163+
"text": [
164+
"0.4\n"
165+
]
166+
}
167+
],
168+
"prompt_number": 14
169+
},
170+
{
171+
"cell_type": "markdown",
172+
"metadata": {},
173+
"source": [
174+
"If we now introduce evidence that the candidate's interview was good, and evaluate for $ P(Experience=1\\mid Interview=2) $"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"collapsed": false,
180+
"input": [
181+
"tcpd=getTableCPD()\n",
182+
"tcpd.specificquery(dict(Experience='1'),dict(Interview='2'))"
183+
],
184+
"language": "python",
185+
"metadata": {},
186+
"outputs": [
187+
{
188+
"metadata": {},
189+
"output_type": "pyout",
190+
"prompt_number": 15,
191+
"text": [
192+
"0.8641975308641975"
193+
]
194+
}
195+
],
196+
"prompt_number": 15
197+
},
198+
{
199+
"cell_type": "markdown",
200+
"metadata": {},
201+
"source": [
202+
"we see that the probability that the candidate was highly experienced increases, which follows the reasoning that the candidate must have good experience or education, or both. In Evidential reasoning, we reason from effect to cause. "
203+
]
204+
},
205+
{
206+
"cell_type": "markdown",
207+
"metadata": {},
208+
"source": [
209+
"## Intercausal reasoning\n",
210+
"\n",
211+
"Intercausal reasoning, as the name suggests, is a type of reasoning where multiple causes of a single effect interact. We first determine what is the prior probability of having high relevant experience. On evaluating $ P(Experience=1) $"
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"collapsed": false,
217+
"input": [
218+
"tcpd=getTableCPD()\n",
219+
"tcpd.specificquery(dict(Experience='1'),{})"
220+
],
221+
"language": "python",
222+
"metadata": {},
223+
"outputs": [
224+
{
225+
"metadata": {},
226+
"output_type": "pyout",
227+
"prompt_number": 16,
228+
"text": [
229+
"0.4000000000000001"
230+
]
231+
}
232+
],
233+
"prompt_number": 16
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"metadata": {},
238+
"source": [
239+
"Introducing evidence that the interview went extremely well, we think that the candidate must be quite experienced. Evaluating for $ P(Experience=1 \\mid Interview=2) $"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"collapsed": false,
245+
"input": [
246+
"tcpd=getTableCPD()\n",
247+
"tcpd.specificquery(dict(Experience='1'),dict(Interview='2'))"
248+
],
249+
"language": "python",
250+
"metadata": {},
251+
"outputs": [
252+
{
253+
"metadata": {},
254+
"output_type": "pyout",
255+
"prompt_number": 17,
256+
"text": [
257+
"0.8641975308641975"
258+
]
259+
}
260+
],
261+
"prompt_number": 17
262+
},
263+
{
264+
"cell_type": "markdown",
265+
"metadata": {},
266+
"source": [
267+
"The Bayes net confirms what we think is true, and the probability of high experience goes up from 0.4 to 0.86. Now if we introduce evidence the the candidate didn't have good grades and still managed to get a good score in the interview, we may conclude that the candidate must be so experienced that his grades didn't matter at all. Evaluating for $ P(Experience=1\u2223Interview=2,Grades=0) $"
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"collapsed": false,
273+
"input": [
274+
"tcpd=getTableCPD()\n",
275+
"tcpd.specificquery(dict(Experience='1'),dict(Interview='2',Grades='0'))"
276+
],
277+
"language": "python",
278+
"metadata": {},
279+
"outputs": [
280+
{
281+
"metadata": {},
282+
"output_type": "pyout",
283+
"prompt_number": 18,
284+
"text": [
285+
"0.9090909090909091"
286+
]
287+
}
288+
],
289+
"prompt_number": 18
290+
},
291+
{
292+
"cell_type": "markdown",
293+
"metadata": {},
294+
"source": [
295+
"which confirms what we were thinking, even though the probability of high experience went up only a little, it strengthens our belief about the candidate's high experience. This example shows the interplay between the two parents of Interview, which are Experience and Grades, and shows us that if we know one of the causes behind an effect, it reduces the importance of the other cause. In other words, we have explained away the poor grades on observing the experience of the candidate. This phenomenon is commonly calle 'Explaining Away'. "
296+
]
297+
}
298+
],
299+
"metadata": {}
300+
}
301+
]
302+
}

0 commit comments

Comments
 (0)