// Code generated by Stan version 2.21.0

#include <stan/model/model_header.hpp>

namespace model320c7c9c1115_29751082be245fd123effa69bea6639b_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", "model320c7c9c1115_29751082be245fd123effa69bea6639b");
    reader.add_event(112, 110, "end", "model320c7c9c1115_29751082be245fd123effa69bea6639b");
    return reader;
}

template <typename T0__, typename T1__, typename T2__>
Eigen::Matrix<typename boost::math::tools::promote_args<T0__, T1__, T2__>::type, Eigen::Dynamic, Eigen::Dynamic>
scale_r_cor(const Eigen::Matrix<T0__, Eigen::Dynamic, Eigen::Dynamic>& z,
                const Eigen::Matrix<T1__, Eigen::Dynamic, 1>& SD,
                const Eigen::Matrix<T2__, Eigen::Dynamic, Eigen::Dynamic>& L, std::ostream* pstream__) {
    typedef typename boost::math::tools::promote_args<T0__, T1__, T2__>::type local_scalar_t__;
    typedef local_scalar_t__ fun_return_scalar_t__;
    const static bool propto__ = true;
    (void) propto__;
        local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
        (void) DUMMY_VAR__;  // suppress unused var warning

    int current_statement_begin__ = -1;
    try {

        current_statement_begin__ = 13;
        return stan::math::promote_scalar<fun_return_scalar_t__>(transpose(multiply(diag_pre_multiply(SD, L), z)));
    } 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 ***");
    }
}


struct scale_r_cor_functor__ {
    template <typename T0__, typename T1__, typename T2__>
        Eigen::Matrix<typename boost::math::tools::promote_args<T0__, T1__, T2__>::type, Eigen::Dynamic, Eigen::Dynamic>
    operator()(const Eigen::Matrix<T0__, Eigen::Dynamic, Eigen::Dynamic>& z,
                const Eigen::Matrix<T1__, Eigen::Dynamic, 1>& SD,
                const Eigen::Matrix<T2__, Eigen::Dynamic, Eigen::Dynamic>& L, std::ostream* pstream__) const {
        return scale_r_cor(z, SD, L, pstream__);
    }
};

class model320c7c9c1115_29751082be245fd123effa69bea6639b
  : public stan::model::model_base_crtp<model320c7c9c1115_29751082be245fd123effa69bea6639b> {
private:
        int N;
        vector_d Y;
        int K;
        matrix_d X;
        double lasso_df;
        double lasso_scale;
        int N_1;
        int M_1;
        std::vector<int> J_1;
        vector_d Z_1_1;
        int N_2;
        int M_2;
        std::vector<int> J_2;
        vector_d Z_2_1;
        vector_d Z_2_2;
        int NC_2;
        int prior_only;
        int Kc;
        matrix_d Xc;
        vector_d means_X;
public:
    model320c7c9c1115_29751082be245fd123effa69bea6639b(rstan::io::rlist_ref_var_context& context__,
        std::ostream* pstream__ = 0)
        : model_base_crtp(0) {
        ctor_body(context__, 0, pstream__);
    }

    model320c7c9c1115_29751082be245fd123effa69bea6639b(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__ = "model320c7c9c1115_29751082be245fd123effa69bea6639b_namespace::model320c7c9c1115_29751082be245fd123effa69bea6639b";
        (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__ = 17;
            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__ = 18;
            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__ = 19;
            context__.validate_dims("data initialization", "K", "int", context__.to_vec());
            K = int(0);
            vals_i__ = context__.vals_i("K");
            pos__ = 0;
            K = vals_i__[pos__++];
            check_greater_or_equal(function__, "K", K, 1);

            current_statement_begin__ = 20;
            validate_non_negative_index("X", "N", N);
            validate_non_negative_index("X", "K", K);
            context__.validate_dims("data initialization", "X", "matrix_d", context__.to_vec(N,K));
            X = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(N, K);
            vals_r__ = context__.vals_r("X");
            pos__ = 0;
            size_t X_j_2_max__ = K;
            size_t X_j_1_max__ = N;
            for (size_t j_2__ = 0; j_2__ < X_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < X_j_1_max__; ++j_1__) {
                    X(j_1__, j_2__) = vals_r__[pos__++];
                }
            }

            current_statement_begin__ = 22;
            context__.validate_dims("data initialization", "lasso_df", "double", context__.to_vec());
            lasso_df = double(0);
            vals_r__ = context__.vals_r("lasso_df");
            pos__ = 0;
            lasso_df = vals_r__[pos__++];
            check_greater_or_equal(function__, "lasso_df", lasso_df, 0);

            current_statement_begin__ = 23;
            context__.validate_dims("data initialization", "lasso_scale", "double", context__.to_vec());
            lasso_scale = double(0);
            vals_r__ = context__.vals_r("lasso_scale");
            pos__ = 0;
            lasso_scale = vals_r__[pos__++];
            check_greater_or_equal(function__, "lasso_scale", lasso_scale, 0);

            current_statement_begin__ = 25;
            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__ = 26;
            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__ = 27;
            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__ = 29;
            validate_non_negative_index("Z_1_1", "N", N);
            context__.validate_dims("data initialization", "Z_1_1", "vector_d", context__.to_vec(N));
            Z_1_1 = Eigen::Matrix<double, Eigen::Dynamic, 1>(N);
            vals_r__ = context__.vals_r("Z_1_1");
            pos__ = 0;
            size_t Z_1_1_j_1_max__ = N;
            for (size_t j_1__ = 0; j_1__ < Z_1_1_j_1_max__; ++j_1__) {
                Z_1_1(j_1__) = vals_r__[pos__++];
            }

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

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

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

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

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

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

            current_statement_begin__ = 38;
            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
            current_statement_begin__ = 41;
            Kc = int(0);
            stan::math::fill(Kc, std::numeric_limits<int>::min());
            stan::math::assign(Kc,(K - 1));

            current_statement_begin__ = 42;
            validate_non_negative_index("Xc", "N", N);
            validate_non_negative_index("Xc", "Kc", Kc);
            Xc = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>(N, Kc);
            stan::math::fill(Xc, DUMMY_VAR__);

            current_statement_begin__ = 43;
            validate_non_negative_index("means_X", "Kc", Kc);
            means_X = Eigen::Matrix<double, Eigen::Dynamic, 1>(Kc);
            stan::math::fill(means_X, DUMMY_VAR__);

            // execute transformed data statements
            current_statement_begin__ = 44;
            for (int i = 2; i <= K; ++i) {

                current_statement_begin__ = 45;
                stan::model::assign(means_X, 
                            stan::model::cons_list(stan::model::index_uni((i - 1)), stan::model::nil_index_list()), 
                            mean(stan::model::rvalue(X, stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni(i), stan::model::nil_index_list())), "X")), 
                            "assigning variable means_X");
                current_statement_begin__ = 46;
                stan::model::assign(Xc, 
                            stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni((i - 1)), stan::model::nil_index_list())), 
                            subtract(stan::model::rvalue(X, stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni(i), stan::model::nil_index_list())), "X"), get_base1(means_X, (i - 1), "means_X", 1)), 
                            "assigning variable Xc");
            }

            // validate transformed data

            // validate, set parameter ranges
            num_params_r__ = 0U;
            param_ranges_i__.clear();
            current_statement_begin__ = 50;
            validate_non_negative_index("b", "Kc", Kc);
            num_params_r__ += Kc;
            current_statement_begin__ = 51;
            num_params_r__ += 1;
            current_statement_begin__ = 53;
            num_params_r__ += 1;
            current_statement_begin__ = 54;
            num_params_r__ += 1;
            current_statement_begin__ = 55;
            validate_non_negative_index("sd_1", "M_1", M_1);
            num_params_r__ += M_1;
            current_statement_begin__ = 56;
            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);
            current_statement_begin__ = 57;
            validate_non_negative_index("sd_2", "M_2", M_2);
            num_params_r__ += M_2;
            current_statement_begin__ = 58;
            validate_non_negative_index("z_2", "M_2", M_2);
            validate_non_negative_index("z_2", "N_2", N_2);
            num_params_r__ += (M_2 * N_2);
            current_statement_begin__ = 59;
            validate_non_negative_index("L_2", "M_2", M_2);
            validate_non_negative_index("L_2", "M_2", M_2);
            num_params_r__ += ((M_2 * (M_2 - 1)) / 2);
        } 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 ***");
        }
    }

    ~model320c7c9c1115_29751082be245fd123effa69bea6639b() { }


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

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

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

        current_statement_begin__ = 54;
        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__ = 55;
        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__ = 56;
        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__());
            }
        }

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

        current_statement_begin__ = 58;
        if (!(context__.contains_r("z_2")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable z_2 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("z_2");
        pos__ = 0U;
        validate_non_negative_index("z_2", "M_2", M_2);
        validate_non_negative_index("z_2", "N_2", N_2);
        context__.validate_dims("parameter initialization", "z_2", "matrix_d", context__.to_vec(M_2,N_2));
        Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> z_2(M_2, N_2);
        size_t z_2_j_2_max__ = N_2;
        size_t z_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < z_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < z_2_j_1_max__; ++j_1__) {
                z_2(j_1__, j_2__) = vals_r__[pos__++];
            }
        }
        try {
            writer__.matrix_unconstrain(z_2);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable z_2: ") + e.what()), current_statement_begin__, prog_reader__());
        }

        current_statement_begin__ = 59;
        if (!(context__.contains_r("L_2")))
            stan::lang::rethrow_located(std::runtime_error(std::string("Variable L_2 missing")), current_statement_begin__, prog_reader__());
        vals_r__ = context__.vals_r("L_2");
        pos__ = 0U;
        validate_non_negative_index("L_2", "M_2", M_2);
        validate_non_negative_index("L_2", "M_2", M_2);
        context__.validate_dims("parameter initialization", "L_2", "matrix_d", context__.to_vec(M_2,M_2));
        Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> L_2(M_2, M_2);
        size_t L_2_j_2_max__ = M_2;
        size_t L_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < L_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < L_2_j_1_max__; ++j_1__) {
                L_2(j_1__, j_2__) = vals_r__[pos__++];
            }
        }
        try {
            writer__.cholesky_factor_corr_unconstrain(L_2);
        } catch (const std::exception& e) {
            stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable L_2: ") + 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__ = 50;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> b;
            (void) b;  // dummy to suppress unused var warning
            if (jacobian__)
                b = in__.vector_constrain(Kc, lp__);
            else
                b = in__.vector_constrain(Kc);

            current_statement_begin__ = 51;
            local_scalar_t__ Intercept;
            (void) Intercept;  // dummy to suppress unused var warning
            if (jacobian__)
                Intercept = in__.scalar_constrain(lp__);
            else
                Intercept = in__.scalar_constrain();

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

            current_statement_begin__ = 54;
            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__ = 55;
            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__ = 56;
            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));
            }

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

            current_statement_begin__ = 58;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, Eigen::Dynamic> z_2;
            (void) z_2;  // dummy to suppress unused var warning
            if (jacobian__)
                z_2 = in__.matrix_constrain(M_2, N_2, lp__);
            else
                z_2 = in__.matrix_constrain(M_2, N_2);

            current_statement_begin__ = 59;
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, Eigen::Dynamic> L_2;
            (void) L_2;  // dummy to suppress unused var warning
            if (jacobian__)
                L_2 = in__.cholesky_factor_corr_constrain(M_2, lp__);
            else
                L_2 = in__.cholesky_factor_corr_constrain(M_2);

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

            current_statement_begin__ = 63;
            validate_non_negative_index("r_2", "N_2", N_2);
            validate_non_negative_index("r_2", "M_2", M_2);
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, Eigen::Dynamic> r_2(N_2, M_2);
            stan::math::initialize(r_2, DUMMY_VAR__);
            stan::math::fill(r_2, DUMMY_VAR__);

            current_statement_begin__ = 65;
            validate_non_negative_index("r_2_1", "N_2", N_2);
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> r_2_1(N_2);
            stan::math::initialize(r_2_1, DUMMY_VAR__);
            stan::math::fill(r_2_1, DUMMY_VAR__);

            current_statement_begin__ = 66;
            validate_non_negative_index("r_2_2", "N_2", N_2);
            Eigen::Matrix<local_scalar_t__, Eigen::Dynamic, 1> r_2_2(N_2);
            stan::math::initialize(r_2_2, DUMMY_VAR__);
            stan::math::fill(r_2_2, DUMMY_VAR__);

            // transformed parameters block statements
            current_statement_begin__ = 67;
            stan::math::assign(r_1_1, multiply(get_base1(sd_1, 1, "sd_1", 1), get_base1(z_1, 1, "z_1", 1)));
            current_statement_begin__ = 69;
            stan::math::assign(r_2, scale_r_cor(z_2, sd_2, L_2, pstream__));
            current_statement_begin__ = 70;
            stan::math::assign(r_2_1, stan::model::rvalue(r_2, stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni(1), stan::model::nil_index_list())), "r_2"));
            current_statement_begin__ = 71;
            stan::math::assign(r_2_2, stan::model::rvalue(r_2, stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni(2), stan::model::nil_index_list())), "r_2"));

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

            current_statement_begin__ = 62;
            size_t r_1_1_j_1_max__ = N_1;
            for (size_t j_1__ = 0; j_1__ < r_1_1_j_1_max__; ++j_1__) {
                if (stan::math::is_uninitialized(r_1_1(j_1__))) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: r_1_1" << "(" << j_1__ << ")";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable r_1_1: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }
            current_statement_begin__ = 63;
            size_t r_2_j_1_max__ = N_2;
            size_t r_2_j_2_max__ = M_2;
            for (size_t j_1__ = 0; j_1__ < r_2_j_1_max__; ++j_1__) {
                for (size_t j_2__ = 0; j_2__ < r_2_j_2_max__; ++j_2__) {
                    if (stan::math::is_uninitialized(r_2(j_1__, j_2__))) {
                        std::stringstream msg__;
                        msg__ << "Undefined transformed parameter: r_2" << "(" << j_1__ << ", " << j_2__ << ")";
                        stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable r_2: ") + msg__.str()), current_statement_begin__, prog_reader__());
                    }
                }
            }
            current_statement_begin__ = 65;
            size_t r_2_1_j_1_max__ = N_2;
            for (size_t j_1__ = 0; j_1__ < r_2_1_j_1_max__; ++j_1__) {
                if (stan::math::is_uninitialized(r_2_1(j_1__))) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: r_2_1" << "(" << j_1__ << ")";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable r_2_1: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }
            current_statement_begin__ = 66;
            size_t r_2_2_j_1_max__ = N_2;
            for (size_t j_1__ = 0; j_1__ < r_2_2_j_1_max__; ++j_1__) {
                if (stan::math::is_uninitialized(r_2_2(j_1__))) {
                    std::stringstream msg__;
                    msg__ << "Undefined transformed parameter: r_2_2" << "(" << j_1__ << ")";
                    stan::lang::rethrow_located(std::runtime_error(std::string("Error initializing variable r_2_2: ") + msg__.str()), current_statement_begin__, prog_reader__());
                }
            }

            // model body

            current_statement_begin__ = 75;
            if (as_bool(logical_negation(prior_only))) {
                {
                current_statement_begin__ = 77;
                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__);
                stan::math::assign(mu,add(Intercept, rep_vector(0.0, N)));


                current_statement_begin__ = 78;
                for (int n = 1; n <= N; ++n) {

                    current_statement_begin__ = 80;
                    stan::model::assign(mu, 
                                stan::model::cons_list(stan::model::index_uni(n), stan::model::nil_index_list()), 
                                (stan::model::rvalue(mu, stan::model::cons_list(stan::model::index_uni(n), stan::model::nil_index_list()), "mu") + (((get_base1(r_1_1, get_base1(J_1, n, "J_1", 1), "r_1_1", 1) * get_base1(Z_1_1, n, "Z_1_1", 1)) + (get_base1(r_2_1, get_base1(J_2, n, "J_2", 1), "r_2_1", 1) * get_base1(Z_2_1, n, "Z_2_1", 1))) + (get_base1(r_2_2, get_base1(J_2, n, "J_2", 1), "r_2_2", 1) * get_base1(Z_2_2, n, "Z_2_2", 1)))), 
                                "assigning variable mu");
                }
                current_statement_begin__ = 82;
                lp_accum__.add(normal_id_glm_log(Y, Xc, mu, b, sigma));
                }
            }
            current_statement_begin__ = 85;
            lp_accum__.add(double_exponential_log(b, 0, (lasso_scale * lasso_inv_lambda)));
            current_statement_begin__ = 86;
            lp_accum__.add(student_t_log(Intercept, 3, 46.4, 4.2));
            current_statement_begin__ = 87;
            lp_accum__.add(chi_square_log(lasso_inv_lambda, lasso_df));
            current_statement_begin__ = 88;
            lp_accum__.add((student_t_log(sigma, 3, 0, 4.2) - (1 * student_t_ccdf_log(0, 3, 0, 4.2))));
            current_statement_begin__ = 90;
            lp_accum__.add((normal_log(sd_1, 0.1, 1) - (1 * normal_ccdf_log(0, 0.1, 1))));
            current_statement_begin__ = 92;
            lp_accum__.add(std_normal_log(get_base1(z_1, 1, "z_1", 1)));
            current_statement_begin__ = 93;
            lp_accum__.add((normal_log(sd_2, 5, 1) - (2 * normal_ccdf_log(0, 5, 1))));
            current_statement_begin__ = 95;
            lp_accum__.add(std_normal_log(to_vector(z_2)));
            current_statement_begin__ = 96;
            lp_accum__.add(lkj_corr_cholesky_log(L_2, 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");
        names__.push_back("Intercept");
        names__.push_back("lasso_inv_lambda");
        names__.push_back("sigma");
        names__.push_back("sd_1");
        names__.push_back("z_1");
        names__.push_back("sd_2");
        names__.push_back("z_2");
        names__.push_back("L_2");
        names__.push_back("r_1_1");
        names__.push_back("r_2");
        names__.push_back("r_2_1");
        names__.push_back("r_2_2");
        names__.push_back("b_Intercept");
        names__.push_back("Cor_2");
        names__.push_back("cor_2");
    }


    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(Kc);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        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(M_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(M_2);
        dims__.push_back(N_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(M_2);
        dims__.push_back(M_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N_1);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N_2);
        dims__.push_back(M_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(N_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(M_2);
        dims__.push_back(M_2);
        dimss__.push_back(dims__);
        dims__.resize(0);
        dims__.push_back(NC_2);
        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__ = "model320c7c9c1115_29751082be245fd123effa69bea6639b_namespace::write_array";
        (void) function__;  // dummy to suppress unused var warning

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

        double Intercept = in__.scalar_constrain();
        vars__.push_back(Intercept);

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

        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__));
            }
        }

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

        Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> z_2 = in__.matrix_constrain(M_2, N_2);
        size_t z_2_j_2_max__ = N_2;
        size_t z_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < z_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < z_2_j_1_max__; ++j_1__) {
                vars__.push_back(z_2(j_1__, j_2__));
            }
        }

        Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> L_2 = in__.cholesky_factor_corr_constrain(M_2);
        size_t L_2_j_2_max__ = M_2;
        size_t L_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < L_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < L_2_j_1_max__; ++j_1__) {
                vars__.push_back(L_2(j_1__, j_2__));
            }
        }

        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__ = 62;
            validate_non_negative_index("r_1_1", "N_1", N_1);
            Eigen::Matrix<double, Eigen::Dynamic, 1> r_1_1(N_1);
            stan::math::initialize(r_1_1, DUMMY_VAR__);
            stan::math::fill(r_1_1, DUMMY_VAR__);

            current_statement_begin__ = 63;
            validate_non_negative_index("r_2", "N_2", N_2);
            validate_non_negative_index("r_2", "M_2", M_2);
            Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> r_2(N_2, M_2);
            stan::math::initialize(r_2, DUMMY_VAR__);
            stan::math::fill(r_2, DUMMY_VAR__);

            current_statement_begin__ = 65;
            validate_non_negative_index("r_2_1", "N_2", N_2);
            Eigen::Matrix<double, Eigen::Dynamic, 1> r_2_1(N_2);
            stan::math::initialize(r_2_1, DUMMY_VAR__);
            stan::math::fill(r_2_1, DUMMY_VAR__);

            current_statement_begin__ = 66;
            validate_non_negative_index("r_2_2", "N_2", N_2);
            Eigen::Matrix<double, Eigen::Dynamic, 1> r_2_2(N_2);
            stan::math::initialize(r_2_2, DUMMY_VAR__);
            stan::math::fill(r_2_2, DUMMY_VAR__);

            // do transformed parameters statements
            current_statement_begin__ = 67;
            stan::math::assign(r_1_1, multiply(get_base1(sd_1, 1, "sd_1", 1), get_base1(z_1, 1, "z_1", 1)));
            current_statement_begin__ = 69;
            stan::math::assign(r_2, scale_r_cor(z_2, sd_2, L_2, pstream__));
            current_statement_begin__ = 70;
            stan::math::assign(r_2_1, stan::model::rvalue(r_2, stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni(1), stan::model::nil_index_list())), "r_2"));
            current_statement_begin__ = 71;
            stan::math::assign(r_2_2, stan::model::rvalue(r_2, stan::model::cons_list(stan::model::index_omni(), stan::model::cons_list(stan::model::index_uni(2), stan::model::nil_index_list())), "r_2"));

            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_1_j_1_max__ = N_1;
                for (size_t j_1__ = 0; j_1__ < r_1_1_j_1_max__; ++j_1__) {
                    vars__.push_back(r_1_1(j_1__));
                }
                size_t r_2_j_2_max__ = M_2;
                size_t r_2_j_1_max__ = N_2;
                for (size_t j_2__ = 0; j_2__ < r_2_j_2_max__; ++j_2__) {
                    for (size_t j_1__ = 0; j_1__ < r_2_j_1_max__; ++j_1__) {
                        vars__.push_back(r_2(j_1__, j_2__));
                    }
                }
                size_t r_2_1_j_1_max__ = N_2;
                for (size_t j_1__ = 0; j_1__ < r_2_1_j_1_max__; ++j_1__) {
                    vars__.push_back(r_2_1(j_1__));
                }
                size_t r_2_2_j_1_max__ = N_2;
                for (size_t j_1__ = 0; j_1__ < r_2_2_j_1_max__; ++j_1__) {
                    vars__.push_back(r_2_2(j_1__));
                }
            }
            if (!include_gqs__) return;
            // declare and define generated quantities
            current_statement_begin__ = 100;
            double b_Intercept;
            (void) b_Intercept;  // dummy to suppress unused var warning
            stan::math::initialize(b_Intercept, DUMMY_VAR__);
            stan::math::fill(b_Intercept, DUMMY_VAR__);
            stan::math::assign(b_Intercept,(Intercept - dot_product(means_X, b)));

            current_statement_begin__ = 102;
            validate_non_negative_index("Cor_2", "M_2", M_2);
            validate_non_negative_index("Cor_2", "M_2", M_2);
            Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> Cor_2(M_2, M_2);
            stan::math::initialize(Cor_2, DUMMY_VAR__);
            stan::math::fill(Cor_2, DUMMY_VAR__);
            stan::math::assign(Cor_2,multiply_lower_tri_self_transpose(L_2));

            current_statement_begin__ = 103;
            validate_non_negative_index("cor_2", "NC_2", NC_2);
            Eigen::Matrix<double, Eigen::Dynamic, 1> cor_2(NC_2);
            stan::math::initialize(cor_2, DUMMY_VAR__);
            stan::math::fill(cor_2, DUMMY_VAR__);

            // generated quantities statements
            current_statement_begin__ = 105;
            for (int k = 1; k <= M_2; ++k) {

                current_statement_begin__ = 106;
                for (int j = 1; j <= (k - 1); ++j) {

                    current_statement_begin__ = 107;
                    stan::model::assign(cor_2, 
                                stan::model::cons_list(stan::model::index_uni((choose((k - 1), 2) + j)), stan::model::nil_index_list()), 
                                get_base1(Cor_2, j, k, "Cor_2", 1), 
                                "assigning variable cor_2");
                }
            }

            // validate, write generated quantities
            current_statement_begin__ = 100;
            vars__.push_back(b_Intercept);

            current_statement_begin__ = 102;
            stan::math::check_corr_matrix(function__, "Cor_2", Cor_2);

            size_t Cor_2_j_2_max__ = M_2;
            size_t Cor_2_j_1_max__ = M_2;
            for (size_t j_2__ = 0; j_2__ < Cor_2_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < Cor_2_j_1_max__; ++j_1__) {
                    vars__.push_back(Cor_2(j_1__, j_2__));
                }
            }

            current_statement_begin__ = 103;
            check_greater_or_equal(function__, "cor_2", cor_2, -(1));
            check_less_or_equal(function__, "cor_2", cor_2, 1);

            size_t cor_2_j_1_max__ = NC_2;
            for (size_t j_1__ = 0; j_1__ < cor_2_j_1_max__; ++j_1__) {
                vars__.push_back(cor_2(j_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 ***");
        }
    }

    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 "model320c7c9c1115_29751082be245fd123effa69bea6639b";
    }


    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_j_1_max__ = Kc;
        for (size_t j_1__ = 0; j_1__ < b_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        param_name_stream__.str(std::string());
        param_name_stream__ << "Intercept";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "lasso_inv_lambda";
        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());
            }
        }
        size_t sd_2_j_1_max__ = M_2;
        for (size_t j_1__ = 0; j_1__ < sd_2_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "sd_2" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t z_2_j_2_max__ = N_2;
        size_t z_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < z_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < z_2_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "z_2" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }
        size_t L_2_j_2_max__ = M_2;
        size_t L_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < L_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < L_2_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "L_2" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__ && !include_tparams__) return;

        if (include_tparams__) {
            size_t r_1_1_j_1_max__ = N_1;
            for (size_t j_1__ = 0; j_1__ < r_1_1_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_1_1" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t r_2_j_2_max__ = M_2;
            size_t r_2_j_1_max__ = N_2;
            for (size_t j_2__ = 0; j_2__ < r_2_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < r_2_j_1_max__; ++j_1__) {
                    param_name_stream__.str(std::string());
                    param_name_stream__ << "r_2" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
                    param_names__.push_back(param_name_stream__.str());
                }
            }
            size_t r_2_1_j_1_max__ = N_2;
            for (size_t j_1__ = 0; j_1__ < r_2_1_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_2_1" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t r_2_2_j_1_max__ = N_2;
            for (size_t j_1__ = 0; j_1__ < r_2_2_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_2_2" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__) return;
        param_name_stream__.str(std::string());
        param_name_stream__ << "b_Intercept";
        param_names__.push_back(param_name_stream__.str());
        size_t Cor_2_j_2_max__ = M_2;
        size_t Cor_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < Cor_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < Cor_2_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "Cor_2" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }
        size_t cor_2_j_1_max__ = NC_2;
        for (size_t j_1__ = 0; j_1__ < cor_2_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "cor_2" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
    }


    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_j_1_max__ = Kc;
        for (size_t j_1__ = 0; j_1__ < b_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "b" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        param_name_stream__.str(std::string());
        param_name_stream__ << "Intercept";
        param_names__.push_back(param_name_stream__.str());
        param_name_stream__.str(std::string());
        param_name_stream__ << "lasso_inv_lambda";
        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());
            }
        }
        size_t sd_2_j_1_max__ = M_2;
        for (size_t j_1__ = 0; j_1__ < sd_2_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "sd_2" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t z_2_j_2_max__ = N_2;
        size_t z_2_j_1_max__ = M_2;
        for (size_t j_2__ = 0; j_2__ < z_2_j_2_max__; ++j_2__) {
            for (size_t j_1__ = 0; j_1__ < z_2_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "z_2" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }
        size_t L_2_j_1_max__ = ((M_2 * (M_2 - 1)) / 2);
        for (size_t j_1__ = 0; j_1__ < L_2_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "L_2" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }

        if (!include_gqs__ && !include_tparams__) return;

        if (include_tparams__) {
            size_t r_1_1_j_1_max__ = N_1;
            for (size_t j_1__ = 0; j_1__ < r_1_1_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_1_1" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t r_2_j_2_max__ = M_2;
            size_t r_2_j_1_max__ = N_2;
            for (size_t j_2__ = 0; j_2__ < r_2_j_2_max__; ++j_2__) {
                for (size_t j_1__ = 0; j_1__ < r_2_j_1_max__; ++j_1__) {
                    param_name_stream__.str(std::string());
                    param_name_stream__ << "r_2" << '.' << j_1__ + 1 << '.' << j_2__ + 1;
                    param_names__.push_back(param_name_stream__.str());
                }
            }
            size_t r_2_1_j_1_max__ = N_2;
            for (size_t j_1__ = 0; j_1__ < r_2_1_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_2_1" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
            size_t r_2_2_j_1_max__ = N_2;
            for (size_t j_1__ = 0; j_1__ < r_2_2_j_1_max__; ++j_1__) {
                param_name_stream__.str(std::string());
                param_name_stream__ << "r_2_2" << '.' << j_1__ + 1;
                param_names__.push_back(param_name_stream__.str());
            }
        }

        if (!include_gqs__) return;
        param_name_stream__.str(std::string());
        param_name_stream__ << "b_Intercept";
        param_names__.push_back(param_name_stream__.str());
        size_t Cor_2_j_1_max__ = ((M_2 * (M_2 - 1)) / 2);
        for (size_t j_1__ = 0; j_1__ < Cor_2_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "Cor_2" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
        size_t cor_2_j_1_max__ = NC_2;
        for (size_t j_1__ = 0; j_1__ < cor_2_j_1_max__; ++j_1__) {
            param_name_stream__.str(std::string());
            param_name_stream__ << "cor_2" << '.' << j_1__ + 1;
            param_names__.push_back(param_name_stream__.str());
        }
    }

}; // model

}  // namespace

typedef model320c7c9c1115_29751082be245fd123effa69bea6639b_namespace::model320c7c9c1115_29751082be245fd123effa69bea6639b 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