-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbinom.pl
208 lines (166 loc) · 7.13 KB
/
dbinom.pl
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
:- module(dbinom, []).
% Binomial density
:- use_module(library(http/html_write)).
:- use_module(session).
:- use_module(table).
:- use_module(r_session).
:- use_module(library(mcclass)).
:- use_module(mathml).
:- use_module(navbar).
navbar:page(dbinom, "Binomial probability").
task(exactprob).
:- discontiguous intermediate/2, expert/5, buggy/5, feedback/4, hint/4, r_hook/1.
math_hook(n, 'N').
math_hook(p0, pi).
mcint:r_hook(p).
mcint:r_hook(n).
mcint:r_hook(k).
mcint:r_hook(p0).
mcint:r_hook(factorial(_N)).
mcint:r_hook(choose(_N, _K)).
mcint:mono((choose)/2, [+, +]).
mcint:mono((factorial)/1, [+]).
render
--> {start(item(_K, N, P0)) },
html(
[ div(class(card), div(class("card-body"),
[ h1(class("card-title"), "Binary outcomes"),
p(class("card-text"),
[ "Consider a clinical study with ", \mmlm(r(N)), " patients. ",
"We assume that the success probability ",
"is ", \mmlm(r(P0)), " in all patients, and that the ",
"successes occur independently."
])]))]).
task(exactprob)
--> { start(item(K, _N, _P0)),
session_data(resp(dbinom, exactprob, Resp), resp(dbinom, exactprob, '#.##'))
},
html(\htmlform([ "What is the probability for exactly ", \mmlm(r(K)), " ",
"successes?" ], exactprob, Resp)).
intermediate(exactprob, item).
start(item(k, n, p0)).
% Recognise as a binomial density
intermediate(exactprob, dbinom).
expert(exactprob, stage(2), From, To, [step(expert, dbinom, [])]) :-
From = item(K, N, P0),
To = { '<-'(p, dbinom(K, N, P0)) }.
feedback(dbinom, [], _Col, Feed) =>
Feed = [ "Correctly recognised the problem as a binomial probability." ].
hint(dbinom, [], _Col, Hint) =>
Hint = [ "This is a binomial probability." ].
% Convert to product
intermediate(exactprob, bernoulli).
expert(exactprob, stage(2), From, To, [step(expert, prod, [K, N, P0])]) :-
From = dbinom(K, N, P0),
To = choose(N, K) * bernoulli(K, N, P0).
feedback(prod, [_K, _N, _P0], _Col, Feed) =>
Feed = [ "Correctly identified the formula for the binomial probability." ].
hint(prod, [K, N, P0], Col, Hint) =>
Hint = [ "The formula for the binomial probability ",
"is ", \mmlm(Col, choose(N, K) * bernoulli(K, N, P0)), "."
].
% Successes and failures
intermediate(exactprob, successes).
intermediate(exactprob, failures).
expert(exactprob, stage(2), From, To, [step(expert, bern, [K, N, P0])]) :-
From = bernoulli(K, N, P0),
To = successes(K, P0) * failures(N - K, 1 - P0).
feedback(bern, [K, N, _P0], Col, Feed) =>
Feed = [ "Correctly determined the probability for a sequence ",
"of ", \mmlm(Col, K), " successes ",
"and ", \mmlm(Col, N - K), " failures."
].
hint(bern, [K, N, _P0], Col, Hint) =>
Hint = [ "Determine the probability for a sequence ",
"of ", \mmlm(Col, K), " successes ",
"and ", \mmlm(Col, N - K), " failures."
].
% Successes
expert(exactprob, stage(2), From, To, [step(expert, success, [K, P0])]) :-
From = successes(K, P0),
To = P0^K.
feedback(success, [K, _P0], Col, Feed) =>
Feed = [ "Correctly determined the probability for ", \mmlm(Col, K), " ",
"independent successes."
].
hint(success, [K, _P0], Col, Hint) =>
Hint = [ "Determine the probability for ", \mmlm(Col, K), " independent ",
"successes."
].
% Failures - same as successes (this may change)
expert(exactprob, stage(2), From, To, [step(expert, failure, [K, P0])]) :-
From = failures(K, P0),
To = P0^K.
feedback(failure, [K, _P0], Col, Feed) =>
Feed = [ "Correctly determined the probability for ", \mmlm(Col, K), " ",
"independent failures."
].
hint(failure, [K, _P0], Col, Hint) =>
Hint = [ "Determine the probability for ", \mmlm(Col, K), " independent ",
"failures."
].
% Forget binomial coefficient
buggy(exactprob, stage(2), From, To, [step(buggy, nochoose, [K, N])]) :-
From = dbinom(K, N, P0),
To = omit_left(nochoose, choose(N, K) * bernoulli(K, N, P0)).
feedback(nochoose, [_K, _N], _Col, Feed) =>
Feed = [ "The binomial coefficient with the number of permutations was ",
"omitted." ].
hint(nochoose, [K, N], Col, Hint) =>
Hint = [ "Do not forget to multiply everything with the number of ",
"permutations ", \mmlm(Col, choose(N, K)), "."
].
% Treat binomial coefficient like a fraction
buggy(exactprob, stage(2), From, To, [step(buggy, choosefrac, [K, N])]) :-
From = choose(N, K),
To = instead(choosefrac, dfrac(N, K), choose(N, K)).
feedback(choosefrac, [K, N], Col, Feed) =>
Feed = [ "Please determine the number of permutations using the ",
"binomial coefficient ",
\mmlm(Col, choose(N, K) =
dfrac(factorial(N), factorial(K)*factorial(N-K))), "." ].
hint(choosefrac, [K, N], Col, Hint) =>
Hint = [ "The number of permutations is determined using the binomial ",
"coefficient ", \mmlm(Col, choose(N, K)), "."
].
% Omit (N-k)! in the denominator of the binomial coefficient
buggy(exactprob, stage(2), From, To, [step(buggy, choosefail, [K, N])]) :-
From = choose(N, K),
To = instead(choosefail, dfrac(factorial(N), factorial(K)), choose(N, K)).
feedback(choosefail, [K, N], Col, Feed) =>
Feed = [ "Please determine the number of permutations using the ",
"binomial coefficient ",
\mmlm(Col, choose(N, K) =
dfrac(factorial(N), factorial(K) * color(choosefail, factorial(N-K)))), "." ].
hint(choosefail, [K, N], Col, Hint) =>
Hint = [ "The number of permutations is determined using the binomial ",
"coefficient ", \mmlm(Col, choose(N, K) =
dfrac(factorial(N), factorial(K)*factorial(N-K))), "."
].
% Confuse successes and failures
buggy(exactprob, stage(2), From, To, [step(buggy, succfail, [K, N, P0])]) :-
From = bernoulli(K, N, P0),
To = instead(succfail, successes(K, 1 - P0), successes(K, P0)) *
instead(succfail, failures(N - K, P0), failures(N - K, 1 - P0)).
feedback(succfail, [_K, _N, P0], Col, Feed) =>
Feed = [ "The probabilities ", \mmlm(Col, color(succfail, P0)), " for ",
"success and ", \mmlm(Col, color(succfail, 1 - P0)), " for ",
"failure were confused."
].
hint(succfail, [_K, _N, P0], Col, Hint) =>
Hint = [ "Make sure not to confuse the ",
"probabilities ", \mmlm(Col, P0), " for success ",
"and ", \mmlm(Col, 1 - P0), " for failure."
].
% Forget failures
buggy(exactprob, stage(2), From, To, [step(buggy, nofail, [K, N, P0])]) :-
From = bernoulli(K, N, P0),
To = omit_right(nofail, successes(K, P0) * failures(N - K, 1 - P0)).
feedback(nofail, [K, N, _P0], Col, Feed) =>
Feed = [ "The probability for the ", \mmlm(Col, color(nofail, N - K)), " ",
"failures was omitted."
].
hint(nofail, [K, N, _P0], Col, Hint) =>
Hint = [ "Make sure not to forget the ",
"probability for the ", \mmlm(Col, N - K), " failures."
].