-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear_regression_model.cpp
354 lines (288 loc) · 13.3 KB
/
linear_regression_model.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
// Code generated by Stan version 2.20.0
#include <stan/model/model_header.hpp>
namespace linear_regression_model_namespace {
using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using stan::io::dump;
using stan::math::lgamma;
using stan::model::prob_grad;
using namespace stan::math;
static int current_statement_begin__;
stan::io::program_reader prog_reader__() {
stan::io::program_reader reader;
reader.add_event(0, 0, "start", "linear_regression.stan");
reader.add_event(17, 15, "end", "linear_regression.stan");
return reader;
}
class linear_regression_model
: public stan::model::model_base_crtp<linear_regression_model> {
private:
double observation;
double covariate;
public:
linear_regression_model(stan::io::var_context& context__,
std::ostream* pstream__ = 0)
: model_base_crtp(0) {
ctor_body(context__, 0, pstream__);
}
linear_regression_model(stan::io::var_context& context__,
unsigned int random_seed__,
std::ostream* pstream__ = 0)
: model_base_crtp(0) {
ctor_body(context__, random_seed__, pstream__);
}
void ctor_body(stan::io::var_context& context__,
unsigned int random_seed__,
std::ostream* pstream__) {
typedef double local_scalar_t__;
boost::ecuyer1988 base_rng__ =
stan::services::util::create_rng(random_seed__, 0);
(void) base_rng__; // suppress unused var warning
current_statement_begin__ = -1;
static const char* function__ = "linear_regression_model_namespace::linear_regression_model";
(void) function__; // dummy to suppress unused var warning
size_t pos__;
(void) pos__; // dummy to suppress unused var warning
std::vector<int> vals_i__;
std::vector<double> vals_r__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
// initialize data block variables from context__
current_statement_begin__ = 2;
context__.validate_dims("data initialization", "observation", "double", context__.to_vec());
observation = double(0);
vals_r__ = context__.vals_r("observation");
pos__ = 0;
observation = vals_r__[pos__++];
current_statement_begin__ = 3;
context__.validate_dims("data initialization", "covariate", "double", context__.to_vec());
covariate = double(0);
vals_r__ = context__.vals_r("covariate");
pos__ = 0;
covariate = vals_r__[pos__++];
// initialize transformed data variables
// execute transformed data statements
// validate transformed data
// validate, set parameter ranges
num_params_r__ = 0U;
param_ranges_i__.clear();
current_statement_begin__ = 7;
num_params_r__ += 1;
current_statement_begin__ = 8;
num_params_r__ += 1;
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
}
~linear_regression_model() { }
void transform_inits(const stan::io::var_context& context__,
std::vector<int>& params_i__,
std::vector<double>& params_r__,
std::ostream* pstream__) const {
typedef double local_scalar_t__;
stan::io::writer<double> writer__(params_r__, params_i__);
size_t pos__;
(void) pos__; // dummy call to supress warning
std::vector<double> vals_r__;
std::vector<int> vals_i__;
current_statement_begin__ = 7;
if (!(context__.contains_r("coefficient")))
stan::lang::rethrow_located(std::runtime_error(std::string("Variable coefficient missing")), current_statement_begin__, prog_reader__());
vals_r__ = context__.vals_r("coefficient");
pos__ = 0U;
context__.validate_dims("parameter initialization", "coefficient", "double", context__.to_vec());
double coefficient(0);
coefficient = vals_r__[pos__++];
try {
writer__.scalar_unconstrain(coefficient);
} catch (const std::exception& e) {
stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable coefficient: ") + e.what()), current_statement_begin__, prog_reader__());
}
current_statement_begin__ = 8;
if (!(context__.contains_r("observation_noise_scale")))
stan::lang::rethrow_located(std::runtime_error(std::string("Variable observation_noise_scale missing")), current_statement_begin__, prog_reader__());
vals_r__ = context__.vals_r("observation_noise_scale");
pos__ = 0U;
context__.validate_dims("parameter initialization", "observation_noise_scale", "double", context__.to_vec());
double observation_noise_scale(0);
observation_noise_scale = vals_r__[pos__++];
try {
writer__.scalar_lb_unconstrain(0.0, observation_noise_scale);
} catch (const std::exception& e) {
stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable observation_noise_scale: ") + e.what()), current_statement_begin__, prog_reader__());
}
params_r__ = writer__.data_r();
params_i__ = writer__.data_i();
}
void transform_inits(const stan::io::var_context& context,
Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
std::ostream* pstream__) const {
std::vector<double> params_r_vec;
std::vector<int> params_i_vec;
transform_inits(context, params_i_vec, params_r_vec, pstream__);
params_r.resize(params_r_vec.size());
for (int i = 0; i < params_r.size(); ++i)
params_r(i) = params_r_vec[i];
}
template <bool propto__, bool jacobian__, typename T__>
T__ log_prob(std::vector<T__>& params_r__,
std::vector<int>& params_i__,
std::ostream* pstream__ = 0) const {
typedef T__ local_scalar_t__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // dummy to suppress unused var warning
T__ lp__(0.0);
stan::math::accumulator<T__> lp_accum__;
try {
stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
// model parameters
current_statement_begin__ = 7;
local_scalar_t__ coefficient;
(void) coefficient; // dummy to suppress unused var warning
if (jacobian__)
coefficient = in__.scalar_constrain(lp__);
else
coefficient = in__.scalar_constrain();
current_statement_begin__ = 8;
local_scalar_t__ observation_noise_scale;
(void) observation_noise_scale; // dummy to suppress unused var warning
if (jacobian__)
observation_noise_scale = in__.scalar_lb_constrain(0.0, lp__);
else
observation_noise_scale = in__.scalar_lb_constrain(0.0);
// model body
current_statement_begin__ = 12;
lp_accum__.add(normal_log<propto__>(coefficient, 0.0, 1.0));
current_statement_begin__ = 13;
lp_accum__.add(exponential_log<propto__>(observation_noise_scale, 1.0));
current_statement_begin__ = 14;
lp_accum__.add(normal_log<propto__>(observation, (coefficient * covariate), observation_noise_scale));
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
lp_accum__.add(lp__);
return lp_accum__.sum();
} // log_prob()
template <bool propto, bool jacobian, typename T_>
T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
std::ostream* pstream = 0) const {
std::vector<T_> vec_params_r;
vec_params_r.reserve(params_r.size());
for (int i = 0; i < params_r.size(); ++i)
vec_params_r.push_back(params_r(i));
std::vector<int> vec_params_i;
return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
}
void get_param_names(std::vector<std::string>& names__) const {
names__.resize(0);
names__.push_back("coefficient");
names__.push_back("observation_noise_scale");
}
void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
dimss__.resize(0);
std::vector<size_t> dims__;
dims__.resize(0);
dimss__.push_back(dims__);
dims__.resize(0);
dimss__.push_back(dims__);
}
template <typename RNG>
void write_array(RNG& base_rng__,
std::vector<double>& params_r__,
std::vector<int>& params_i__,
std::vector<double>& vars__,
bool include_tparams__ = true,
bool include_gqs__ = true,
std::ostream* pstream__ = 0) const {
typedef double local_scalar_t__;
vars__.resize(0);
stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
static const char* function__ = "linear_regression_model_namespace::write_array";
(void) function__; // dummy to suppress unused var warning
// read-transform, write parameters
double coefficient = in__.scalar_constrain();
vars__.push_back(coefficient);
double observation_noise_scale = in__.scalar_lb_constrain(0.0);
vars__.push_back(observation_noise_scale);
double lp__ = 0.0;
(void) lp__; // dummy to suppress unused var warning
stan::math::accumulator<double> lp_accum__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
if (!include_tparams__ && !include_gqs__) return;
try {
if (!include_gqs__ && !include_tparams__) return;
if (!include_gqs__) return;
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
}
template <typename RNG>
void write_array(RNG& base_rng,
Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
bool include_tparams = true,
bool include_gqs = true,
std::ostream* pstream = 0) const {
std::vector<double> params_r_vec(params_r.size());
for (int i = 0; i < params_r.size(); ++i)
params_r_vec[i] = params_r(i);
std::vector<double> vars_vec;
std::vector<int> params_i_vec;
write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
vars.resize(vars_vec.size());
for (int i = 0; i < vars.size(); ++i)
vars(i) = vars_vec[i];
}
std::string model_name() const {
return "linear_regression_model";
}
void constrained_param_names(std::vector<std::string>& param_names__,
bool include_tparams__ = true,
bool include_gqs__ = true) const {
std::stringstream param_name_stream__;
param_name_stream__.str(std::string());
param_name_stream__ << "coefficient";
param_names__.push_back(param_name_stream__.str());
param_name_stream__.str(std::string());
param_name_stream__ << "observation_noise_scale";
param_names__.push_back(param_name_stream__.str());
if (!include_gqs__ && !include_tparams__) return;
if (include_tparams__) {
}
if (!include_gqs__) return;
}
void unconstrained_param_names(std::vector<std::string>& param_names__,
bool include_tparams__ = true,
bool include_gqs__ = true) const {
std::stringstream param_name_stream__;
param_name_stream__.str(std::string());
param_name_stream__ << "coefficient";
param_names__.push_back(param_name_stream__.str());
param_name_stream__.str(std::string());
param_name_stream__ << "observation_noise_scale";
param_names__.push_back(param_name_stream__.str());
if (!include_gqs__ && !include_tparams__) return;
if (include_tparams__) {
}
if (!include_gqs__) return;
}
}; // model
} // namespace
typedef linear_regression_model_namespace::linear_regression_model stan_model;
stan::model::model_base& new_model(
stan::io::var_context& data_context,
unsigned int seed,
std::ostream* msg_stream) {
stan_model* m = new stan_model(data_context, seed, msg_stream);
return *m;
}