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