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

Skip to content

Moved Tests for Probability #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions tests/test_probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,61 @@ def tests():
p = likelihood_weighting('Earthquake', {}, burglary, 1000)
assert p[True], p[False] == (0.002, 0.998)

def test_probdist_basic():
P = ProbDist('Flip')
P['H'], P['T'] = 0.25, 0.75;
assert P['H'] == 0.25

def test_probdist_frequency():
P = ProbDist('X', {'lo': 125, 'med': 375, 'hi': 500})
assert (P['lo'], P['med'], P['hi']) == (0.125, 0.375, 0.5)

def test_probdist_normalize():
P = ProbDist('Flip')
P['H'], P['T'] = 35, 65
P = P.normalize()
assert (P.prob['H'], P.prob['T']) == (0.350, 0.650)

def test_jointprob():
P = JointProbDist(['X', 'Y'])
P[1, 1] = 0.25
assert P[1, 1] == 0.25
P[dict(X=0, Y=1)] = 0.5
assert P[dict(X=0, Y=1)] == 0.5

def test_event_values():
assert event_values ({'A': 10, 'B': 9, 'C': 8}, ['C', 'A']) == (8, 10)
assert event_values ((1, 2), ['C', 'A']) == (1, 2)

def test_enumerate_joint_ask():
P = JointProbDist(['X', 'Y'])
P[0,0] = 0.25
P[0,1] = 0.5
P[1,1] = P[2,1] = 0.125
assert enumerate_joint_ask('X', dict(Y=1),
P).show_approx() == '0: 0.667, 1: 0.167, 2: 0.167'

def test_bayesnode_p():
bn = BayesNode('X', 'Burglary', {T: 0.2, F: 0.625})
assert bn.p(False, {'Burglary': False, 'Earthquake': True}) == 0.375

def test_enumeration_ask():
assert enumeration_ask('Burglary',
dict(JohnCalls=T, MaryCalls=T), burglary).show_approx() == 'False: 0.716, True: 0.284'

def test_elemination_ask():
elimination_ask('Burglary', dict(JohnCalls=T, MaryCalls=T),
burglary).show_approx() == 'False: 0.716, True: 0.284'

def test_rejection_sampling():
random.seed(47)
rejection_sampling('Burglary', dict(JohnCalls=T, MaryCalls=T),
burglary, 10000).show_approx() == 'False: 0.7, True: 0.3'

def test_likelihood_weighting():
random.seed(1017)
assert likelihood_weighting('Burglary', dict(JohnCalls=T, MaryCalls=T),
burglary, 10000).show_approx() == 'False: 0.702, True: 0.298'

if __name__ == '__main__':
pytest.main()