-
Notifications
You must be signed in to change notification settings - Fork 1
/
MagicMethod.gs
148 lines (123 loc) · 4.13 KB
/
MagicMethod.gs
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
function normDiff(oldvals, newvals) {
var total = 0
for (var i = 0; i < oldvals.length; i++) {
total += Math.pow(newvals[i] - oldvals[i], 2)
}
return Math.sqrt(total)
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function difficulties_from_scores(scores, actual_scores, test_weights) {
var students = actual_scores.length
var tests = actual_scores[0].length
if (test_weights.length === 1 && Array.isArray(test_weights[0])) {
test_weights = test_weights[0]
}
test_weights = test_weights.map(parseFloat)
if (scores.length === 1 && Array.isArray(scores[0])) {
scores = scores[0]
}
scores = scores.map(parseFloat)
var numerators = Array(tests)
var denominators = Array(tests)
for (var j = 0; j < tests; ++j) {
var numerator = 0
var denominator = 0
for (var i = 0; i < students; ++i) {
if (isNumeric(actual_scores[i][j])) {
numerator += test_weights[j] * scores[i] * parseFloat(actual_scores[i][j])
denominator += test_weights[j] * Math.pow(scores[i], 2)
}
}
numerators[j] = numerator
denominators[j] = denominator
}
var lambda_coeff = 0
var lambda = 1
for (var j = 0; j < tests; ++j) {
lambda_coeff += 1/denominators[j]
lambda -= numerators[j] / denominators[j]
}
lambda /= lambda_coeff
var difficulties = Array(tests)
for (var j = 0; j < tests; ++j) {
difficulties[j] = (numerators[j] + lambda) / denominators[j]
}
return difficulties
}
function scores_from_difficulties(difficulties, actual_scores, test_weights) {
var students = actual_scores.length
var tests = actual_scores[0].length
if (test_weights.length === 1 && Array.isArray(test_weights[0])) {
test_weights = test_weights[0]
}
test_weights = test_weights.map(parseFloat)
if (difficulties.length === 1 && Array.isArray(difficulties[0])) {
difficulties = difficulties[0]
}
difficulties = difficulties.map(parseFloat)
var scores = Array(students)
for (var i = 0; i < students; i++) {
var numerator = 0
var denominator = 0
for (var j = 0; j < tests; j++) {
if (isNumeric(actual_scores[i][j])) {
numerator += test_weights[j] * difficulties[j] * parseFloat(actual_scores[i][j])
denominator += test_weights[j] * Math.pow(difficulties[j], 2)
}
}
scores[i] = numerator / denominator
}
return scores
}
function magicmethod(actual_scores, test_weights, max_scores, tolerance=1e-3) {
var students = actual_scores.length
var tests = actual_scores[0].length
if (!test_weights || test_weights.length === 0) {
test_weights = Array(tests).fill(1)
}
if (Array.isArray(max_scores)) {
if (max_scores.length === 1 && Array.isArray(max_scores[0])) {
max_scores = max_scores[0]
}
max_scores = max_scores.map(parseFloat)
if (max_scores.length === tests) {
for (var i = 0; i < students; ++i) {
for (var j = 0; j < tests; ++j) {
if (isNumeric(actual_scores[i][j])) {
actual_scores[i][j] = parseFloat(actual_scores[i][j]) / max_scores[j]
}
}
}
}
}
if (test_weights.length === 1 && Array.isArray(test_weights[0])) {
test_weights = test_weights[0]
}
test_weights = test_weights.map(parseFloat)
if (test_weights.length < tests) {
test_weights = test_weights.concat(Array(tests - test_weights.length).fill(1))
}
var difficulties = Array(tests)
for (var j = 0; j < tests; ++j) {
difficulties[j] = 1/tests
}
var old_difficulties
var scores
do {
scores = scores_from_difficulties(difficulties, actual_scores, test_weights)
old_difficulties = difficulties
difficulties = difficulties_from_scores(scores, actual_scores, test_weights)
}
while (normDiff(old_difficulties, difficulties) > tolerance)
return [scores, difficulties]
}
function magicscores(actual_scores, test_weights, max_scores, tolerance=1e-3) {
var result = magicmethod(actual_scores, test_weights, max_scores, tolerance)
return result[0]
}
function magicdifficulties(actual_scores, test_weights, max_scores, tolerance=1e-3) {
var result = magicmethod(actual_scores, test_weights, max_scores, tolerance)
return result[1]
}