-
Notifications
You must be signed in to change notification settings - Fork 1
/
computeKTparams_SA.java
278 lines (227 loc) · 7.19 KB
/
computeKTparams_SA.java
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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;
public class computeKTparams_SA {
/**
* This class expects data sorted on Skill and then on Student in the below mentioned format
* num lesson student skill cell right eol
* 1 Z3.Three-FactorZCros2008 student102 META-DETERMINE-DXO cell 0 eol
* */
public String students_[] = new String[27600];// Number of instances
public String skill_[] = new String[27600];
public double right_[] = new double[27600];
public int skillends_[] = new int[15];//Number of Skills
public int skillnum = -1;
public final boolean lnminus1_estimation = false;
public final boolean bounded = true;
public final boolean L0Tbounded = false;
public Map<String,Double> top = new HashMap<String, Double>();
public final double stepSize = 0.05;
public final double minVal = 0.000001;
public final Integer totalSteps = 1000000;
class BKTParams {
public double L0, G, S, T;
public BKTParams(Double init) {
if (init < 0) {
this.L0 = Math.random()*top.get("L0");
this.G = Math.random()*top.get("G");
this.S = Math.random()*top.get("S");
this.T = Math.random()*top.get("T");
} else {
this.L0 = init;
this.G = init;
this.S = init;
this.T = init;
}
}
public BKTParams(BKTParams copy, Boolean randStep) {
this.L0 = copy.L0;
this.G = copy.G;
this.S = copy.S;
this.T = copy.T;
if (randStep) {
Double randomchange = Math.random();
Double thisStep = 2.*(Math.random()-0.5)*stepSize;
// Randomly change one of the BKT parameters.
if ( randomchange <= 0.25 ) {
this.L0 = Math.max(Math.min(this.L0 + thisStep,top.get("L0")),minVal);
} else if ( randomchange <= 0.5 ) {
this.T = Math.max(Math.min(this.T + thisStep,top.get("T")),minVal);
} else if ( randomchange <= 0.75 ) {
this.G = Math.max(Math.min(this.G + thisStep,top.get("G")),minVal);
} else {
this.S = Math.max(Math.min(this.S + thisStep,top.get("S")),minVal);
}
}
}
public BKTParams(BKTParams copy) {
this(copy, false);
}
}
public StreamTokenizer create_tokenizer(String infile) {
try {
StreamTokenizer st = new StreamTokenizer(new FileReader(infile));
st.wordChars(95, 95);
return st;
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
return null;
}
public void read_in_data(StreamTokenizer st_) {
int actnum = 0;
try {
int tt = 724;
skillnum = -1;
String prevskill = "FLURG";
tt = st_.nextToken();
tt = st_.nextToken();
tt = st_.nextToken();
tt = st_.nextToken();
tt = st_.nextToken();
tt = st_.nextToken();
tt = st_.nextToken();
while (tt != StreamTokenizer.TT_EOF) {
tt = st_.nextToken(); // num
if (tt == StreamTokenizer.TT_EOF) {
prevskill = skill_[actnum - 1];
if (skillnum > -1)
skillends_[skillnum] = actnum - 1;
break;
}
tt = st_.nextToken(); // lesson
tt = st_.nextToken();
students_[actnum] = st_.sval;
tt = st_.nextToken();
skill_[actnum] = st_.sval;
tt = st_.nextToken(); // cell
tt = st_.nextToken();
right_[actnum] = st_.nval;
tt = st_.nextToken(); // eol
actnum++;
if (!skill_[actnum - 1].equals(prevskill)) {
prevskill = skill_[actnum - 1];
if (skillnum > -1)
skillends_[skillnum] = actnum - 2;
skillnum++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public double findGOOF(int start, int end, BKTParams params) {
double SSR = 0.0;
String prevstudent = "FWORPLEJOHN"; // A random student id.
double prevL = 0.0;
double prevLgivenresult = 0.0;
double newL = 0.0;
double likelihoodcorrect = 0.0;
Integer count = 0;
for (int i = start; i <= end; i++) {
if (!students_[i].equals(prevstudent)) {
prevL = params.L0;
prevstudent = students_[i];
}
if (lnminus1_estimation)
likelihoodcorrect = prevL;
else
likelihoodcorrect = (prevL * (1.0 - params.S)) + ((1.0 - prevL) * params.G);
if ( right_[i] != -1.0 ) {
SSR += (right_[i] - likelihoodcorrect) * (right_[i] - likelihoodcorrect);
count++;
}
if ( right_[i] == -1.0 ) {
prevLgivenresult = prevL;
} else {
prevLgivenresult = right_[i]*((prevL * (1.0 - params.S)) / ((prevL * (1 - params.S)) + ((1.0 - prevL) * (params.G))));
prevLgivenresult += (1-right_[i])*((prevL * params.S) / ((prevL * params.S) + ((1.0 - prevL) * (1.0 - params.G))));
}
newL = prevLgivenresult + (1.0 - prevLgivenresult) * params.T;
prevL = newL;
}
if ( count == 0 ) return 0;
return Math.sqrt(SSR/count); // Using the RMSE instead of the SSR
}
public void fit_skill_model(int curskill) {
if (L0Tbounded) {
top.put("L0",0.85);
top.put("T", 0.3);
} else {
top.put("L0",0.999999);
top.put("T",0.999999);
}
if (bounded) {
top.put("G", 0.3);
top.put("S", 0.1);
} else {
top.put("G",0.999999);
top.put("S",0.999999);
}
// oldParams is randomized.
BKTParams oldParams = new BKTParams(-1.);
BKTParams bestParams = new BKTParams(0.01);
double oldRMSE = 1.;
double newRMSE = 1.;
double bestRMSE = 9999999.0;
double prevBestRMSE = 9999999.0;
double temp = 0.005;
int startact = 0;
if (curskill > 0)
startact = skillends_[curskill - 1] + 1;
int endact = skillends_[curskill];
// Get the initial RMSE.
oldRMSE = findGOOF(startact, endact, oldParams);
for ( Integer i = 0; i < totalSteps; i++ ) {
// Take a random step.
BKTParams newParams = new BKTParams(oldParams, true);
newRMSE = findGOOF(startact, endact, newParams);
if ( Math.random() <= Math.exp((oldRMSE-newRMSE)/temp) ) { // Accept (otherwise move is rejected)
oldParams = new BKTParams(newParams);
oldRMSE = newRMSE;
}
if ( newRMSE < bestRMSE ) { // This method allows the RMSE to increase, but we're interested
bestParams = new BKTParams(newParams); // in the global minimum, so save the minimum values as the "best."
bestRMSE = newRMSE;
}
if ( i % 10000 == 0 && i > 0 ) { // Every 10,000 steps, decrease the "temperature."
if ( bestRMSE == prevBestRMSE ) break; // If the best estimate didn't change, we're done.
prevBestRMSE = bestRMSE;
temp = temp/2.0;
}
}
System.out.print(skill_[startact]);
System.out.print("\t");
System.out.print(bestParams.L0);
System.out.print("\t");
System.out.print(bestParams.G);
System.out.print("\t");
System.out.print(bestParams.S);
System.out.print("\t");
System.out.print(bestParams.T);
System.out.print("\t");
System.out.print(bestRMSE);
System.out.println("\teol");
}
public void computelzerot(String infile_) {
StreamTokenizer st_ = create_tokenizer(infile_);
if (st_ != null) {
read_in_data(st_);
System.out.println("skill\tL0\tG\tS\tT\tRMSE\teol");
for (int curskill = 0; curskill <= skillnum; curskill++) {
fit_skill_model(curskill);
}
}
}
public static void main(String args[]) {
if (args.length < 1) {
System.err.println("Please specify the location of the log file.");
} else {
String infile_ = args[0];//Needs to be tab delimited
computeKTparams_SA m = new computeKTparams_SA();
m.computelzerot(infile_);
}
}
}