// Generated by rstantools.  Do not edit by hand.

/*
    ForceChoice is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ForceChoice is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ForceChoice.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MODELS_HPP
#define MODELS_HPP
#define STAN__SERVICES__COMMAND_HPP
#ifndef USE_STANC3
#define USE_STANC3
#endif
#include <rstan/rstaninc.hpp>
// Code generated by stanc v2.32.2
#include <stan/model/model_header.hpp>
namespace model_MGPCM_namespace {
using stan::model::model_base_crtp;
using namespace stan::math;
stan::math::profile_map profiles__;
static constexpr std::array<const char*, 123> locations_array__ =
  {" (found before start of program)",
  " (in 'string', line 31, column 2 to column 38)",
  " (in 'string', line 32, column 2 to column 29)",
  " (in 'string', line 33, column 2 to column 33)",
  " (in 'string', line 34, column 2 to column 31)",
  " (in 'string', line 37, column 2 to column 26)",
  " (in 'string', line 38, column 2 to column 24)",
  " (in 'string', line 39, column 2 to column 21)",
  " (in 'string', line 40, column 2 to column 20)",
  " (in 'string', line 105, column 2 to column 20)",
  " (in 'string', line 106, column 2 to column 24)",
  " (in 'string', line 43, column 4 to column 16)",
  " (in 'string', line 50, column 10 to column 24)",
  " (in 'string', line 49, column 15 to line 51, column 9)",
  " (in 'string', line 47, column 10 to column 32)",
  " (in 'string', line 48, column 10 to column 19)",
  " (in 'string', line 46, column 33 to line 49, column 9)",
  " (in 'string', line 46, column 8 to line 51, column 9)",
  " (in 'string', line 45, column 21 to line 52, column 7)",
  " (in 'string', line 45, column 6 to line 52, column 7)",
  " (in 'string', line 44, column 19 to line 53, column 5)",
  " (in 'string', line 44, column 4 to line 53, column 5)",
  " (in 'string', line 42, column 2 to line 54, column 3)",
  " (in 'string', line 57, column 4 to column 16)",
  " (in 'string', line 59, column 6 to column 20)",
  " (in 'string', line 61, column 8 to column 30)",
  " (in 'string', line 62, column 8 to column 17)",
  " (in 'string', line 60, column 34 to line 63, column 7)",
  " (in 'string', line 60, column 6 to line 63, column 7)",
  " (in 'string', line 66, column 10 to column 24)",
  " (in 'string', line 65, column 49 to line 67, column 9)",
  " (in 'string', line 65, column 8 to line 67, column 9)",
  " (in 'string', line 64, column 37 to line 68, column 7)",
  " (in 'string', line 64, column 6 to line 68, column 7)",
  " (in 'string', line 58, column 19 to line 69, column 5)",
  " (in 'string', line 58, column 4 to line 69, column 5)",
  " (in 'string', line 56, column 2 to line 70, column 3)",
  " (in 'string', line 76, column 6 to column 50)",
  " (in 'string', line 75, column 11 to line 77, column 5)",
  " (in 'string', line 74, column 6 to column 53)",
  " (in 'string', line 73, column 15 to line 75, column 5)",
  " (in 'string', line 73, column 4 to line 77, column 5)",
  " (in 'string', line 72, column 17 to line 78, column 3)",
  " (in 'string', line 72, column 2 to line 78, column 3)",
  " (in 'string', line 81, column 4 to column 51)",
  " (in 'string', line 80, column 21 to line 82, column 3)",
  " (in 'string', line 80, column 2 to line 82, column 3)",
  " (in 'string', line 110, column 4 to column 21)",
  " (in 'string', line 108, column 4 to column 53)",
  " (in 'string', line 107, column 2 to line 110, column 21)",
  " (in 'string', line 112, column 4 to column 19)",
  " (in 'string', line 113, column 4 to column 27)",
  " (in 'string', line 114, column 11 to column 12)",
  " (in 'string', line 114, column 4 to column 17)",
  " (in 'string', line 116, column 6 to column 41)",
  " (in 'string', line 115, column 4 to line 116, column 41)",
  " (in 'string', line 117, column 4 to column 48)",
  " (in 'string', line 111, column 21 to line 118, column 3)",
  " (in 'string', line 111, column 2 to line 118, column 3)",
  " (in 'string', line 85, column 2 to column 36)",
  " (in 'string', line 86, column 2 to column 33)",
  " (in 'string', line 88, column 4 to column 34)",
  " (in 'string', line 87, column 13 to line 89, column 3)",
  " (in 'string', line 87, column 2 to line 89, column 3)",
  " (in 'string', line 91, column 4 to column 32)",
  " (in 'string', line 90, column 17 to line 92, column 3)",
  " (in 'string', line 90, column 2 to line 92, column 3)",
  " (in 'string', line 95, column 4 to column 19)",
  " (in 'string', line 96, column 4 to column 27)",
  " (in 'string', line 97, column 11 to column 12)",
  " (in 'string', line 97, column 4 to column 23)",
  " (in 'string', line 99, column 6 to column 47)",
  " (in 'string', line 98, column 19 to line 100, column 5)",
  " (in 'string', line 98, column 4 to line 100, column 5)",
  " (in 'string', line 101, column 4 to column 57)",
  " (in 'string', line 94, column 21 to line 102, column 3)",
  " (in 'string', line 94, column 2 to line 102, column 3)",
  " (in 'string', line 3, column 2 to column 21)",
  " (in 'string', line 4, column 2 to column 17)",
  " (in 'string', line 5, column 2 to column 17)",
  " (in 'string', line 6, column 2 to column 17)",
  " (in 'string', line 7, column 2 to column 24)",
  " (in 'string', line 8, column 8 to column 13)",
  " (in 'string', line 8, column 2 to column 48)",
  " (in 'string', line 9, column 8 to column 13)",
  " (in 'string', line 9, column 2 to column 41)",
  " (in 'string', line 10, column 8 to column 13)",
  " (in 'string', line 10, column 2 to column 41)",
  " (in 'string', line 11, column 8 to column 9)",
  " (in 'string', line 11, column 2 to column 52)",
  " (in 'string', line 12, column 9 to column 10)",
  " (in 'string', line 12, column 12 to column 13)",
  " (in 'string', line 12, column 2 to column 24)",
  " (in 'string', line 13, column 2 to column 12)",
  " (in 'string', line 14, column 2 to column 24)",
  " (in 'string', line 15, column 2 to column 12)",
  " (in 'string', line 16, column 2 to column 24)",
  " (in 'string', line 17, column 9 to column 10)",
  " (in 'string', line 17, column 2 to column 21)",
  " (in 'string', line 20, column 2 to column 22)",
  " (in 'string', line 24, column 8 to column 25)",
  " (in 'string', line 23, column 31 to line 25, column 7)",
  " (in 'string', line 23, column 6 to line 25, column 7)",
  " (in 'string', line 22, column 19 to line 26, column 5)",
  " (in 'string', line 22, column 4 to line 26, column 5)",
  " (in 'string', line 21, column 17 to line 27, column 3)",
  " (in 'string', line 21, column 2 to line 27, column 3)",
  " (in 'string', line 28, column 2 to column 41)",
  " (in 'string', line 31, column 18 to column 29)",
  " (in 'string', line 32, column 9 to column 20)",
  " (in 'string', line 33, column 23 to column 24)",
  " (in 'string', line 34, column 8 to column 9)",
  " (in 'string', line 34, column 18 to column 19)",
  " (in 'string', line 37, column 18 to column 19)",
  " (in 'string', line 37, column 21 to column 22)",
  " (in 'string', line 38, column 9 to column 10)",
  " (in 'string', line 38, column 12 to column 20)",
  " (in 'string', line 39, column 9 to column 10)",
  " (in 'string', line 39, column 12 to column 13)",
  " (in 'string', line 40, column 9 to column 14)",
  " (in 'string', line 105, column 9 to column 10)",
  " (in 'string', line 105, column 12 to column 13)",
  " (in 'string', line 106, column 9 to column 14)"};
#include <stan_meta_header.hpp>
class model_MGPCM final : public model_base_crtp<model_MGPCM> {
private:
  int N_obs;
  int N;
  int I;
  int D;
  int max_poly;
  std::vector<int> y;
  std::vector<int> pid;
  std::vector<int> iid;
  std::vector<int> length_poly;
  Eigen::Matrix<double,-1,-1> Q_matrix_data__;
  double a_mu;
  double a_sigma;
  double d_mu;
  double d_sigma;
  Eigen::Matrix<double,-1,1> theta_mu_data__;
  int a_free_size;
  int d_free_size;
  Eigen::Map<Eigen::Matrix<double,-1,-1>> Q_matrix{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double,-1,1>> theta_mu{nullptr, 0};
public:
  ~model_MGPCM() {}
  model_MGPCM(stan::io::var_context& context__, unsigned int
              random_seed__ = 0, std::ostream* pstream__ = nullptr)
      : model_base_crtp(0) {
    int current_statement__ = 0;
    using local_scalar_t__ = double;
    boost::ecuyer1988 base_rng__ =
      stan::services::util::create_rng(random_seed__, 0);
    // suppress unused var warning
    (void) base_rng__;
    static constexpr const char* function__ =
      "model_MGPCM_namespace::model_MGPCM";
    // suppress unused var warning
    (void) function__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    try {
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      current_statement__ = 77;
      context__.validate_dims("data initialization", "N_obs", "int",
        std::vector<size_t>{});
      N_obs = std::numeric_limits<int>::min();
      current_statement__ = 77;
      N_obs = context__.vals_i("N_obs")[(1 - 1)];
      current_statement__ = 77;
      stan::math::check_greater_or_equal(function__, "N_obs", N_obs, 1);
      current_statement__ = 78;
      context__.validate_dims("data initialization", "N", "int",
        std::vector<size_t>{});
      N = std::numeric_limits<int>::min();
      current_statement__ = 78;
      N = context__.vals_i("N")[(1 - 1)];
      current_statement__ = 78;
      stan::math::check_greater_or_equal(function__, "N", N, 1);
      current_statement__ = 79;
      context__.validate_dims("data initialization", "I", "int",
        std::vector<size_t>{});
      I = std::numeric_limits<int>::min();
      current_statement__ = 79;
      I = context__.vals_i("I")[(1 - 1)];
      current_statement__ = 79;
      stan::math::check_greater_or_equal(function__, "I", I, 1);
      current_statement__ = 80;
      context__.validate_dims("data initialization", "D", "int",
        std::vector<size_t>{});
      D = std::numeric_limits<int>::min();
      current_statement__ = 80;
      D = context__.vals_i("D")[(1 - 1)];
      current_statement__ = 80;
      stan::math::check_greater_or_equal(function__, "D", D, 1);
      current_statement__ = 81;
      context__.validate_dims("data initialization", "max_poly", "int",
        std::vector<size_t>{});
      max_poly = std::numeric_limits<int>::min();
      current_statement__ = 81;
      max_poly = context__.vals_i("max_poly")[(1 - 1)];
      current_statement__ = 81;
      stan::math::check_greater_or_equal(function__, "max_poly", max_poly, 1);
      current_statement__ = 82;
      stan::math::validate_non_negative_index("y", "N_obs", N_obs);
      current_statement__ = 83;
      context__.validate_dims("data initialization", "y", "int",
        std::vector<size_t>{static_cast<size_t>(N_obs)});
      y = std::vector<int>(N_obs, std::numeric_limits<int>::min());
      current_statement__ = 83;
      y = context__.vals_i("y");
      current_statement__ = 83;
      stan::math::check_greater_or_equal(function__, "y", y, 0);
      current_statement__ = 83;
      stan::math::check_less_or_equal(function__, "y", y, (max_poly - 1));
      current_statement__ = 84;
      stan::math::validate_non_negative_index("pid", "N_obs", N_obs);
      current_statement__ = 85;
      context__.validate_dims("data initialization", "pid", "int",
        std::vector<size_t>{static_cast<size_t>(N_obs)});
      pid = std::vector<int>(N_obs, std::numeric_limits<int>::min());
      current_statement__ = 85;
      pid = context__.vals_i("pid");
      current_statement__ = 85;
      stan::math::check_greater_or_equal(function__, "pid", pid, 1);
      current_statement__ = 85;
      stan::math::check_less_or_equal(function__, "pid", pid, N);
      current_statement__ = 86;
      stan::math::validate_non_negative_index("iid", "N_obs", N_obs);
      current_statement__ = 87;
      context__.validate_dims("data initialization", "iid", "int",
        std::vector<size_t>{static_cast<size_t>(N_obs)});
      iid = std::vector<int>(N_obs, std::numeric_limits<int>::min());
      current_statement__ = 87;
      iid = context__.vals_i("iid");
      current_statement__ = 87;
      stan::math::check_greater_or_equal(function__, "iid", iid, 1);
      current_statement__ = 87;
      stan::math::check_less_or_equal(function__, "iid", iid, I);
      current_statement__ = 88;
      stan::math::validate_non_negative_index("length_poly", "I", I);
      current_statement__ = 89;
      context__.validate_dims("data initialization", "length_poly", "int",
        std::vector<size_t>{static_cast<size_t>(I)});
      length_poly = std::vector<int>(I, std::numeric_limits<int>::min());
      current_statement__ = 89;
      length_poly = context__.vals_i("length_poly");
      current_statement__ = 89;
      stan::math::check_greater_or_equal(function__, "length_poly",
        length_poly, 2);
      current_statement__ = 89;
      stan::math::check_less_or_equal(function__, "length_poly", length_poly,
        max_poly);
      current_statement__ = 90;
      stan::math::validate_non_negative_index("Q_matrix", "I", I);
      current_statement__ = 91;
      stan::math::validate_non_negative_index("Q_matrix", "D", D);
      current_statement__ = 92;
      context__.validate_dims("data initialization", "Q_matrix", "double",
        std::vector<size_t>{static_cast<size_t>(I), static_cast<size_t>(D)});
      Q_matrix_data__ = Eigen::Matrix<double,-1,-1>::Constant(I, D,
                          std::numeric_limits<double>::quiet_NaN());
      new (&Q_matrix)
        Eigen::Map<Eigen::Matrix<double,-1,-1>>(Q_matrix_data__.data(), I, D);
      {
        std::vector<local_scalar_t__> Q_matrix_flat__;
        current_statement__ = 92;
        Q_matrix_flat__ = context__.vals_r("Q_matrix");
        current_statement__ = 92;
        pos__ = 1;
        current_statement__ = 92;
        for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
          current_statement__ = 92;
          for (int sym2__ = 1; sym2__ <= I; ++sym2__) {
            current_statement__ = 92;
            stan::model::assign(Q_matrix, Q_matrix_flat__[(pos__ - 1)],
              "assigning variable Q_matrix", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 92;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 93;
      context__.validate_dims("data initialization", "a_mu", "double",
        std::vector<size_t>{});
      a_mu = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 93;
      a_mu = context__.vals_r("a_mu")[(1 - 1)];
      current_statement__ = 94;
      context__.validate_dims("data initialization", "a_sigma", "double",
        std::vector<size_t>{});
      a_sigma = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 94;
      a_sigma = context__.vals_r("a_sigma")[(1 - 1)];
      current_statement__ = 94;
      stan::math::check_greater_or_equal(function__, "a_sigma", a_sigma, 0);
      current_statement__ = 95;
      context__.validate_dims("data initialization", "d_mu", "double",
        std::vector<size_t>{});
      d_mu = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 95;
      d_mu = context__.vals_r("d_mu")[(1 - 1)];
      current_statement__ = 96;
      context__.validate_dims("data initialization", "d_sigma", "double",
        std::vector<size_t>{});
      d_sigma = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 96;
      d_sigma = context__.vals_r("d_sigma")[(1 - 1)];
      current_statement__ = 96;
      stan::math::check_greater_or_equal(function__, "d_sigma", d_sigma, 0);
      current_statement__ = 97;
      stan::math::validate_non_negative_index("theta_mu", "D", D);
      current_statement__ = 98;
      context__.validate_dims("data initialization", "theta_mu", "double",
        std::vector<size_t>{static_cast<size_t>(D)});
      theta_mu_data__ = Eigen::Matrix<double,-1,1>::Constant(D,
                          std::numeric_limits<double>::quiet_NaN());
      new (&theta_mu)
        Eigen::Map<Eigen::Matrix<double,-1,1>>(theta_mu_data__.data(), D);
      {
        std::vector<local_scalar_t__> theta_mu_flat__;
        current_statement__ = 98;
        theta_mu_flat__ = context__.vals_r("theta_mu");
        current_statement__ = 98;
        pos__ = 1;
        current_statement__ = 98;
        for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
          current_statement__ = 98;
          stan::model::assign(theta_mu, theta_mu_flat__[(pos__ - 1)],
            "assigning variable theta_mu", stan::model::index_uni(sym1__));
          current_statement__ = 98;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 99;
      a_free_size = std::numeric_limits<int>::min();
      current_statement__ = 99;
      a_free_size = 0;
      current_statement__ = 106;
      for (int i = 1; i <= I; ++i) {
        current_statement__ = 104;
        for (int j = 1; j <= D; ++j) {
          current_statement__ = 102;
          if (stan::math::logical_eq(
                stan::model::rvalue(Q_matrix, "Q_matrix",
                  stan::model::index_uni(i), stan::model::index_uni(j)), 1)) {
            current_statement__ = 100;
            a_free_size = (a_free_size + 1);
          }
        }
      }
      current_statement__ = 107;
      d_free_size = std::numeric_limits<int>::min();
      current_statement__ = 107;
      d_free_size = (stan::math::sum(length_poly) - I);
      current_statement__ = 108;
      stan::math::validate_non_negative_index("a_free", "a_free_size",
        a_free_size);
      current_statement__ = 109;
      stan::math::validate_non_negative_index("d_free", "d_free_size",
        d_free_size);
      current_statement__ = 110;
      stan::math::validate_non_negative_index("L_Corr", "D", D);
      current_statement__ = 110;
      stan::math::validate_non_negative_index("L_Corr", "D", D);
      current_statement__ = 111;
      stan::math::validate_non_negative_index("theta_raw", "N", N);
      current_statement__ = 112;
      stan::math::validate_non_negative_index("theta_raw", "D", D);
      current_statement__ = 113;
      stan::math::validate_non_negative_index("a", "I", I);
      current_statement__ = 114;
      stan::math::validate_non_negative_index("a", "D", D);
      current_statement__ = 115;
      stan::math::validate_non_negative_index("d", "I", I);
      current_statement__ = 116;
      stan::math::validate_non_negative_index("d", "max_poly", max_poly);
      current_statement__ = 117;
      stan::math::validate_non_negative_index("theta", "N", N);
      current_statement__ = 118;
      stan::math::validate_non_negative_index("theta", "D", D);
      current_statement__ = 119;
      stan::math::validate_non_negative_index("eta", "N_obs", N_obs);
      current_statement__ = 120;
      stan::math::validate_non_negative_index("Corr", "D", D);
      current_statement__ = 121;
      stan::math::validate_non_negative_index("Corr", "D", D);
      current_statement__ = 122;
      stan::math::validate_non_negative_index("log_lik", "N_obs", N_obs);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    num_params_r__ = a_free_size + d_free_size + ((D * (D - 1)) / 2) + (N *
      D);
  }
  inline std::string model_name() const final {
    return "model_MGPCM";
  }
  inline std::vector<std::string> model_compile_info() const noexcept {
    return std::vector<std::string>{"stanc_version = stanc3 v2.32.2",
             "stancflags = --allow-undefined"};
  }
  template <bool propto__, bool jacobian__, typename VecR, typename VecI,
            stan::require_vector_like_t<VecR>* = nullptr,
            stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr>
  inline stan::scalar_type_t<VecR>
  log_prob_impl(VecR& params_r__, VecI& params_i__, std::ostream*
                pstream__ = nullptr) const {
    using T__ = stan::scalar_type_t<VecR>;
    using local_scalar_t__ = T__;
    T__ lp__(0.0);
    stan::math::accumulator<T__> lp_accum__;
    stan::io::deserializer<local_scalar_t__> in__(params_r__, params_i__);
    int current_statement__ = 0;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    static constexpr const char* function__ =
      "model_MGPCM_namespace::log_prob";
    // suppress unused var warning
    (void) function__;
    try {
      Eigen::Matrix<local_scalar_t__,-1,1> a_free =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(a_free_size,
          DUMMY_VAR__);
      current_statement__ = 1;
      a_free = in__.template read_constrain_lb<
                 Eigen::Matrix<local_scalar_t__,-1,1>, jacobian__>(0, lp__,
                 a_free_size);
      Eigen::Matrix<local_scalar_t__,-1,1> d_free =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(d_free_size,
          DUMMY_VAR__);
      current_statement__ = 2;
      d_free = in__.template read<
                 Eigen::Matrix<local_scalar_t__,-1,1>>(d_free_size);
      Eigen::Matrix<local_scalar_t__,-1,-1> L_Corr =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(D, D, DUMMY_VAR__);
      current_statement__ = 3;
      L_Corr = in__.template read_constrain_cholesky_factor_corr<
                 Eigen::Matrix<local_scalar_t__,-1,-1>, jacobian__>(lp__, D);
      std::vector<Eigen::Matrix<local_scalar_t__,-1,1>> theta_raw =
        std::vector<Eigen::Matrix<local_scalar_t__,-1,1>>(N,
          Eigen::Matrix<local_scalar_t__,-1,1>::Constant(D, DUMMY_VAR__));
      current_statement__ = 4;
      theta_raw = in__.template read<
                    std::vector<Eigen::Matrix<local_scalar_t__,-1,1>>>(N, D);
      Eigen::Matrix<local_scalar_t__,-1,-1> a =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(I, D, DUMMY_VAR__);
      Eigen::Matrix<local_scalar_t__,-1,-1> d =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(I, max_poly,
          DUMMY_VAR__);
      Eigen::Matrix<local_scalar_t__,-1,-1> theta =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(N, D, DUMMY_VAR__);
      Eigen::Matrix<local_scalar_t__,-1,1> eta =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(N_obs, DUMMY_VAR__);
      {
        int idx = std::numeric_limits<int>::min();
        current_statement__ = 11;
        idx = 1;
        current_statement__ = 21;
        for (int i = 1; i <= I; ++i) {
          current_statement__ = 19;
          for (int j = 1; j <= D; ++j) {
            current_statement__ = 17;
            if (stan::math::logical_eq(
                  stan::model::rvalue(Q_matrix, "Q_matrix",
                    stan::model::index_uni(i), stan::model::index_uni(j)), 1)) {
              current_statement__ = 14;
              stan::model::assign(a,
                stan::model::rvalue(a_free, "a_free",
                  stan::model::index_uni(idx)), "assigning variable a",
                stan::model::index_uni(i), stan::model::index_uni(j));
              current_statement__ = 15;
              idx = (idx + 1);
            } else {
              current_statement__ = 12;
              stan::model::assign(a, 0.0, "assigning variable a",
                stan::model::index_uni(i), stan::model::index_uni(j));
            }
          }
        }
      }
      {
        int idx = std::numeric_limits<int>::min();
        current_statement__ = 23;
        idx = 1;
        current_statement__ = 35;
        for (int i = 1; i <= I; ++i) {
          current_statement__ = 24;
          stan::model::assign(d, 0.0, "assigning variable d",
            stan::model::index_uni(i), stan::model::index_uni(1));
          current_statement__ = 28;
          for (int k = 2; k <=
               stan::model::rvalue(length_poly, "length_poly",
                 stan::model::index_uni(i)); ++k) {
            current_statement__ = 25;
            stan::model::assign(d,
              stan::model::rvalue(d_free, "d_free",
                stan::model::index_uni(idx)), "assigning variable d",
              stan::model::index_uni(i), stan::model::index_uni(k));
            current_statement__ = 26;
            idx = (idx + 1);
          }
          current_statement__ = 33;
          if (stan::math::logical_lt(
                stan::model::rvalue(length_poly, "length_poly",
                  stan::model::index_uni(i)), max_poly)) {
            current_statement__ = 31;
            for (int k =
                   (stan::model::rvalue(length_poly, "length_poly",
                      stan::model::index_uni(i)) + 1); k <= max_poly; ++k) {
              current_statement__ = 29;
              stan::model::assign(d, 0.0, "assigning variable d",
                stan::model::index_uni(i), stan::model::index_uni(k));
            }
          }
        }
      }
      current_statement__ = 43;
      for (int n = 1; n <= N; ++n) {
        current_statement__ = 41;
        if (stan::math::logical_gt(D, 1)) {
          current_statement__ = 39;
          stan::model::assign(theta,
            stan::math::transpose(
              stan::math::add(theta_mu,
                stan::math::multiply(L_Corr,
                  stan::model::rvalue(theta_raw, "theta_raw",
                    stan::model::index_uni(n))))),
            "assigning variable theta", stan::model::index_uni(n));
        } else {
          current_statement__ = 37;
          stan::model::assign(theta,
            (stan::model::rvalue(theta_mu, "theta_mu",
               stan::model::index_uni(1)) +
            stan::model::rvalue(
              stan::model::rvalue(theta_raw, "theta_raw",
                stan::model::index_uni(n)), "theta_raw[n]",
              stan::model::index_uni(1))), "assigning variable theta",
            stan::model::index_uni(n), stan::model::index_uni(1));
        }
      }
      current_statement__ = 46;
      for (int n = 1; n <= N_obs; ++n) {
        current_statement__ = 44;
        stan::model::assign(eta,
          stan::math::dot_product(
            stan::model::rvalue(theta, "theta",
              stan::model::index_uni(
                stan::model::rvalue(pid, "pid", stan::model::index_uni(n)))),
            stan::model::rvalue(a, "a",
              stan::model::index_uni(
                stan::model::rvalue(iid, "iid", stan::model::index_uni(n))))),
          "assigning variable eta", stan::model::index_uni(n));
      }
      current_statement__ = 5;
      stan::math::check_greater_or_equal(function__, "a", a, 0);
      {
        current_statement__ = 59;
        lp_accum__.add(stan::math::lognormal_lpdf<propto__>(a_free, a_mu,
                         a_sigma));
        current_statement__ = 60;
        lp_accum__.add(stan::math::normal_lpdf<propto__>(d_free, d_mu,
                         d_sigma));
        current_statement__ = 63;
        if (stan::math::logical_gt(D, 1)) {
          current_statement__ = 61;
          lp_accum__.add(stan::math::lkj_corr_cholesky_lpdf<propto__>(L_Corr,
                           1));
        }
        current_statement__ = 66;
        for (int n = 1; n <= N; ++n) {
          current_statement__ = 64;
          lp_accum__.add(stan::math::std_normal_lpdf<propto__>(
                           stan::model::rvalue(theta_raw, "theta_raw",
                             stan::model::index_uni(n))));
        }
        current_statement__ = 76;
        for (int n = 1; n <= N_obs; ++n) {
          int i = std::numeric_limits<int>::min();
          current_statement__ = 67;
          i = stan::model::rvalue(iid, "iid", stan::model::index_uni(n));
          int K = std::numeric_limits<int>::min();
          current_statement__ = 68;
          K = stan::model::rvalue(length_poly, "length_poly",
                stan::model::index_uni(i));
          current_statement__ = 69;
          stan::math::validate_non_negative_index("log_prob", "K", K);
          Eigen::Matrix<local_scalar_t__,-1,1> log_prob =
            Eigen::Matrix<local_scalar_t__,-1,1>::Constant(K, DUMMY_VAR__);
          current_statement__ = 73;
          for (int k = 1; k <= K; ++k) {
            current_statement__ = 71;
            stan::model::assign(log_prob, (((k - 1) *
              stan::model::rvalue(eta, "eta", stan::model::index_uni(n))) +
              stan::model::rvalue(d, "d", stan::model::index_uni(i),
                stan::model::index_uni(k))), "assigning variable log_prob",
              stan::model::index_uni(k));
          }
          current_statement__ = 74;
          lp_accum__.add((stan::model::rvalue(log_prob, "log_prob",
                            stan::model::index_uni(
                              (stan::model::rvalue(y, "y",
                                 stan::model::index_uni(n)) + 1))) -
            stan::math::log_sum_exp(log_prob)));
        }
      }
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    lp_accum__.add(lp__);
    return lp_accum__.sum();
  }
  template <typename RNG, typename VecR, typename VecI, typename VecVar,
            stan::require_vector_like_vt<std::is_floating_point,
            VecR>* = nullptr, stan::require_vector_like_vt<std::is_integral,
            VecI>* = nullptr, stan::require_vector_vt<std::is_floating_point,
            VecVar>* = nullptr>
  inline void
  write_array_impl(RNG& base_rng__, VecR& params_r__, VecI& params_i__,
                   VecVar& vars__, const bool
                   emit_transformed_parameters__ = true, const bool
                   emit_generated_quantities__ = true, std::ostream*
                   pstream__ = nullptr) const {
    using local_scalar_t__ = double;
    stan::io::deserializer<local_scalar_t__> in__(params_r__, params_i__);
    stan::io::serializer<local_scalar_t__> out__(vars__);
    static constexpr bool propto__ = true;
    // suppress unused var warning
    (void) propto__;
    double lp__ = 0.0;
    // suppress unused var warning
    (void) lp__;
    int current_statement__ = 0;
    stan::math::accumulator<double> lp_accum__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    constexpr bool jacobian__ = false;
    static constexpr const char* function__ =
      "model_MGPCM_namespace::write_array";
    // suppress unused var warning
    (void) function__;
    try {
      Eigen::Matrix<double,-1,1> a_free =
        Eigen::Matrix<double,-1,1>::Constant(a_free_size,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 1;
      a_free = in__.template read_constrain_lb<
                 Eigen::Matrix<local_scalar_t__,-1,1>, jacobian__>(0, lp__,
                 a_free_size);
      Eigen::Matrix<double,-1,1> d_free =
        Eigen::Matrix<double,-1,1>::Constant(d_free_size,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 2;
      d_free = in__.template read<
                 Eigen::Matrix<local_scalar_t__,-1,1>>(d_free_size);
      Eigen::Matrix<double,-1,-1> L_Corr =
        Eigen::Matrix<double,-1,-1>::Constant(D, D,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 3;
      L_Corr = in__.template read_constrain_cholesky_factor_corr<
                 Eigen::Matrix<local_scalar_t__,-1,-1>, jacobian__>(lp__, D);
      std::vector<Eigen::Matrix<double,-1,1>> theta_raw =
        std::vector<Eigen::Matrix<double,-1,1>>(N,
          Eigen::Matrix<double,-1,1>::Constant(D,
            std::numeric_limits<double>::quiet_NaN()));
      current_statement__ = 4;
      theta_raw = in__.template read<
                    std::vector<Eigen::Matrix<local_scalar_t__,-1,1>>>(N, D);
      Eigen::Matrix<double,-1,-1> a =
        Eigen::Matrix<double,-1,-1>::Constant(I, D,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,-1> d =
        Eigen::Matrix<double,-1,-1>::Constant(I, max_poly,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,-1> theta =
        Eigen::Matrix<double,-1,-1>::Constant(N, D,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,1> eta =
        Eigen::Matrix<double,-1,1>::Constant(N_obs,
          std::numeric_limits<double>::quiet_NaN());
      out__.write(a_free);
      out__.write(d_free);
      out__.write(L_Corr);
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
          out__.write(theta_raw[(sym2__ - 1)][(sym1__ - 1)]);
        }
      }
      if (stan::math::logical_negation(
            (stan::math::primitive_value(emit_transformed_parameters__) ||
            stan::math::primitive_value(emit_generated_quantities__)))) {
        return ;
      }
      {
        int idx = std::numeric_limits<int>::min();
        current_statement__ = 11;
        idx = 1;
        current_statement__ = 21;
        for (int i = 1; i <= I; ++i) {
          current_statement__ = 19;
          for (int j = 1; j <= D; ++j) {
            current_statement__ = 17;
            if (stan::math::logical_eq(
                  stan::model::rvalue(Q_matrix, "Q_matrix",
                    stan::model::index_uni(i), stan::model::index_uni(j)), 1)) {
              current_statement__ = 14;
              stan::model::assign(a,
                stan::model::rvalue(a_free, "a_free",
                  stan::model::index_uni(idx)), "assigning variable a",
                stan::model::index_uni(i), stan::model::index_uni(j));
              current_statement__ = 15;
              idx = (idx + 1);
            } else {
              current_statement__ = 12;
              stan::model::assign(a, 0.0, "assigning variable a",
                stan::model::index_uni(i), stan::model::index_uni(j));
            }
          }
        }
      }
      {
        int idx = std::numeric_limits<int>::min();
        current_statement__ = 23;
        idx = 1;
        current_statement__ = 35;
        for (int i = 1; i <= I; ++i) {
          current_statement__ = 24;
          stan::model::assign(d, 0.0, "assigning variable d",
            stan::model::index_uni(i), stan::model::index_uni(1));
          current_statement__ = 28;
          for (int k = 2; k <=
               stan::model::rvalue(length_poly, "length_poly",
                 stan::model::index_uni(i)); ++k) {
            current_statement__ = 25;
            stan::model::assign(d,
              stan::model::rvalue(d_free, "d_free",
                stan::model::index_uni(idx)), "assigning variable d",
              stan::model::index_uni(i), stan::model::index_uni(k));
            current_statement__ = 26;
            idx = (idx + 1);
          }
          current_statement__ = 33;
          if (stan::math::logical_lt(
                stan::model::rvalue(length_poly, "length_poly",
                  stan::model::index_uni(i)), max_poly)) {
            current_statement__ = 31;
            for (int k =
                   (stan::model::rvalue(length_poly, "length_poly",
                      stan::model::index_uni(i)) + 1); k <= max_poly; ++k) {
              current_statement__ = 29;
              stan::model::assign(d, 0.0, "assigning variable d",
                stan::model::index_uni(i), stan::model::index_uni(k));
            }
          }
        }
      }
      current_statement__ = 43;
      for (int n = 1; n <= N; ++n) {
        current_statement__ = 41;
        if (stan::math::logical_gt(D, 1)) {
          current_statement__ = 39;
          stan::model::assign(theta,
            stan::math::transpose(
              stan::math::add(theta_mu,
                stan::math::multiply(L_Corr,
                  stan::model::rvalue(theta_raw, "theta_raw",
                    stan::model::index_uni(n))))),
            "assigning variable theta", stan::model::index_uni(n));
        } else {
          current_statement__ = 37;
          stan::model::assign(theta,
            (stan::model::rvalue(theta_mu, "theta_mu",
               stan::model::index_uni(1)) +
            stan::model::rvalue(
              stan::model::rvalue(theta_raw, "theta_raw",
                stan::model::index_uni(n)), "theta_raw[n]",
              stan::model::index_uni(1))), "assigning variable theta",
            stan::model::index_uni(n), stan::model::index_uni(1));
        }
      }
      current_statement__ = 46;
      for (int n = 1; n <= N_obs; ++n) {
        current_statement__ = 44;
        stan::model::assign(eta,
          stan::math::dot_product(
            stan::model::rvalue(theta, "theta",
              stan::model::index_uni(
                stan::model::rvalue(pid, "pid", stan::model::index_uni(n)))),
            stan::model::rvalue(a, "a",
              stan::model::index_uni(
                stan::model::rvalue(iid, "iid", stan::model::index_uni(n))))),
          "assigning variable eta", stan::model::index_uni(n));
      }
      current_statement__ = 5;
      stan::math::check_greater_or_equal(function__, "a", a, 0);
      if (emit_transformed_parameters__) {
        out__.write(a);
        out__.write(d);
        out__.write(theta);
        out__.write(eta);
      }
      if (stan::math::logical_negation(emit_generated_quantities__)) {
        return ;
      }
      Eigen::Matrix<double,-1,-1> Corr =
        Eigen::Matrix<double,-1,-1>::Constant(D, D,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,1> log_lik =
        Eigen::Matrix<double,-1,1>::Constant(N_obs,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 49;
      if (stan::math::logical_gt(D, 1)) {
        current_statement__ = 48;
        stan::model::assign(Corr,
          stan::math::multiply_lower_tri_self_transpose(L_Corr),
          "assigning variable Corr");
      } else {
        current_statement__ = 47;
        stan::model::assign(Corr, 1.0, "assigning variable Corr",
          stan::model::index_uni(1), stan::model::index_uni(1));
      }
      current_statement__ = 58;
      for (int n = 1; n <= N_obs; ++n) {
        int i = std::numeric_limits<int>::min();
        current_statement__ = 50;
        i = stan::model::rvalue(iid, "iid", stan::model::index_uni(n));
        int K = std::numeric_limits<int>::min();
        current_statement__ = 51;
        K = stan::model::rvalue(length_poly, "length_poly",
              stan::model::index_uni(i));
        current_statement__ = 52;
        stan::math::validate_non_negative_index("lp", "K", K);
        Eigen::Matrix<double,-1,1> lp =
          Eigen::Matrix<double,-1,1>::Constant(K,
            std::numeric_limits<double>::quiet_NaN());
        current_statement__ = 55;
        for (int k = 1; k <= K; ++k) {
          current_statement__ = 54;
          stan::model::assign(lp, (((k - 1) *
            stan::model::rvalue(eta, "eta", stan::model::index_uni(n))) +
            stan::model::rvalue(d, "d", stan::model::index_uni(i),
              stan::model::index_uni(k))), "assigning variable lp",
            stan::model::index_uni(k));
        }
        current_statement__ = 56;
        stan::model::assign(log_lik,
          (stan::model::rvalue(lp, "lp",
             stan::model::index_uni(
               (stan::model::rvalue(y, "y", stan::model::index_uni(n)) + 1)))
          - stan::math::log_sum_exp(lp)), "assigning variable log_lik",
          stan::model::index_uni(n));
      }
      out__.write(Corr);
      out__.write(log_lik);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
  }
  template <typename VecVar, typename VecI,
            stan::require_vector_t<VecVar>* = nullptr,
            stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr>
  inline void
  unconstrain_array_impl(const VecVar& params_r__, const VecI& params_i__,
                         VecVar& vars__, std::ostream* pstream__ = nullptr) const {
    using local_scalar_t__ = double;
    stan::io::deserializer<local_scalar_t__> in__(params_r__, params_i__);
    stan::io::serializer<local_scalar_t__> out__(vars__);
    int current_statement__ = 0;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    try {
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      Eigen::Matrix<local_scalar_t__,-1,1> a_free =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(a_free_size,
          DUMMY_VAR__);
      current_statement__ = 1;
      stan::model::assign(a_free,
        in__.read<Eigen::Matrix<local_scalar_t__,-1,1>>(a_free_size),
        "assigning variable a_free");
      out__.write_free_lb(0, a_free);
      Eigen::Matrix<local_scalar_t__,-1,1> d_free =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(d_free_size,
          DUMMY_VAR__);
      current_statement__ = 2;
      stan::model::assign(d_free,
        in__.read<Eigen::Matrix<local_scalar_t__,-1,1>>(d_free_size),
        "assigning variable d_free");
      out__.write(d_free);
      Eigen::Matrix<local_scalar_t__,-1,-1> L_Corr =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(D, D, DUMMY_VAR__);
      current_statement__ = 3;
      stan::model::assign(L_Corr,
        in__.read<Eigen::Matrix<local_scalar_t__,-1,-1>>(D, D),
        "assigning variable L_Corr");
      out__.write_free_cholesky_factor_corr(L_Corr);
      std::vector<Eigen::Matrix<local_scalar_t__,-1,1>> theta_raw =
        std::vector<Eigen::Matrix<local_scalar_t__,-1,1>>(N,
          Eigen::Matrix<local_scalar_t__,-1,1>::Constant(D, DUMMY_VAR__));
      current_statement__ = 4;
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        current_statement__ = 4;
        for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
          current_statement__ = 4;
          stan::model::assign(theta_raw, in__.read<local_scalar_t__>(),
            "assigning variable theta_raw", stan::model::index_uni(sym2__),
            stan::model::index_uni(sym1__));
        }
      }
      out__.write(theta_raw);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
  }
  template <typename VecVar, stan::require_vector_t<VecVar>* = nullptr>
  inline void
  transform_inits_impl(const stan::io::var_context& context__, VecVar&
                       vars__, std::ostream* pstream__ = nullptr) const {
    using local_scalar_t__ = double;
    stan::io::serializer<local_scalar_t__> out__(vars__);
    int current_statement__ = 0;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    // suppress unused var warning
    (void) DUMMY_VAR__;
    try {
      current_statement__ = 1;
      context__.validate_dims("parameter initialization", "a_free", "double",
        std::vector<size_t>{static_cast<size_t>(a_free_size)});
      current_statement__ = 2;
      context__.validate_dims("parameter initialization", "d_free", "double",
        std::vector<size_t>{static_cast<size_t>(d_free_size)});
      current_statement__ = 3;
      context__.validate_dims("parameter initialization", "L_Corr", "double",
        std::vector<size_t>{static_cast<size_t>(D), static_cast<size_t>(D)});
      current_statement__ = 4;
      context__.validate_dims("parameter initialization", "theta_raw",
        "double",
        std::vector<size_t>{static_cast<size_t>(N), static_cast<size_t>(D)});
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      Eigen::Matrix<local_scalar_t__,-1,1> a_free =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(a_free_size,
          DUMMY_VAR__);
      {
        std::vector<local_scalar_t__> a_free_flat__;
        current_statement__ = 1;
        a_free_flat__ = context__.vals_r("a_free");
        current_statement__ = 1;
        pos__ = 1;
        current_statement__ = 1;
        for (int sym1__ = 1; sym1__ <= a_free_size; ++sym1__) {
          current_statement__ = 1;
          stan::model::assign(a_free, a_free_flat__[(pos__ - 1)],
            "assigning variable a_free", stan::model::index_uni(sym1__));
          current_statement__ = 1;
          pos__ = (pos__ + 1);
        }
      }
      out__.write_free_lb(0, a_free);
      Eigen::Matrix<local_scalar_t__,-1,1> d_free =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(d_free_size,
          DUMMY_VAR__);
      {
        std::vector<local_scalar_t__> d_free_flat__;
        current_statement__ = 2;
        d_free_flat__ = context__.vals_r("d_free");
        current_statement__ = 2;
        pos__ = 1;
        current_statement__ = 2;
        for (int sym1__ = 1; sym1__ <= d_free_size; ++sym1__) {
          current_statement__ = 2;
          stan::model::assign(d_free, d_free_flat__[(pos__ - 1)],
            "assigning variable d_free", stan::model::index_uni(sym1__));
          current_statement__ = 2;
          pos__ = (pos__ + 1);
        }
      }
      out__.write(d_free);
      Eigen::Matrix<local_scalar_t__,-1,-1> L_Corr =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(D, D, DUMMY_VAR__);
      {
        std::vector<local_scalar_t__> L_Corr_flat__;
        current_statement__ = 3;
        L_Corr_flat__ = context__.vals_r("L_Corr");
        current_statement__ = 3;
        pos__ = 1;
        current_statement__ = 3;
        for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
          current_statement__ = 3;
          for (int sym2__ = 1; sym2__ <= D; ++sym2__) {
            current_statement__ = 3;
            stan::model::assign(L_Corr, L_Corr_flat__[(pos__ - 1)],
              "assigning variable L_Corr", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 3;
            pos__ = (pos__ + 1);
          }
        }
      }
      out__.write_free_cholesky_factor_corr(L_Corr);
      std::vector<Eigen::Matrix<local_scalar_t__,-1,1>> theta_raw =
        std::vector<Eigen::Matrix<local_scalar_t__,-1,1>>(N,
          Eigen::Matrix<local_scalar_t__,-1,1>::Constant(D, DUMMY_VAR__));
      {
        std::vector<local_scalar_t__> theta_raw_flat__;
        current_statement__ = 4;
        theta_raw_flat__ = context__.vals_r("theta_raw");
        current_statement__ = 4;
        pos__ = 1;
        current_statement__ = 4;
        for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
          current_statement__ = 4;
          for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
            current_statement__ = 4;
            stan::model::assign(theta_raw, theta_raw_flat__[(pos__ - 1)],
              "assigning variable theta_raw", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 4;
            pos__ = (pos__ + 1);
          }
        }
      }
      out__.write(theta_raw);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
  }
  inline void
  get_param_names(std::vector<std::string>& names__, const bool
                  emit_transformed_parameters__ = true, const bool
                  emit_generated_quantities__ = true) const {
    names__ = std::vector<std::string>{"a_free", "d_free", "L_Corr",
                "theta_raw"};
    if (emit_transformed_parameters__) {
      std::vector<std::string> temp{"a", "d", "theta", "eta"};
      names__.reserve(names__.size() + temp.size());
      names__.insert(names__.end(), temp.begin(), temp.end());
    }
    if (emit_generated_quantities__) {
      std::vector<std::string> temp{"Corr", "log_lik"};
      names__.reserve(names__.size() + temp.size());
      names__.insert(names__.end(), temp.begin(), temp.end());
    }
  }
  inline void
  get_dims(std::vector<std::vector<size_t>>& dimss__, const bool
           emit_transformed_parameters__ = true, const bool
           emit_generated_quantities__ = true) const {
    dimss__ = std::vector<std::vector<size_t>>{std::vector<size_t>{static_cast<
                                                                    size_t>(
                                                                    a_free_size)},
                std::vector<size_t>{static_cast<size_t>(d_free_size)},
                std::vector<size_t>{static_cast<size_t>(D),
                  static_cast<size_t>(D)},
                std::vector<size_t>{static_cast<size_t>(N),
                  static_cast<size_t>(D)}};
    if (emit_transformed_parameters__) {
      std::vector<std::vector<size_t>>
        temp{std::vector<size_t>{static_cast<size_t>(I),
               static_cast<size_t>(D)},
             std::vector<size_t>{static_cast<size_t>(I),
               static_cast<size_t>(max_poly)},
             std::vector<size_t>{static_cast<size_t>(N),
               static_cast<size_t>(D)},
             std::vector<size_t>{static_cast<size_t>(N_obs)}};
      dimss__.reserve(dimss__.size() + temp.size());
      dimss__.insert(dimss__.end(), temp.begin(), temp.end());
    }
    if (emit_generated_quantities__) {
      std::vector<std::vector<size_t>>
        temp{std::vector<size_t>{static_cast<size_t>(D),
               static_cast<size_t>(D)},
             std::vector<size_t>{static_cast<size_t>(N_obs)}};
      dimss__.reserve(dimss__.size() + temp.size());
      dimss__.insert(dimss__.end(), temp.begin(), temp.end());
    }
  }
  inline void
  constrained_param_names(std::vector<std::string>& param_names__, bool
                          emit_transformed_parameters__ = true, bool
                          emit_generated_quantities__ = true) const final {
    for (int sym1__ = 1; sym1__ <= a_free_size; ++sym1__) {
      param_names__.emplace_back(std::string() + "a_free" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= d_free_size; ++sym1__) {
      param_names__.emplace_back(std::string() + "d_free" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
      for (int sym2__ = 1; sym2__ <= D; ++sym2__) {
        param_names__.emplace_back(std::string() + "L_Corr" + '.' +
          std::to_string(sym2__) + '.' + std::to_string(sym1__));
      }
    }
    for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
      for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
        param_names__.emplace_back(std::string() + "theta_raw" + '.' +
          std::to_string(sym2__) + '.' + std::to_string(sym1__));
      }
    }
    if (emit_transformed_parameters__) {
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= I; ++sym2__) {
          param_names__.emplace_back(std::string() + "a" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= max_poly; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= I; ++sym2__) {
          param_names__.emplace_back(std::string() + "d" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
          param_names__.emplace_back(std::string() + "theta" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= N_obs; ++sym1__) {
        param_names__.emplace_back(std::string() + "eta" + '.' +
          std::to_string(sym1__));
      }
    }
    if (emit_generated_quantities__) {
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= D; ++sym2__) {
          param_names__.emplace_back(std::string() + "Corr" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= N_obs; ++sym1__) {
        param_names__.emplace_back(std::string() + "log_lik" + '.' +
          std::to_string(sym1__));
      }
    }
  }
  inline void
  unconstrained_param_names(std::vector<std::string>& param_names__, bool
                            emit_transformed_parameters__ = true, bool
                            emit_generated_quantities__ = true) const final {
    for (int sym1__ = 1; sym1__ <= a_free_size; ++sym1__) {
      param_names__.emplace_back(std::string() + "a_free" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= d_free_size; ++sym1__) {
      param_names__.emplace_back(std::string() + "d_free" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= ((D * (D - 1)) / 2); ++sym1__) {
      param_names__.emplace_back(std::string() + "L_Corr" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
      for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
        param_names__.emplace_back(std::string() + "theta_raw" + '.' +
          std::to_string(sym2__) + '.' + std::to_string(sym1__));
      }
    }
    if (emit_transformed_parameters__) {
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= I; ++sym2__) {
          param_names__.emplace_back(std::string() + "a" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= max_poly; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= I; ++sym2__) {
          param_names__.emplace_back(std::string() + "d" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= N; ++sym2__) {
          param_names__.emplace_back(std::string() + "theta" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= N_obs; ++sym1__) {
        param_names__.emplace_back(std::string() + "eta" + '.' +
          std::to_string(sym1__));
      }
    }
    if (emit_generated_quantities__) {
      for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= D; ++sym2__) {
          param_names__.emplace_back(std::string() + "Corr" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= N_obs; ++sym1__) {
        param_names__.emplace_back(std::string() + "log_lik" + '.' +
          std::to_string(sym1__));
      }
    }
  }
  inline std::string get_constrained_sizedtypes() const {
    return std::string("[{\"name\":\"a_free\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(a_free_size) + "},\"block\":\"parameters\"},{\"name\":\"d_free\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(d_free_size) + "},\"block\":\"parameters\"},{\"name\":\"L_Corr\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(D) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"parameters\"},{\"name\":\"theta_raw\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(N) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(D) + "}},\"block\":\"parameters\"},{\"name\":\"a\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(I) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"transformed_parameters\"},{\"name\":\"d\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(I) + ",\"cols\":" + std::to_string(max_poly) + "},\"block\":\"transformed_parameters\"},{\"name\":\"theta\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(N) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"transformed_parameters\"},{\"name\":\"eta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(N_obs) + "},\"block\":\"transformed_parameters\"},{\"name\":\"Corr\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(D) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"generated_quantities\"},{\"name\":\"log_lik\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(N_obs) + "},\"block\":\"generated_quantities\"}]");
  }
  inline std::string get_unconstrained_sizedtypes() const {
    return std::string("[{\"name\":\"a_free\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(a_free_size) + "},\"block\":\"parameters\"},{\"name\":\"d_free\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(d_free_size) + "},\"block\":\"parameters\"},{\"name\":\"L_Corr\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(((D * (D - 1)) /2)) + "},\"block\":\"parameters\"},{\"name\":\"theta_raw\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(N) + ",\"element_type\":{\"name\":\"vector\",\"length\":" + std::to_string(D) + "}},\"block\":\"parameters\"},{\"name\":\"a\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(I) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"transformed_parameters\"},{\"name\":\"d\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(I) + ",\"cols\":" + std::to_string(max_poly) + "},\"block\":\"transformed_parameters\"},{\"name\":\"theta\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(N) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"transformed_parameters\"},{\"name\":\"eta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(N_obs) + "},\"block\":\"transformed_parameters\"},{\"name\":\"Corr\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(D) + ",\"cols\":" + std::to_string(D) + "},\"block\":\"generated_quantities\"},{\"name\":\"log_lik\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(N_obs) + "},\"block\":\"generated_quantities\"}]");
  }
  // Begin method overload boilerplate
  template <typename RNG> inline void
  write_array(RNG& base_rng, Eigen::Matrix<double,-1,1>& params_r,
              Eigen::Matrix<double,-1,1>& vars, const bool
              emit_transformed_parameters = true, const bool
              emit_generated_quantities = true, std::ostream*
              pstream = nullptr) const {
    const size_t num_params__ = (((a_free_size + d_free_size) + (D * D)) + (N
      * D));
    const size_t num_transformed = emit_transformed_parameters * (((((I * D)
      + (I * max_poly)) + (N * D)) + N_obs));
    const size_t num_gen_quantities = emit_generated_quantities * (((D * D) +
      N_obs));
    const size_t num_to_write = num_params__ + num_transformed +
      num_gen_quantities;
    std::vector<int> params_i;
    vars = Eigen::Matrix<double,-1,1>::Constant(num_to_write,
             std::numeric_limits<double>::quiet_NaN());
    write_array_impl(base_rng, params_r, params_i, vars,
      emit_transformed_parameters, emit_generated_quantities, pstream);
  }
  template <typename RNG> inline void
  write_array(RNG& base_rng, std::vector<double>& params_r, std::vector<int>&
              params_i, std::vector<double>& vars, bool
              emit_transformed_parameters = true, bool
              emit_generated_quantities = true, std::ostream*
              pstream = nullptr) const {
    const size_t num_params__ = (((a_free_size + d_free_size) + (D * D)) + (N
      * D));
    const size_t num_transformed = emit_transformed_parameters * (((((I * D)
      + (I * max_poly)) + (N * D)) + N_obs));
    const size_t num_gen_quantities = emit_generated_quantities * (((D * D) +
      N_obs));
    const size_t num_to_write = num_params__ + num_transformed +
      num_gen_quantities;
    vars = std::vector<double>(num_to_write,
             std::numeric_limits<double>::quiet_NaN());
    write_array_impl(base_rng, params_r, params_i, vars,
      emit_transformed_parameters, emit_generated_quantities, pstream);
  }
  template <bool propto__, bool jacobian__, typename T_> inline T_
  log_prob(Eigen::Matrix<T_,-1,1>& params_r, std::ostream* pstream = nullptr) const {
    Eigen::Matrix<int,-1,1> params_i;
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
  }
  template <bool propto__, bool jacobian__, typename T_> inline T_
  log_prob(std::vector<T_>& params_r, std::vector<int>& params_i,
           std::ostream* pstream = nullptr) const {
    return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
  }
  inline void
  transform_inits(const stan::io::var_context& context,
                  Eigen::Matrix<double,-1,1>& params_r, std::ostream*
                  pstream = nullptr) const final {
    std::vector<double> params_r_vec(params_r.size());
    std::vector<int> params_i;
    transform_inits(context, params_i, params_r_vec, pstream);
    params_r = Eigen::Map<Eigen::Matrix<double,-1,1>>(params_r_vec.data(),
                 params_r_vec.size());
  }
  inline void
  transform_inits(const stan::io::var_context& context, std::vector<int>&
                  params_i, std::vector<double>& vars, std::ostream*
                  pstream__ = nullptr) const {
    vars.resize(num_params_r__);
    transform_inits_impl(context, vars, pstream__);
  }
  inline void
  unconstrain_array(const std::vector<double>& params_constrained,
                    std::vector<double>& params_unconstrained, std::ostream*
                    pstream = nullptr) const {
    const std::vector<int> params_i;
    params_unconstrained = std::vector<double>(num_params_r__,
                             std::numeric_limits<double>::quiet_NaN());
    unconstrain_array_impl(params_constrained, params_i,
      params_unconstrained, pstream);
  }
  inline void
  unconstrain_array(const Eigen::Matrix<double,-1,1>& params_constrained,
                    Eigen::Matrix<double,-1,1>& params_unconstrained,
                    std::ostream* pstream = nullptr) const {
    const std::vector<int> params_i;
    params_unconstrained = Eigen::Matrix<double,-1,1>::Constant(num_params_r__,
                             std::numeric_limits<double>::quiet_NaN());
    unconstrain_array_impl(params_constrained, params_i,
      params_unconstrained, pstream);
  }
};
}
using stan_model = model_MGPCM_namespace::model_MGPCM;
#ifndef USING_R
// Boilerplate
stan::model::model_base&
new_model(stan::io::var_context& data_context, unsigned int seed,
          std::ostream* msg_stream) {
  stan_model* m = new stan_model(data_context, seed, msg_stream);
  return *m;
}
stan::math::profile_map& get_stan_profile_data() {
  return model_MGPCM_namespace::profiles__;
}
#endif
#endif
