-
Notifications
You must be signed in to change notification settings - Fork 24
/
predicthmm.cpp
335 lines (308 loc) · 12.9 KB
/
predicthmm.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
/*
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 <string.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <map>
#include <list>
#include "utils.h"
#include "HMMProblem.h"
#include "InputUtil.h"
//#include "HMMProblemPiG.h"
#include "HMMProblemPiGK.h"
#include "HMMProblemAGK.h"
#include "HMMProblemPiAGK.h"
#include "HMMProblemPiABGK.h"
#include "HMMProblemComp.h"
#include "HMMProblemSlicedAB.h"
#include "HMMProblemSlicedA.h"
#include "HMMProblemPiGKww.h"
using namespace std;
#define COLUMNS 4
struct param param;
//static char *line = NULL;
NUMBER* metrics;
map<string,NCAT> data_map_group_fwd;
map<NCAT,string> data_map_group_bwd;
map<string,NCAT> map_step;
map<string,NCAT> model_map_skill_fwd;
map<NCAT,string> model_map_skill_bwd;
void exit_with_help();
void parse_arguments(int argc, char **argv, char *input_file_name, char *model_file_name, char *predict_file_name);
void read_predict_data(const char *filename);
void predict(const char *predict_file, HMMProblem *hmm);
int main (int argc, char ** argv) {
clock_t tm0 = clock();
printf("predicthmm starting...\n");
set_param_defaults(¶m);
char input_file[1024];
char model_file[1024];
char predict_file[1024];
parse_arguments(argc, argv, input_file, model_file, predict_file);
// param.predictions = 2; // do not force it on
// read data
if(param.binaryinput==0) {
InputUtil::readTxt(input_file, ¶m);
} else {
InputUtil::readBin(input_file, ¶m);
}
// read model header
FILE *fid = fopen(model_file,"r");
if(fid == NULL)
{
fprintf(stderr,"Can't read model file %s\n",model_file);
exit(1);
}
int max_line_length = 1024;
char *line = Malloc(char,(size_t)max_line_length);
NDAT line_no = 0;
struct param param_model;
set_param_defaults(¶m_model);
bool overwrite = false;
// if(overwrite)
readSolverInfo(fid, ¶m_model, &line_no);
// else
// readSolverInfo(fid, &initparam, &line_no);
// copy partial info from param_model to param
if(param.nO==0) param.nO = param_model.nO;
param.structure = param_model.structure;
param.solver = param_model.solver;
param.solver_setting = param_model.solver_setting;
// copy number of states from the model
param.nS = param_model.nS;
// if number of states or observations >2, then no check
if( param.nO>2 || param.nS>2) {
param.do_not_check_constraints = 1;
}
// copy number of slices from the model
param.nZ = param_model.nZ;
//
// create hmm Object
//
HMMProblem *hmm = NULL;
switch(param.structure)
{
case STRUCTURE_SKILL: // Conjugate Gradient Descent
// case STRUCTURE_GROUP: // Conjugate Gradient Descent
hmm = new HMMProblem(¶m);
break;
// case STRUCTURE_PIg: // Gradient Descent: PI by group, A,B by skill
// hmm = new HMMProblemPiG(¶m);
// break;
case STRUCTURE_SKABslc: // Conjugate Gradient Descent
hmm = new HMMProblemSlicedAB(¶m);
break;
case STRUCTURE_SKAslc: // Conjugate Gradient Descent
hmm = new HMMProblemSlicedA(¶m);
break;
case STRUCTURE_PIgk: // Gradient Descent, pLo=f(K,G), other by K
hmm = new HMMProblemPiGK(¶m);
break;
case STRUCTURE_PIgkww: // Gradient Descent, pLo=f(K,G), other by K
hmm = new HMMProblemPiGKww(¶m);
break;
case STRUCTURE_PIAgk: // Gradient Descent, pLo=f(K,G), pT=f(K,G), other by K
hmm = new HMMProblemPiAGK(¶m);
break;
case STRUCTURE_Agk: // Gradient Descent, pT=f(K,G), other by K
hmm = new HMMProblemAGK(¶m);
break;
// case STRUCTURE_Agki: // Gradient Descent, pT=f(K,G), other by K
// hmm = new HMMProblemAGKi(¶m);
// break;
case STRUCTURE_PIABgk: // Gradient Descent, pT=f(K,G), other by K
hmm = new HMMProblemPiABGK(¶m);
break;
// case BKT_GD_T: // Gradient Descent with Transfer
// hmm = new HMMProblemKT(¶m);
// break;
case STRUCTURE_COMP: // Gradient Descent, pT=f(K,G), other by K
hmm = new HMMProblemComp(¶m);
break;
// case STRUCTURE_ELOK: // Gradient Descent, pT=f(K,G), other by K
// hmm = new HMMProblemEloK(¶m);
// break;
}
// read model body
hmm->readModelBody(fid, ¶m_model, &line_no, overwrite);
fclose(fid);
free(line);
if(param.quiet == 0)
printf("input read, nO=%d, nG=%d, nK=%d, nI=%d, nZ=%d\n",param.nO, param.nG, param.nK, param.nI, param.nZ);
clock_t tm = clock();
// if(param.metrics>0 || param.predictions>0) {
metrics = Calloc(NUMBER, (size_t)7);// LL, AIC, BIC, RMSE, RMSEnonull, Acc, Acc_nonull;
// }
HMMProblem::predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix, &hmm, 1, NULL);
// predict(predict_file, hmm);
if(param.quiet == 0)
printf("predicting is done in %8.6f seconds\n",(NUMBER)(clock()-tm)/CLOCKS_PER_SEC);
//if( param.predictions>0 ) {
printf("trained model LL=%15.7f (%15.7f), AIC=%8.6f, BIC=%8.6f, RMSE=%8.6f (%8.6f), Acc=%8.6f (%8.6f)\n",
metrics[0], metrics[1], // ll's
2*hmm->getNparams() + 2*metrics[0], hmm->getNparams()*safelog(param.N) + 2*metrics[0],
metrics[2], metrics[3], // rmse's
metrics[4], metrics[5]); // acc's
//}
free(metrics);
destroy_input_data(¶m);
delete hmm;
if(param.quiet == 0)
printf("overall time running is %8.6f seconds\n",(NUMBER)(clock()-tm0)/CLOCKS_PER_SEC);
return 0;
}
void exit_with_help() {
printf(
"Usage: predicthmm [options] input_file model_file [predicted_response_file]\n"
"options:\n"
"-q : quiet mode, without output, 0-no (default), or 1-yes\n"
"-d : delimiter for multiple skills per observation; 0-single skill per\n"
" observation (default), otherwise -- delimiter character, e.g. '-d ~'.\n"
"-b : treat input file as binary input file (specifications TBA).\n"
"-p : report model predictions on the train set 0-no (default), 1-yes; 2-yes,\n"
" plus output state probability; works with -v and -m parameters.\n"
"-U : controls how update to the probability distribution of the states is\n"
" updated. Takes the following format '-U r|g[,t|g]', where first\n"
" character controls how prediction treats known observations, second -- how\n"
" prediction treats unknown observations, and third -- whether to output\n"
" probabilities of priors. Dealing with known observations 'r' - reveal\n"
" actual observations for the update of state probability distribution (makes\n"
" sense for modeling how an actual system would work), 'g' - 'guessing' the\n"
" observation based on the predicted outcomes (arg max) -- more appropriate\n"
" when comparing models (so that no information about observation is never\n"
" revealed). Dealing with unknown observations (marked as '.' -- dot): 't' --\n"
" use transition matrix only, 'g' -- 'guess' the observation.\n"
" Default (if ommitted) is '-U r,t'.\n"
" For examle, '-U g,g would require 'guessing' of what the observation was\n"
" using model parameters and the running value of the probabilities of state\n"
" distributions.\n"
);
exit(1);
}
void parse_arguments(int argc, char **argv, char *input_file_name, char *model_file_name, char *predict_file_name) {
// parse command line options, starting from 1 (0 is path to executable)
// go in pairs, looking at whether first in pair starts with '-', if not, stop parsing arguments
int i, n;
char * ch;
for(i=1;i<argc;i++)
{
if(argv[i][0] != '-') break; // end of options stop parsing
if(++i>=argc)
exit_with_help();
switch(argv[i-1][1])
{
case 'q':
param.quiet = (NPAR)atoi(argv[i]);
if(param.quiet!=0 && param.quiet!=1) {
fprintf(stderr,"ERROR! Quiet param should be 0 or 1\n");
exit_with_help();
}
break;
case 'd':
param.multiskill = argv[i][0]; // just grab first character (later, maybe several)
break;
case 'b':
param.binaryinput = atoi( strtok(argv[i],"\t\n\r"));
break;
case 'p':
param.predictions = atoi(argv[i]);
if(param.predictions<0 || param.predictions>3) {
fprintf(stderr,"a flag of whether to report predictions for training data (-p) should be 0, 1, 2, or 3\n");
exit_with_help();
}
break;
case 't':
param.sliced = (NPAR)atoi(argv[i]);
if(param.sliced!=0 && param.sliced!=1) {
fprintf(stderr,"ERROR! Time parameter should be either 0 (off) or 1(on)\n");
exit_with_help();
}
break;
case 'U':
param.update_known = *strtok(argv[i],",\t\n\r");
ch = strtok(NULL, ",\t\n\r");
param.update_unknown = ch[0];
if( (param.update_known!='r' && param.update_known!='g') ||
(param.update_unknown!='t' && param.update_unknown!='g') ) {
fprintf(stderr,"specification of how probabilities of states should be updated (-U) is incorrect, it sould be r|g[,t|g] \n");
exit_with_help();
}
break;
case '0': // what was init_params for 'trainhmm' is now default_params for 'predicthmm'
int len;
len = (int)strlen( argv[i] );
// count delimiters
n = 1; // start with 1
for(int j=0;j<len;j++) {
n += (argv[i][j]==',')?1:0;
if( (argv[i][j] >= 'a' && argv[i][j] <= 'z') || (argv[i][j] >= 'A' && argv[i][j] <= 'Z') ) {
strcpy(param.initfile, argv[i]);
break;
}
}
// init parameters
if(param.init_params!=NULL) free(param.init_params);
param.init_params = Calloc(NUMBER, (size_t)n);
param.init_params_n = (NPAR)n;
// read params and write to params
param.init_params[0] = atof( strtok(argv[i],",\t\n\r") );
for(int j=1; j<n; j++) {
param.init_params[j] = atof( strtok(NULL,",\t\n\r") );
}
break;
default:
fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
exit_with_help();
break;
}
}
if(param.cv_target_obs>0 && param.metrics>0) { // correct for 0-start coding
fprintf(stderr,"values for -v and -m cannot be both non-zeros\n");
exit_with_help();
}
// next argument should be input (training) file name
if(i>=argc) // if not
exit_with_help(); // leave
strcpy(input_file_name, argv[i++]); // copy and advance
if(i>=argc) { // no model file name specified
fprintf(stderr,"no model file specified\n");
exit_with_help(); // leave
}
else {
strcpy(model_file_name,argv[i++]); // copy and advance
if(i>=argc) {// no prediction file name specified
//strcpy(predict_file_name,"predict_hmm.txt"); // the predict file too
param.predictions = 0;
}
else {
// param.predictions = 1;
strcpy(predict_file_name,argv[i]);
}
}
}