Skip to content

Commit a799aea

Browse files
committed
updating stan_csv_reader
1 parent 452f630 commit a799aea

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed

basic_estimators/bernoulli.cpp

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
// Code generated by Stan version 1.2
2+
3+
#include <stan/model/model_header.hpp>
4+
5+
namespace bernoulli_model_namespace {
6+
7+
using std::vector;
8+
using std::string;
9+
using std::stringstream;
10+
using stan::agrad::var;
11+
using stan::model::prob_grad_ad;
12+
using stan::math::get_base1;
13+
using stan::math::stan_print;
14+
using stan::io::dump;
15+
using std::istream;
16+
using namespace stan::math;
17+
using namespace stan::prob;
18+
using namespace stan::agrad;
19+
20+
typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
21+
typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
22+
typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
23+
typedef Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,1> vector_v;
24+
typedef Eigen::Matrix<stan::agrad::var,1,Eigen::Dynamic> row_vector_v;
25+
typedef Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,Eigen::Dynamic> matrix_v;
26+
27+
class bernoulli_model : public prob_grad_ad {
28+
private:
29+
int N;
30+
vector<int> y;
31+
public:
32+
bernoulli_model(stan::io::var_context& context__,
33+
std::ostream* pstream__ = 0)
34+
: prob_grad_ad::prob_grad_ad(0) {
35+
static const char* function__ = "bernoulli_model_namespace::bernoulli_model(%1%)";
36+
(void) function__; // dummy call to supress warning
37+
size_t pos__;
38+
(void) pos__; // dummy call to supress warning
39+
std::vector<int> vals_i__;
40+
std::vector<double> vals_r__;
41+
context__.validate_dims("data initialization", "N", "int", context__.to_vec());
42+
N = int(0);
43+
vals_i__ = context__.vals_i("N");
44+
pos__ = 0;
45+
N = vals_i__[pos__++];
46+
context__.validate_dims("data initialization", "y", "int", context__.to_vec(N));
47+
stan::math::validate_non_negative_index("y", "N", N);
48+
y = std::vector<int>(N,int(0));
49+
vals_i__ = context__.vals_i("y");
50+
pos__ = 0;
51+
size_t y_limit_0__ = N;
52+
for (size_t i_0__ = 0; i_0__ < y_limit_0__; ++i_0__) {
53+
y[i_0__] = vals_i__[pos__++];
54+
}
55+
// validate data
56+
try {
57+
check_greater_or_equal(function__,N,0,"N");
58+
} catch (std::domain_error& e) { throw std::domain_error(std::string("Invalid value of N: ") + std::string(e.what())); };
59+
for (int k0__ = 0; k0__ < N; ++k0__) {
60+
try {
61+
check_greater_or_equal(function__,y[k0__],0,"y[k0__]");
62+
check_less_or_equal(function__,y[k0__],1,"y[k0__]");
63+
} catch (std::domain_error& e) { throw std::domain_error(std::string("Invalid value of y: ") + std::string(e.what())); };
64+
}
65+
66+
// validate transformed data
67+
68+
set_param_ranges();
69+
} // dump ctor
70+
71+
void set_param_ranges() {
72+
num_params_r__ = 0U;
73+
param_ranges_i__.clear();
74+
++num_params_r__;
75+
}
76+
77+
void transform_inits(const stan::io::var_context& context__,
78+
std::vector<int>& params_i__,
79+
std::vector<double>& params_r__) {
80+
stan::io::writer<double> writer__(params_r__,params_i__);
81+
size_t pos__;
82+
std::vector<double> vals_r__;
83+
std::vector<int> vals_i__;
84+
85+
86+
if (!(context__.contains_r("theta")))
87+
throw std::runtime_error("variable theta missing");
88+
vals_r__ = context__.vals_r("theta");
89+
pos__ = 0U;
90+
context__.validate_dims("initialization", "theta", "double", context__.to_vec());
91+
double theta(0);
92+
theta = vals_r__[pos__++];
93+
writer__.scalar_lub_unconstrain(0,1,theta);
94+
params_r__ = writer__.data_r();
95+
params_i__ = writer__.data_i();
96+
}
97+
98+
var log_prob(vector<var>& params_r__,
99+
vector<int>& params_i__,
100+
std::ostream* pstream__ = 0) {
101+
102+
// Note: this is not a memory leak. Memory will be cleaned up with the arena allocator
103+
stan::agrad::vari* DUMMY_VARI_PTR__ = new vari(std::numeric_limits<double>::quiet_NaN(),false);
104+
stan::agrad::var DUMMY_VAR__ = var(DUMMY_VARI_PTR__);
105+
(void) DUMMY_VAR__; // suppress unused var warning
106+
107+
var lp__(0.0);
108+
109+
// model parameters
110+
stan::io::reader<var> in__(params_r__,params_i__);
111+
112+
var theta = in__.scalar_lub_constrain(0,1,lp__);
113+
(void) theta; // supress unused variable warning
114+
115+
// transformed parameters
116+
117+
// initialized transformed params to avoid seg fault on val access
118+
119+
120+
// validate transformed parameters
121+
122+
const char* function__ = "validate transformed params %1%";
123+
(void) function__; // dummy to suppress unused var warning
124+
// model body
125+
lp__ += stan::prob::beta_log<true>(theta, 1, 1);
126+
for (int n = 1; n <= N; ++n) {
127+
lp__ += stan::prob::bernoulli_log<true>(get_base1(y,n,"y",1), theta);
128+
}
129+
130+
return lp__;
131+
132+
} // log_prob(...var...)
133+
134+
135+
void get_param_names(std::vector<std::string>& names__) {
136+
names__.resize(0);
137+
names__.push_back("theta");
138+
}
139+
140+
141+
void get_dims(std::vector<std::vector<size_t> >& dimss__) {
142+
dimss__.resize(0);
143+
std::vector<size_t> dims__;
144+
dims__.resize(0);
145+
dimss__.push_back(dims__);
146+
}
147+
148+
void write_array(std::vector<double>& params_r__,
149+
std::vector<int>& params_i__,
150+
std::vector<double>& vars__,
151+
std::ostream* pstream__ = 0) {
152+
vars__.resize(0);
153+
stan::io::reader<double> in__(params_r__,params_i__);
154+
static const char* function__ = "bernoulli_model_namespace::write_array(%1%)";
155+
(void) function__; // dummy call to supress warning
156+
// read-transform, write parameters
157+
double theta = in__.scalar_lub_constrain(0,1);
158+
vars__.push_back(theta);
159+
160+
// declare and define transformed parameters
161+
double lp__ = 0.0;
162+
(void) lp__; // dummy call to supress warning
163+
164+
165+
// validate transformed parameters
166+
167+
// write transformed parameters
168+
169+
// declare and define generated quantities
170+
171+
172+
// validate generated quantities
173+
174+
// write generated quantities
175+
}
176+
177+
178+
void write_csv_header(std::ostream& o__) {
179+
stan::io::csv_writer writer__(o__);
180+
writer__.comma();
181+
o__ << "theta";
182+
writer__.newline();
183+
}
184+
185+
void write_csv(std::vector<double>& params_r__,
186+
std::vector<int>& params_i__,
187+
std::ostream& o__,
188+
std::ostream* pstream__ = 0) {
189+
stan::io::reader<double> in__(params_r__,params_i__);
190+
stan::io::csv_writer writer__(o__);
191+
static const char* function__ = "bernoulli_model_namespace::write_csv(%1%)";
192+
(void) function__; // dummy call to supress warning
193+
// read-transform, write parameters
194+
double theta = in__.scalar_lub_constrain(0,1);
195+
writer__.write(theta);
196+
197+
// declare, define and validate transformed parameters
198+
double lp__ = 0.0;
199+
(void) lp__; // dummy call to supress warning
200+
201+
202+
203+
// write transformed parameters
204+
205+
// declare and define generated quantities
206+
207+
208+
// validate generated quantities
209+
210+
// write generated quantities
211+
writer__.newline();
212+
}
213+
214+
}; // model
215+
216+
} // namespace
217+
218+
int main(int argc, const char* argv[]) {
219+
try {
220+
stan::gm::nuts_command<bernoulli_model_namespace::bernoulli_model>(argc,argv);
221+
} catch (std::exception& e) {
222+
std::cerr << std::endl << "Exception: " << e.what() << std::endl;
223+
std::cerr << "Diagnostic information: " << std::endl << boost::diagnostic_information(e) << std::endl;
224+
return -1;
225+
}
226+
}
227+

0 commit comments

Comments
 (0)