Debian Codegraph @

contrib/ms-sbvar/switch_dw/state_space/sbvar/sbvar_probabilities.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner and Tao Zha
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "switch.h"
19 #include "switchio.h"
20 #include "VARio.h"
21 #include "dw_parse_cmd.h"
22 #include "dw_ascii.h"
23 #include "dw_error.h"
24 #include "dw_sbvar_command_line.h"
25 #include "dw_std.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 /*
31 Attempt to set up model from command line. Command line options are the
32 following
33
34 -filtered
35 If this argument is set, then the filtered probabilities are computed. The
36 default value is off.
37
38 -smoothed
39 If this argument is set, then the smoothed probabilities are computed. The
40 default value is on if neither -filtered or -real_time_smoothed are set.
41
42 -real_time_smoothed
43 If this augument is set, then the smoothed probabilities based on time t
44 information are computed for 0 <= t <= nobs. The default value is off.
45
46 */
47 void dw_sbvar_probabilities_command_line(int nargs, char **args)
48 {
49 char *out_filename;
50 TStateModel *model;
51 TVector *probabilities, **rt_probabilities;
52 int s, t, tau, smoothed=0, filtered=0, real_time=0;
53 FILE *f_out;
54 TVARCommandLine *cmd;
55
56 // Setup model and initial parameters
57 if (!(cmd=CreateTStateModel_VARCommandLine(nargs,args,(TVARCommandLine*)NULL)))
58 dw_Error(MEM_ERR);
59 else
60 if (!cmd->model)
61 dw_UserError("Unable to setup SBVAR from command line parameters");
62 else
63 if (!cmd->parameters_filename_actual)
64 dw_UserError("Unable to initialize parameters from command line parameters");
65 else
66 {
67 model=cmd->model;
68
69 // filtered probabilities
70 if (dw_FindArgument_String(nargs,args,"filtered") != -1)
71 filtered=1;
72
73 // real-time smoothed probabilities
74 if (dw_FindArgument_String(nargs,args,"real_time_smoothed") != -1)
75 real_time=1;
76
77 // smoothed probabilities
78 if ((!filtered && !real_time) || (dw_FindArgument_String(nargs,args,"smoothed") != -1))
79 smoothed=1;
80
81 // Output tag
82 OutTag_VARCommandLine(cmd);
83
84 if (smoothed)
85 {
86 out_filename=CreateFilenameFromTag("%ssmoothed_%s.out",cmd->out_tag,cmd->out_directory);
87 if ((f_out=dw_CreateTextFile(out_filename)))
88 {
89 probabilities=dw_CreateArray_vector(model->sv->nstates);
90 for (s=model->sv->nstates-1; s >= 0; s--)
91 probabilities[s]=ProbabilitiesState((TVector)NULL,s,model);
92 for (t=0; t <= model->nobs; t++)
93 {
94 for (s=0; s < model->sv->nstates; s++)
95 fprintf(f_out,"%lf ",ElementV(probabilities[s],t));
96 fprintf(f_out,"\n");
97 }
98 dw_FreeArray(probabilities);
99 fclose(f_out);
100 }
101 dw_free(out_filename);
102 }
103
104 if (filtered)
105 {
106 out_filename=CreateFilenameFromTag("%sfiltered_%s.out",cmd->out_tag,cmd->out_directory);
107 if ((f_out=dw_CreateTextFile(out_filename)))
108 {
109 probabilities=dw_CreateArray_vector(model->sv->nstates);
110 for (s=model->sv->nstates-1; s >= 0; s--)
111 {
112 probabilities[s]=CreateVector(model->nobs);
113 for (t=model->nobs; t >= 0; t--)
114 ElementV(probabilities[s],t)=ProbabilityStateConditionalCurrent(s,t,model);
115 }
116 for (t=0; t <= model->nobs; t++)
117 {
118 for (s=0; s < model->sv->nstates; s++)
119 fprintf(f_out,"%lf ",ElementV(probabilities[s],t));
120 fprintf(f_out,"\n");
121 }
122 dw_FreeArray(probabilities);
123 fclose(f_out);
124 }
125 dw_free(out_filename);
126 }
127
128 if (real_time)
129 {
130 out_filename=CreateFilenameFromTag("%sreal_time_smoothed_%s.out",cmd->out_tag,cmd->out_directory);
131 if ((f_out=dw_CreateTextFile(out_filename)))
132 {
133 //rt_probabilities=(TVector**)dw_CreateArray_pointer(model->nobs+1,&dw_free);
134 rt_probabilities=(TVector**)dw_CreateArray_array(model->nobs+1);
135 for (tau=model->nobs; tau >= 0; tau--)
136 rt_probabilities[tau]=RealTimeSmoothedProbabilities((TVector*)NULL,tau,model);
137 for (t=0; t <= model->nobs; t++)
138 {
139 for (tau=0; tau < t; tau++)
140 for (s=0; s < model->sv->nstates; s++)
141 fprintf(f_out,", ");
142 for ( ; tau <= model->nobs; tau++)
143 for (s=0; s < model->sv->nstates; s++)
144 fprintf(f_out,"%lf, ",ElementV(rt_probabilities[tau][t],s));
145 fprintf(f_out,"\n");
146 }
147 dw_FreeArray(rt_probabilities);
148 fclose(f_out);
149 }
150 dw_free(out_filename);
151 }
152 }
153
154 Free_VARCommandLine(cmd);
155 }