This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchi_square_choices.py
executable file
·417 lines (380 loc) · 12.7 KB
/
chi_square_choices.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python
import os
import copy
import random
import string
from scipy.stats.distributions import chi2
### types of errors
# show 3 tables of chi square calculations
# - table 1 the real answer
# - table 2 divide by the observed
# - table 3 random choice of
# - - divide by observed squared
# - - forget to square the top divide by observed squared
# - - forget to square the top divide by observed
# student chooses which one is correct, and decides whether or not to reject the null.
#the table numbering answer choice is off
print("There is a problem with table numbering")
#sys.exit(1)
error_types = {
0: 'divide by observed squared',
1: 'forget to square the top divide by observed squared',
2: 'forget to square the top divide by observed',
}
choices = [
'table 1 is correct and we <b>reject</b> the null hypothesis',
'table 2 is correct and we <b>reject</b> the null hypothesis',
'table 3 is correct and we <b>reject</b> the null hypothesis',
'table 1 is correct and we <b>accept</b> the null hypothesis',
'table 2 is correct and we <b>accept</b> the null hypothesis',
'table 3 is correct and we <b>accept</b> the null hypothesis',
]
#===================
#===================
def get_p_value(chisq, df):
#print("chisq={0}, df={1}".format(chisq, df))
pvalue = chi2.sf(float(chisq), int(df))
return float(pvalue)
#===================
#===================
def get_critical_value(alpha_criterion, df):
critical_value = chi2.ppf(1.0 - float(alpha_criterion), int(df))
#print(chisq)
return float(critical_value)
#===================
#===================
def createObservedProgeny(N=160, ratio="9:2:4:1"):
#female lay 100 eggs per day
bins = ratio.split(':')
floats = []
for b in bins:
floats.append(float(b))
total = sum(floats)
#print(floats)
probs = []
sumprob = 0
for f in floats:
sumprob += f/total
probs.append(sumprob)
#print(probs)
count_dict = {}
for i in range(N):
r = random.random()
for j in range(len(probs)):
if r < probs[j]:
count_dict[j] = count_dict.get(j, 0) + 1
break
#print(count_dict)
count_list = []
for j in range(len(probs)):
count_list.append(count_dict.get(j, 0))
#print(count_list)
return count_list
#===================
#===================
def divideByObservedError(ratio, observed=None):
if observed is None:
observed = [90]
while 88 <= observed[0] <= 92 or 9 <= observed[3] <= 11:
observed = createObservedProgeny(ratio=ratio)
expected = [90,30,30,10]
stats_list = []
chisq = 0.0
for j in range(len(observed)):
row = []
obs = observed[j]
exp = expected[j]
row.append(exp)
row.append(obs)
chirow = (obs-exp)**2/float(obs)
calc = "<sup>({0}-{1})<sup>2</sup></sup>⁄ <sub>{2}</sub>".format(obs, exp, obs)
row.append(calc)
chistr = "{0:.3f}".format(chirow)
row.append(chistr)
chisq += chirow
stats_list.append(row)
chistr = "{0:.3f}".format(chisq)
stats_list.append(chistr)
return stats_list
#===================
#===================
def divideByObservedAndSquareError(ratio, observed=None):
if observed is None:
observed = [90]
while 88 <= observed[0] <= 92 or 9 <= observed[3] <= 11:
observed = createObservedProgeny(ratio=ratio)
expected = [90,30,30,10]
stats_list = []
chisq = 0.0
for j in range(len(observed)):
row = []
obs = observed[j]
exp = expected[j]
row.append(exp)
row.append(obs)
chirow = (obs-exp)**2/float(obs**2)
calc = "<sup>({0}-{1})<sup>2</sup></sup>⁄ <sub>{2}<sup>2</sup></sub>".format(obs, exp, obs)
row.append(calc)
chistr = "{0:.3f}".format(chirow)
row.append(chistr)
chisq += chirow
stats_list.append(row)
chistr = "{0:.3f}".format(chisq)
stats_list.append(chistr)
return stats_list
#===================
#===================
def noSquareError(ratio, observed=None):
if observed is None:
observed = [90]
while 88 <= observed[0] <= 92 or 9 <= observed[3] <= 11:
observed = createObservedProgeny(ratio=ratio)
expected = [90,30,30,10]
stats_list = []
chisq = 0.0
for j in range(len(observed)):
row = []
obs = observed[j]
exp = expected[j]
row.append(exp)
row.append(obs)
chirow = (obs-exp)/float(exp)
calc = "<sup>({0}-{1})</sup>⁄ <sub>{2}</sub>".format(obs, exp, exp)
row.append(calc)
chistr = "{0:.3f}".format(chirow)
row.append(chistr)
chisq += chirow
stats_list.append(row)
chistr = "{0:.3f}".format(chisq)
stats_list.append(chistr)
return stats_list
#===================
#===================
def normalGoodStats(ratio, observed=None):
if observed is None:
observed = [90]
while 88 <= observed[0] <= 92 or 9 <= observed[3] <= 11:
observed = createObservedProgeny(ratio=ratio)
expected = [90,30,30,10]
stats_list = []
chisq = 0.0
for j in range(len(observed)):
row = []
obs = observed[j]
exp = expected[j]
row.append(exp)
row.append(obs)
chirow = (obs-exp)**2/float(exp)
calc = "<sup>({0}-{1})<sup>2</sup></sup>⁄ <sub>{2}</sub>".format(obs, exp, exp)
row.append(calc)
chistr = "{0:.3f}".format(chirow)
row.append(chistr)
chisq += chirow
stats_list.append(row)
chistr = "{0:.3f}".format(chisq)
stats_list.append(chistr)
return stats_list
#===================
#===================
def createDataTable(stats_list, title=None):
numcol = len(stats_list[0])
table = '<table border=1 style="border: 1px solid black; border-collapse: collapse; ">'
table += '<colgroup width="160"></colgroup> '
table += '<colgroup width="80"></colgroup> '
table += '<colgroup width="80"></colgroup> '
table += '<colgroup width="100"></colgroup> '
table += '<colgroup width="80"></colgroup> '
if title is not None:
table += "<tr>"
table += " <th align='center' colspan='5' style='background-color: silver'>{0}</th> ".format(title)
table += "</tr>"
table += "<tr>"
table += " <th align='center' style='background-color: lightgray'>Phenotype</th> "
table += " <th align='center' style='background-color: lightgray'>Expected</th> "
table += " <th align='center' style='background-color: lightgray'>Observed</th> "
table += " <th align='center' style='background-color: lightgray'>Calculation</th> "
table += " <th align='center' style='background-color: lightgray'>Statistic</th> "
table += "</tr>"
table += "<tr>"
table += " <td> Yellow Round (Y–R–)</td>"
for j in range(numcol):
stat = stats_list[0][j]
table += " <td align='center'>{0}</td>".format(stat)
table += "</tr>"
table += "<tr>"
table += " <td> Yellow Wrinkled (Y–rr)</td>"
for j in range(numcol):
stat = stats_list[1][j]
table += " <td align='center'>{0}</td>".format(stat)
table += "</tr>"
table += "<tr>"
table += " <td> Green Round (yyR–)</td>"
for j in range(numcol):
stat = stats_list[2][j]
table += " <td align='center'>{0}</td>".format(stat)
table += "</tr>"
table += "<tr>"
table += " <td> Green Wrinkled (yyrr)</td>"
for j in range(numcol):
stat = stats_list[3][j]
table += " <td align='center'>{0}</td>".format(stat)
table += "</tr>"
table += "<tr>"
table += " <td colspan='{0}' align='right' style='background-color: lightgray'>(sum) χ<sup>2</sup> = </td>".format(numcol)
stat = stats_list[4]
table += " <td align='center'>{0}</td>".format(stat)
table += "</tr>"
table += "</table>"
return table
#===================
#===================
def make_chi_square_table():
max_df = 4
p_values = [0.95, 0.90, 0.75, 0.5, 0.25, 0.1, 0.05, 0.01]
table = '<table border=1 style="border: 1px solid gray; border-collapse: collapse; ">'
table += '<colgroup width="100"></colgroup> '
for p in p_values:
table += '<colgroup width="60"></colgroup> '
table += "<tr>"
table += " <th align='center' colspan='{0}' style='background-color: gainsboro'>Table of Chi-Squared (χ<sup>2</sup>) Critical Values</th>".format(len(p_values)+1)
table += "</tr>"
table += "<tr>"
table += " <th rowspan='2' align='center' style='background-color: silver'>Degrees of Freedom</th>"
table += " <th align='center' colspan='{0}' style='background-color: silver'>Probability</th>".format(len(p_values))
table += "</tr>"
table += "<tr>"
for p in p_values:
table += " <th align='center' style='background-color: gainsboro'>{0:.2f}</th>".format(p)
table += "</tr>"
for df in range(1, max_df+1):
table += "<tr>"
table += " <th align='center' style='background-color: silver'>{0:d}</th>".format(df)
for p in p_values:
chisq = get_critical_value(p, df)
table += " <td align='center'>{0:.2f}</td>".format(chisq)
table += "</tr>"
table += "</table>"
return table
#===================
#===================
def questionContent():
question = ''
question += "<p>Your lab partner is trying again (eye roll) and did another a chi-squared (χ<sup>2</sup>) test "
question += "on the F<sub>2</sub> generation in a dihybid cross based on your lab data (above). "
question += "They wanted to know if the results confirm the expected phenotype ratios.</p>"
question += "<p>You helped them set up the null hypothesis, so you know that part is correct, "
question += "but they got confused and were unsure about how to calculate the "
question += "chi-squared (χ<sup>2</sup>) value. So much so that "
question += "they did it three (3) different ways.</p> "
question += "<p>Before you ask your instructor for a new lab partner, "
question += "<b>tell them which table is correct AND whether they can accept or reject "
question += "the null hypothesis</b> using the information provided.</p> "
return question
#===================
#===================
def getChiSquareResult(final_chisq, df, alpha):
critical_value = get_critical_value(alpha, df)
if final_chisq > critical_value:
return 'reject_null'
elif final_chisq <= critical_value:
return 'accept_null'
return None
#===================
#===================
def makeQuestion(error_type, desired_result):
"""
error type to NOT include
0: 'divide by observed squared',
1: 'forget to square the top divide by observed squared',
2: 'forget to square the top divide by observed',
"""
if desired_result == 'reject':
ratio = '8:2:4:2'
elif desired_result == 'accept':
ratio = '9:3:3:1'
observed = [90, 30, 30, 10]
while 88 <= observed[0] <= 92 or 9 <= observed[3] <= 11 or observed[3] > 19:
observed = createObservedProgeny(ratio=ratio)
chi_square_table = make_chi_square_table()
answer_stat_list = normalGoodStats(ratio, observed)
number_stats = []
i = -1
#print(error_type)
for method in (divideByObservedError, divideByObservedAndSquareError, noSquareError):
i += 1
#print(i)
if i == error_type:
continue
wrong_stats_list = method(ratio, observed)
number_stats.append(wrong_stats_list)
#take the tables and shuffle them
number_stats.append(answer_stat_list)
shuffle_map = list(range(len(number_stats)))
random.shuffle(shuffle_map)
#print(shuffle_map)
shuffled_tables = []
for i, index in enumerate(shuffle_map):
stats_list = number_stats[index]
numbers_table = createDataTable(stats_list, "Table {0}".format(i+1))
shuffled_tables.append(numbers_table)
#use the real values
final_chisq = float(answer_stat_list[-1])
df = 3
alpha = 0.05
result = getChiSquareResult(final_chisq, df, alpha)
if not result.startswith(desired_result):
print("Woah! Unexpected result, it's okay though.")
print(desired_result, "!=", result)
print(final_chisq)
#sys.exit(1)
#print(desired_result, result)
# this line below needed fixing for this problem to work!!!
answer_num = shuffle_map.index(2)
#print("answer_num", answer_num)
if result == 'accept_null':
answer_num += 3
# write the question content
question = questionContent()
complete_question = chi_square_table+" <br/> "
for number_table in shuffled_tables:
complete_question += number_table+" <br/> "
complete_question += " <hr/> "
complete_question += question
return complete_question, answer_num
def getCode():
source = string.ascii_uppercase + string.digits
code = ''
for i in range(5):
code += random.choice(source)
code += ' - '
return code
#===================
#===================
#===================
#===================
if __name__ == '__main__':
duplicates = 1
letters = "ABCDEFGHI"
outfile = 'bbq-' + os.path.splitext(os.path.basename(__file__))[0] + '-questions.txt'
print('writing to file: '+outfile)
f = open(outfile, 'w')
for i in range(duplicates):
for error_type in error_types:
for desired_result in ('accept', 'reject'):
print("")
print(desired_result)
complete_question, answer_num = makeQuestion(error_type, desired_result)
f.write("MC\t" + getCode() + complete_question)
answer = choices[answer_num]
choices_copy = copy.copy(choices)
#random.shuffle(choices_copy)
for k, c in enumerate(choices_copy):
if c == answer:
prefix = "*"
status = "Correct"
else:
prefix = ""
status = "Incorrect"
f.write("\t{0}\t{1}".format(c, status))
print("{0}{1}. {2}".format(prefix, letters[k], c))
f.write("\n")