File size: 9,933 Bytes
cbbfd51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hand-worked cases for Class Test. Run: python3 tests/test_class_test.py"""
import csv, io, math, os, re, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from envs.class_test import gen

def close(a, b, tol=1e-5): return abs(a - b) <= tol
def table(text): return list(csv.DictReader(io.StringIO(text)))

# ---------------------------------------------------------------- marking
key, marks = ["A", "B", "C", "D"], [1, 2, 3, 2]
assert gen.max_marks(key, marks) == 8
# right, right, wrong, blank: 1 + 2 = 3 without a penalty
assert gen.mark_sheet(["A", "B", "D", ""], key, marks) == 3.0
# a quarter off the 3-mark item: 3 - 0.75 = 2.25; a half off: 3 - 1.5 = 1.5. The blank loses nothing.
assert gen.mark_sheet(["A", "B", "D", ""], key, marks, 0.25) == 2.25
assert gen.mark_sheet(["A", "B", "D", ""], key, marks, 0.5) == 1.5
# all wrong with half marks off: -(0.5 + 1 + 1.5 + 1) = -4, recorded as 0
assert gen.mark_sheet(["B", "C", "D", "A"], key, marks, 0.5) == 0.0
# one right (2 marks), three wrong at a quarter: 2 - 0.25 - 0.75 - 0.5 = 0.5
assert gen.mark_sheet(["B", "B", "D", "A"], key, marks, 0.25) == 0.5
# "BD" on the item keyed B is a wrong answer: 1 - 0.5*2 + 3 + 2 = 5
assert gen.mark_sheet(["A", "BD", "C", "D"], key, marks, 0.5) == 5.0
# item 3 withdrawn: maximum 1 + 2 + 2 = 5; a "correct-looking" or wrong answer on it changes nothing
void = ["A", "B", "VOID", "D"]
assert gen.max_marks(void, marks) == 5
assert gen.mark_sheet(["A", "B", "C", "D"], void, marks, 0.5) == 5.0
assert gen.mark_sheet(["A", "C", "A", "D"], void, marks, 0.5) == 2.0      # 1 - 1 + 0 + 2
# grades on the exact percentage: 2.25 / 8 = 28.125%, 5 / 8 = 62.5%
bounds = [("A", 80), ("B", 65), ("C", 50), ("D", 40)]
assert [gen.grade_for(p, bounds) for p in (100, 80, 79.99, 65, 62.5, 50, 49.9, 40, 39.99, 28.125, 0)] == \
       ["A", "A", "B", "B", "C", "C", "D", "D", "U", "U", "U"]

# ---------------------------------------------------------------- item analysis
# 27% groups: 2.7 -> 3, 3.51 -> 4, 5.4 -> 5, 6.48 -> 6, 8.1 -> 8, 9.45 -> 9, 12.15 -> 12
assert [gen.group_size(n) for n in (10, 13, 20, 24, 30, 35, 45)] == [3, 4, 5, 6, 8, 9, 12]
ids = ["S%03d" % i for i in range(1, 11)]
M = [[1, 1, 1, 1],   # S001 total 4
     [1, 1, 1, 0],   # S002 3
     [1, 1, 0, 1],   # S003 3
     [1, 1, 0, 0],   # S004 2
     [1, 0, 1, 0],   # S005 2
     [0, 1, 0, 1],   # S006 2
     [1, 0, 0, 0],   # S007 1
     [0, 0, 1, 0],   # S008 1
     [0, 0, 0, 0],   # S009 0
     [0, 0, 0, 1]]   # S010 1
# facility: Q1 6/10, Q2 5/10, Q3 4/10, Q4 4/10
assert [gen.facility(M, j) for j in range(4)] == [0.6, 0.5, 0.4, 0.4]
# order: S001(4) S002 S003(3) S004 S005 S006(2) S007 S008 S010(1) S009(0); n_group = 3
# upper = S001 S002 S003, lower = S008 S010 S009 (S007 has the same total as S008 and S010 but an earlier id, so it is out)
assert [ids[i] for i in gen.ranked(ids, M)] == ["S001", "S002", "S003", "S004", "S005", "S006", "S007", "S008", "S010", "S009"]
# Q1: (3 - 0) / 3 = 1 (it would be 2/3 if S007 were in the lower group); Q2: (3 - 0) / 3 = 1
# Q3: upper S001 S002 = 2, lower S008 = 1 -> 1/3; Q4: upper S001 S003 = 2, lower S010 = 1 -> 1/3
assert [round(gen.discrimination(ids, M, j), 6) for j in range(4)] == [1.0, 1.0, 0.333333, 0.333333]
# the same rows in another order give the same answer (the order comes from totals and ids, not from row position)
perm = [9, 3, 7, 0, 5, 2, 8, 1, 6, 4]
assert [round(gen.discrimination([ids[i] for i in perm], [M[i] for i in perm], j), 6) for j in range(4)] == [1.0, 1.0, 0.333333, 0.333333]
# totals 4,3,3,2,2,2,1,1,0,1: sum 19, mean 1.9, sum of squares 49; population variance 4.9 - 3.61 = 1.29
tot = [sum(r) for r in M]
assert close(gen.var_p(tot), 1.29, 1e-12)
# KR-20: sum pq = 0.24 + 0.25 + 0.24 + 0.24 = 0.97; 4/3 * (1 - 0.97/1.29) = 4/3 * 0.248062 = 0.330749
assert close(gen.kr20(M), 0.330749, 1e-6)
# point-biserial Q1: right = S001-S005, S007 with totals 4,3,3,2,2,1 -> M1 = 2.5; wrong = 2,1,0,1 -> M0 = 1.0
# s = sqrt(1.29) = 1.135782; (1.5 / 1.135782) * sqrt(0.24) = 1.320676 * 0.489898 = 0.646997
assert close(gen.point_biserial(M, 0), 0.646997, 1e-6)
# Q4: right totals 4,3,2,1 -> 2.5; wrong 3,2,2,1,1,0 -> 1.5; (1 / 1.135782) * 0.489898 = 0.431331
assert close(gen.point_biserial(M, 3), 0.431331, 1e-6)
# the point-biserial is the Pearson correlation of the 0/1 item with the total: Q1 cov = 15/10 - 0.6*1.9 = 0.36,
# r = 0.36 / (sqrt(0.24) * sqrt(1.29)) = 0.646997
def pearson(xs, ys):
    n = float(len(xs)); mx, my = sum(xs) / n, sum(ys) / n
    return sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / math.sqrt(sum((x - mx) ** 2 for x in xs) * sum((y - my) ** 2 for y in ys))
for j in range(4): assert close(gen.point_biserial(M, j), pearson([r[j] for r in M], tot), 1e-12)
# KR-20 of a perfect Guttman pattern on 2 items, 4 students (0,0),(1,0),(1,0),(1,1): p = 0.75, 0.25, sum pq = 0.375;
# totals 0,1,1,2 variance 0.5; KR-20 = 2 * (1 - 0.75) = 0.5
assert close(gen.kr20([[0, 0], [1, 0], [1, 0], [1, 1]]), 0.5, 1e-12)
# KR-20 is Cronbach's alpha for 0/1 items (with population variances throughout): item variance = p(1 - p)
assert close(gen.kr20(M), 4 / 3.0 * (1 - sum(gen.var_p([r[j] for r in M]) for j in range(4)) / gen.var_p(tot)), 1e-12)

# ---------------------------------------------------------------- distractors
resp = ["A"] * 22 + ["B"] * 10 + ["C"] * 1 + ["D"] * 5 + [""] * 2            # 40 students, key A
assert gen.option_counts(resp, "ABCD") == [22, 10, 1, 5]
# 5% of 40 is 2 students: C with 1 (2.5%) is non-functioning, D with 5 (12.5%) is not
assert gen.non_functioning([22, 10, 1, 5], "ABCD", "A", 40) == ["C"]
# exactly 5% (2 of 40) is not "under 5%"
assert gen.non_functioning([22, 10, 2, 4], "ABCD", "A", 40) == []
# 2 of 41 = 4.9% is under; a key chosen by nobody is never listed; five options, two dead ones
assert gen.non_functioning([22, 10, 2, 5], "ABCD", "A", 41) == ["C"]
assert gen.non_functioning([20, 0, 18, 2], "ABCD", "B", 41) == ["D"]
assert gen.non_functioning([0, 30, 1, 12, 17], "ABCDE", "E", 60) == ["A", "C"]
# a distractor beats the key only with strictly more choices
assert gen.beaten([10, 22, 1, 5], "ABCD", "A") is True
assert gen.beaten([10, 10, 1, 5], "ABCD", "A") is False
assert gen.beaten([22, 10, 1, 5], "ABCD", "A") is False

# ---------------------------------------------------------------- moderation
# the textbook set 2,4,4,4,5,5,7,9: mean 5, squared deviations 9+1+1+1+0+0+4+16 = 32, population SD sqrt(32/8) = 2
xs = [2, 4, 4, 4, 5, 5, 7, 9]
assert gen.pmean(xs) == 5.0 and gen.psd(xs) == 2.0
# target mean 60, SD 10: 9 -> 60 + (4/2)*10 = 80; 2 -> 60 - 1.5*10 = 45; 5 -> 60
assert [gen.rescale(x, 5.0, 2.0, 60, 10) for x in (9, 2, 5)] == [80.0, 45.0, 60.0]
# out of 75: 80 is limited to 75. Target mean 10, SD 10: 2 -> 10 - 15 = -5, limited to 0
assert gen.rescale(9, 5.0, 2.0, 60, 10, 75) == 75.0 and gen.rescale(2, 5.0, 2.0, 10, 10, 75) == 0.0
assert gen.rescale(7, 5.0, 2.0, 60, 10, 75) == 70.0
# marker adjustment, pairs are (marker, moderator): differences 3, 2, 4 -> mean 3 > 2, so add 3
assert gen.marker_adjustment([(10, 13), (12, 14), (8, 12)], 2.0) == (3.0, 3.0)
# differences 1, -1, 2 -> mean 2/3, inside the tolerance: nothing added
d, add = gen.marker_adjustment([(10, 11), (12, 11), (8, 10)], 2.0)
assert close(d, 2 / 3.0, 1e-12) and add == 0.0
# generous marker: differences -4, -2 -> mean -3, beyond 2.5 downwards: add -3
assert gen.marker_adjustment([(10, 6), (12, 10)], 2.5) == (-3.0, -3.0)
# a mean difference of exactly the tolerance is not "more than" it
assert gen.marker_adjustment([(10, 12), (12, 14)], 2.0) == (2.0, 0.0)

# ---------------------------------------------------------------- end to end, from the task files
# item analysis, difficulty 3: recompute from responses.csv and key.csv with plain arithmetic
t = gen.generate(4, 3, "item-analysis")
keyof = dict((r["item"], r["key"]) for r in table(t.files["key.csv"]))
rows = [r for r in table(t.files["responses.csv"]) if r["status"] != "absent"]
items = sorted(keyof, key=lambda q: int(q[1:]))
score = dict((r["student_id"], [1 if r[q] == keyof[q] else 0 for q in items]) for r in rows)
n, k = len(score), len(items)
total = dict((s, sum(v)) for s, v in score.items())
mean_t = sum(total.values()) / float(n)
var_t = sum((x - mean_t) ** 2 for x in total.values()) / n
order = sorted(score, key=lambda s: (-total[s], s))
g = int(0.27 * n + 0.5)
want = dict((c["path"], c) for c in t.checks if "path" in c)
assert want["n_students"]["value"] == n and want["n_group"]["value"] == g
ps = []
for j, q in enumerate(items):
    p = sum(v[j] for v in score.values()) / float(n); ps.append(p)
    disc = (sum(score[s][j] for s in order[:g]) - sum(score[s][j] for s in order[n - g:])) / float(g)
    r_pb = pearson([score[s][j] for s in order], [total[s] for s in order])
    assert abs(want["items.%s.facility" % q]["value"] - p) <= 0.001
    assert abs(want["items.%s.discrimination" % q]["value"] - disc) <= 0.001
    assert abs(want["items.%s.point_biserial" % q]["value"] - r_pb) <= 0.001
assert abs(want["kr20"]["value"] - k / (k - 1.0) * (1 - sum(p * (1 - p) for p in ps) / var_t)) <= 0.001
# moderation, difficulty 1: population SD and the linear formula, from marks.csv and the notes
t = gen.generate(3, 1, "moderation")
notes = t.files["moderation_notes.md"]
tm = float(re.search(r"target mean is (\d+)", notes).group(1)); ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1))
mk = dict((r["student_id"], float(r["mark"])) for r in table(t.files["marks.csv"]))
m = sum(mk.values()) / len(mk); sd = math.sqrt(sum((x - m) ** 2 for x in mk.values()) / len(mk))
want = dict((c["path"], c) for c in t.checks if "path" in c)
assert abs(want["raw_mean"]["value"] - m) <= 0.01 and abs(want["raw_sd"]["value"] - sd) <= 0.01
for s, x in mk.items(): assert abs(want["students.%s.scaled" % s]["value"] - (tm + (x - m) / sd * ts)) <= 0.1

print("class_test: all hand-worked cases pass")