-
Notifications
You must be signed in to change notification settings - Fork 24
/
HMMProblemSt.cpp
1874 lines (1696 loc) · 74.8 KB
/
HMMProblemSt.cpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012-2017, Michael (Mikhail) Yudelson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Michael (Mikhail) Yudelson nor the
names of other contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "utilsSt.h"
#include "FitBitSt.h"
#include <math.h>
#include "HMMProblemSt.h"
#include <map>
HMMProblemSt::HMMProblemSt() {
}
HMMProblemSt::HMMProblemSt(struct task *task) {
// printf("HMMProblemSt::HMMProblemSt\n");
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
this->model_param_n = task->nK * 4;
init(task);
}// HMMProblemSt
void HMMProblemSt::init(struct task *task) {
// printf("HMMProblemSt::init\n");
// init
this->task = task;
this->non01constraints = true;
this->null_obs_ratios = Calloc(NUMBER, (size_t)this->task->nO);
// this->neg_log_lik = 0;
this->null_skill_obs = 0;
this->null_skill_obs_prob = 0;
if( this->task->solver == METHOD_CGD && this->task->solver_setting == -1)
this->task->solver_setting = 1; // default Fletcher-Reeves
NPAR nS = this->task->nS, nO = this->task->nO;
NCAT nK = this->task->nK, nG = this->task->nG;
//
// setup params
//
this->skill1_n = nS * (1 + nS + nO); // number of params per 1 skill slot
this->param_skill = init1D<NUMBER>( (NDAT)(this->skill1_n*nK) );
this->gradient_skill = init1D<NUMBER>( (NDAT)(this->skill1_n*nK) );
this->active_set_block = initToValue1D<NPAR>((NDAT)nK, 1);
this->unblocked_simplex = initToValue1D<NPAR>((NDAT)(nK * (1 + 2 * nS)), 1);
NPAR i, j, m;
int offset, idx;
// write default parameters first
NUMBER sum = 0;
// vv populate skill parameters
for(NCAT k=0; k<nK; k++) {
// vv populate PI
for(i=0; i<((nS)-1); i++) {
this->param_skill[ k*this->skill1_n + i ] = this->task->init_param_values[i];
sum += this->task->init_param_values[i];
} // ^^ populate PI
this->param_skill[ k*this->skill1_n + nS - 1 ] = 1 - sum;
// vv populate A
offset = (int)(nS-1);
for(i=0; i<nS; i++) {
sum = 0;
for(j=0; j<((nS)-1); j++) {
idx = (int)(offset + i*((nS)-1) + j);
this->param_skill[ k*this->skill1_n + nS + i*nS + j ] = this->task->init_param_values[idx];
sum += this->task->init_param_values[idx];
}
this->param_skill[ k*this->skill1_n + nS + i*nS + nS-1 ] = 1 - sum;
} // ^^ populate A
// vv populate B
offset = (int)((nS-1) + nS*(nS-1));
for(i=0; i<nS; i++) {
sum = 0;
for(m=0; m<((nO)-1); m++) {
idx = (int)(offset + i*((nO)-1) + m);
this->param_skill[ k*this->skill1_n + nS + nS*nS + i*nO + m ] = this->task->init_param_values[idx];
sum += this->task->init_param_values[idx];
}
this->param_skill[ k*this->skill1_n + nS + nS*nS + i*nO + nO - 1 ] = 1 - sum;
} // ^^ populate B
} // ^^ populate skill parameters
// check only first mass-produced skill
if( this->task->do_not_check_constraints==0 && !checkSkillConstraints(this->param_skill)) {
fprintf(stderr,"params do not meet constraints.\n");
exit(1);
}
if(task->initfile[0]!=0) { // from setup file
// if needs be -- read in init params from a file
this->readModel(task->initfile, false /* read and upload but not overwrite*/);
}
// init and populate boundaries
this->lb_param_skill = init1D<NUMBER>(this->skill1_n);
cpy1D(this->task->param_values_lb, this->lb_param_skill, this->skill1_n);
this->ub_param_skill = init1D<NUMBER>(this->skill1_n);
cpy1D(this->task->param_values_ub, this->ub_param_skill, this->skill1_n);
// init supportive data structures
NDAT Nst = this->task->Nst;
this->alpha = init2D<NUMBER>(Nst, nS); // forward variables, 2D array of Nst rows nS values in each ((1..nS)){Nst}
this->po_param_gk = init2D<NUMBER>(nG, nK); //
this->loglik_k = init1D<NUMBER>(nK); //
this->ndat_k = init1D<NDAT>(nK);
this->beta = init2D<NUMBER>(Nst, nS); // backward variables, 2D array of Nst rows nS values in each ((1..nS)){Nst}
this->scale = (this->task->is_scaled==1) ? initToValue1D<NUMBER>(Nst, 0) : NULL;
this->gamma = NULL;
this->xi = NULL;
if(this->task->solver == METHOD_BW) {
this->gamma = init2D<NUMBER>(Nst, nS);
this->xi = init3D<NUMBER>(Nst, nS, nS);
}
this->backward_ix = init1D<NDAT>(Nst); // Nst sized array of pointing to previous student-skill repetition, -1 means none before
this->forward_ix = init1D<NDAT>(Nst); // Nst sized array of pointing to next student-skill repetition, -1 means no more
this->is_fwd_bwd_built=false; // flag for noting whether backward_ix & forward_ix are already built
this->group_skill_map = init3D<NUMBER>(nG, nK, nS);//UNBOOST
}// HMMProblemSt::init
HMMProblemSt::~HMMProblemSt() {
// printf("HMMProblemSt::~HMMProblemSt\n");
NDAT Nst = this->task->Nst;
// printf("before null_obs_ratios\n");
if(this->null_obs_ratios != NULL) free(this->null_obs_ratios);
// printf("before param_skill\n");
if(this->param_skill != NULL) free(this->param_skill);
// printf("before gradient_skill\n");
if(this->gradient_skill != NULL) free(this->gradient_skill);
// printf("before lb_param_skill\n");
if(this->lb_param_skill != NULL) free(this->lb_param_skill);
// printf("before ub_param_skill\n");
if(this->ub_param_skill != NULL) free(this->ub_param_skill);
// printf("before alpha %d\n",this->alpha != NULL);
if(this->alpha != NULL) free2D(this->alpha, Nst);
// printf("before po_param_gk\n");
if(this->po_param_gk != NULL) free2D(this->po_param_gk, this->task->nG);
// printf("before loglik_k\n");
if(this->loglik_k != NULL) free(this->loglik_k);
// printf("before ndat_k\n");
if(this->ndat_k != NULL) free(this->ndat_k);
// printf("before beta\n");
if(this->beta != NULL) free2D(this->beta, Nst);
// printf("before scale\n");
if(this->scale != NULL) free(this->scale);
// printf("before gamma\n");
if(this->gamma != NULL) free2D(this->gamma, Nst);
// printf("before xi\n");
if(this->xi != NULL) free3D(this->xi, Nst, this->task->nS);
// printf("before backward_ix\n");
if(this->backward_ix != NULL) free(this->backward_ix);
// printf("before forward_ix\n");
if(this->forward_ix != NULL) free(this->forward_ix);
free3D(this->group_skill_map, this->task->nG, this->task->nK); //UNBOOST
// printf("before active_set_block\n");
free(this->active_set_block);
// printf("before unblocked_simplex\n");
free(this->unblocked_simplex);
}// ~HMMProblemSt
NDAT HMMProblemSt::PI(NCAT k, NPAR i) {
return this->skill1_n*k + i;
}
NDAT HMMProblemSt::A(NCAT k, NPAR i, NPAR j) {
NPAR nS = this->task->nS;
return this->skill1_n*k + nS + i * nS + j;
}
NDAT HMMProblemSt::B(NCAT k, NPAR i, NPAR o){
NPAR nS = this->task->nS, nO = this->task->nO;
return this->skill1_n*k + (1+nS)*nS + i * nO + o;
}
NUMBER* HMMProblemSt::getPI(NCAT x) {
if( x > (this->task->nK-1) ) {
fprintf(stderr,"While accessing PI, skill index %d exceeded the last skill index %d.\n", x, this->task->nK-1);
exit(1);
}
return &this->param_skill[this->skill1_n*x + 0];
}
NUMBER* HMMProblemSt::getA(NCAT x) {
NPAR nS = this->task->nS;
NCAT nK = this->task->nK;
if( x > (nK-1) ) {
fprintf(stderr,"While accessing PI, skill index %d exceeded the last skill index %d.\n", x, nK-1);
exit(1);
}
return &this->param_skill[this->skill1_n*x + nS];
}
NUMBER* HMMProblemSt::getB(NCAT x) {
NPAR nS = this->task->nS;
NCAT nK = this->task->nK;
if( x > (nK-1) ) {
fprintf(stderr,"While accessing PI, skill index %d exceeded the last skill index %d.\n", x, nK-1);
exit(1);
}
return &this->param_skill[ this->skill1_n*x + nS*(nS+1) ];
}
NUMBER HMMProblemSt::getPI(NCAT x, NPAR i) {
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
NCAT nK = this->task->nK;
if( x > (this->task->nK-1) ) {
fprintf(stderr,"While accessing PI, skill index %d exceeded the last skill index %d.\n", x, nK-1);
exit(1);
}
// checks done
return this->param_skill[this->skill1_n*x + i];
}
NUMBER HMMProblemSt::getA(NCAT x, NPAR i, NPAR j) {
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
NPAR nS = this->task->nS;
NCAT nK = this->task->nK;
if( x > (nK-1) ) {
fprintf(stderr,"While accessing PI, skill index %d exceeded the last skill index %d.\n", x, nK-1);
exit(1);
}
// checks done
return this->param_skill[this->skill1_n*x + nS + i*nS + j];
}
NUMBER HMMProblemSt::getB(NCAT x, NPAR i, NPAR m) {
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
NPAR nS = this->task->nS;
NPAR nO = this->task->nO;
NCAT nK = this->task->nK;
if( x > (nK-1) ) {
fprintf(stderr,"While accessing PI, skill index %d exceeded the last skill index %d.\n", x, nK-1);
exit(1);
}
if(m<0)
return 1;
// checks done
return this->param_skill[this->skill1_n*x + nS * (1 + nS )+ i*nO + m];
}
// getters for computing alpha, beta, gamma
NUMBER HMMProblemSt::getPI(struct context* ctx, NPAR i) {
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
// checks done
return this->param_skill[this->skill1_n*ctx->k + i];
}
// getters for computing alpha, beta, gamma
NUMBER HMMProblemSt::getA (struct context* ctx, NPAR i, NPAR j) {
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
// checks done
NPAR nS = this->task->nS;
return this->param_skill[this->skill1_n*ctx->k + nS + i*nS + j];
}
// getters for computing alpha, beta, gamma
NUMBER HMMProblemSt::getB (struct context* ctx, NPAR i, NPAR m) {
// if(task->structure != STRUCTURE_SKILL) {
// fprintf(stderr,"Model structure specified is not supported and should have been caught earlier\n");
// exit(1);
// }
// checks done
NPAR nS = this->task->nS;
NPAR nO = this->task->nO;
// special attention for "unknonw" observations, i.e. the observation was there but we do not know what it is
// in this case we simply return 1, effectively resulting in no change in \alpha or \beta vatiables
if(m<0)
return 1;
return this->param_skill[this->skill1_n*ctx->k + nS * (1 + nS )+ i*nO + m];
}
bool HMMProblemSt::checkSkillConstraints(NUMBER* param_skill) {
NPAR nS = this->task->nS, nO = this->task->nO;
NCAT skill1_n = nS * (1 + nS + nO); // number of params per 1 skill slot
NUMBER sum = 0.0;
for(NPAR l=0; l<skill1_n; l++) {
if( param_skill[l]>1.0 || param_skill[l]<0.0)
return false;
sum += param_skill[l];
}
if( sum/(skill1_n/2) != 1.0 )
return false;
return true;
}
void HMMProblemSt::initAlphaEtAl() {
NPAR nS = this->task->nS;
NCAT nK = this->task->nK, nG = this->task->nG;
NDAT Nst = this->task->Nst;
NPAR f_is_scaled = this->task->is_scaled==1;
toZero2D(this->alpha, Nst, nS);
// if scaled, new parameter values are multiplied, if not scaled – added
if(f_is_scaled)
toValue2D(this->po_param_gk, nG, nK, 1.0);
else
toZero2D(this->po_param_gk, nG, nK);
toZero1D(this->loglik_k, nK);
toValue3D(this->group_skill_map, (NDAT)nG, (NDAT)nK, (NDAT)nS, -1.0); // -1 means not set (use pLo)
}
NUMBER HMMProblemSt::computeAlphaAndPOParam(NUMBER *metrics_res) {
NPAR nS = this->task->nS, nO = this->task->nO, o = -1, isTarget;
NDAT Nst = this->task->Nst, N = this->task->N;
NCAT g,k, nK = this->task->nK, nG = this->task->nG;
NPAR f_metrics_target_obs = this->task->metrics_target_obs;
NPAR f_is_scaled = this->task->is_scaled==1;
// prepare values
NUMBER loglik = 0.0, loglik_nonull = 0.0, sse = 0.0, sse_nonull = 0.0, ncorr = 0.0, ncorr_nonull = 0.0;
NUMBER p = 0.0;///, corr = 0.0;
// initialize reused parameters before run
// NUMBER ***group_skill_map; // traced state values for student (group) * skill //UNBOOST
// group_skill_map = init3D<NUMBER>(nG, nK, nS);//UNBOOST
// toValue3D(group_skill_map, (NDAT)nG, (NDAT)nK, (NDAT)nS, -1.0); // -1 means not set (use pLo)
initAlphaEtAl();
NDAT **alpha_gk_ix = NULL;
NUMBER **predict = this->task->dat_predict; // running value of prediction, do we save it?
NUMBER *predict_k = this->task->dat_predict_k; // running value of prediction, do we save it?
if(metrics_res!=NULL & this->task->predictions==2) {
if(predict_k==NULL) predict_k = initToValue1D<NUMBER>(this->task->Nst,0.0);
else toZero1D<NUMBER>(predict_k, this->task->Nst);
}
// toZero2D(predict,N,nO); // don't blast everything, the predictions are inited to 0 line by line as the overall data is updated according to the active set
struct context* ctx = new context;
if(!this->is_fwd_bwd_built) { // if helper structures were not built
toValue1D(this->forward_ix, Nst, -1);
toValue1D(this->backward_ix, Nst, -1);
toValue1D(this->ndat_k, nK, 0);
alpha_gk_ix = initToValue2D<NDAT>(nG, nK, -1);
}
for(NDAT t=0; t<N; t++) {
o = this->task->dat_obs[t]; // observation y in the data is 1-right, 0-wrong; math assumes HMM-Scalable 1-right, 2-wrong, with -1 taken out, so 0-right, 1-wrong
isTarget = this->task->metrics_target_obs == o;
//corr = 1-o; // corr 1 - right, 0 - wrong
g = this->task->dat_group[t]; // -1, because in data they were 1-starting
ctx->g=g;
ctx->o=o;
ctx->t=t;
// grab skill array (if exists)
NCAT *ar;
NPAR n, i;
getSkillsAtRow(this->task, t, &ar, &n);
// deal with null skill
if(ar[0]<0 && metrics_res!=NULL) { // account for no skill label only if we need metrics (likely called from predict)
isTarget = this->null_skill_obs==o;
// old: rmse += pow(isTarget - hmm->null_skill_obs_prob,2);
sse += pow(isTarget - this->null_obs_ratios[f_metrics_target_obs],2);
// old: accuracy += isTarget == (hmm->null_skill_obs_prob>=0.5);
ncorr += isTarget == (this->null_obs_ratios[f_metrics_target_obs]==maxn(this->null_obs_ratios,nO) && this->null_obs_ratios[f_metrics_target_obs] > 1/nO);
loglik -= isTarget*safelog(this->null_skill_obs_prob) + (1-isTarget)*safelog(1 - this->null_skill_obs_prob);
for(NPAR m=0; m<nO; m++) {
predict[t][m] = this->null_obs_ratios[m];
}
}
if(ar[0]<0) {
continue;
}
// produce corrects first
producePCorrect(group_skill_map, ar, n, predict[t], ctx); //UNBOOST
NCAT n_active = 0;
for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);
for(int l=0; l<n && n_active==n/**/; l++) {
k = ar[l];
ctx->k=k;
NDAT tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
if(f_is_scaled) this->scale[tt] = 0;
NDAT pre_tt = (this->is_fwd_bwd_built) ? this->backward_ix[tt]: alpha_gk_ix[g][k]; // previous tt index of forward variable alpha, alpha_gk_ix[g][k] is running (prior) tt index of this group/skill (gk)
if(pre_tt==-1) { // it's alpha(1,i)
// compute \alpha_1(i) = \pi_i b_i(o_1)
for(i=0; i<nS; i++) {
this->alpha[tt][i] = getPI(ctx,i) * ((o<0)?1:getB(ctx,i,o)); // if observatiob unknown use 1
if(f_is_scaled) {
this->scale[tt] += this->alpha[tt][i];
}
}
} else { // it's alpha(t,i)
// compute \alpha_{t}(i) = b_j(o_{t})\sum_{j=1}^N{\alpha_{t-1}(j) a_{ji}}
for(NPAR i=0; i<nS; i++) {
for(NPAR j=0; j<nS; j++) {
alpha[tt][i] += alpha[pre_tt][j] * getA(ctx,j,i);
}
this->alpha[tt][i] *= ((o<0)?1:getB(ctx,i,o)); // if observatiob unknown use 1
// if( this->alpha[t][i] < 0 || this->alpha[t][i] > 1)
// fprintf(stderr, "ERROR! alpha value is not within [0, 1] range!\n");
if(f_is_scaled) this->scale[tt] += alpha[tt][i];
}
}
// update backward_ix, forward_ix if necessary
if(!this->is_fwd_bwd_built) {
if( pre_tt != -1 ) { // there exists a prior datapoint for this student and skill
this->forward_ix[pre_tt] = tt; // point from that previous position to current tt index
this->backward_ix[tt] = pre_tt; // point from current position to the previous
}
// now update the last tt index
alpha_gk_ix[g][k] = tt;
// count datapoints per k
this->ndat_k[k]++;
}// update backward_ix, forward_ix if necessary
} // all skills in a row
// update per row values, skill, group, Elo, etc
updateValuesLocal(group_skill_map, ar, n, predict[t], ctx); //UNBOOST
// update obj val
p = safe01num( predict[t][f_metrics_target_obs]);
loglik -= safelog( p)* isTarget + safelog(1-p)*(1-isTarget);
if(metrics_res!=NULL) { // we are predicting
loglik_nonull -= safelog( p)* isTarget + safelog(1-p)*(1-isTarget);
sse += pow(isTarget-predict[t][f_metrics_target_obs],2);
sse_nonull += pow(isTarget-predict[t][f_metrics_target_obs],2);
ncorr += isTarget == (predict[t][f_metrics_target_obs]==maxn(predict[t],nO) && predict[t][f_metrics_target_obs]>1/nO);
ncorr_nonull += isTarget == (predict[t][f_metrics_target_obs]==maxn(predict[t],nO) && predict[t][f_metrics_target_obs]>1/nO);
if(this->task->predictions==2) {
for(int l=0; l<n; l++) {
k = ar[l];
NDAT tt = l + this->task->dat_skill_rix[t];
predict_k[tt] = group_skill_map[g][k][0];
}
}
}
} // all rows in the data
// compute this->po_param_gk and this->loglik_k
for(int t=0; t<N; t++) {
g = this->task->dat_group[t]; // -1, because in data they were 1-starting
NCAT *ar;
NPAR n, i;
getSkillsAtRow(this->task, t, &ar, &n);
NCAT n_active = 0;
for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);
for(int l=0; l<n && (n_active==n)/**/; l++) {
k = ar[l];
ctx->k=k;
NDAT tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
if(f_is_scaled) // if scaled – multiply all scales
this->po_param_gk[g][k] *= this->scale[tt];
else { // if not scaled, add alpha's for the last row in g/k sequence
if(this->forward_ix[tt]==-1) {
for(i=0; i<nS; i++) this->po_param_gk[g][k] += this->alpha[tt][i];
this->loglik_k[k] -= safelog(this->po_param_gk[g][k]);
}
}
}
} // compute this->po_param_gk & compute this->loglik_k
// compute this->loglik_k
// recycle
this->is_fwd_bwd_built = true; // set in any way
if(alpha_gk_ix!=NULL) free2D(alpha_gk_ix, nG);
if(metrics_res != NULL) {
metrics_res[0] = loglik;
metrics_res[1] = loglik_nonull;
metrics_res[2] = sse;
metrics_res[3] = sse_nonull;
metrics_res[4] = ncorr;
metrics_res[5] = ncorr_nonull;
if(this->task->predictions==2) {
this->task->dat_predict_k = predict_k;
}
}
// free3D(group_skill_map, this->task->nG, this->task->nK); //UNBOOST
delete ctx;
return loglik; //TODO, figure out a diff way to sum it, and not multiple times
}
void HMMProblemSt::computeBeta() {
NPAR nS = this->task->nS, o = -1;//, isTarget;
NDAT Nst = this->task->Nst, N = this->task->N;
NCAT g,k;
NPAR f_is_scaled = this->task->is_scaled==1;
struct context* ctx = new context;
toZero2D(this->beta, Nst, nS);
// Backwards pass
for(int t=(N-1); t>=0; t--) {
if(t==96) {
int a =0;
}
o = this->task->dat_obs[t]; // observation y in the data is 1-right, 0-wrong; math assumes HMM-Scalable 1-right, 2-wrong, with -1 taken out, so 0-right, 1-wrong
// isTarget = this->task->metrics_target_obs == o;
g = this->task->dat_group[t]; // -1, because in data they were 1-starting
ctx->g=g;
ctx->o=o;
ctx->t=t;
// grab skill array (if exists)
NCAT *ar;
NPAR n, i, j;
getSkillsAtRow(this->task, t, &ar, &n);
// deal with null skill
if(ar[0]<0) { // if no skill label
// fprintf(stderr, "WARNING! We are not dealing with skill-less observation\n");
continue;
}
NCAT n_active = 0;
for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);
for(NPAR l=0; l<n && (n_active==n)/**/; l++) {
k = ar[l];
ctx->k=k;
NDAT tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
NDAT fwd_ix = this->forward_ix[tt];
if(fwd_ix==-1) { // this is the end of student,skill sequence, no forward index (-1)
for(i=0;i<nS;i++)
beta[tt][i] = (f_is_scaled)?this->scale[tt]:1.0;
} else { // not the end of student,skill sequence
// o here is o from next step in oo_tt
NPAR o_next = this->task->dat_obs_stacked[fwd_ix];
for(i=0; i<nS; i++) {
for(j=0; j<nS; j++)
this->beta[tt][i] += this->beta[fwd_ix][j] * getA(ctx,i,j) * ((o_next<0)?1:getB(ctx,j,o_next)); // if observatiob unknown use 1
if(f_is_scaled) this->beta[tt][i] *= this->scale[tt];
}
}
} // all skills in a row
// tt_off -= fd->skill_n[t]; // tt offset
} // all rows in the data
delete ctx;
}
void HMMProblemSt::computeXiGamma(){
NPAR nS = this->task->nS, o_tp1 = -1, i, j;
NDAT Nst = this->task->Nst, N = this->task->N;
NCAT g,k;
// prepare values
toZero2D(this->gamma, Nst, nS);
toZero3D(this->xi, Nst, nS, nS);
struct context* ctx = new context;
NUMBER denom = 0.0;
for(NDAT t=0; t<N; t++) {
g = this->task->dat_group[t];
ctx->g=g;
ctx->t=t;
// grab skill array (if exists)
NCAT *ar;
NPAR n;
getSkillsAtRow(this->task, t, &ar, &n);
// deal with null skill
if(ar[0]<0) { // if no skill label
// fprintf(stderr, "WARNING! We are not dealing with skill-less observation\n");
continue;
}
NCAT n_active = 0;
for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);
for(int l=0; l<n && (n_active==n)/**/; l++) {
k = ar[l];
ctx->k=k;
NDAT tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
NDAT next_tt = this->forward_ix[tt];
if(next_tt==-1) continue;
o_tp1 = this->task->dat_obs_stacked[next_tt];
denom = 0.0;
for(i=0; i<nS; i++) {
for(j=0; j<nS; j++) {
denom += this->alpha[tt][i] * getA(ctx,i,j) * this->beta[next_tt][j] * ((o_tp1<0)?1:getB(ctx,j,o_tp1));
}
}
for(i=0; i<nS; i++) {
for(j=0; j<nS; j++) {
this->xi[tt][i][j] = this->alpha[tt][i] * getA(ctx,i,j) * this->beta[next_tt][j] * ((o_tp1<0)?1:getB(ctx,j,o_tp1)) / ((denom>0)?denom:1); //
this->gamma[tt][i] += this->xi[tt][i][j];
}
}
} // all row's skills
} // all N rows
delete ctx;
}
NUMBER HMMProblemSt::computeGradients() {
NPAR nS = this->task->nS, o = -1;//, isTarget;
NDAT Nst = this->task->Nst, N = this->task->N, c = 0;
NCAT g,k, nK = this->task->nK;
// prepare values
toZero2D(this->alpha, Nst, nS);
struct context* ctx = new context;
toZero1D(this->gradient_skill, this->skill1_n*nK);
NUMBER loglik = computeAlphaAndPOParam(NULL); // just alpha and p(O|param)
computeBeta();
// this->toFileAlphaBetaObs("trainhmmst_alphabetaobs0.txt", 0); // printing out alpha and beta arrays
// main gradient computation
for(int t=0; t<N; t++) {
o = this->task->dat_obs[t]; // observation y in the data is 1-right, 0-wrong; math assumes HMM-Scalable 1-right, 2-wrong, with -1 taken out, so 0-right, 1-wrong
//isTarget = this->task->metrics_target_obs == o;
g = this->task->dat_group[t]; // -1, because in data they were 1-starting
ctx->g=g;
ctx->o=o;
ctx->t=t;
// grab skill array (if exists)
NCAT *ar;
NPAR n, i, j;
getSkillsAtRow(this->task, t, &ar, &n);
// deal with null skill
if(ar[0]<0) { // if no skill label
// fprintf(stderr, "WARNING! We are not dealing with skill-less observation\n");
continue;
}
NCAT n_active = 0;
for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);
for(int l=0; l<n && (n_active==n)/**/; l++) {
k = ar[l];
ctx->k=k;
NDAT tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
NDAT tt_m1 = this->backward_ix[tt];
// grad PI
if (tt_m1==-1) {
c += (k==0);
for(i=0; i<nS; i++) {
this->gradient_skill[PI(k,i)] -= this->beta[tt][i] * ((o<0)?1:this->param_skill[B(k,i,o)]) / safe0num(this->po_param_gk[g][k]);
//fb->gradPI[i] -= dt->beta[t][i] * ((o<0)?1:fb->B[i][o]) / safe0num(dt->p_O_param);
}
} else {
// grad A
for(i=0; i<nS; i++)
for(j=0; j<nS; j++)
this->gradient_skill[A(k,i,j)] -= this->beta[tt][j] * ((o<0)?1:this->param_skill[B(k,j,o)]) * this->alpha[tt_m1][i] / safe0num(this->po_param_gk[g][k]);
}
// grad B
for(i=0; i<nS; i++)
this->gradient_skill[B(k,i,o)] -= this->alpha[tt][i] * this->beta[tt][i] / safe0num(this->po_param_gk[g][k] * this->param_skill[B(k,i,o)]);
}// for all skills in a row
}// for all rows
// if( this->task->Cslices>0 ) { // penalty
// fb->addL2Penalty(FBV_PI, this->p, (NUMBER)ndat);
// fb->addL2Penalty(FBV_A, this->p, (NUMBER)ndat);
// fb->addL2Penalty(FBV_B, this->p, (NUMBER)ndat);
// }
// NUMBER sum = 0;
// for(int l=0;l<(this->skill1_n*nK);l++) sum += this->gradient_skill[l];
delete ctx;
return loglik;
} // computeGradients()
void HMMProblemSt::toFile(const char *filename) {
// switch(this->task->structure)
// {
// case STRUCTURE_SKILL:
toFileSkill(filename);
// break;
// case STRUCTURE_GROUP:
// toFileGroup(filename);
// break;
// default:
// fprintf(stderr,"Solver specified is not supported.\n");
// break;
// }
}
void HMMProblemSt::toFileSkill(const char *filename) {
NPAR nS = this->task->nS, nO = this->task->nO;
NCAT nK = this->task->nK;
FILE *fid = fopen(filename,"w");
if(fid == NULL) {
fprintf(stderr,"Can't write output model file %s\n",filename);
exit(1);
}
// write solved id
writeSolverInfo(fid, this->task);
fprintf(fid,"Null skill ratios\t");
for(NPAR m=0; m<nO; m++)
fprintf(fid," %10.7f%s",this->null_obs_ratios[m],(m==(this->task->nO-1))?"\n":"\t");
NCAT k;
std::map<NCAT,std::string>::iterator it;
for(k=0;k<nK;k++) {
it = this->task->map_skill_bwd->find(k);
fprintf(fid,"%d\t%s\n",k,it->second.c_str());
NPAR i,j,m;
fprintf(fid,"PI\t");
for(i=0; i<nS; i++)
fprintf(fid,"%12.10f%s",this->param_skill[PI(k,i)],(i==(nS-1))?"\n":"\t");
fprintf(fid,"A\t");
for(i=0; i<nS; i++)
for(j=0; j<nS; j++)
fprintf(fid,"%12.10f%s",this->param_skill[A(k,i,j)],(i==(nS-1) && j==(nS-1))?"\n":"\t");
fprintf(fid,"B\t");
for(i=0; i<nS; i++)
for(m=0; m<nO; m++)
fprintf(fid,"%12.10f%s",this->param_skill[B(k,i,m)],(i==(nS-1) && m==(nO-1))?"\n":"\t");
}
fclose(fid);
}
void HMMProblemSt::toFileGroup(const char *filename) {
NPAR nS = this->task->nS, nO = this->task->nO;
NCAT nG = this->task->nG;
FILE *fid = fopen(filename,"w");
if(fid == NULL) {
fprintf(stderr,"Can't write output model file %s\n",filename);
exit(1);
}
// write solved id
writeSolverInfo(fid, this->task);
fprintf(fid,"Null skill ratios\t");
for(NPAR m=0; m<nO; m++)
fprintf(fid," %10.7f%s",this->null_obs_ratios[m],(m==(nO-1))?"\n":"\t");
NCAT g;
std::map<NCAT,std::string>::iterator it;
for(g=0;g<nG;g++) {
it = this->task->map_group_bwd->find(g);
fprintf(fid,"%d\t%s\n",g,it->second.c_str());
NPAR i,j,m;
fprintf(fid,"PI\t");
for(i=0; i<=nS; i++)
fprintf(fid,"%12.10f%s",this->param_skill[PI(g,i)],(i==(nS-1))?"\n":"\t");
fprintf(fid,"A\t");
for(i=0; i<=nS; i++)
for(j=0; j<=nS; j++)
fprintf(fid,"%12.10f%s",this->param_skill[A(g,i,j)],(i==(nS-1) && j==(nS-1))?"\n":"\t");
fprintf(fid,"B\t");
for(i=0; i<=nS; i++)
for(m=0; m<=nO; m++)
fprintf(fid,"%12.10f%s",this->param_skill[B(g,i,m)],(i==(nS-1) && m==(nO-1))?"\n":"\t");
}
fclose(fid);
}
//void HMMProblemSt::producePCorrect(NUMBER*** group_skill_map, NUMBER* local_pred, NCAT* ks, NCAT nks, struct data* dt) {
void HMMProblemSt::producePCorrect(NUMBER*** group_skill_map, NCAT* skills, NPAR n_skills, NUMBER* local_pred, struct context *ctx) {
NPAR m, i, nO = this->task->nO, nS = this->task->nS;
NCAT k;
NUMBER *local_pred_inner = init1D<NUMBER>(nO);
for(m=0; m<nO; m++) local_pred[m] = 0.0;
for(int l=0; l<n_skills; l++) {
for(m=0; m<nO; m++) local_pred_inner[m] = 0.0;
k = skills[l];
ctx->k=k;
NUMBER* group_skill_map_gk = group_skill_map[ctx->g][k];
for(i=0; i<nS; i++) {
if(/*group_skill_map[ctx->g][k]*/group_skill_map_gk[i]==-1) {
/*group_skill_map[ctx->g][k]*/group_skill_map_gk[i] = getPI(ctx,i);
}
for(m=0; m<nO; m++)
local_pred_inner[m] += /*group_skill_map[ctx->g][k]*/group_skill_map_gk[i] * getB(ctx,i,m);
}
for(m=0; m<nO; m++)
local_pred[m] += local_pred_inner[m];
}
if(n_skills>1) {
for(m=0; m<nO; m++)
local_pred[m] /= n_skills;
}
projectsimplex(local_pred, nO);
free(local_pred_inner);
}
void HMMProblemSt::updateValuesLocal(NUMBER*** group_skill_map, NCAT* skills, NPAR n_skills, NUMBER* local_pred, struct context *ctx) {
NPAR i, j, nS = this->task->nS;//, o = this->task->dat_obs[ctx->t];
NCAT k, g = ctx->g;
NUMBER pLe_denom = 0.0;
NUMBER *pLe = init1D<NUMBER>(nS);
NUMBER* group_skill_map_gk; // TODO, try to see time change
// update pL
for(NPAR l=0; l<n_skills; l++) {
k = skills[l];
ctx->k = k;
group_skill_map_gk = group_skill_map[g][k]; // TODO, try to see time change
// NUMBER* pLbit = gsm(g,k); //BOOST
if(ctx->o>-1) { // known observations //
// update p(L)
pLe_denom = 0.0;
// 1. pLe = (L .* B(:,o)) ./ ( L'*B(:,o)+1e-8 );
for(i=0; i<nS; i++)
pLe_denom += /*group_skill_map[g][k]*/group_skill_map_gk[i] * getB(ctx,i,ctx->o); // TODO: this is local_pred[o]!!!//UNBOOST
// pLe_denom += pLbit[i] * getB(ctx,i,o); //BOOST
for(i=0; i<nS; i++)
pLe[i] = /*group_skill_map[g][k]*/group_skill_map_gk[i] * getB(ctx,i,ctx->o) / safe0num(pLe_denom); //UNBOOST
// pLe[i] = pLbit[i] * getB(ctx,i,o) / safe0num(pLe_denom); //BOOST
// 2. L = (pLe'*A)';
for(i=0; i<nS; i++)
/*group_skill_map[g][k]*/group_skill_map_gk[i]= 0.0; //UNBOOST
// pLbit[i]= 0.0; //BOOST
for(j=0; j<nS; j++)
for(i=0; i<nS; i++)
/*group_skill_map[g][k]*/group_skill_map_gk[j] += pLe[i] * getA(ctx,i,j);//A[i][j]; //UNBOOST
// pLbit[j] += pLe[i] * getA(ctx,i,j);//A[i][j]; //BOOST
} else { // unknown observation
// 2. L = (pL'*A)';
for(i=0; i<nS; i++)
pLe[i] = /*group_skill_map[g][k]*/group_skill_map_gk[i]; // copy first; //UNBOOST
// pLe[i] = pLbit[i]; // copy first; //BOOST
for(i=0; i<nS; i++)
/*group_skill_map[g][k]*/group_skill_map_gk[i] = 0.0; // erase old value //UNBOOST
// pLbit[i] = 0.0; // erase old value //BOOST
for(j=0; j<nS; j++)
for(i=0; i<nS; i++)
/*group_skill_map[g][k]*/group_skill_map_gk[j] += pLe[i] * getA(ctx,i,j);//UNBOOST
// pLbit[j] += pLe[i] * getA(ctx,i,j);//BOOST
}// observations
projectsimplex(/*group_skill_map[g][k]*/group_skill_map_gk, nS); // addition to make sure there's no side effects //UNBOOST
// projectsimplex(pLbit, nS); // addition to make sure there's no side effects //BOOST
}
free(pLe);
}
//void HMMProblemSt::predict(NUMBER* metrics, const char *filename, NPAR* dat_obs, NCAT *dat_group, NCAT *dat_skill, StripedArray<NCAT*> *dat_multiskill) {
void HMMProblemSt::predict(NUMBER* metrics, const char *filename,
//NDAT N, NPAR* dat_obs, NCAT *dat_group, NCAT *dat_skill, NCAT *dat_skill_stacked, NCAT *dat_skill_rcount, NDAT *dat_skill_rix,
struct task* task,
HMMProblemSt **hmms, NPAR nhmms, NPAR *hmm_idx) {
NDAT t;
NPAR i, /*o,*/ h, n, m;
NPAR nS = /*hmms[0]->*/task->nS, nO = /*hmms[0]->*/task->nO;
NCAT *ar, nK = /*hmms[0]->*/task->nK, nG = /*hmms[0]->*/task->nG;
char f_multiskill = /*hmms[0]->*/task->multiskill;
char f_update_known = /*hmms[0]->*/task->update_known;
char f_update_unknown = /*hmms[0]->*/task->update_unknown;
int f_predictions = /*hmms[0]->*/task->predictions;
int f_metrics_target_obs = /*hmms[0]->*/task->metrics_target_obs;
for(i=1; i<nhmms; i++) {
if( nS != hmms[i]->task->nS || nO != hmms[i]->task->nO || nK != hmms[i]->task->nK ||
nG != hmms[i]->task->nG || // N != hmms[i]->task->N || N_null != hmms[i]->task->N_null || // all internal N's are different
f_multiskill != hmms[i]->task->multiskill ||
f_update_known != hmms[i]->task->update_known ||
f_update_unknown != hmms[i]->task->update_unknown ||
f_predictions != hmms[i]->task->predictions ||
f_metrics_target_obs != hmms[i]->task->metrics_target_obs) {
fprintf(stderr,"Error! One of count variables (N, N_null, nS, nO, nK, nG) or flags (multiskill, predictions, metrics_target_obs, update_known, update_unknown) does not have the same value across multiple models\n");
exit(1);
}
}
// initialize and run
// NUMBER ***predict_hmm = init1D<NUMBER**>(nhmms);
NUMBER **metrics_hmm = init2D<NUMBER>(nhmms,6);
NDAT N_all = 0;
NDAT N_nnul_all = 0;
NUMBER loglik_all = 0.0;
NUMBER loglik_nnul_all = 0.0;
NUMBER sse_all = 0.0;
NUMBER sse_nnul_all = 0.0;
NDAT ncorr_all = 0;
NDAT ncorr_nnul_all = 0;
for(NPAR h=0;h<nhmms;h++) {
// predict_hmm[h] = init2D<NUMBER>(hmms[h]->task->N,nO);
hmms[h]->computeAlphaAndPOParam(metrics_hmm[h]);
N_all += hmms[h]->task->N;
N_nnul_all += hmms[h]->task->N_null;
loglik_all += metrics_hmm[h][0];
loglik_nnul_all += metrics_hmm[h][1];
sse_all += metrics_hmm[h][2];
sse_nnul_all += metrics_hmm[h][3];
ncorr_all += (NDAT)metrics_hmm[h][4];
ncorr_nnul_all += (NDAT)metrics_hmm[h][5];
}
// print predictions
FILE *fid = NULL; // file for storing prediction should that be necessary
HMMProblemSt *hmm = NULL;
if(f_predictions>0) {
fid = fopen(filename,"w");
if(fid == NULL)
{
fprintf(stderr,"Can't write output model file %s\n",filename);
exit(1);
}
for(t=0; t<task->N; t++) {
h = (hmm_idx!=NULL)?hmm_idx[t]:0;
//o = dat_obs[t];
hmm = hmms[h];
getSkillsAtRow(hmm->task, t, &ar, &n);
if(ar[0]<0) {// if no skills
for(m=0; m<nO; m++) fprintf(fid,"%12.10f%s",hmm->null_obs_ratios[m],(m<(nO-1))?"\t":"\n");
} else { // skills present
for(m=0; m<nO; m++) {
fprintf(fid,"%12.10f%s",hmm->task->dat_predict[t][m],(m<(nO-1))?"\t": ((f_predictions==1)?"\n":"\t") );// if we print states of KCs, continut
}
if(f_predictions==2) { // if we print out states of KC's as welll
// fprintf(stderr,"WARNING! Support for printing pL is not re-implemented \n");
for(int l=0; l<n; l++) { // all KC here
NDAT tt = l + hmm->task->dat_skill_rix[t];
fprintf(fid,"%12.10f%s",hmm->task->dat_predict_k[tt], (l==(n-1) && l==(n-1))?"\n":"\t"); // nnon boost // if end of all states: end line//UNBOOST
}
}
} // skills presen
}
fclose(fid);
}// print predictions out
// if necessary guess the obsevaion using Pi and B
if(f_update_known=='g') {
fprintf(stderr,"WARNING! Guessing for local update is not re-implemented \n");
// NUMBER max_local_pred=0;
// NPAR ix_local_pred=0;
// for(m=0; m<nO; m++) {
// if( local_pred[m]>max_local_pred ) {
// max_local_pred = local_pred[m];
// ix_local_pred = m;
// }
// }
// o = ix_local_pred;
}
if(f_predictions==3) { // if we print out states of KC's as welll
fprintf(stderr,"WARNING! Writing prediction after the local update is not re-implemented \n");
// for(int l=0; l<n; l++) { // all KC here
// fprintf(fid,"%12.10f%s",group_skill_map[g][ ar[l] ][0], (l==(n-1) && l==(n-1))?"\n":"\t"); // nnon boost // if end of all states: end line//UNBOOST
// // fprintf(fid,"%12.10f%s",gsm(g, ar[l] )[0], (l==(n-1) && l==(n-1))?"\n":"\t"); // if end of all states: end line //BOOST
// }
}
// tally metrics
if(metrics != NULL) {
metrics[0] = loglik_all;
metrics[1] = loglik_nnul_all;
metrics[2] = sqrt(sse_all/N_all);
metrics[3] = sqrt(sse_nnul_all/(N_all-N_nnul_all));
metrics[4] = (NUMBER)ncorr_all/N_all;
metrics[5] = (NUMBER)ncorr_nnul_all/(N_all-N_nnul_all);
}
// recycle