// Code generated by Stan version 2.21.0

#include <stan/model/model_header.hpp>

namespace model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d_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", "model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d");
    reader.add_event(76, 74, "end", "model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d");
    return reader;
}

class model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d
  : public stan::model::model_base_crtp<model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d> {
private:
        int N;
        vector_d Y;
        int K_ult;
        matrix_d X_ult;
        int K_omega;
        matrix_d X_omega;
        int K_theta;
        matrix_d X_theta;
        std::vector<int> C_1;
        int N_1;
        int M_1;
        std::vector<int> J_1;
        vector_d Z_1_ult_1;
        int prior_only;
public:
    model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d(rstan::io::rlist_ref_var_context& context__,
        std::ostream* pstream__ = 0)
        : model_base_crtp(0) {
        ctor_body(context__, 0, pstream__);
    }

    model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d(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__ = "model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d_namespace::model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d";
        (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__ = 5;
            context__.validate_dims("data initialization", "N", "int", context__.to_vec());
            N = int(0);
            vals_i__ = context__.vals_i("N");
            pos__ = 0;
            N = vals_i__[pos__++];
            check_greater_or_equal(function__, "N", N, 1);

            current_statement_begin__ = 6;
            validate_non_negative_index("Y", "N", N);
            context__.validate_dims("data initialization", "Y", "vector_d", context__.to_vec(N));
            Y = Eigen::Matrix<double, Eigen::Dynamic, 1>(N);
            vals_r__ = context__.vals_r("Y");
            pos__ = 0;
            size_t Y_j_1_max__ = N;
            for (size_t j_1__ = 0; j_1__ < Y_j_1_max__; ++j_1__) {
                Y(j_1__) = vals_r__[pos__++];
            }

            current_statement_begin__ = 7;
            context__.validate_dims("data initialization", "K_ult", "int", context__.to_vec());
            K_ult = int(0);
            vals_i__ = context__.vals_i("K_ult");
            pos__ = 0;
            K_ult = vals_i__[pos__++];
            check_greater_or_equal(function__, "K_ult", K_ult, 1);

            current_statement_begin__ = 8;
            validate_non_negative_index("X_ult", "N", N);
            validate_non_negative_index("X_ult", "K_ult", K_ult);
            context__.validate_dims("data initialization", "X_ult", "matrix_d", context__.to_vec(N,K_ult));
            X_ult = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(N, K_ult);
            vals_r__ = context__.vals_r("X_ult");
            pos__ = 0;
            size_t X_ult_j_2_max__ = K_ult;
            size_t X_ult_j_1_max__ = N;
            for (size_t j_2__ = 0; j_2__ < X_ult_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < X_ult_j_1_max__; ++j_1__) {
                    X_ult(j_1__, j_2__) = vals_r__[pos__++];
                }
            }

            current_statement_begin__ = 9;
            context__.validate_dims("data initialization", "K_omega", "int", context__.to_vec());
            K_omega = int(0);
            vals_i__ = context__.vals_i("K_omega");
            pos__ = 0;
            K_omega = vals_i__[pos__++];
            check_greater_or_equal(function__, "K_omega", K_omega, 1);

            current_statement_begin__ = 10;
            validate_non_negative_index("X_omega", "N", N);
            validate_non_negative_index("X_omega", "K_omega", K_omega);
            context__.validate_dims("data initialization", "X_omega", "matrix_d", context__.to_vec(N,K_omega));
            X_omega = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(N, K_omega);
            vals_r__ = context__.vals_r("X_omega");
            pos__ = 0;
            size_t X_omega_j_2_max__ = K_omega;
            size_t X_omega_j_1_max__ = N;
            for (size_t j_2__ = 0; j_2__ < X_omega_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < X_omega_j_1_max__; ++j_1__) {
                    X_omega(j_1__, j_2__) = vals_r__[pos__++];
                }
            }

            current_statement_begin__ = 11;
            context__.validate_dims("data initialization", "K_theta", "int", context__.to_vec());
            K_theta = int(0);
            vals_i__ = context__.vals_i("K_theta");
            pos__ = 0;
            K_theta = vals_i__[pos__++];
            check_greater_or_equal(function__, "K_theta", K_theta, 1);

            current_statement_begin__ = 12;
            validate_non_negative_index("X_theta", "N", N);
            validate_non_negative_index("X_theta", "K_theta", K_theta);
            context__.validate_dims("data initialization", "X_theta", "matrix_d", context__.to_vec(N,K_theta));
            X_theta = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(N, K_theta);
            vals_r__ = context__.vals_r("X_theta");
            pos__ = 0;
            size_t X_theta_j_2_max__ = K_theta;
            size_t X_theta_j_1_max__ = N;
            for (size_t j_2__ = 0; j_2__ < X_theta_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < X_theta_j_1_max__; ++j_1__) {
                    X_theta(j_1__, j_2__) = vals_r__[pos__++];
                }
            }

            current_statement_begin__ = 14;
            validate_non_negative_index("C_1", "N", N);
            context__.validate_dims("data initialization", "C_1", "int", context__.to_vec(N));
            C_1 = std::vector<int>(N, int(0));
            vals_i__ = context__.vals_i("C_1");
            pos__ = 0;
            size_t C_1_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < C_1_k_0_max__; ++k_0__) {
                C_1[k_0__] = vals_i__[pos__++];
            }

            current_statement_begin__ = 16;
            context__.validate_dims("data initialization", "N_1", "int", context__.to_vec());
            N_1 = int(0);
            vals_i__ = context__.vals_i("N_1");
            pos__ = 0;
            N_1 = vals_i__[pos__++];
            check_greater_or_equal(function__, "N_1", N_1, 1);

            current_statement_begin__ = 17;
            context__.validate_dims("data initialization", "M_1", "int", context__.to_vec());
            M_1 = int(0);
            vals_i__ = context__.vals_i("M_1");
            pos__ = 0;
            M_1 = vals_i__[pos__++];
            check_greater_or_equal(function__, "M_1", M_1, 1);

            current_statement_begin__ = 18;
            validate_non_negative_index("J_1", "N", N);
            context__.validate_dims("data initialization", "J_1", "int", context__.to_vec(N));
            J_1 = std::vector<int>(N, int(0));
            vals_i__ = context__.vals_i("J_1");
            pos__ = 0;
            size_t J_1_k_0_max__ = N;
            for (size_t k_0__ = 0; k_0__ < J_1_k_0_max__; ++k_0__) {
                J_1[k_0__] = vals_i__[pos__++];
            }
            size_t J_1_i_0_max__ = N;
            for (size_t i_0__ = 0; i_0__ < J_1_i_0_max__; ++i_0__) {
                check_greater_or_equal(function__, "J_1[i_0__]", J_1[i_0__], 1);
            }

            current_statement_begin__ = 20;
            validate_non_negative_index("Z_1_ult_1", "N", N);
            context__.validate_dims("data initialization", "Z_1_ult_1", "vector_d", context__.to_vec(N));
            Z_1_ult_1 = Eigen::Matrix<double, Eigen::Dynamic, 1>(N);
            vals_r__ = context__.vals_r("Z_1_ult_1");
            pos__ = 0;
            size_t Z_1_ult_1_j_1_max__ = N;
            for (size_t j_1__ = 0; j_1__ < Z_1_ult_1_j_1_max__; ++j_1__) {
                Z_1_ult_1(j_1__) = vals_r__[pos__++];
            }

            current_statement_begin__ = 21;
            context__.validate_dims("data initialization", "prior_only", "int", context__.to_vec());
            prior_only = int(0);
            vals_i__ = context__.vals_i("prior_only");
            pos__ = 0;
            prior_only = vals_i__[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__ = 26;
            validate_non_negative_index("b_ult", "K_ult", K_ult);
            num_params_r__ += K_ult;
            current_statement_begin__ = 27;
            validate_non_negative_index("b_omega", "K_omega", K_omega);
            num_params_r__ += K_omega;
            current_statement_begin__ = 28;
            validate_non_negative_index("b_theta", "K_theta", K_theta);
            num_params_r__ += K_theta;
            current_statement_begin__ = 29;
            num_params_r__ += 1;
            current_statement_begin__ = 30;
            validate_non_negative_index("sd_1", "M_1", M_1);
            num_params_r__ += M_1;
            current_statement_begin__ = 31;
            validate_non_negative_index("z_1", "N_1", N_1);
            validate_non_negative_index("z_1", "M_1", M_1);
            num_params_r__ += (N_1 * M_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 ***");
        }
    }

    ~model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d() { }


    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__ = 26;
        if (!(context__.contains_r("b_ult")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable b_ult missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("b_ult");
        pos__ = 0U;
        validate_non_negative_index("b_ult", "K_ult", K_ult);
        context__.validate_dims("parameter initialization", "b_ult", "vector_d", context__.to_vec(K_ult));
        Eigen::Matrix<double, Eigen::Dynamic, 1> b_ult(K_ult);
        size_t b_ult_j_1_max__ = K_ult;
        for (size_t j_1__ = 0; j_1__ < b_ult_j_1_max__; ++j_1__) {
            b_ult(j_1__) = vals_r__[pos__++];
        }
        try {
            writer__.vector_unconstrain(b_ult);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable b_ult: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 27;
        if (!(context__.contains_r("b_omega")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable b_omega missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("b_omega");
        pos__ = 0U;
        validate_non_negative_index("b_omega", "K_omega", K_omega);
        context__.validate_dims("parameter initialization", "b_omega", "vector_d", context__.to_vec(K_omega));
        Eigen::Matrix<double, Eigen::Dynamic, 1> b_omega(K_omega);
        size_t b_omega_j_1_max__ = K_omega;
        for (size_t j_1__ = 0; j_1__ < b_omega_j_1_max__; ++j_1__) {
            b_omega(j_1__) = vals_r__[pos__++];
        }
        try {
            writer__.vector_unconstrain(b_omega);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable b_omega: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 28;
        if (!(context__.contains_r("b_theta")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable b_theta missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("b_theta");
        pos__ = 0U;
        validate_non_negative_index("b_theta", "K_theta", K_theta);
        context__.validate_dims("parameter initialization", "b_theta", "vector_d", context__.to_vec(K_theta));
        Eigen::Matrix<double, Eigen::Dynamic, 1> b_theta(K_theta);
        size_t b_theta_j_1_max__ = K_theta;
        for (size_t j_1__ = 0; j_1__ < b_theta_j_1_max__; ++j_1__) {
            b_theta(j_1__) = vals_r__[pos__++];
        }
        try {
            writer__.vector_unconstrain(b_theta);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable b_theta: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 29;
        if (!(context__.contains_r("sigma")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable sigma missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("sigma");
        pos__ = 0U;
        context__.validate_dims("parameter initialization", "sigma", "double", context__.to_vec());
        double sigma(0);
        sigma = vals_r__[pos__++];
        try {
            writer__.scalar_lb_unconstrain(0, sigma);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable sigma: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 30;
        if (!(context__.contains_r("sd_1")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable sd_1 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("sd_1");
        pos__ = 0U;
        validate_non_negative_index("sd_1", "M_1", M_1);
        context__.validate_dims("parameter initialization", "sd_1", "vector_d", context__.to_vec(M_1));
        Eigen::Matrix<double, Eigen::Dynamic, 1> sd_1(M_1);
        size_t sd_1_j_1_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < sd_1_j_1_max__; ++j_1__) {
            sd_1(j_1__) = vals_r__[pos__++];
        }
        try {
            writer__.vector_lb_unconstrain(0, sd_1);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable sd_1: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 31;
        if (!(context__.contains_r("z_1")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable z_1 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("z_1");
        pos__ = 0U;
        validate_non_negative_index("z_1", "N_1", N_1);
        validate_non_negative_index("z_1", "M_1", M_1);
        context__.validate_dims("parameter initialization", "z_1", "vector_d", context__.to_vec(M_1,N_1));
        std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1> > z_1(M_1, Eigen::Matrix<double, Eigen::Dynamic, 1>(N_1));
        size_t z_1_j_1_max__ = N_1;
        size_t z_1_k_0_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < z_1_j_1_max__; ++j_1__) {
            for (size_t k_0__ = 0; k_0__ < z_1_k_0_max__; ++k_0__) {
                z_1[k_0__](j_1__) = vals_r__[pos__++];
            }
        }
        size_t z_1_i_0_max__ = M_1;
        for (size_t i_0__ = 0; i_0__ < z_1_i_0_max__; ++i_0__) {
            try {
                writer__.vector_unconstrain(z_1[i_0__]);
            } catch (const std::exception& e) {
                stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable z_1: ") + 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__ = 26;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> b_ult;
            (void) b_ult;  // dummy to suppress unused var warning
            if (jacobian__)
                b_ult = in__.vector_constrain(K_ult, lp__);
            else
                b_ult = in__.vector_constrain(K_ult);

            current_statement_begin__ = 27;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> b_omega;
            (void) b_omega;  // dummy to suppress unused var warning
            if (jacobian__)
                b_omega = in__.vector_constrain(K_omega, lp__);
            else
                b_omega = in__.vector_constrain(K_omega);

            current_statement_begin__ = 28;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> b_theta;
            (void) b_theta;  // dummy to suppress unused var warning
            if (jacobian__)
                b_theta = in__.vector_constrain(K_theta, lp__);
            else
                b_theta = in__.vector_constrain(K_theta);

            current_statement_begin__ = 29;
            local_scalar_t__ sigma;
            (void) sigma;  // dummy to suppress unused var warning
            if (jacobian__)
                sigma = in__.scalar_lb_constrain(0, lp__);
            else
                sigma = in__.scalar_lb_constrain(0);

            current_statement_begin__ = 30;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> sd_1;
            (void) sd_1;  // dummy to suppress unused var warning
            if (jacobian__)
                sd_1 = in__.vector_lb_constrain(0, M_1, lp__);
            else
                sd_1 = in__.vector_lb_constrain(0, M_1);

            current_statement_begin__ = 31;
            std::vector<Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> > z_1;
            size_t z_1_d_0_max__ = M_1;
            z_1.reserve(z_1_d_0_max__);
            for (size_t d_0__ = 0; d_0__ < z_1_d_0_max__; ++d_0__) {
                if (jacobian__)
                    z_1.push_back(in__.vector_constrain(N_1, lp__));
                else
                    z_1.push_back(in__.vector_constrain(N_1));
            }

            // transformed parameters
            current_statement_begin__ = 34;
            validate_non_negative_index("r_1_ult_1", "N_1", N_1);
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> r_1_ult_1(N_1);
            stan::math::initialize(r_1_ult_1, DUMMY_VAR__);
            stan::math::fill(r_1_ult_1, DUMMY_VAR__);

            current_statement_begin__ = 35;
            local_scalar_t__ lprior;
            (void) lprior;  // dummy to suppress unused var warning
            stan::math::initialize(lprior, DUMMY_VAR__);
            stan::math::fill(lprior, DUMMY_VAR__);
            stan::math::assign(lprior,0);

            // transformed parameters block statements
            current_statement_begin__ = 36;
            stan::math::assign(r_1_ult_1, multiply(get_base1(sd_1, 1, "sd_1", 1), get_base1(z_1, 1, "z_1", 1)));
            current_statement_begin__ = 37;
            stan::math::assign(lprior, (lprior + normal_log(b_ult, 5000, 1000)));
            current_statement_begin__ = 38;
            stan::math::assign(lprior, (lprior + normal_log(b_omega, 1, 2)));
            current_statement_begin__ = 39;
            stan::math::assign(lprior, (lprior + normal_log(b_theta, 45, 10)));
            current_statement_begin__ = 40;
            stan::math::assign(lprior, (lprior + (student_t_log(sigma, 3, 0, 1963.7) - (1 * student_t_ccdf_log(0, 3, 0, 1963.7)))));
            current_statement_begin__ = 42;
            stan::math::assign(lprior, (lprior + (student_t_log(sd_1, 3, 0, 1963.7) - (1 * student_t_ccdf_log(0, 3, 0, 1963.7)))));

            // validate transformed parameters
            const char* function__ = "validate transformed params";
            (void) function__;  // dummy to suppress unused var warning

            current_statement_begin__ = 34;
            size_t r_1_ult_1_j_1_max__ = N_1;
            for (size_t j_1__ = 0; j_1__ < r_1_ult_1_j_1_max__; ++j_1__) {
                if (stan::math::is_uninitialized(r_1_ult_1(j_1__))) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: r_1_ult_1" << "(" << j_1__ << ")";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable r_1_ult_1: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }
            current_statement_begin__ = 35;
            if (stan::math::is_uninitialized(lprior)) {
                std::stringstream msg__;
                msg__ << "Undefined transformed parameter: lprior";
                stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable lprior: ") + msg__.str()), current_statement_begin__, prog_reader__());
            }

            // model body

            current_statement_begin__ = 47;
            if (as_bool(logical_negation(prior_only))) {
                {
                current_statement_begin__ = 49;
                validate_non_negative_index("nlp_ult", "N", N);
                Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> nlp_ult(N);
                stan::math::initialize(nlp_ult, DUMMY_VAR__);
                stan::math::fill(nlp_ult, DUMMY_VAR__);
                stan::math::assign(nlp_ult,rep_vector(0.0, N));

                current_statement_begin__ = 51;
                validate_non_negative_index("nlp_omega", "N", N);
                Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> nlp_omega(N);
                stan::math::initialize(nlp_omega, DUMMY_VAR__);
                stan::math::fill(nlp_omega, DUMMY_VAR__);
                stan::math::assign(nlp_omega,rep_vector(0.0, N));

                current_statement_begin__ = 53;
                validate_non_negative_index("nlp_theta", "N", N);
                Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> nlp_theta(N);
                stan::math::initialize(nlp_theta, DUMMY_VAR__);
                stan::math::fill(nlp_theta, DUMMY_VAR__);
                stan::math::assign(nlp_theta,rep_vector(0.0, N));

                current_statement_begin__ = 55;
                validate_non_negative_index("mu", "N", N);
                Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> mu(N);
                stan::math::initialize(mu, DUMMY_VAR__);
                stan::math::fill(mu, DUMMY_VAR__);


                current_statement_begin__ = 56;
                stan::math::assign(nlp_ult, add(nlp_ult, multiply(X_ult, b_ult)));
                current_statement_begin__ = 57;
                stan::math::assign(nlp_omega, add(nlp_omega, multiply(X_omega, b_omega)));
                current_statement_begin__ = 58;
                stan::math::assign(nlp_theta, add(nlp_theta, multiply(X_theta, b_theta)));
                current_statement_begin__ = 59;
                for (int n = 1; n <= N; ++n) {

                    current_statement_begin__ = 61;
                    stan::model::assign(nlp_ult, 
                                stan::model::cons_list(stan::model::index_uni(n), stan::model::nil_index_list()), 
                                (stan::model::rvalue(nlp_ult, stan::model::cons_list(stan::model::index_uni(n), stan::model::nil_index_list()), "nlp_ult") + (get_base1(r_1_ult_1, get_base1(J_1, n, "J_1", 1), "r_1_ult_1", 1) * get_base1(Z_1_ult_1, n, "Z_1_ult_1", 1))), 
                                "assigning variable nlp_ult");
                }
                current_statement_begin__ = 63;
                for (int n = 1; n <= N; ++n) {

                    current_statement_begin__ = 65;
                    stan::model::assign(mu, 
                                stan::model::cons_list(stan::model::index_uni(n), stan::model::nil_index_list()), 
                                (get_base1(nlp_ult, n, "nlp_ult", 1) * (1 - stan::math::exp(-(pow((get_base1(C_1, n, "C_1", 1) / get_base1(nlp_theta, n, "nlp_theta", 1)), get_base1(nlp_omega, n, "nlp_omega", 1)))))), 
                                "assigning variable mu");
                }
                current_statement_begin__ = 67;
                lp_accum__.add(normal_log(Y, mu, sigma));
                }
            }
            current_statement_begin__ = 70;
            lp_accum__.add(lprior);
            current_statement_begin__ = 71;
            lp_accum__.add(std_normal_log(get_base1(z_1, 1, "z_1", 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 ***");
        }

        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("b_ult");
        names__.push_back("b_omega");
        names__.push_back("b_theta");
        names__.push_back("sigma");
        names__.push_back("sd_1");
        names__.push_back("z_1");
        names__.push_back("r_1_ult_1");
        names__.push_back("lprior");
    }


    void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
        dimss__.resize(0);
        std::vector<size_t> dims__;
        dims__.resize(0);
        dims__.push_back(K_ult);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(K_omega);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(K_theta);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(M_1);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(M_1);
        dims__.push_back(N_1);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N_1);
        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__ = "model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d_namespace::write_array";
        (void) function__;  // dummy to suppress unused var warning

        // read-transform, write parameters
        Eigen::Matrix<double, Eigen::Dynamic, 1> b_ult = in__.vector_constrain(K_ult);
        size_t b_ult_j_1_max__ = K_ult;
        for (size_t j_1__ = 0; j_1__ < b_ult_j_1_max__; ++j_1__) {
            vars__.push_back(b_ult(j_1__));
        }

        Eigen::Matrix<double, Eigen::Dynamic, 1> b_omega = in__.vector_constrain(K_omega);
        size_t b_omega_j_1_max__ = K_omega;
        for (size_t j_1__ = 0; j_1__ < b_omega_j_1_max__; ++j_1__) {
            vars__.push_back(b_omega(j_1__));
        }

        Eigen::Matrix<double, Eigen::Dynamic, 1> b_theta = in__.vector_constrain(K_theta);
        size_t b_theta_j_1_max__ = K_theta;
        for (size_t j_1__ = 0; j_1__ < b_theta_j_1_max__; ++j_1__) {
            vars__.push_back(b_theta(j_1__));
        }

        double sigma = in__.scalar_lb_constrain(0);
        vars__.push_back(sigma);

        Eigen::Matrix<double, Eigen::Dynamic, 1> sd_1 = in__.vector_lb_constrain(0, M_1);
        size_t sd_1_j_1_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < sd_1_j_1_max__; ++j_1__) {
            vars__.push_back(sd_1(j_1__));
        }

        std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1> > z_1;
        size_t z_1_d_0_max__ = M_1;
        z_1.reserve(z_1_d_0_max__);
        for (size_t d_0__ = 0; d_0__ < z_1_d_0_max__; ++d_0__) {
            z_1.push_back(in__.vector_constrain(N_1));
        }
        size_t z_1_j_1_max__ = N_1;
        size_t z_1_k_0_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < z_1_j_1_max__; ++j_1__) {
            for (size_t k_0__ = 0; k_0__ < z_1_k_0_max__; ++k_0__) {
                vars__.push_back(z_1[k_0__](j_1__));
            }
        }

        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 {
            // declare and define transformed parameters
            current_statement_begin__ = 34;
            validate_non_negative_index("r_1_ult_1", "N_1", N_1);
            Eigen::Matrix<double, Eigen::Dynamic, 1> r_1_ult_1(N_1);
            stan::math::initialize(r_1_ult_1, DUMMY_VAR__);
            stan::math::fill(r_1_ult_1, DUMMY_VAR__);

            current_statement_begin__ = 35;
            double lprior;
            (void) lprior;  // dummy to suppress unused var warning
            stan::math::initialize(lprior, DUMMY_VAR__);
            stan::math::fill(lprior, DUMMY_VAR__);
            stan::math::assign(lprior,0);

            // do transformed parameters statements
            current_statement_begin__ = 36;
            stan::math::assign(r_1_ult_1, multiply(get_base1(sd_1, 1, "sd_1", 1), get_base1(z_1, 1, "z_1", 1)));
            current_statement_begin__ = 37;
            stan::math::assign(lprior, (lprior + normal_log(b_ult, 5000, 1000)));
            current_statement_begin__ = 38;
            stan::math::assign(lprior, (lprior + normal_log(b_omega, 1, 2)));
            current_statement_begin__ = 39;
            stan::math::assign(lprior, (lprior + normal_log(b_theta, 45, 10)));
            current_statement_begin__ = 40;
            stan::math::assign(lprior, (lprior + (student_t_log(sigma, 3, 0, 1963.7) - (1 * student_t_ccdf_log(0, 3, 0, 1963.7)))));
            current_statement_begin__ = 42;
            stan::math::assign(lprior, (lprior + (student_t_log(sd_1, 3, 0, 1963.7) - (1 * student_t_ccdf_log(0, 3, 0, 1963.7)))));

            if (!include_gqs__ && !include_tparams__) return;
            // validate transformed parameters
            const char* function__ = "validate transformed params";
            (void) function__;  // dummy to suppress unused var warning

            // write transformed parameters
            if (include_tparams__) {
                size_t r_1_ult_1_j_1_max__ = N_1;
                for (size_t j_1__ = 0; j_1__ < r_1_ult_1_j_1_max__; ++j_1__) {
                    vars__.push_back(r_1_ult_1(j_1__));
                }
                vars__.push_back(lprior);
            }
            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 "model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d";
    }


    void constrained_param_names(std::vector<std::string>& param_names__,
                                 bool include_tparams__ = true,
                                 bool include_gqs__ = true) const {
        std::stringstream param_name_stream__;
        size_t b_ult_j_1_max__ = K_ult;
        for (size_t j_1__ = 0; j_1__ < b_ult_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b_ult" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t b_omega_j_1_max__ = K_omega;
        for (size_t j_1__ = 0; j_1__ < b_omega_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b_omega" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t b_theta_j_1_max__ = K_theta;
        for (size_t j_1__ = 0; j_1__ < b_theta_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b_theta" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        param_name_stream__.str(std::string());
        param_name_stream__ << "sigma";
        param_names__.push_back(param_name_stream__.str());
        size_t sd_1_j_1_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < sd_1_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "sd_1" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t z_1_j_1_max__ = N_1;
        size_t z_1_k_0_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < z_1_j_1_max__; ++j_1__) {
            for (size_t k_0__ = 0; k_0__ < z_1_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "z_1" << '.' << k_0__ + 1 << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__ && !include_tparams__) return;

        if (include_tparams__) {
            size_t r_1_ult_1_j_1_max__ = N_1;
            for (size_t j_1__ = 0; j_1__ < r_1_ult_1_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_1_ult_1" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            param_name_stream__.str(std::string());
            param_name_stream__ << "lprior";
            param_names__.push_back(param_name_stream__.str());
        }

        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__;
        size_t b_ult_j_1_max__ = K_ult;
        for (size_t j_1__ = 0; j_1__ < b_ult_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b_ult" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t b_omega_j_1_max__ = K_omega;
        for (size_t j_1__ = 0; j_1__ < b_omega_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b_omega" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t b_theta_j_1_max__ = K_theta;
        for (size_t j_1__ = 0; j_1__ < b_theta_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b_theta" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        param_name_stream__.str(std::string());
        param_name_stream__ << "sigma";
        param_names__.push_back(param_name_stream__.str());
        size_t sd_1_j_1_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < sd_1_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "sd_1" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t z_1_j_1_max__ = N_1;
        size_t z_1_k_0_max__ = M_1;
        for (size_t j_1__ = 0; j_1__ < z_1_j_1_max__; ++j_1__) {
            for (size_t k_0__ = 0; k_0__ < z_1_k_0_max__; ++k_0__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "z_1" << '.' << k_0__ + 1 << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__ && !include_tparams__) return;

        if (include_tparams__) {
            size_t r_1_ult_1_j_1_max__ = N_1;
            for (size_t j_1__ = 0; j_1__ < r_1_ult_1_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_1_ult_1" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            param_name_stream__.str(std::string());
            param_name_stream__ << "lprior";
            param_names__.push_back(param_name_stream__.str());
        }

        if (!include_gqs__) return;
    }

}; // model

}  // namespace

typedef model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d_namespace::model108a35779422a_952de8d079b0fe70cfffa04bdd391f3d stan_model;

#ifndef USING_R

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