Skip to content

Instantly share code, notes, and snippets.

@SteveBronder
Created July 9, 2020 17:35
Show Gist options
  • Select an option

  • Save SteveBronder/2adad071633820119e3b33bf4faebad6 to your computer and use it in GitHub Desktop.

Select an option

Save SteveBronder/2adad071633820119e3b33bf4faebad6 to your computer and use it in GitHub Desktop.
// Code generated by stanc 458a7933
#include <stan/model/model_header.hpp>
namespace bernoulli_model_namespace {
inline void validate_positive_index(const char* var_name, const char* expr,
int val) {
if (val < 1) {
std::stringstream msg;
msg << "Found dimension size less than one in simplex declaration"
<< "; variable=" << var_name << "; dimension size expression=" << expr
<< "; expression value=" << val;
std::string msg_str(msg.str());
throw std::invalid_argument(msg_str.c_str());
}
}
inline void validate_unit_vector_index(const char* var_name, const char* expr,
int val) {
if (val <= 1) {
std::stringstream msg;
if (val == 1) {
msg << "Found dimension size one in unit vector declaration."
<< " One-dimensional unit vector is discrete"
<< " but the target distribution must be continuous."
<< " variable=" << var_name << "; dimension size expression=" << expr;
} else {
msg << "Found dimension size less than one in unit vector declaration"
<< "; variable=" << var_name << "; dimension size expression=" << expr
<< "; expression value=" << val;
}
std::string msg_str(msg.str());
throw std::invalid_argument(msg_str.c_str());
}
}
using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using std::pow;
using stan::io::dump;
using stan::math::lgamma;
using stan::model::model_base_crtp;
using stan::model::rvalue;
using stan::model::cons_list;
using stan::model::index_uni;
using stan::model::index_max;
using stan::model::index_min;
using stan::model::index_min_max;
using stan::model::index_multi;
using stan::model::index_omni;
using stan::model::nil_index_list;
using namespace stan::math;
static int current_statement__ = 0;
static const std::vector<string> locations_array__ = {" (found before start of program)",
" (in 'examples/bernoulli/bernoulli.stan', line 6, column 2 to column 66)",
" (in 'examples/bernoulli/bernoulli.stan', line 2, column 2 to column 27)"};
class bernoulli_model final : public model_base_crtp<bernoulli_model> {
private:
int pos__;
std::vector<std::vector<int>> data_indices;
public:
~bernoulli_model() final { }
std::string model_name() const final { return "bernoulli_model"; }
std::vector<std::string> model_compile_info() const {
std::vector<std::string> stanc_info;
stanc_info.push_back("stanc_version = stanc3 458a7933");
stanc_info.push_back("stancflags = ");
return stanc_info;
}
bernoulli_model(stan::io::var_context& context__,
unsigned int random_seed__ = 0,
std::ostream* pstream__ = nullptr) : model_base_crtp(0) {
using local_scalar_t__ = double ;
boost::ecuyer1988 base_rng__ =
stan::services::util::create_rng(random_seed__, 0);
(void) base_rng__; // suppress unused var warning
static const char* function__ = "bernoulli_model_namespace::bernoulli_model";
(void) function__; // suppress unused var warning
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
pos__ = std::numeric_limits<int>::min();
pos__ = 1;
current_statement__ = 2;
validate_non_negative_index("data_indices", "10", 10);
current_statement__ = 2;
validate_non_negative_index("data_indices", "10", 10);
current_statement__ = 2;
context__.validate_dims("data initialization","data_indices","int",
context__.to_vec(10, 10));
data_indices = std::vector<std::vector<int>>(10, std::vector<int>(10, std::numeric_limits<int>::min()));
{
std::vector<int> data_indices_flat__;
current_statement__ = 2;
assign(data_indices_flat__, nil_index_list(),
context__.vals_i("data_indices"),
"assigning variable data_indices_flat__");
current_statement__ = 2;
pos__ = 1;
current_statement__ = 2;
for (int sym1__ = 1; sym1__ <= 10; ++sym1__) {
current_statement__ = 2;
for (int sym2__ = 1; sym2__ <= 10; ++sym2__) {
current_statement__ = 2;
assign(data_indices,
cons_list(index_uni(sym2__),
cons_list(index_uni(sym1__), nil_index_list())),
data_indices_flat__[(pos__ - 1)],
"assigning variable data_indices");
current_statement__ = 2;
pos__ = (pos__ + 1);}}
}
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
num_params_r__ = 0U;
try {
current_statement__ = 1;
validate_non_negative_index("stuff", "(1 ? 3 : 5)", (1 ? 3 : 5));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1),
nil_index_list())), "data_indices"));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(2), cons_list(index_uni(2), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2),
nil_index_list())), "data_indices"));
num_params_r__ += (1 ? 3 : 5) * rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1),
nil_index_list())),
"data_indices") * rvalue(
data_indices,
cons_list(
index_uni(2),
cons_list(
index_uni(2),
nil_index_list())),
"data_indices");
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
}
template <bool propto__, bool jacobian__, typename T__>
inline T__ log_prob(std::vector<T__>& params_r__,
std::vector<int>& params_i__,
std::ostream* pstream__ = nullptr) const {
using local_scalar_t__ = T__;
T__ lp__(0.0);
stan::math::accumulator<T__> lp_accum__;
static const char* function__ = "bernoulli_model_namespace::log_prob";
(void) function__; // suppress unused var warning
stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
current_statement__ = 1;
validate_non_negative_index("stuff", "(1 ? 3 : 5)", (1 ? 3 : 5));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1),
nil_index_list())), "data_indices"));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(2), cons_list(index_uni(2), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2),
nil_index_list())), "data_indices"));
std::vector<Eigen::Matrix<local_scalar_t__, -1, -1>> stuff;
stuff = std::vector<Eigen::Matrix<local_scalar_t__, -1, -1>>((1 ? 3 : 5), Eigen::Matrix<local_scalar_t__, -1, -1>(
rvalue(data_indices,
cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),
"data_indices"), rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices")));
stan::math::fill(stuff, DUMMY_VAR__);
current_statement__ = 1;
for (int sym1__ = 1; sym1__ <= (1 ? 3 : 5); ++sym1__) {
current_statement__ = 1;
assign(stuff, cons_list(index_uni(sym1__), nil_index_list()),
in__.matrix(
rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())), "data_indices"),
rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())), "data_indices")),
"assigning variable stuff");}
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// 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 <typename RNG>
inline void write_array(RNG& base_rng__, std::vector<double>& params_r__,
std::vector<int>& params_i__,
std::vector<double>& vars__,
bool emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = true,
std::ostream* pstream__ = nullptr) const {
using local_scalar_t__ = double;
vars__.resize(0);
stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
static const char* function__ = "bernoulli_model_namespace::write_array";
(void) function__; // suppress unused var warning
(void) function__; // suppress unused var warning
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
try {
current_statement__ = 1;
validate_non_negative_index("stuff", "(1 ? 3 : 5)", (1 ? 3 : 5));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1),
nil_index_list())), "data_indices"));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(2), cons_list(index_uni(2), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2),
nil_index_list())), "data_indices"));
std::vector<Eigen::Matrix<double, -1, -1>> stuff;
stuff = std::vector<Eigen::Matrix<double, -1, -1>>((1 ? 3 : 5), Eigen::Matrix<double, -1, -1>(
rvalue(data_indices,
cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),
"data_indices"), rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices")));
stan::math::fill(stuff, std::numeric_limits<double>::quiet_NaN());
current_statement__ = 1;
for (int sym1__ = 1; sym1__ <= (1 ? 3 : 5); ++sym1__) {
current_statement__ = 1;
assign(stuff, cons_list(index_uni(sym1__), nil_index_list()),
in__.matrix(
rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())), "data_indices"),
rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())), "data_indices")),
"assigning variable stuff");}
for (int sym1__ = 1;
sym1__ <= rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices"); ++sym1__) {
for (int sym2__ = 1;
sym2__ <= rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())),
"data_indices"); ++sym2__) {
for (int sym3__ = 1; sym3__ <= (1 ? 3 : 5); ++sym3__) {
vars__.emplace_back(
rvalue(stuff,
cons_list(index_uni(sym3__),
cons_list(index_uni(sym2__),
cons_list(index_uni(sym1__), nil_index_list()))),
"stuff"));}}}
if (logical_negation((primitive_value(emit_transformed_parameters__) ||
primitive_value(emit_generated_quantities__)))) {
return ;
}
if (logical_negation(emit_generated_quantities__)) {
return ;
}
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
} // write_array()
inline void transform_inits(const stan::io::var_context& context__,
std::vector<int>& params_i__,
std::vector<double>& vars__,
std::ostream* pstream__) const
final {
using local_scalar_t__ = double;
vars__.clear();
vars__.reserve(num_params_r__);
try {
int pos__;
pos__ = std::numeric_limits<int>::min();
pos__ = 1;
current_statement__ = 1;
validate_non_negative_index("stuff", "(1 ? 3 : 5)", (1 ? 3 : 5));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1),
nil_index_list())), "data_indices"));
current_statement__ = 1;
validate_non_negative_index("stuff",
"rvalue(data_indices,\n cons_list(index_uni(2), cons_list(index_uni(2), nil_index_list())),\n \"data_indices\")",
rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2),
nil_index_list())), "data_indices"));
std::vector<Eigen::Matrix<double, -1, -1>> stuff;
stuff = std::vector<Eigen::Matrix<double, -1, -1>>((1 ? 3 : 5), Eigen::Matrix<double, -1, -1>(
rvalue(data_indices,
cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())),
"data_indices"), rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices")));
stan::math::fill(stuff, std::numeric_limits<double>::quiet_NaN());
{
std::vector<local_scalar_t__> stuff_flat__;
current_statement__ = 1;
assign(stuff_flat__, nil_index_list(), context__.vals_r("stuff"),
"assigning variable stuff_flat__");
current_statement__ = 1;
pos__ = 1;
current_statement__ = 1;
for (int sym1__ = 1;
sym1__ <= rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices"); ++sym1__) {
current_statement__ = 1;
for (int sym2__ = 1;
sym2__ <= rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())),
"data_indices"); ++sym2__) {
current_statement__ = 1;
for (int sym3__ = 1; sym3__ <= (1 ? 3 : 5); ++sym3__) {
current_statement__ = 1;
assign(stuff,
cons_list(index_uni(sym3__),
cons_list(index_uni(sym2__),
cons_list(index_uni(sym1__), nil_index_list()))),
stuff_flat__[(pos__ - 1)], "assigning variable stuff");
current_statement__ = 1;
pos__ = (pos__ + 1);}}}
}
for (int sym1__ = 1; sym1__ <= (1 ? 3 : 5); ++sym1__) {
for (int sym2__ = 1;
sym2__ <= rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices"); ++sym2__) {
for (int sym3__ = 1;
sym3__ <= rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())),
"data_indices"); ++sym3__) {
vars__.emplace_back(
rvalue(stuff,
cons_list(index_uni(sym1__),
cons_list(index_uni(sym3__),
cons_list(index_uni(sym2__), nil_index_list()))),
"stuff"));}}}
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
} // transform_inits()
inline void get_param_names(std::vector<std::string>& names__) const {
names__.clear();
names__.emplace_back("stuff");
} // get_param_names()
inline void get_dims(std::vector<std::vector<size_t>>& dimss__) const
final {
dimss__.clear();
dimss__.emplace_back(std::vector<size_t>{static_cast<size_t>((1 ? 3 : 5))
,
static_cast<size_t>(rvalue(
data_indices,
cons_list(
index_uni(1),
cons_list(
index_uni(1),
nil_index_list())),
"data_indices"))
,
static_cast<size_t>(rvalue(
data_indices,
cons_list(
index_uni(2),
cons_list(
index_uni(2),
nil_index_list())),
"data_indices"))
});
} // get_dims()
inline void constrained_param_names(
std::vector<std::string>& param_names__,
bool emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = true) const
final {
for (int sym1__ = 1;
sym1__ <= rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices"); ++sym1__) {
{
for (int sym2__ = 1;
sym2__ <= rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())),
"data_indices"); ++sym2__) {
{
for (int sym3__ = 1; sym3__ <= (1 ? 3 : 5); ++sym3__) {
{
param_names__.emplace_back(std::string() + "stuff" + '.' + std::to_string(sym3__) + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__));
}}
}}
}}
if (emit_transformed_parameters__) {
}
if (emit_generated_quantities__) {
}
} // constrained_param_names()
inline void unconstrained_param_names(
std::vector<std::string>& param_names__,
bool emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = true) const
final {
for (int sym1__ = 1;
sym1__ <= rvalue(data_indices,
cons_list(index_uni(2),
cons_list(index_uni(2), nil_index_list())),
"data_indices"); ++sym1__) {
{
for (int sym2__ = 1;
sym2__ <= rvalue(data_indices,
cons_list(index_uni(1),
cons_list(index_uni(1), nil_index_list())),
"data_indices"); ++sym2__) {
{
for (int sym3__ = 1; sym3__ <= (1 ? 3 : 5); ++sym3__) {
{
param_names__.emplace_back(std::string() + "stuff" + '.' + std::to_string(sym3__) + '.' + std::to_string(sym2__) + '.' + std::to_string(sym1__));
}}
}}
}}
if (emit_transformed_parameters__) {
}
if (emit_generated_quantities__) {
}
} // unconstrained_param_names()
inline std::string get_constrained_sizedtypes() const {
stringstream s__;
s__ << "[{\"name\":\"stuff\",\"type\":{\"name\":\"array\",\"length\":" << (1 ? 3 : 5) << ",\"element_type\":{\"name\":\"matrix\",\"rows\":" << rvalue(data_indices, cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())), \\"data_indices\\") << ",\"cols\":" << rvalue(data_indices, cons_list(index_uni(2), cons_list(index_uni(2), nil_index_list())), \\"data_indices\\") << "}},\"block\":\"parameters\"}]";
return s__.str();
} // get_constrained_sizedtypes()
inline std::string get_unconstrained_sizedtypes() const {
stringstream s__;
s__ << "[{\"name\":\"stuff\",\"type\":{\"name\":\"array\",\"length\":" << (1 ? 3 : 5) << ",\"element_type\":{\"name\":\"matrix\",\"rows\":" << rvalue(data_indices, cons_list(index_uni(1), cons_list(index_uni(1), nil_index_list())), \\"data_indices\\") << ",\"cols\":" << rvalue(data_indices, cons_list(index_uni(2), cons_list(index_uni(2), nil_index_list())), \\"data_indices\\") << "}},\"block\":\"parameters\"}]";
return s__.str();
} // get_unconstrained_sizedtypes()
// Begin method overload boilerplate
template <typename RNG>
inline void write_array(RNG& base_rng__,
Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
bool emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = true,
std::ostream* pstream = nullptr) 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,
emit_transformed_parameters__, emit_generated_quantities__, pstream);
vars.resize(vars_vec.size());
for (int i = 0; i < vars.size(); ++i)
vars(i) = vars_vec[i];
}
template <bool propto__, bool jacobian__, typename T_>
inline T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
std::ostream* pstream = nullptr) 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);
}
inline void transform_inits(const stan::io::var_context& context,
Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
std::ostream* pstream__ = nullptr) 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];
}
};
}
using stan_model = bernoulli_model_namespace::bernoulli_model;
#ifndef USING_R
// Boilerplate
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;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment