
// Code generated by stanc v2.31.0
#include <stan/model/model_header.hpp>
namespace prophet_model_namespace {

using stan::model::model_base_crtp;
using namespace stan::math;


stan::math::profile_map profiles__;
static constexpr std::array<const char*, 96> locations_array__ = 
{" (found before start of program)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 110, column 2 to column 9)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 111, column 2 to column 9)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 112, column 2 to column 18)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 113, column 2 to column 26)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 114, column 2 to column 17)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 118, column 2 to column 18)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 124, column 4 to column 29)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 123, column 35 to line 125, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 123, column 9 to line 125, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 122, column 4 to column 64)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 121, column 35 to line 123, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 121, column 9 to line 125, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 120, column 4 to column 54)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 119, column 28 to line 121, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 119, column 2 to line 125, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 130, column 2 to column 19)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 131, column 2 to column 19)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 132, column 2 to column 37)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 133, column 2 to column 29)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 134, column 2 to column 27)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 137, column 2 to line 142, column 4)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 88, column 2 to column 8)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 89, column 2 to column 17)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 90, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 90, column 2 to column 14)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 91, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 91, column 2 to column 16)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 92, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 92, column 2 to column 14)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 93, column 2 to column 8)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 94, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 94, column 2 to column 21)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 95, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 95, column 11 to column 12)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 95, column 2 to column 16)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 96, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 96, column 2 to column 19)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 97, column 2 to column 20)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 98, column 2 to column 22)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 99, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 99, column 2 to column 16)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 100, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 100, column 2 to column 16)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 104, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 104, column 12 to column 13)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 104, column 2 to column 61)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 105, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 105, column 12 to column 13)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 105, column 2 to column 47)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 106, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 106, column 12 to column 13)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 106, column 2 to column 47)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 112, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 114, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 118, column 9 to column 10)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 9, column 11 to column 12)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 9, column 14 to column 15)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 9, column 4 to column 19)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 10, column 15 to column 16)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 10, column 4 to column 24)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 11, column 4 to column 15)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 14, column 4 to column 28)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 15, column 4 to column 33)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 16, column 4 to column 15)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 21, column 8 to column 26)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 22, column 8 to column 28)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 20, column 58 to line 23, column 7)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 20, column 6 to line 23, column 7)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 24, column 6 to column 19)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 19, column 19 to line 25, column 5)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 19, column 4 to line 25, column 5)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 26, column 4 to column 13)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 7, column 73 to line 27, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 32, column 11 to column 12)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 32, column 4 to column 20)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 33, column 11 to column 16)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 33, column 4 to column 22)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 34, column 4 to column 14)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 37, column 4 to column 51)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 40, column 4 to column 13)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 42, column 6 to column 66)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 43, column 6 to column 29)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 41, column 19 to line 44, column 5)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 41, column 4 to line 44, column 5)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 45, column 4 to column 17)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 31, column 78 to line 46, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 58, column 11 to column 12)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 58, column 4 to column 20)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 60, column 4 to column 53)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 61, column 4 to column 70)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 57, column 4 to line 62, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 74, column 4 to column 65)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 73, column 4 to line 75, column 3)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 83, column 4 to column 28)",
 " (in '/croot/prophet_1689928750133/work/build/lib.linux-ppc64le-cpython-311/prophet/stan_model/prophet.stan', line 82, column 4 to line 84, column 3)"};

struct logistic_gamma_functor__ {
  template <typename T0__, typename T1__, typename T2__, typename T3__,
            stan::require_all_t<stan::is_stan_scalar<T0__>,
                                stan::is_stan_scalar<T1__>,
                                stan::is_col_vector<T2__>,
                                stan::is_vt_not_complex<T2__>,
                                stan::is_col_vector<T3__>,
                                stan::is_vt_not_complex<T3__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                       stan::base_type_t<T3__>>, -1, 1>
  operator()(const T0__& k, const T1__& m, const T2__& delta,
             const T3__& t_change, const int& S, std::ostream* pstream__) const;
};
struct logistic_trend_functor__ {
  template <typename T0__, typename T1__, typename T2__, typename T3__,
            typename T4__, typename T5__, typename T6__,
            stan::require_all_t<stan::is_stan_scalar<T0__>,
                                stan::is_stan_scalar<T1__>,
                                stan::is_col_vector<T2__>,
                                stan::is_vt_not_complex<T2__>,
                                stan::is_col_vector<T3__>,
                                stan::is_vt_not_complex<T3__>,
                                stan::is_col_vector<T4__>,
                                stan::is_vt_not_complex<T4__>,
                                stan::is_eigen_matrix_dynamic<T5__>,
                                stan::is_vt_not_complex<T5__>,
                                stan::is_col_vector<T6__>,
                                stan::is_vt_not_complex<T6__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                       stan::base_type_t<T3__>, stan::base_type_t<T4__>,
                       stan::promote_args_t<stan::base_type_t<T5__>,
                                            stan::base_type_t<T6__>>>, -1, 1>
  operator()(const T0__& k, const T1__& m, const T2__& delta, const T3__& t,
             const T4__& cap, const T5__& A, const T6__& t_change,
             const int& S, std::ostream* pstream__) const;
};
struct linear_trend_functor__ {
  template <typename T0__, typename T1__, typename T2__, typename T3__,
            typename T4__, typename T5__,
            stan::require_all_t<stan::is_stan_scalar<T0__>,
                                stan::is_stan_scalar<T1__>,
                                stan::is_col_vector<T2__>,
                                stan::is_vt_not_complex<T2__>,
                                stan::is_col_vector<T3__>,
                                stan::is_vt_not_complex<T3__>,
                                stan::is_eigen_matrix_dynamic<T4__>,
                                stan::is_vt_not_complex<T4__>,
                                stan::is_col_vector<T5__>,
                                stan::is_vt_not_complex<T5__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                       stan::base_type_t<T3__>, stan::base_type_t<T4__>,
                       stan::promote_args_t<stan::base_type_t<T5__>>>, -1, 1>
  operator()(const T0__& k, const T1__& m, const T2__& delta, const T3__& t,
             const T4__& A, const T5__& t_change, std::ostream* pstream__) const;
};
struct get_changepoint_matrix_functor__ {
  template <typename T0__, typename T1__,
            stan::require_all_t<stan::is_col_vector<T0__>,
                                stan::is_vt_not_complex<T0__>,
                                stan::is_col_vector<T1__>,
                                stan::is_vt_not_complex<T1__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<stan::base_type_t<T0__>, stan::base_type_t<T1__>>, -1, -1>
  operator()(const T0__& t, const T1__& t_change, const int& T, const int& S,
             std::ostream* pstream__) const;
};
struct flat_trend_functor__ {
  template <typename T0__,
            stan::require_all_t<stan::is_stan_scalar<T0__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__>, -1, 1>
  operator()(const T0__& m, const int& T, std::ostream* pstream__) const;
};

template <typename T0__, typename T1__,
          stan::require_all_t<stan::is_col_vector<T0__>,
                              stan::is_vt_not_complex<T0__>,
                              stan::is_col_vector<T1__>,
                              stan::is_vt_not_complex<T1__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<stan::base_type_t<T0__>, stan::base_type_t<T1__>>, -1, -1>
  get_changepoint_matrix(const T0__& t_arg__, const T1__& t_change_arg__,
                         const int& T, const int& S, std::ostream* pstream__) {
    using local_scalar_t__ =
            stan::promote_args_t<stan::base_type_t<T0__>,
                                 stan::base_type_t<T1__>>;
    int current_statement__ = 0; 
    const auto& t = stan::math::to_ref(t_arg__);
    const auto& t_change = stan::math::to_ref(t_change_arg__);
    static constexpr bool propto__ = true;
    (void) propto__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning
    try {
      current_statement__ = 56;
      stan::math::validate_non_negative_index("A", "T", T);
      current_statement__ = 57;
      stan::math::validate_non_negative_index("A", "S", S);
      Eigen::Matrix<local_scalar_t__, -1, -1> A =
         Eigen::Matrix<local_scalar_t__, -1, -1>::Constant(T, S, DUMMY_VAR__);
      current_statement__ = 59;
      stan::math::validate_non_negative_index("a_row", "S", S);
      Eigen::Matrix<local_scalar_t__, 1, -1> a_row =
         Eigen::Matrix<local_scalar_t__, 1, -1>::Constant(S, DUMMY_VAR__);
      int cp_idx = std::numeric_limits<int>::min();
      current_statement__ = 62;
      stan::model::assign(A, stan::math::rep_matrix(0, T, S),
        "assigning variable A");
      current_statement__ = 63;
      stan::model::assign(a_row, stan::math::rep_row_vector(0, S),
        "assigning variable a_row");
      current_statement__ = 64;
      cp_idx = 1;
      current_statement__ = 71;
      for (int i = 1; i <= T; ++i) {
        current_statement__ = 68;
        while ((stan::math::primitive_value(
               stan::math::logical_lte(cp_idx, S)) &&
               stan::math::primitive_value(
               stan::math::logical_gte(
                 stan::model::rvalue(t, "t", stan::model::index_uni(i)),
                 stan::model::rvalue(t_change, "t_change",
                   stan::model::index_uni(cp_idx)))))) {
          current_statement__ = 65;
          stan::model::assign(a_row, 1,
            "assigning variable a_row", stan::model::index_uni(cp_idx));
          current_statement__ = 66;
          cp_idx = (cp_idx + 1);
        }
        current_statement__ = 69;
        stan::model::assign(A, a_row,
          "assigning variable A", stan::model::index_uni(i));
      }
      current_statement__ = 72;
      return A;
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    }
template <typename T0__, typename T1__, typename T2__, typename T3__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_col_vector<T2__>,
                              stan::is_vt_not_complex<T2__>,
                              stan::is_col_vector<T3__>,
                              stan::is_vt_not_complex<T3__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                     stan::base_type_t<T3__>>, -1, 1>
  logistic_gamma(const T0__& k, const T1__& m, const T2__& delta_arg__,
                 const T3__& t_change_arg__, const int& S,
                 std::ostream* pstream__) {
    using local_scalar_t__ =
            stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                                 stan::base_type_t<T3__>>;
    int current_statement__ = 0; 
    const auto& delta = stan::math::to_ref(delta_arg__);
    const auto& t_change = stan::math::to_ref(t_change_arg__);
    static constexpr bool propto__ = true;
    (void) propto__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning
    try {
      current_statement__ = 74;
      stan::math::validate_non_negative_index("gamma", "S", S);
      Eigen::Matrix<local_scalar_t__, -1, 1> gamma =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(S, DUMMY_VAR__);
      current_statement__ = 76;
      stan::math::validate_non_negative_index("k_s", "S + 1", (S + 1));
      Eigen::Matrix<local_scalar_t__, -1, 1> k_s =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant((S + 1),
           DUMMY_VAR__);
      local_scalar_t__ m_pr = DUMMY_VAR__;
      current_statement__ = 79;
      stan::model::assign(k_s,
        stan::math::append_row(k,
          stan::math::add(k, stan::math::cumulative_sum(delta))),
        "assigning variable k_s");
      current_statement__ = 80;
      m_pr = m;
      current_statement__ = 84;
      for (int i = 1; i <= S; ++i) {
        current_statement__ = 81;
        stan::model::assign(gamma,
          ((stan::model::rvalue(t_change, "t_change",
              stan::model::index_uni(i)) - m_pr) *
            (1 -
              (stan::model::rvalue(k_s, "k_s", stan::model::index_uni(i)) /
                stan::model::rvalue(k_s, "k_s",
                  stan::model::index_uni((i + 1)))))),
          "assigning variable gamma", stan::model::index_uni(i));
        current_statement__ = 82;
        m_pr = (m_pr +
                 stan::model::rvalue(gamma, "gamma",
                   stan::model::index_uni(i)));
      }
      current_statement__ = 85;
      return gamma;
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    }
template <typename T0__, typename T1__, typename T2__, typename T3__,
          typename T4__, typename T5__, typename T6__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_col_vector<T2__>,
                              stan::is_vt_not_complex<T2__>,
                              stan::is_col_vector<T3__>,
                              stan::is_vt_not_complex<T3__>,
                              stan::is_col_vector<T4__>,
                              stan::is_vt_not_complex<T4__>,
                              stan::is_eigen_matrix_dynamic<T5__>,
                              stan::is_vt_not_complex<T5__>,
                              stan::is_col_vector<T6__>,
                              stan::is_vt_not_complex<T6__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                     stan::base_type_t<T3__>, stan::base_type_t<T4__>,
                     stan::promote_args_t<stan::base_type_t<T5__>,
                                          stan::base_type_t<T6__>>>, -1, 1>
  logistic_trend(const T0__& k, const T1__& m, const T2__& delta_arg__,
                 const T3__& t_arg__, const T4__& cap_arg__,
                 const T5__& A_arg__, const T6__& t_change_arg__,
                 const int& S, std::ostream* pstream__) {
    using local_scalar_t__ =
            stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                                 stan::base_type_t<T3__>,
                                 stan::base_type_t<T4__>,
                                 stan::promote_args_t<stan::base_type_t<T5__>,
                                                      stan::base_type_t<T6__>>>;
    int current_statement__ = 0; 
    const auto& delta = stan::math::to_ref(delta_arg__);
    const auto& t = stan::math::to_ref(t_arg__);
    const auto& cap = stan::math::to_ref(cap_arg__);
    const auto& A = stan::math::to_ref(A_arg__);
    const auto& t_change = stan::math::to_ref(t_change_arg__);
    static constexpr bool propto__ = true;
    (void) propto__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning
    try {
      current_statement__ = 87;
      stan::math::validate_non_negative_index("gamma", "S", S);
      Eigen::Matrix<local_scalar_t__, -1, 1> gamma =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(S, DUMMY_VAR__);
      current_statement__ = 89;
      stan::model::assign(gamma,
        logistic_gamma(k, m, delta, t_change, S, pstream__),
        "assigning variable gamma");
      current_statement__ = 90;
      return stan::math::elt_multiply(cap,
               stan::math::inv_logit(
                 stan::math::elt_multiply(
                   stan::math::add(k, stan::math::multiply(A, delta)),
                   stan::math::subtract(t,
                     stan::math::add(m, stan::math::multiply(A, gamma))))));
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    }
template <typename T0__, typename T1__, typename T2__, typename T3__,
          typename T4__, typename T5__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_col_vector<T2__>,
                              stan::is_vt_not_complex<T2__>,
                              stan::is_col_vector<T3__>,
                              stan::is_vt_not_complex<T3__>,
                              stan::is_eigen_matrix_dynamic<T4__>,
                              stan::is_vt_not_complex<T4__>,
                              stan::is_col_vector<T5__>,
                              stan::is_vt_not_complex<T5__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                     stan::base_type_t<T3__>, stan::base_type_t<T4__>,
                     stan::promote_args_t<stan::base_type_t<T5__>>>, -1, 1>
  linear_trend(const T0__& k, const T1__& m, const T2__& delta_arg__,
               const T3__& t_arg__, const T4__& A_arg__,
               const T5__& t_change_arg__, std::ostream* pstream__) {
    using local_scalar_t__ =
            stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                                 stan::base_type_t<T3__>,
                                 stan::base_type_t<T4__>,
                                 stan::promote_args_t<stan::base_type_t<T5__>>>;
    int current_statement__ = 0; 
    const auto& delta = stan::math::to_ref(delta_arg__);
    const auto& t = stan::math::to_ref(t_arg__);
    const auto& A = stan::math::to_ref(A_arg__);
    const auto& t_change = stan::math::to_ref(t_change_arg__);
    static constexpr bool propto__ = true;
    (void) propto__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning
    try {
      current_statement__ = 92;
      return stan::math::add(
               stan::math::elt_multiply(
                 stan::math::add(k, stan::math::multiply(A, delta)), t),
               stan::math::add(m,
                 stan::math::multiply(A,
                   stan::math::elt_multiply(stan::math::minus(t_change),
                     delta))));
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    }
template <typename T0__,
          stan::require_all_t<stan::is_stan_scalar<T0__>>* = nullptr>
  Eigen::Matrix<stan::promote_args_t<T0__>, -1, 1>
  flat_trend(const T0__& m, const int& T, std::ostream* pstream__) {
    using local_scalar_t__ = stan::promote_args_t<T0__>;
    int current_statement__ = 0; 
    static constexpr bool propto__ = true;
    (void) propto__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning
    try {
      current_statement__ = 94;
      return stan::math::rep_vector(m, T);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    }
template <typename T0__, typename T1__, typename T2__, typename T3__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_col_vector<T2__>,
                              stan::is_vt_not_complex<T2__>,
                              stan::is_col_vector<T3__>,
                              stan::is_vt_not_complex<T3__>>*>
Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                     stan::base_type_t<T3__>>, -1, 1>
logistic_gamma_functor__::operator()(const T0__& k, const T1__& m,
                                     const T2__& delta, const T3__& t_change,
                                     const int& S, std::ostream* pstream__) 
const
{
  return logistic_gamma(k, m, delta, t_change, S, pstream__);
}

template <typename T0__, typename T1__, typename T2__, typename T3__,
          typename T4__, typename T5__, typename T6__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_col_vector<T2__>,
                              stan::is_vt_not_complex<T2__>,
                              stan::is_col_vector<T3__>,
                              stan::is_vt_not_complex<T3__>,
                              stan::is_col_vector<T4__>,
                              stan::is_vt_not_complex<T4__>,
                              stan::is_eigen_matrix_dynamic<T5__>,
                              stan::is_vt_not_complex<T5__>,
                              stan::is_col_vector<T6__>,
                              stan::is_vt_not_complex<T6__>>*>
Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                     stan::base_type_t<T3__>, stan::base_type_t<T4__>,
                     stan::promote_args_t<stan::base_type_t<T5__>,
                                          stan::base_type_t<T6__>>>, -1, 1>
logistic_trend_functor__::operator()(const T0__& k, const T1__& m,
                                     const T2__& delta, const T3__& t,
                                     const T4__& cap, const T5__& A,
                                     const T6__& t_change, const int& S,
                                     std::ostream* pstream__)  const
{
  return logistic_trend(k, m, delta, t, cap, A, t_change, S, pstream__);
}

template <typename T0__, typename T1__, typename T2__, typename T3__,
          typename T4__, typename T5__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_col_vector<T2__>,
                              stan::is_vt_not_complex<T2__>,
                              stan::is_col_vector<T3__>,
                              stan::is_vt_not_complex<T3__>,
                              stan::is_eigen_matrix_dynamic<T4__>,
                              stan::is_vt_not_complex<T4__>,
                              stan::is_col_vector<T5__>,
                              stan::is_vt_not_complex<T5__>>*>
Eigen::Matrix<stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>,
                     stan::base_type_t<T3__>, stan::base_type_t<T4__>,
                     stan::promote_args_t<stan::base_type_t<T5__>>>, -1, 1>
linear_trend_functor__::operator()(const T0__& k, const T1__& m,
                                   const T2__& delta, const T3__& t,
                                   const T4__& A, const T5__& t_change,
                                   std::ostream* pstream__)  const
{
  return linear_trend(k, m, delta, t, A, t_change, pstream__);
}

template <typename T0__, typename T1__,
          stan::require_all_t<stan::is_col_vector<T0__>,
                              stan::is_vt_not_complex<T0__>,
                              stan::is_col_vector<T1__>,
                              stan::is_vt_not_complex<T1__>>*>
Eigen::Matrix<stan::promote_args_t<stan::base_type_t<T0__>, stan::base_type_t<T1__>>, -1, -1>
get_changepoint_matrix_functor__::operator()(const T0__& t,
                                             const T1__& t_change,
                                             const int& T, const int& S,
                                             std::ostream* pstream__)  const
{
  return get_changepoint_matrix(t, t_change, T, S, pstream__);
}

template <typename T0__, stan::require_all_t<stan::is_stan_scalar<T0__>>*>
Eigen::Matrix<stan::promote_args_t<T0__>, -1, 1>
flat_trend_functor__::operator()(const T0__& m, const int& T,
                                 std::ostream* pstream__)  const
{
  return flat_trend(m, T, pstream__);
}

 class prophet_model final : public model_base_crtp<prophet_model> {

 private:
  int T;
  int K;
  Eigen::Matrix<double, -1, 1> t_data__;
  Eigen::Matrix<double, -1, 1> cap_data__;
  Eigen::Matrix<double, -1, 1> y_data__;
  int S;
  Eigen::Matrix<double, -1, 1> t_change_data__;
  Eigen::Matrix<double, -1, -1> X_data__;
  Eigen::Matrix<double, -1, 1> sigmas_data__;
  double tau;
  int trend_indicator;
  Eigen::Matrix<double, -1, 1> s_a_data__;
  Eigen::Matrix<double, -1, 1> s_m_data__;
  Eigen::Matrix<double, -1, -1> A_data__;
  Eigen::Matrix<double, -1, -1> X_sa_data__;
  Eigen::Matrix<double, -1, -1> X_sm_data__; 
  Eigen::Map<Eigen::Matrix<double, -1, 1>> t{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, 1>> cap{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, 1>> y{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, 1>> t_change{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, -1>> X{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double, -1, 1>> sigmas{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, 1>> s_a{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, 1>> s_m{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double, -1, -1>> A{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double, -1, -1>> X_sa{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double, -1, -1>> X_sm{nullptr, 0, 0};
 
 public:
  ~prophet_model() { }
  
  inline std::string model_name() const final { return "prophet_model"; }

  inline std::vector<std::string> model_compile_info() const noexcept {
    return std::vector<std::string>{"stanc_version = stanc3 v2.31.0", "stancflags = "};
  }
  
  
  prophet_model(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);
    (void) base_rng__;  // suppress unused var warning
    static constexpr const char* function__ = "prophet_model_namespace::prophet_model";
    (void) function__;  // suppress unused var warning
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning
    try {
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      current_statement__ = 22;
      context__.validate_dims("data initialization","T","int",
           std::vector<size_t>{});
      T = std::numeric_limits<int>::min();
      
      
      current_statement__ = 22;
      T = context__.vals_i("T")[(1 - 1)];
      current_statement__ = 23;
      context__.validate_dims("data initialization","K","int",
           std::vector<size_t>{});
      K = std::numeric_limits<int>::min();
      
      
      current_statement__ = 23;
      K = context__.vals_i("K")[(1 - 1)];
      current_statement__ = 23;
      stan::math::check_greater_or_equal(function__, "K", K, 1);
      current_statement__ = 24;
      stan::math::validate_non_negative_index("t", "T", T);
      current_statement__ = 25;
      context__.validate_dims("data initialization","t","double",
           std::vector<size_t>{static_cast<size_t>(T)});
      t_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(T,
          std::numeric_limits<double>::quiet_NaN());
      new (&t) Eigen::Map<Eigen::Matrix<double, -1, 1>>(t_data__.data(), T);
      
      {
        std::vector<local_scalar_t__> t_flat__;
        current_statement__ = 25;
        t_flat__ = context__.vals_r("t");
        current_statement__ = 25;
        pos__ = 1;
        current_statement__ = 25;
        for (int sym1__ = 1; sym1__ <= T; ++sym1__) {
          current_statement__ = 25;
          stan::model::assign(t, t_flat__[(pos__ - 1)],
            "assigning variable t", stan::model::index_uni(sym1__));
          current_statement__ = 25;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 26;
      stan::math::validate_non_negative_index("cap", "T", T);
      current_statement__ = 27;
      context__.validate_dims("data initialization","cap","double",
           std::vector<size_t>{static_cast<size_t>(T)});
      cap_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(T,
          std::numeric_limits<double>::quiet_NaN());
      new (&cap) Eigen::Map<Eigen::Matrix<double, -1, 1>>(cap_data__.data(), T);
        
      
      {
        std::vector<local_scalar_t__> cap_flat__;
        current_statement__ = 27;
        cap_flat__ = context__.vals_r("cap");
        current_statement__ = 27;
        pos__ = 1;
        current_statement__ = 27;
        for (int sym1__ = 1; sym1__ <= T; ++sym1__) {
          current_statement__ = 27;
          stan::model::assign(cap, cap_flat__[(pos__ - 1)],
            "assigning variable cap", stan::model::index_uni(sym1__));
          current_statement__ = 27;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 28;
      stan::math::validate_non_negative_index("y", "T", T);
      current_statement__ = 29;
      context__.validate_dims("data initialization","y","double",
           std::vector<size_t>{static_cast<size_t>(T)});
      y_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(T,
          std::numeric_limits<double>::quiet_NaN());
      new (&y) Eigen::Map<Eigen::Matrix<double, -1, 1>>(y_data__.data(), T);
      
      {
        std::vector<local_scalar_t__> y_flat__;
        current_statement__ = 29;
        y_flat__ = context__.vals_r("y");
        current_statement__ = 29;
        pos__ = 1;
        current_statement__ = 29;
        for (int sym1__ = 1; sym1__ <= T; ++sym1__) {
          current_statement__ = 29;
          stan::model::assign(y, y_flat__[(pos__ - 1)],
            "assigning variable y", stan::model::index_uni(sym1__));
          current_statement__ = 29;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 30;
      context__.validate_dims("data initialization","S","int",
           std::vector<size_t>{});
      S = std::numeric_limits<int>::min();
      
      
      current_statement__ = 30;
      S = context__.vals_i("S")[(1 - 1)];
      current_statement__ = 31;
      stan::math::validate_non_negative_index("t_change", "S", S);
      current_statement__ = 32;
      context__.validate_dims("data initialization","t_change","double",
           std::vector<size_t>{static_cast<size_t>(S)});
      t_change_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(S,
          std::numeric_limits<double>::quiet_NaN());
      new (&t_change) Eigen::Map<Eigen::Matrix<double, -1, 1>>(t_change_data__.data(), S);
        
      
      {
        std::vector<local_scalar_t__> t_change_flat__;
        current_statement__ = 32;
        t_change_flat__ = context__.vals_r("t_change");
        current_statement__ = 32;
        pos__ = 1;
        current_statement__ = 32;
        for (int sym1__ = 1; sym1__ <= S; ++sym1__) {
          current_statement__ = 32;
          stan::model::assign(t_change, t_change_flat__[(pos__ - 1)],
            "assigning variable t_change", stan::model::index_uni(sym1__));
          current_statement__ = 32;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 33;
      stan::math::validate_non_negative_index("X", "T", T);
      current_statement__ = 34;
      stan::math::validate_non_negative_index("X", "K", K);
      current_statement__ = 35;
      context__.validate_dims("data initialization","X","double",
           std::vector<size_t>{static_cast<size_t>(T),
            static_cast<size_t>(K)});
      X_data__ = 
        Eigen::Matrix<double, -1, -1>::Constant(T, K,
          std::numeric_limits<double>::quiet_NaN());
      new (&X) Eigen::Map<Eigen::Matrix<double, -1, -1>>(X_data__.data(), T, K);
        
      
      {
        std::vector<local_scalar_t__> X_flat__;
        current_statement__ = 35;
        X_flat__ = context__.vals_r("X");
        current_statement__ = 35;
        pos__ = 1;
        current_statement__ = 35;
        for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
          current_statement__ = 35;
          for (int sym2__ = 1; sym2__ <= T; ++sym2__) {
            current_statement__ = 35;
            stan::model::assign(X, X_flat__[(pos__ - 1)],
              "assigning variable X", stan::model::index_uni(sym2__),
                                        stan::model::index_uni(sym1__));
            current_statement__ = 35;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 36;
      stan::math::validate_non_negative_index("sigmas", "K", K);
      current_statement__ = 37;
      context__.validate_dims("data initialization","sigmas","double",
           std::vector<size_t>{static_cast<size_t>(K)});
      sigmas_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(K,
          std::numeric_limits<double>::quiet_NaN());
      new (&sigmas) Eigen::Map<Eigen::Matrix<double, -1, 1>>(sigmas_data__.data(), K);
        
      
      {
        std::vector<local_scalar_t__> sigmas_flat__;
        current_statement__ = 37;
        sigmas_flat__ = context__.vals_r("sigmas");
        current_statement__ = 37;
        pos__ = 1;
        current_statement__ = 37;
        for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
          current_statement__ = 37;
          stan::model::assign(sigmas, sigmas_flat__[(pos__ - 1)],
            "assigning variable sigmas", stan::model::index_uni(sym1__));
          current_statement__ = 37;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 38;
      context__.validate_dims("data initialization","tau","double",
           std::vector<size_t>{});
      tau = std::numeric_limits<double>::quiet_NaN();
      
      
      current_statement__ = 38;
      tau = context__.vals_r("tau")[(1 - 1)];
      current_statement__ = 38;
      stan::math::check_greater_or_equal(function__, "tau", tau, 0);
      current_statement__ = 39;
      context__.validate_dims("data initialization","trend_indicator","int",
           std::vector<size_t>{});
      trend_indicator = std::numeric_limits<int>::min();
      
      
      current_statement__ = 39;
      trend_indicator = context__.vals_i("trend_indicator")[(1 - 1)];
      current_statement__ = 40;
      stan::math::validate_non_negative_index("s_a", "K", K);
      current_statement__ = 41;
      context__.validate_dims("data initialization","s_a","double",
           std::vector<size_t>{static_cast<size_t>(K)});
      s_a_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(K,
          std::numeric_limits<double>::quiet_NaN());
      new (&s_a) Eigen::Map<Eigen::Matrix<double, -1, 1>>(s_a_data__.data(), K);
        
      
      {
        std::vector<local_scalar_t__> s_a_flat__;
        current_statement__ = 41;
        s_a_flat__ = context__.vals_r("s_a");
        current_statement__ = 41;
        pos__ = 1;
        current_statement__ = 41;
        for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
          current_statement__ = 41;
          stan::model::assign(s_a, s_a_flat__[(pos__ - 1)],
            "assigning variable s_a", stan::model::index_uni(sym1__));
          current_statement__ = 41;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 42;
      stan::math::validate_non_negative_index("s_m", "K", K);
      current_statement__ = 43;
      context__.validate_dims("data initialization","s_m","double",
           std::vector<size_t>{static_cast<size_t>(K)});
      s_m_data__ = 
        Eigen::Matrix<double, -1, 1>::Constant(K,
          std::numeric_limits<double>::quiet_NaN());
      new (&s_m) Eigen::Map<Eigen::Matrix<double, -1, 1>>(s_m_data__.data(), K);
        
      
      {
        std::vector<local_scalar_t__> s_m_flat__;
        current_statement__ = 43;
        s_m_flat__ = context__.vals_r("s_m");
        current_statement__ = 43;
        pos__ = 1;
        current_statement__ = 43;
        for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
          current_statement__ = 43;
          stan::model::assign(s_m, s_m_flat__[(pos__ - 1)],
            "assigning variable s_m", stan::model::index_uni(sym1__));
          current_statement__ = 43;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 44;
      stan::math::validate_non_negative_index("A", "T", T);
      current_statement__ = 45;
      stan::math::validate_non_negative_index("A", "S", S);
      current_statement__ = 46;
      A_data__ = 
        Eigen::Matrix<double, -1, -1>::Constant(T, S,
          std::numeric_limits<double>::quiet_NaN());
      new (&A) Eigen::Map<Eigen::Matrix<double, -1, -1>>(A_data__.data(), T, S);
        
      
      current_statement__ = 46;
      stan::model::assign(A,
        get_changepoint_matrix(t, t_change, T, S, pstream__),
        "assigning variable A");
      current_statement__ = 47;
      stan::math::validate_non_negative_index("X_sa", "T", T);
      current_statement__ = 48;
      stan::math::validate_non_negative_index("X_sa", "K", K);
      current_statement__ = 49;
      X_sa_data__ = 
        Eigen::Matrix<double, -1, -1>::Constant(T, K,
          std::numeric_limits<double>::quiet_NaN());
      new (&X_sa) Eigen::Map<Eigen::Matrix<double, -1, -1>>(X_sa_data__.data(), T, K);
        
      
      current_statement__ = 49;
      stan::model::assign(X_sa,
        stan::math::elt_multiply(X,
          stan::math::rep_matrix(stan::math::transpose(s_a), T)),
        "assigning variable X_sa");
      current_statement__ = 50;
      stan::math::validate_non_negative_index("X_sm", "T", T);
      current_statement__ = 51;
      stan::math::validate_non_negative_index("X_sm", "K", K);
      current_statement__ = 52;
      X_sm_data__ = 
        Eigen::Matrix<double, -1, -1>::Constant(T, K,
          std::numeric_limits<double>::quiet_NaN());
      new (&X_sm) Eigen::Map<Eigen::Matrix<double, -1, -1>>(X_sm_data__.data(), T, K);
        
      
      current_statement__ = 52;
      stan::model::assign(X_sm,
        stan::math::elt_multiply(X,
          stan::math::rep_matrix(stan::math::transpose(s_m), T)),
        "assigning variable X_sm");
      current_statement__ = 53;
      stan::math::validate_non_negative_index("delta", "S", S);
      current_statement__ = 54;
      stan::math::validate_non_negative_index("beta", "K", K);
      current_statement__ = 55;
      stan::math::validate_non_negative_index("trend", "T", T);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    num_params_r__ = 1 + 1 + S + 1 + K;
    
  }
  
  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());
    (void) DUMMY_VAR__;  // suppress unused var warning
    static constexpr const char* function__ = "prophet_model_namespace::log_prob";
    (void) function__;  // suppress unused var warning
    
    try {
      local_scalar_t__ k = DUMMY_VAR__;
      current_statement__ = 1;
      k = in__.template read<local_scalar_t__>();
      local_scalar_t__ m = DUMMY_VAR__;
      current_statement__ = 2;
      m = in__.template read<local_scalar_t__>();
      Eigen::Matrix<local_scalar_t__, -1, 1> delta =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(S, DUMMY_VAR__);
      current_statement__ = 3;
      delta = in__.template read<Eigen::Matrix<local_scalar_t__, -1, 1>>(S);
      local_scalar_t__ sigma_obs = DUMMY_VAR__;
      current_statement__ = 4;
      sigma_obs = in__.template read_constrain_lb<local_scalar_t__, 
                    jacobian__>(0, lp__);
      Eigen::Matrix<local_scalar_t__, -1, 1> beta =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(K, DUMMY_VAR__);
      current_statement__ = 5;
      beta = in__.template read<Eigen::Matrix<local_scalar_t__, -1, 1>>(K);
      Eigen::Matrix<local_scalar_t__, -1, 1> trend =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(T, DUMMY_VAR__);
      current_statement__ = 15;
      if (stan::math::logical_eq(trend_indicator, 0)) {
        current_statement__ = 13;
        stan::model::assign(trend,
          linear_trend(k, m, delta, t, A, t_change, pstream__),
          "assigning variable trend");
      } else {
        current_statement__ = 12;
        if (stan::math::logical_eq(trend_indicator, 1)) {
          current_statement__ = 10;
          stan::model::assign(trend,
            logistic_trend(k, m, delta, t, cap, A, t_change, S, pstream__),
            "assigning variable trend");
        } else {
          current_statement__ = 9;
          if (stan::math::logical_eq(trend_indicator, 2)) {
            current_statement__ = 7;
            stan::model::assign(trend, flat_trend(m, T, pstream__),
              "assigning variable trend");
          } 
        }
      }
      {
        current_statement__ = 16;
        lp_accum__.add(stan::math::normal_lpdf<propto__>(k, 0, 5));
        current_statement__ = 17;
        lp_accum__.add(stan::math::normal_lpdf<propto__>(m, 0, 5));
        current_statement__ = 18;
        lp_accum__.add(
          stan::math::double_exponential_lpdf<propto__>(delta, 0, tau));
        current_statement__ = 19;
        lp_accum__.add(stan::math::normal_lpdf<propto__>(sigma_obs, 0, 0.5));
        current_statement__ = 20;
        lp_accum__.add(stan::math::normal_lpdf<propto__>(beta, 0, sigmas));
        current_statement__ = 21;
        lp_accum__.add(
          stan::math::normal_id_glm_lpdf<propto__>(y, X_sa,
            stan::math::elt_multiply(trend,
              stan::math::add(1, stan::math::multiply(X_sm, beta))), beta,
            sigma_obs));
      }
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    lp_accum__.add(lp__);
    return lp_accum__.sum();
    } // log_prob_impl() 
    
  template <typename RNG, typename VecR, typename VecI, typename VecVar, 
  stan::require_vector_like_vt<std::is_floating_point, VecR>* = nullptr, 
  stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr, 
  stan::require_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;
    (void) propto__;
    double lp__ = 0.0;
    (void) lp__;  // dummy to suppress unused var warning
    int current_statement__ = 0; 
    stan::math::accumulator<double> lp_accum__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    constexpr bool jacobian__ = false;
    (void) DUMMY_VAR__;  // suppress unused var warning
    static constexpr const char* function__ = "prophet_model_namespace::write_array";
    (void) function__;  // suppress unused var warning
    
    try {
      double k = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 1;
      k = in__.template read<local_scalar_t__>();
      double m = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 2;
      m = in__.template read<local_scalar_t__>();
      Eigen::Matrix<double, -1, 1> delta =
         Eigen::Matrix<double, -1, 1>::Constant(S,
           std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 3;
      delta = in__.template read<Eigen::Matrix<local_scalar_t__, -1, 1>>(S);
      double sigma_obs = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 4;
      sigma_obs = in__.template read_constrain_lb<local_scalar_t__, 
                    jacobian__>(0, lp__);
      Eigen::Matrix<double, -1, 1> beta =
         Eigen::Matrix<double, -1, 1>::Constant(K,
           std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 5;
      beta = in__.template read<Eigen::Matrix<local_scalar_t__, -1, 1>>(K);
      Eigen::Matrix<double, -1, 1> trend =
         Eigen::Matrix<double, -1, 1>::Constant(T,
           std::numeric_limits<double>::quiet_NaN());
      out__.write(k);
      out__.write(m);
      out__.write(delta);
      out__.write(sigma_obs);
      out__.write(beta);
      if (stan::math::logical_negation((stan::math::primitive_value(
            emit_transformed_parameters__) || stan::math::primitive_value(
            emit_generated_quantities__)))) {
        return ;
      } 
      current_statement__ = 15;
      if (stan::math::logical_eq(trend_indicator, 0)) {
        current_statement__ = 13;
        stan::model::assign(trend,
          linear_trend(k, m, delta, t, A, t_change, pstream__),
          "assigning variable trend");
      } else {
        current_statement__ = 12;
        if (stan::math::logical_eq(trend_indicator, 1)) {
          current_statement__ = 10;
          stan::model::assign(trend,
            logistic_trend(k, m, delta, t, cap, A, t_change, S, pstream__),
            "assigning variable trend");
        } else {
          current_statement__ = 9;
          if (stan::math::logical_eq(trend_indicator, 2)) {
            current_statement__ = 7;
            stan::model::assign(trend, flat_trend(m, T, pstream__),
              "assigning variable trend");
          } 
        }
      }
      if (emit_transformed_parameters__) {
        out__.write(trend);
      } 
      if (stan::math::logical_negation(emit_generated_quantities__)) {
        return ;
      } 
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    } // write_array_impl() 
    
  template <typename VecVar, typename VecI, 
  stan::require_vector_t<VecVar>* = nullptr, 
  stan::require_vector_like_vt<std::is_integral, VecI>* = nullptr> 
  inline void transform_inits_impl(VecVar& params_r__, 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());
    
    try {
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      local_scalar_t__ k = DUMMY_VAR__;
      k = in__.read<local_scalar_t__>();
      out__.write(k);
      local_scalar_t__ m = DUMMY_VAR__;
      m = in__.read<local_scalar_t__>();
      out__.write(m);
      Eigen::Matrix<local_scalar_t__, -1, 1> delta =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(S, DUMMY_VAR__);
      for (int sym1__ = 1; sym1__ <= S; ++sym1__) {
        stan::model::assign(delta, in__.read<local_scalar_t__>(),
          "assigning variable delta", stan::model::index_uni(sym1__));
      }
      out__.write(delta);
      local_scalar_t__ sigma_obs = DUMMY_VAR__;
      sigma_obs = in__.read<local_scalar_t__>();
      out__.write_free_lb(0, sigma_obs);
      Eigen::Matrix<local_scalar_t__, -1, 1> beta =
         Eigen::Matrix<local_scalar_t__, -1, 1>::Constant(K, DUMMY_VAR__);
      for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
        stan::model::assign(beta, in__.read<local_scalar_t__>(),
          "assigning variable beta", stan::model::index_uni(sym1__));
      }
      out__.write(beta);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    } // transform_inits_impl() 
    
  inline void get_param_names(std::vector<std::string>& names__) const {
    
    names__ = std::vector<std::string>{"k", "m", "delta", "sigma_obs",
      "beta", "trend"};
    
    } // get_param_names() 
    
  inline void get_dims(std::vector<std::vector<size_t>>& dimss__) const {
    
    dimss__ = std::vector<std::vector<size_t>>{std::vector<size_t>{},
      std::vector<size_t>{}, std::vector<size_t>{static_cast<size_t>(S)},
      std::vector<size_t>{}, std::vector<size_t>{static_cast<size_t>(K)},
      std::vector<size_t>{static_cast<size_t>(T)}};
    
    } // get_dims() 
    
  inline void constrained_param_names(
                                      std::vector<std::string>& param_names__,
                                      bool emit_transformed_parameters__ = true,
                                      bool emit_generated_quantities__ = true) const
    final {
    
    param_names__.emplace_back(std::string() + "k");
    param_names__.emplace_back(std::string() + "m");
    for (int sym1__ = 1; sym1__ <= S; ++sym1__) {
      {
        param_names__.emplace_back(std::string() + "delta" + '.' + std::to_string(sym1__));
      } 
    }
    param_names__.emplace_back(std::string() + "sigma_obs");
    for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
      {
        param_names__.emplace_back(std::string() + "beta" + '.' + std::to_string(sym1__));
      } 
    }
    if (emit_transformed_parameters__) {
      for (int sym1__ = 1; sym1__ <= T; ++sym1__) {
        {
          param_names__.emplace_back(std::string() + "trend" + '.' + std::to_string(sym1__));
        } 
      }
    }
    
    if (emit_generated_quantities__) {
      
    }
    
    } // constrained_param_names() 
    
  inline void unconstrained_param_names(
                                        std::vector<std::string>& param_names__,
                                        bool emit_transformed_parameters__ = true,
                                        bool emit_generated_quantities__ = true) const
    final {
    
    param_names__.emplace_back(std::string() + "k");
    param_names__.emplace_back(std::string() + "m");
    for (int sym1__ = 1; sym1__ <= S; ++sym1__) {
      {
        param_names__.emplace_back(std::string() + "delta" + '.' + std::to_string(sym1__));
      } 
    }
    param_names__.emplace_back(std::string() + "sigma_obs");
    for (int sym1__ = 1; sym1__ <= K; ++sym1__) {
      {
        param_names__.emplace_back(std::string() + "beta" + '.' + std::to_string(sym1__));
      } 
    }
    if (emit_transformed_parameters__) {
      for (int sym1__ = 1; sym1__ <= T; ++sym1__) {
        {
          param_names__.emplace_back(std::string() + "trend" + '.' + std::to_string(sym1__));
        } 
      }
    }
    
    if (emit_generated_quantities__) {
      
    }
    
    } // unconstrained_param_names() 
    
  inline std::string get_constrained_sizedtypes() const {
    
    return std::string("[{\"name\":\"k\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"m\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"delta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(S) + "},\"block\":\"parameters\"},{\"name\":\"sigma_obs\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"beta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(K) + "},\"block\":\"parameters\"},{\"name\":\"trend\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(T) + "},\"block\":\"transformed_parameters\"}]");
    
    } // get_constrained_sizedtypes() 
    
  inline std::string get_unconstrained_sizedtypes() const {
    
    return std::string("[{\"name\":\"k\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"m\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"delta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(S) + "},\"block\":\"parameters\"},{\"name\":\"sigma_obs\",\"type\":{\"name\":\"real\"},\"block\":\"parameters\"},{\"name\":\"beta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(K) + "},\"block\":\"parameters\"},{\"name\":\"trend\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(T) + "},\"block\":\"transformed_parameters\"}]");
    
    } // get_unconstrained_sizedtypes() 
    
  
    // Begin method overload boilerplate
    template <typename RNG>
    inline void write_array(RNG& base_rng,
                            Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
                            Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
                            const bool emit_transformed_parameters = true,
                            const bool emit_generated_quantities = true,
                            std::ostream* pstream = nullptr) const {
      const size_t num_params__ = 
  ((((1 + 1) + S) + 1) + K);
      const size_t num_transformed = emit_transformed_parameters * T;
      const size_t num_gen_quantities = emit_generated_quantities * 0;
      const size_t num_to_write = num_params__ + num_transformed +
        num_gen_quantities;
      std::vector<int> params_i;
      vars = Eigen::Matrix<double, Eigen::Dynamic, 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__ = 
  ((((1 + 1) + S) + 1) + K);
      const size_t num_transformed = emit_transformed_parameters * T;
      const size_t num_gen_quantities = emit_generated_quantities * 0;
      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_,Eigen::Dynamic,1>& params_r,
                       std::ostream* pstream = nullptr) const {
      Eigen::Matrix<int, -1, 1> params_i;
      return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
    }

    template <bool propto__, bool jacobian__, typename T__>
    inline T__ log_prob(std::vector<T__>& params_r,
                        std::vector<int>& params_i,
                        std::ostream* pstream = nullptr) const {
      return log_prob_impl<propto__, jacobian__>(params_r, params_i, pstream);
    }


    inline void transform_inits(const stan::io::var_context& context,
                         Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
                         std::ostream* pstream = nullptr) const final {
      std::vector<double> params_r_vec(params_r.size());
      std::vector<int> params_i;
      transform_inits(context, params_i, params_r_vec, pstream);
      params_r = Eigen::Map<Eigen::Matrix<double,Eigen::Dynamic,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 {
     constexpr std::array<const char*, 5> names__{"k", "m", "delta",
      "sigma_obs", "beta"};
      const std::array<Eigen::Index, 5> constrain_param_sizes__{1, 1, 
       S, 1, K};
      const auto num_constrained_params__ = std::accumulate(
        constrain_param_sizes__.begin(), constrain_param_sizes__.end(), 0);
    
     std::vector<double> params_r_flat__(num_constrained_params__);
     Eigen::Index size_iter__ = 0;
     Eigen::Index flat_iter__ = 0;
     for (auto&& param_name__ : names__) {
       const auto param_vec__ = context.vals_r(param_name__);
       for (Eigen::Index i = 0; i < constrain_param_sizes__[size_iter__]; ++i) {
         params_r_flat__[flat_iter__] = param_vec__[i];
         ++flat_iter__;
       }
       ++size_iter__;
     }
     vars.resize(num_params_r__);
     transform_inits_impl(params_r_flat__, params_i, vars, pstream__);
    } // transform_inits() 
     }; } using stan_model = prophet_model_namespace::prophet_model;

#ifndef USING_R

// Boilerplate
stan::model::model_base& new_model(
        stan::io::var_context& data_context,
        unsigned int seed,
        std::ostream* msg_stream) {
  stan_model* m = new stan_model(data_context, seed, msg_stream);
  return *m;
}

stan::math::profile_map& get_stan_profile_data() {
  return prophet_model_namespace::profiles__;
}

#endif


