Created
March 5, 2021 22:47
-
-
Save bbbales2/0053578d33035ab25560dd5e2b5a6fb1 to your computer and use it in GitHub Desktop.
Constrain model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by stanc 4f8f010 | |
#include <stan/model/model_header.hpp> | |
namespace constrain_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; | |
using stan::math::pow; | |
stan::math::profile_map profiles__; | |
static int current_statement__= 0; | |
static const std::vector<string> locations_array__ = {" (found before start of program)", | |
" (in 'constrain.stan', line 10, column 2 to column 29)", | |
" (in 'constrain.stan', line 14, column 2 to column 36)", | |
" (in 'constrain.stan', line 18, column 2 to column 36)", | |
" (in 'constrain.stan', line 2, column 2 to column 29)", | |
" (in 'constrain.stan', line 6, column 2 to column 36)"}; | |
class constrain_model final : public model_base_crtp<constrain_model> { | |
private: | |
Eigen::Matrix<double, -1, 1> x_d; | |
Eigen::Matrix<double, -1, 1> x_tf; | |
public: | |
~constrain_model() { } | |
inline std::string model_name() const final { return "constrain_model"; } | |
inline std::vector<std::string> model_compile_info() const noexcept { | |
return std::vector<std::string>{"stanc_version = stanc3 4f8f010", "stancflags = "}; | |
} | |
constrain_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__ = "constrain_model_namespace::constrain_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 { | |
int pos__; | |
pos__ = std::numeric_limits<int>::min(); | |
pos__ = 1; | |
current_statement__ = 4; | |
context__.validate_dims("data initialization","x_d","double", | |
context__.to_vec(3)); | |
x_d = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_d, std::numeric_limits<double>::quiet_NaN()); | |
{ | |
std::vector<local_scalar_t__> x_d_flat__; | |
current_statement__ = 4; | |
assign(x_d_flat__, nil_index_list(), context__.vals_r("x_d"), | |
"assigning variable x_d_flat__"); | |
current_statement__ = 4; | |
pos__ = 1; | |
current_statement__ = 4; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 4; | |
assign(x_d, cons_list(index_uni(sym1__), nil_index_list()), | |
x_d_flat__[(pos__ - 1)], "assigning variable x_d"); | |
current_statement__ = 4; | |
pos__ = (pos__ + 1);} | |
} | |
current_statement__ = 4; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 4; | |
current_statement__ = 4; | |
check_greater_or_equal(function__, "x_d[sym1__]", x_d[(sym1__ - 1)], | |
0.0);} | |
current_statement__ = 5; | |
x_tf = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_tf, std::numeric_limits<double>::quiet_NaN()); | |
current_statement__ = 5; | |
assign(x_tf, nil_index_list(), x_d, "assigning variable x_tf"); | |
current_statement__ = 5; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 5; | |
current_statement__ = 5; | |
check_greater_or_equal(function__, "x_tf[sym1__]", | |
x_tf[(sym1__ - 1)], 0.0);} | |
} 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 { | |
num_params_r__ += 3; | |
} 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 VecR, typename VecI, stan::require_vector_like_t<VecR>* = nullptr, stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr> | |
inline stan::scalar_type_t<VecR> log_prob_impl(VecR& params_r__, | |
VecI& params_i__, | |
std::ostream* pstream__ = nullptr) const { | |
using T__ = stan::scalar_type_t<VecR>; | |
using local_scalar_t__ = T__; | |
T__ lp__(0.0); | |
stan::math::accumulator<T__> lp_accum__; | |
static const char* function__ = "constrain_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 { | |
Eigen::Matrix<local_scalar_t__, -1, 1> x_p; | |
x_p = Eigen::Matrix<local_scalar_t__, -1, 1>(3); | |
stan::math::fill(x_p, DUMMY_VAR__); | |
current_statement__ = 1; | |
x_p = in__.vector(3); | |
current_statement__ = 1; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 1; | |
if (jacobian__) { | |
current_statement__ = 1; | |
assign(x_p, cons_list(index_uni(sym1__), nil_index_list()), | |
stan::math::lb_constrain(x_p[(sym1__ - 1)], 0.0, lp__), | |
"assigning variable x_p"); | |
} else { | |
current_statement__ = 1; | |
assign(x_p, cons_list(index_uni(sym1__), nil_index_list()), | |
stan::math::lb_constrain(x_p[(sym1__ - 1)], 0.0), | |
"assigning variable x_p"); | |
}} | |
Eigen::Matrix<local_scalar_t__, -1, 1> x_tp; | |
x_tp = Eigen::Matrix<local_scalar_t__, -1, 1>(3); | |
stan::math::fill(x_tp, DUMMY_VAR__); | |
current_statement__ = 2; | |
assign(x_tp, nil_index_list(), x_p, "assigning variable x_tp"); | |
current_statement__ = 2; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 2; | |
current_statement__ = 2; | |
check_greater_or_equal(function__, "x_tp[sym1__]", | |
x_tp[(sym1__ - 1)], 0.0);} | |
} 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_impl() | |
template <typename RNG, typename VecR, typename VecI, typename VecVar, stan::require_vector_like_vt<std::is_floating_point, VecR>* = nullptr, stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr, stan::require_std_vector_vt<std::is_floating_point, VecVar>* = nullptr> | |
inline void write_array_impl(RNG& base_rng__, VecR& params_r__, | |
VecI& params_i__, VecVar& vars__, | |
const bool emit_transformed_parameters__ = true, | |
const 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__ = "constrain_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 { | |
Eigen::Matrix<double, -1, 1> x_p; | |
x_p = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_p, std::numeric_limits<double>::quiet_NaN()); | |
current_statement__ = 1; | |
x_p = in__.vector(3); | |
current_statement__ = 1; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 1; | |
assign(x_p, cons_list(index_uni(sym1__), nil_index_list()), | |
stan::math::lb_constrain(x_p[(sym1__ - 1)], 0.0), | |
"assigning variable x_p");} | |
Eigen::Matrix<double, -1, 1> x_tp; | |
x_tp = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_tp, std::numeric_limits<double>::quiet_NaN()); | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
vars__.emplace_back(x_p[(sym1__ - 1)]);} | |
if (logical_negation((primitive_value(emit_transformed_parameters__) || | |
primitive_value(emit_generated_quantities__)))) { | |
return ; | |
} | |
current_statement__ = 2; | |
assign(x_tp, nil_index_list(), x_p, "assigning variable x_tp"); | |
current_statement__ = 2; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 2; | |
current_statement__ = 2; | |
check_greater_or_equal(function__, "x_tp[sym1__]", | |
x_tp[(sym1__ - 1)], 0.0);} | |
if (emit_transformed_parameters__) { | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
vars__.emplace_back(x_tp[(sym1__ - 1)]);} | |
} | |
if (logical_negation(emit_generated_quantities__)) { | |
return ; | |
} | |
Eigen::Matrix<double, -1, 1> x_gq; | |
x_gq = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_gq, std::numeric_limits<double>::quiet_NaN()); | |
current_statement__ = 3; | |
assign(x_gq, nil_index_list(), x_p, "assigning variable x_gq"); | |
current_statement__ = 3; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 3; | |
current_statement__ = 3; | |
check_greater_or_equal(function__, "x_gq[sym1__]", | |
x_gq[(sym1__ - 1)], 0.0);} | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
vars__.emplace_back(x_gq[(sym1__ - 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 ***"); | |
} | |
} // write_array_impl() | |
template <typename VecVar, typename VecI, stan::require_std_vector_t<VecVar>* = nullptr, stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr> | |
inline void transform_inits_impl(const stan::io::var_context& context__, | |
VecI& params_i__, VecVar& vars__, | |
std::ostream* pstream__ = nullptr) const { | |
using local_scalar_t__ = double; | |
vars__.clear(); | |
vars__.reserve(num_params_r__); | |
try { | |
int pos__; | |
pos__ = std::numeric_limits<int>::min(); | |
pos__ = 1; | |
Eigen::Matrix<double, -1, 1> x_p; | |
x_p = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_p, std::numeric_limits<double>::quiet_NaN()); | |
{ | |
std::vector<local_scalar_t__> x_p_flat__; | |
current_statement__ = 1; | |
assign(x_p_flat__, nil_index_list(), context__.vals_r("x_p"), | |
"assigning variable x_p_flat__"); | |
current_statement__ = 1; | |
pos__ = 1; | |
current_statement__ = 1; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 1; | |
assign(x_p, cons_list(index_uni(sym1__), nil_index_list()), | |
x_p_flat__[(pos__ - 1)], "assigning variable x_p"); | |
current_statement__ = 1; | |
pos__ = (pos__ + 1);} | |
} | |
Eigen::Matrix<double, -1, 1> x_p_free__; | |
x_p_free__ = Eigen::Matrix<double, -1, 1>(3); | |
stan::math::fill(x_p_free__, std::numeric_limits<double>::quiet_NaN()); | |
current_statement__ = 1; | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
current_statement__ = 1; | |
assign(x_p_free__, cons_list(index_uni(sym1__), nil_index_list()), | |
stan::math::lb_free(x_p[(sym1__ - 1)], 0.0), | |
"assigning variable x_p_free__");} | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
vars__.emplace_back(x_p_free__[(sym1__ - 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 ***"); | |
} | |
} // transform_inits_impl() | |
inline void get_param_names(std::vector<std::string>& names__) const { | |
names__.clear(); | |
names__.emplace_back("x_p"); | |
names__.emplace_back("x_tp"); | |
names__.emplace_back("x_gq"); | |
} // get_param_names() | |
inline void get_dims(std::vector<std::vector<size_t>>& dimss__) const { | |
dimss__.clear(); | |
dimss__.emplace_back(std::vector<size_t>{static_cast<size_t>(3)}); | |
dimss__.emplace_back(std::vector<size_t>{static_cast<size_t>(3)}); | |
dimss__.emplace_back(std::vector<size_t>{static_cast<size_t>(3)}); | |
} // 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__ <= 3; ++sym1__) { | |
{ | |
param_names__.emplace_back(std::string() + "x_p" + '.' + std::to_string(sym1__)); | |
}} | |
if (emit_transformed_parameters__) { | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
{ | |
param_names__.emplace_back(std::string() + "x_tp" + '.' + std::to_string(sym1__)); | |
}} | |
} | |
if (emit_generated_quantities__) { | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
{ | |
param_names__.emplace_back(std::string() + "x_gq" + '.' + std::to_string(sym1__)); | |
}} | |
} | |
} // 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__ <= 3; ++sym1__) { | |
{ | |
param_names__.emplace_back(std::string() + "x_p" + '.' + std::to_string(sym1__)); | |
}} | |
if (emit_transformed_parameters__) { | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
{ | |
param_names__.emplace_back(std::string() + "x_tp" + '.' + std::to_string(sym1__)); | |
}} | |
} | |
if (emit_generated_quantities__) { | |
for (int sym1__ = 1; sym1__ <= 3; ++sym1__) { | |
{ | |
param_names__.emplace_back(std::string() + "x_gq" + '.' + std::to_string(sym1__)); | |
}} | |
} | |
} // unconstrained_param_names() | |
inline std::string get_constrained_sizedtypes() const { | |
stringstream s__; | |
s__ << "[{\"name\":\"x_p\",\"type\":{\"name\":\"vector\",\"length\":" << 3 << "},\"block\":\"parameters\"},{\"name\":\"x_tp\",\"type\":{\"name\":\"vector\",\"length\":" << 3 << "},\"block\":\"transformed_parameters\"},{\"name\":\"x_gq\",\"type\":{\"name\":\"vector\",\"length\":" << 3 << "},\"block\":\"generated_quantities\"}]"; | |
return s__.str(); | |
} // get_constrained_sizedtypes() | |
inline std::string get_unconstrained_sizedtypes() const { | |
stringstream s__; | |
s__ << "[{\"name\":\"x_p\",\"type\":{\"name\":\"vector\",\"length\":" << 3 << "},\"block\":\"parameters\"},{\"name\":\"x_tp\",\"type\":{\"name\":\"vector\",\"length\":" << 3 << "},\"block\":\"transformed_parameters\"},{\"name\":\"x_gq\",\"type\":{\"name\":\"vector\",\"length\":" << 3 << "},\"block\":\"generated_quantities\"}]"; | |
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, | |
const bool emit_transformed_parameters = true, | |
const bool emit_generated_quantities = true, | |
std::ostream* pstream = nullptr) const { | |
std::vector<double> vars_vec(vars.size()); | |
std::vector<int> params_i; | |
write_array_impl(base_rng, params_r, params_i, vars_vec, | |
emit_transformed_parameters, emit_generated_quantities, pstream); | |
vars.resize(vars_vec.size()); | |
for (int i = 0; i < vars.size(); ++i) { | |
vars.coeffRef(i) = vars_vec[i]; | |
} | |
} | |
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 { | |
write_array_impl(base_rng, params_r, params_i, vars, emit_transformed_parameters, emit_generated_quantities, pstream); | |
} | |
template <bool propto__, bool jacobian__, typename T_> | |
inline T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r, | |
std::ostream* pstream = nullptr) const { | |
Eigen::Matrix<int, -1, 1> params_i; | |
return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream); | |
} | |
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 { | |
return log_prob_impl<propto__, jacobian__>(params_r, 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 final { | |
std::vector<double> params_r_vec(params_r.size()); | |
std::vector<int> params_i; | |
transform_inits_impl(context, params_i, params_r_vec, pstream); | |
params_r.resize(params_r_vec.size()); | |
for (int i = 0; i < params_r.size(); ++i) { | |
params_r.coeffRef(i) = params_r_vec[i]; | |
} | |
} | |
inline void transform_inits(const stan::io::var_context& context, | |
std::vector<int>& params_i, | |
std::vector<double>& vars, | |
std::ostream* pstream = nullptr) const final { | |
transform_inits_impl(context, params_i, vars, pstream); | |
} | |
}; | |
} | |
using stan_model = constrain_model_namespace::constrain_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; | |
} | |
stan::math::profile_map& get_stan_profile_data() { | |
return constrain_model_namespace::profiles__; | |
} | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data { | |
vector<lower = 0.0>[3] x_d; | |
} | |
transformed data { | |
vector<lower = 0.0>[3] x_tf = x_d; | |
} | |
parameters { | |
vector<lower = 0.0>[3] x_p; | |
} | |
transformed parameters { | |
vector<lower = 0.0>[3] x_tp = x_p; | |
} | |
generated quantities { | |
vector<lower = 0.0>[3] x_gq = x_p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment