/
sat2_soln.py
86 lines (61 loc) · 2.1 KB
/
sat2_soln.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""This file contains code used in "Think Bayes",
by Allen B. Downey, available from greenteapress.com
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from __future__ import print_function, division
import math
import thinkbayes2
import thinkplot
class Sat(thinkbayes2.Suite, thinkbayes2.Joint):
"""Represents the distribution of p_correct for a test-taker."""
def Likelihood(self, data, hypo):
"""Computes the likelihood of data under hypo.
data: boolean, whether the answer is correct
hypo: pair of (efficacy, difficulty)
"""
correct = data
e, d = hypo
p = ProbCorrect(e, d)
like = p if correct else 1-p
return like
def ProbCorrect(efficacy, difficulty, a=1):
"""Returns the probability that a person gets a question right.
efficacy: personal ability to answer questions
difficulty: how hard the question is
a: parameter that controls the shape of the curve
Returns: float prob
"""
return 1 / (1 + math.exp(-a * (efficacy - difficulty)))
def Update(p, q, correct):
"""Updates p and q according to correct.
p: prior distribution of efficacy for the test-taker
q: prior distribution of difficulty for the question
returns: pair of new Pmfs
"""
joint = thinkbayes2.MakeJoint(p, q)
suite = Sat(joint)
suite.Update(correct)
p, q = suite.marginal(0, label=p.label), suite.marginal(1, label=q.label)
return p, q
def main():
p1 = thinkbayes2.MakeNormalPmf(0, 1, 3, n=101)
p1.label = 'p1'
p2 = p1.Copy(label='p2')
q1 = thinkbayes2.MakeNormalPmf(0, 1, 3, n=101)
q1.label = 'q1'
q2 = q1.Copy(label='q2')
p1, q1 = Update(p1, q1, True)
p1, q2 = Update(p1, q2, True)
p2, q1 = Update(p2, q1, True)
p2, q2 = Update(p2, q2, False)
thinkplot.PrePlot(num=4, rows=2)
thinkplot.Pmfs([p1, p2])
thinkplot.Config(legend=True)
thinkplot.SubPlot(2)
thinkplot.Pmfs([q1, q2])
thinkplot.Show()
print('Prob p1 > p2', p1 > p2)
print('Prob q1 > q2', q1 > q2)
if __name__ == '__main__':
main()