Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d79817f

Browse files
committed
Tightened up probability.py.
1 parent ada74fd commit d79817f

File tree

2 files changed

+177
-248
lines changed

2 files changed

+177
-248
lines changed

probability.doctest

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
>>> cpt = burglary.variable_node('Alarm').cpt
3+
>>> parents = ['Burglary', 'Earthquake']
4+
>>> event = {'Burglary': True, 'Earthquake': True}
5+
>>> print '%4.2f' % cpt.p(True, parents, event)
6+
0.95
7+
>>> event = {'Burglary': False, 'Earthquake': True}
8+
>>> print '%4.2f' % cpt.p(False, parents, event)
9+
0.71
10+
>>> BoolCPT({T: 0.2, F: 0.625}).p(False, ['Burglary'], event)
11+
0.375
12+
>>> BoolCPT(0.75).p(False, [], {})
13+
0.25
14+
15+
(fixme: The following test p_values which has been folded into p().)
16+
>>> cpt = BoolCPT(0.25)
17+
>>> cpt.p_values(F, ())
18+
0.75
19+
>>> cpt = BoolCPT({T: 0.25, F: 0.625})
20+
>>> cpt.p_values(T, (T,))
21+
0.25
22+
>>> cpt.p_values(F, (F,))
23+
0.375
24+
>>> cpt = BoolCPT({(T, T): 0.2, (T, F): 0.31,
25+
... (F, T): 0.5, (F, F): 0.62})
26+
>>> cpt.p_values(T, (T, F))
27+
0.31
28+
>>> cpt.p_values(F, (F, F))
29+
0.38
30+
31+
32+
>>> cpt = BoolCPT({True: 0.2, False: 0.7})
33+
>>> cpt.rand(['A'], {'A': True}) in [True, False]
34+
True
35+
>>> cpt = BoolCPT({(True, True): 0.1, (True, False): 0.3,
36+
... (False, True): 0.5, (False, False): 0.7})
37+
>>> cpt.rand(['A', 'B'], {'A': True, 'B': False}) in [True, False]
38+
True
39+
40+
41+
>>> enumeration_ask('Earthquake', {}, burglary).show_approx()
42+
'False: 0.998, True: 0.002'
43+
44+
45+
>>> s = prior_sample(burglary)
46+
>>> s['Burglary'] in [True, False]
47+
True
48+
>>> s['Alarm'] in [True, False]
49+
True
50+
>>> s['JohnCalls'] in [True, False]
51+
True
52+
>>> len(s)
53+
5
54+
55+
56+
>>> s = {'A': True, 'B': False, 'C': True, 'D': False}
57+
>>> consistent_with(s, {})
58+
True
59+
>>> consistent_with(s, s)
60+
True
61+
>>> consistent_with(s, {'A': False})
62+
False
63+
>>> consistent_with(s, {'D': True})
64+
False
65+
66+
>>> seed(21); p = rejection_sampling('Earthquake', {}, burglary, 1000)
67+
>>> [p[True], p[False]]
68+
[0.001, 0.999]
69+
70+
>>> seed(71); p = likelihood_weighting('Earthquake', {}, burglary, 1000)
71+
>>> [p[True], p[False]]
72+
[0.002, 0.998]

0 commit comments

Comments
 (0)