Skip to content

Instantly share code, notes, and snippets.

@seantalts
Last active May 31, 2019 16:37
Show Gist options
  • Save seantalts/2eee4aaf7a835791b681d5091e43be1b to your computer and use it in GitHub Desktop.
Save seantalts/2eee4aaf7a835791b681d5091e43be1b to your computer and use it in GitHub Desktop.
// Code generated by stanc3 version 0.0.1
#include <stan/model/model_header.hpp>
namespace low_dim_gauss_mix_model_namespace {
using stan::io::dump;
using stan::math::lgamma;
using stan::model::prob_grad;
using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using namespace stan::math;
static char *current_statement__;
class low_dim_gauss_mix_model : public prob_grad {
private:
int N;
Eigen::Matrix<double, -1, 1> y;
public:
~low_dim_gauss_mix_model() {}
static std::string model_name() { return "low_dim_gauss_mix_model"; }
low_dim_gauss_mix_model(stan::io::var_context &context__,
unsigned int random_seed__ = 0,
std::ostream *pstream__ = nullptr)
: prob_grad(0) {
typedef double local_scalar_t__;
boost::ecuyer1988 base_rng__ =
stan::services::util::create_rng(random_seed__, 0);
(void)base_rng__; // suppress unused var warning
static const char *function__ =
"low_dim_gauss_mix_model_namespace::low_dim_gauss_mix_model";
(void)function__; // suppress unused var warning
stan::model::assign(N, stan::model::nil_index_list(),
context__.vals_i("N")[(1 - 1)], "assigning variable N");
check_greater_or_equal(function__, "N", N, 0);
y = Eigen::Matrix<double, -1, 1>(N);
for (size_t sym1__ = 1; sym1__ <= N; ++sym1__) {
stan::model::assign(y,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
context__.vals_r("y")[(sym1__ - 1)],
"assigning variable y[(sym1__ - 1)]");
}
num_params_r__ = 0U;
num_params_r__ += 2;
num_params_r__ += 2;
num_params_r__ += 1;
}
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__; // suppress unused var warning
T__ lp__(0.0);
stan::math::accumulator<T__> lp_accum__;
static const char *function__ =
"low_dim_gauss_mix_model_namespace::log_prob";
(void)function__; // suppress unused var warning
stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
{
Eigen::Matrix<local_scalar_t__, -1, 1> mu;
mu = Eigen::Matrix<local_scalar_t__, -1, 1>(2);
stan::model::assign(mu, stan::model::nil_index_list(), in__.vector(2),
"assigning variable mu");
if (jacobian__) {
stan::model::assign(mu, stan::model::nil_index_list(),
ordered_constrain(mu, lp__),
"assigning variable mu");
} else
stan::model::assign(mu, stan::model::nil_index_list(),
ordered_constrain(mu), "assigning variable mu");
std::vector<local_scalar_t__> sigma;
sigma = std::vector<local_scalar_t__>(2, 0);
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
in__.scalar(), "assigning variable sigma[(sym1__ - 1)]");
}
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
if (jacobian__) {
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
lb_constrain(
stan::model::rvalue(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
"pretty printed e"),
0, lp__),
"assigning variable sigma[(sym1__ - 1)]");
} else
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
lb_constrain(
stan::model::rvalue(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
"pretty printed e"),
0),
"assigning variable sigma[(sym1__ - 1)]");
}
local_scalar_t__ theta;
stan::model::assign(theta, stan::model::nil_index_list(), in__.scalar(),
"assigning variable theta");
if (jacobian__) {
stan::model::assign(theta, stan::model::nil_index_list(),
lub_constrain(theta, 0, 1, lp__),
"assigning variable theta");
} else
stan::model::assign(theta, stan::model::nil_index_list(),
lub_constrain(theta, 0, 1),
"assigning variable theta");
{
lp_accum__.add(normal_log<propto__>(sigma, 0, 2));
lp_accum__.add(normal_log<propto__>(mu, 0, 2));
lp_accum__.add(beta_log<propto__>(theta, 5, 5));
for (size_t n = 1; n <= N; ++n) {
lp_accum__.add(log_mix(
theta,
normal_lpdf<propto__>(
stan::model::rvalue(
y,
stan::model::cons_list(stan::model::index_uni(n),
stan::model::nil_index_list()),
"pretty printed e"),
stan::model::rvalue(
mu,
stan::model::cons_list(stan::model::index_uni(1),
stan::model::nil_index_list()),
"pretty printed e"),
stan::model::rvalue(
sigma,
stan::model::cons_list(stan::model::index_uni(1),
stan::model::nil_index_list()),
"pretty printed e")),
normal_lpdf<propto__>(
stan::model::rvalue(
y,
stan::model::cons_list(stan::model::index_uni(n),
stan::model::nil_index_list()),
"pretty printed e"),
stan::model::rvalue(
mu,
stan::model::cons_list(stan::model::index_uni(2),
stan::model::nil_index_list()),
"pretty printed e"),
stan::model::rvalue(
sigma,
stan::model::cons_list(stan::model::index_uni(2),
stan::model::nil_index_list()),
"pretty printed e"))));
}
}
}
lp_accum__.add(lp__);
return lp_accum__.sum();
} // log_prob()
void get_param_names(std::vector<std::string> &names__) const {
names__.resize(0);
names__.push_back("mu");
names__.push_back("sigma");
names__.push_back("theta");
} // get_param_names()
void get_dims(std::vector<std::vector<size_t>> &dimss__) const {
dimss__.resize(0);
std::vector<size_t> dims__;
dims__.push_back(2);
dimss__.push_back(dims__);
dims__.resize(0);
dims__.push_back(2);
dimss__.push_back(dims__);
dims__.resize(0);
dimss__.push_back(dims__);
dims__.resize(0);
} // get_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 emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = 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__ =
"low_dim_gauss_mix_model_namespace::write_array";
(void)function__; // suppress unused var warning
(void)function__; // suppress unused var warning
{
Eigen::Matrix<double, -1, 1> mu;
mu = Eigen::Matrix<double, -1, 1>(2);
stan::model::assign(mu, stan::model::nil_index_list(), in__.vector(2),
"assigning variable mu");
stan::model::assign(mu, stan::model::nil_index_list(),
ordered_constrain(mu), "assigning variable mu");
std::vector<double> sigma;
sigma = std::vector<double>(2, 0);
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
in__.scalar(), "assigning variable sigma[(sym1__ - 1)]");
}
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
lb_constrain(stan::model::rvalue(sigma,
stan::model::cons_list(
stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
"pretty printed e"),
0),
"assigning variable sigma[(sym1__ - 1)]");
}
double theta;
stan::model::assign(theta, stan::model::nil_index_list(), in__.scalar(),
"assigning variable theta");
stan::model::assign(theta, stan::model::nil_index_list(),
lub_constrain(theta, 0, 1),
"assigning variable theta");
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
vars__.push_back(stan::model::rvalue(
mu,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
"pretty printed e"));
}
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
vars__.push_back(stan::model::rvalue(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
"pretty printed e"));
}
vars__.push_back(theta);
}
} // write_array()
void constrained_param_names(std::vector<std::string> &param_names__,
bool emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = true) const {
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
param_names__.push_back(std::string() + "mu" + '.' +
std::to_string(sym1__));
}
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
{
param_names__.push_back(std::string() + "sigma" + '.' +
std::to_string(sym1__));
}
}
param_names__.push_back(std::string() + "theta");
if (emit_transformed_parameters__) {
}
if (emit_generated_quantities__) {
}
} // constrained_param_names()
void
unconstrained_param_names(std::vector<std::string> &param_names__,
bool emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = true) const {
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
param_names__.push_back(std::string() + "mu" + '.' +
std::to_string(sym1__));
}
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
{
param_names__.push_back(std::string() + "sigma" + '.' +
std::to_string(sym1__));
}
}
param_names__.push_back(std::string() + "theta");
if (emit_transformed_parameters__) {
}
if (emit_generated_quantities__) {
}
} // unconstrained_param_names()
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__);
std::vector<double> vals_r__;
std::vector<int> vals_i__;
{
Eigen::Matrix<double, -1, 1> mu;
mu = Eigen::Matrix<double, -1, 1>(2);
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
stan::model::assign(
mu,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
context__.vals_r("mu")[(sym1__ - 1)],
"assigning variable mu[(sym1__ - 1)]");
}
stan::model::assign(mu, stan::model::nil_index_list(), ordered_free(mu),
"assigning variable mu");
std::vector<double> sigma;
sigma = std::vector<double>(2, 0);
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
context__.vals_r("sigma")[(sym1__ - 1)],
"assigning variable sigma[(sym1__ - 1)]");
}
for (size_t sym1__ = 1; sym1__ <= 2; ++sym1__) {
stan::model::assign(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
lb_free(stan::model::rvalue(
sigma,
stan::model::cons_list(stan::model::index_uni(sym1__),
stan::model::nil_index_list()),
"pretty printed e"),
0),
"assigning variable sigma[(sym1__ - 1)]");
}
double theta;
stan::model::assign(theta, stan::model::nil_index_list(),
context__.vals_r("theta")[(1 - 1)],
"assigning variable theta");
stan::model::assign(theta, stan::model::nil_index_list(),
lub_free(theta, 0, 1), "assigning variable theta");
}
} // transform_inits()
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 emit_transformed_parameters__ = true,
bool emit_generated_quantities__ = 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,
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_>
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);
}
};
} // namespace low_dim_gauss_mix_model_namespace
typedef low_dim_gauss_mix_model_namespace::low_dim_gauss_mix_model stan_model;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment