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

/*
    rts2 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.

    rts2 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 rts2.  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_rtsbayes_namespace {
using stan::model::model_base_crtp;
using namespace stan::math;
stan::math::profile_map profiles__;
static constexpr std::array<const char*, 396> locations_array__ =
  {" (found before start of program)",
  " (in 'string', line 239, column 2 to column 55)",
  " (in 'string', line 240, column 2 to column 57)",
  " (in 'string', line 241, column 2 to column 18)",
  " (in 'string', line 243, column 2 to column 50)",
  " (in 'string', line 244, column 2 to column 66)",
  " (in 'string', line 245, column 2 to column 60)",
  " (in 'string', line 248, column 2 to column 23)",
  " (in 'string', line 249, column 2 to column 26)",
  " (in 'string', line 250, column 2 to column 24)",
  " (in 'string', line 252, column 2 to column 67)",
  " (in 'string', line 254, column 2 to column 41)",
  " (in 'string', line 255, column 2 to column 42)",
  " (in 'string', line 257, column 2 to column 62)",
  " (in 'string', line 380, column 2 to column 36)",
  " (in 'string', line 381, column 2 to column 53)",
  " (in 'string', line 382, column 2 to column 56)",
  " (in 'string', line 265, column 4 to column 27)",
  " (in 'string', line 266, column 4 to column 23)",
  " (in 'string', line 269, column 8 to column 91)",
  " (in 'string', line 268, column 22 to line 270, column 7)",
  " (in 'string', line 268, column 6 to line 270, column 7)",
  " (in 'string', line 267, column 19 to line 271, column 5)",
  " (in 'string', line 267, column 4 to line 271, column 5)",
  " (in 'string', line 273, column 6 to column 57)",
  " (in 'string', line 272, column 19 to line 274, column 5)",
  " (in 'string', line 272, column 4 to line 274, column 5)",
  " (in 'string', line 264, column 9 to line 275, column 3)",
  " (in 'string', line 260, column 4 to column 23)",
  " (in 'string', line 261, column 4 to column 19)",
  " (in 'string', line 262, column 19 to column 53)",
  " (in 'string', line 262, column 4 to column 53)",
  " (in 'string', line 263, column 19 to column 32)",
  " (in 'string', line 263, column 4 to column 32)",
  " (in 'string', line 259, column 15 to line 264, column 3)",
  " (in 'string', line 259, column 2 to line 275, column 3)",
  " (in 'string', line 316, column 8 to column 29)",
  " (in 'string', line 315, column 13 to line 317, column 7)",
  " (in 'string', line 313, column 10 to column 138)",
  " (in 'string', line 312, column 15 to line 314, column 9)",
  " (in 'string', line 311, column 10 to column 69)",
  " (in 'string', line 310, column 16 to line 312, column 9)",
  " (in 'string', line 310, column 8 to line 314, column 9)",
  " (in 'string', line 309, column 14 to line 315, column 7)",
  " (in 'string', line 309, column 6 to line 317, column 7)",
  " (in 'string', line 308, column 18 to line 318, column 5)",
  " (in 'string', line 308, column 4 to line 318, column 5)",
  " (in 'string', line 307, column 24 to line 319, column 3)",
  " (in 'string', line 307, column 9 to line 319, column 3)",
  " (in 'string', line 296, column 6 to column 37)",
  " (in 'string', line 304, column 8 to column 38)",
  " (in 'string', line 303, column 13 to line 305, column 7)",
  " (in 'string', line 301, column 10 to column 105)",
  " (in 'string', line 300, column 15 to line 302, column 9)",
  " (in 'string', line 299, column 10 to column 56)",
  " (in 'string', line 298, column 16 to line 300, column 9)",
  " (in 'string', line 298, column 8 to line 302, column 9)",
  " (in 'string', line 297, column 14 to line 303, column 7)",
  " (in 'string', line 297, column 6 to line 305, column 7)",
  " (in 'string', line 295, column 18 to line 306, column 5)",
  " (in 'string', line 295, column 4 to line 306, column 5)",
  " (in 'string', line 294, column 24 to line 307, column 3)",
  " (in 'string', line 294, column 9 to line 319, column 3)",
  " (in 'string', line 281, column 6 to column 20)",
  " (in 'string', line 280, column 11 to line 282, column 5)",
  " (in 'string', line 279, column 6 to column 54)",
  " (in 'string', line 278, column 19 to line 280, column 5)",
  " (in 'string', line 278, column 4 to line 282, column 5)",
  " (in 'string', line 291, column 8 to column 34)",
  " (in 'string', line 290, column 13 to line 292, column 7)",
  " (in 'string', line 288, column 10 to column 148)",
  " (in 'string', line 287, column 15 to line 289, column 9)",
  " (in 'string', line 286, column 10 to column 74)",
  " (in 'string', line 285, column 16 to line 287, column 9)",
  " (in 'string', line 285, column 8 to line 289, column 9)",
  " (in 'string', line 284, column 14 to line 290, column 7)",
  " (in 'string', line 284, column 6 to line 292, column 7)",
  " (in 'string', line 283, column 18 to line 293, column 5)",
  " (in 'string', line 283, column 4 to line 293, column 5)",
  " (in 'string', line 277, column 17 to line 294, column 3)",
  " (in 'string', line 277, column 2 to line 319, column 3)",
  " (in 'string', line 389, column 4 to column 47)",
  " (in 'string', line 390, column 4 to column 49)",
  " (in 'string', line 393, column 6 to column 61)",
  " (in 'string', line 392, column 4 to line 394, column 5)",
  " (in 'string', line 391, column 4 to line 394, column 5)",
  " (in 'string', line 398, column 10 to line 399, column 144)",
  " (in 'string', line 397, column 43 to line 400, column 9)",
  " (in 'string', line 397, column 8 to line 400, column 9)",
  " (in 'string', line 396, column 20 to line 401, column 7)",
  " (in 'string', line 396, column 6 to line 401, column 7)",
  " (in 'string', line 395, column 24 to line 402, column 5)",
  " (in 'string', line 395, column 4 to line 402, column 5)",
  " (in 'string', line 388, column 9 to line 403, column 3)",
  " (in 'string', line 386, column 6 to column 62)",
  " (in 'string', line 385, column 28 to line 387, column 5)",
  " (in 'string', line 385, column 4 to line 387, column 5)",
  " (in 'string', line 384, column 16 to line 388, column 3)",
  " (in 'string', line 384, column 2 to line 403, column 3)",
  " (in 'string', line 322, column 9 to column 36)",
  " (in 'string', line 322, column 2 to column 47)",
  " (in 'string', line 323, column 9 to column 35)",
  " (in 'string', line 323, column 2 to column 49)",
  " (in 'string', line 327, column 4 to column 41)",
  " (in 'string', line 328, column 4 to column 43)",
  " (in 'string', line 326, column 15 to line 329, column 3)",
  " (in 'string', line 326, column 2 to line 329, column 3)",
  " (in 'string', line 331, column 17 to column 47)",
  " (in 'string', line 331, column 2 to column 47)",
  " (in 'string', line 333, column 4 to column 56)",
  " (in 'string', line 334, column 4 to column 52)",
  " (in 'string', line 332, column 16 to line 335, column 3)",
  " (in 'string', line 332, column 2 to line 335, column 3)",
  " (in 'string', line 336, column 12 to column 29)",
  " (in 'string', line 336, column 2 to column 29)",
  " (in 'string', line 338, column 4 to column 65)",
  " (in 'string', line 337, column 15 to line 339, column 3)",
  " (in 'string', line 337, column 2 to line 339, column 3)",
  " (in 'string', line 350, column 10 to column 64)",
  " (in 'string', line 349, column 15 to line 351, column 9)",
  " (in 'string', line 347, column 12 to column 97)",
  " (in 'string', line 346, column 17 to line 348, column 11)",
  " (in 'string', line 345, column 12 to column 77)",
  " (in 'string', line 344, column 18 to line 346, column 11)",
  " (in 'string', line 344, column 10 to line 348, column 11)",
  " (in 'string', line 343, column 16 to line 349, column 9)",
  " (in 'string', line 343, column 8 to line 351, column 9)",
  " (in 'string', line 342, column 20 to line 352, column 7)",
  " (in 'string', line 342, column 6 to line 352, column 7)",
  " (in 'string', line 341, column 17 to line 353, column 5)",
  " (in 'string', line 341, column 2 to line 353, column 5)",
  " (in 'string', line 375, column 4 to column 42)",
  " (in 'string', line 374, column 9 to line 376, column 3)",
  " (in 'string', line 356, column 4 to column 41)",
  " (in 'string', line 358, column 6 to column 27)",
  " (in 'string', line 357, column 19 to line 359, column 5)",
  " (in 'string', line 357, column 4 to line 359, column 5)",
  " (in 'string', line 368, column 10 to column 166)",
  " (in 'string', line 367, column 8 to line 369, column 9)",
  " (in 'string', line 366, column 8 to line 369, column 9)",
  " (in 'string', line 364, column 6 to line 370, column 7)",
  " (in 'string', line 363, column 6 to line 370, column 7)",
  " (in 'string', line 362, column 4 to line 371, column 5)",
  " (in 'string', line 361, column 4 to line 371, column 5)",
  " (in 'string', line 372, column 4 to column 26)",
  " (in 'string', line 355, column 15 to line 374, column 3)",
  " (in 'string', line 355, column 2 to line 376, column 3)",
  " (in 'string', line 156, column 2 to column 10)",
  " (in 'string', line 157, column 2 to column 35)",
  " (in 'string', line 158, column 2 to column 16)",
  " (in 'string', line 161, column 2 to column 17)",
  " (in 'string', line 162, column 2 to column 17)",
  " (in 'string', line 163, column 2 to column 23)",
  " (in 'string', line 164, column 2 to column 24)",
  " (in 'string', line 165, column 2 to column 9)",
  " (in 'string', line 166, column 8 to column 44)",
  " (in 'string', line 166, column 2 to column 52)",
  " (in 'string', line 167, column 9 to column 16)",
  " (in 'string', line 167, column 17 to column 18)",
  " (in 'string', line 167, column 2 to column 27)",
  " (in 'string', line 168, column 9 to column 19)",
  " (in 'string', line 168, column 2 to column 29)",
  " (in 'string', line 169, column 9 to column 19)",
  " (in 'string', line 169, column 20 to column 21)",
  " (in 'string', line 169, column 2 to column 25)",
  " (in 'string', line 172, column 2 to column 29)",
  " (in 'string', line 173, column 2 to column 26)",
  " (in 'string', line 174, column 8 to column 9)",
  " (in 'string', line 174, column 2 to column 35)",
  " (in 'string', line 175, column 8 to column 9)",
  " (in 'string', line 175, column 2 to column 33)",
  " (in 'string', line 178, column 2 to column 38)",
  " (in 'string', line 179, column 2 to column 27)",
  " (in 'string', line 180, column 2 to column 25)",
  " (in 'string', line 183, column 2 to column 10)",
  " (in 'string', line 184, column 8 to column 34)",
  " (in 'string', line 184, column 2 to column 56)",
  " (in 'string', line 185, column 8 to column 27)",
  " (in 'string', line 185, column 2 to column 50)",
  " (in 'string', line 186, column 9 to column 28)",
  " (in 'string', line 186, column 2 to column 40)",
  " (in 'string', line 191, column 2 to column 17)",
  " (in 'string', line 192, column 2 to column 20)",
  " (in 'string', line 193, column 8 to column 30)",
  " (in 'string', line 193, column 31 to column 50)",
  " (in 'string', line 193, column 2 to column 64)",
  " (in 'string', line 194, column 8 to column 9)",
  " (in 'string', line 194, column 2 to column 18)",
  " (in 'string', line 197, column 8 to column 27)",
  " (in 'string', line 197, column 29 to column 54)",
  " (in 'string', line 197, column 2 to column 63)",
  " (in 'string', line 200, column 9 to column 19)",
  " (in 'string', line 200, column 2 to column 47)",
  " (in 'string', line 202, column 9 to column 47)",
  " (in 'string', line 202, column 49 to column 88)",
  " (in 'string', line 202, column 2 to column 97)",
  " (in 'string', line 203, column 8 to column 51)",
  " (in 'string', line 203, column 2 to column 64)",
  " (in 'string', line 205, column 9 to column 34)",
  " (in 'string', line 205, column 36 to column 58)",
  " (in 'string', line 205, column 2 to column 64)",
  " (in 'string', line 206, column 8 to column 43)",
  " (in 'string', line 206, column 2 to column 63)",
  " (in 'string', line 208, column 9 to column 43)",
  " (in 'string', line 208, column 44 to column 82)",
  " (in 'string', line 208, column 2 to column 92)",
  " (in 'string', line 213, column 8 to line 214, column 75)",
  " (in 'string', line 212, column 29 to line 215, column 7)",
  " (in 'string', line 212, column 6 to line 215, column 7)",
  " (in 'string', line 211, column 27 to line 216, column 5)",
  " (in 'string', line 211, column 4 to line 216, column 5)",
  " (in 'string', line 210, column 32 to line 217, column 3)",
  " (in 'string', line 210, column 2 to line 217, column 3)",
  " (in 'string', line 234, column 6 to column 72)",
  " (in 'string', line 233, column 17 to line 235, column 5)",
  " (in 'string', line 233, column 4 to line 235, column 5)",
  " (in 'string', line 232, column 24 to line 236, column 3)",
  " (in 'string', line 232, column 9 to line 236, column 3)",
  " (in 'string', line 225, column 6 to column 47)",
  " (in 'string', line 224, column 21 to line 226, column 5)",
  " (in 'string', line 224, column 4 to line 226, column 5)",
  " (in 'string', line 229, column 8 to column 106)",
  " (in 'string', line 228, column 22 to line 230, column 7)",
  " (in 'string', line 228, column 6 to line 230, column 7)",
  " (in 'string', line 227, column 17 to line 231, column 5)",
  " (in 'string', line 227, column 4 to line 231, column 5)",
  " (in 'string', line 223, column 24 to line 232, column 3)",
  " (in 'string', line 223, column 9 to line 236, column 3)",
  " (in 'string', line 221, column 6 to column 66)",
  " (in 'string', line 220, column 17 to line 222, column 5)",
  " (in 'string', line 220, column 4 to line 222, column 5)",
  " (in 'string', line 219, column 17 to line 223, column 3)",
  " (in 'string', line 219, column 2 to line 236, column 3)",
  " (in 'string', line 239, column 8 to column 25)",
  " (in 'string', line 240, column 8 to column 25)",
  " (in 'string', line 241, column 9 to column 10)",
  " (in 'string', line 243, column 8 to column 22)",
  " (in 'string', line 244, column 8 to column 53)",
  " (in 'string', line 245, column 9 to column 31)",
  " (in 'string', line 245, column 33 to column 53)",
  " (in 'string', line 248, column 9 to column 19)",
  " (in 'string', line 252, column 9 to column 34)",
  " (in 'string', line 252, column 35 to column 60)",
  " (in 'string', line 254, column 9 to column 31)",
  " (in 'string', line 255, column 9 to column 31)",
  " (in 'string', line 257, column 9 to column 31)",
  " (in 'string', line 257, column 32 to column 57)",
  " (in 'string', line 380, column 9 to column 19)",
  " (in 'string', line 381, column 9 to column 36)",
  " (in 'string', line 382, column 9 to column 34)",
  " (in 'string', line 3, column 11 to column 12)",
  " (in 'string', line 3, column 13 to column 14)",
  " (in 'string', line 3, column 4 to column 38)",
  " (in 'string', line 4, column 4 to column 11)",
  " (in 'string', line 5, column 4 to column 14)",
  " (in 'string', line 6, column 4 to column 19)",
  " (in 'string', line 7, column 4 to column 12)",
  " (in 'string', line 10, column 6 to column 16)",
  " (in 'string', line 11, column 6 to column 24)",
  " (in 'string', line 15, column 8 to column 46)",
  " (in 'string', line 14, column 14 to line 16, column 7)",
  " (in 'string', line 13, column 8 to column 61)",
  " (in 'string', line 12, column 18 to line 14, column 7)",
  " (in 'string', line 12, column 6 to line 16, column 7)",
  " (in 'string', line 9, column 17 to line 17, column 5)",
  " (in 'string', line 9, column 4 to line 17, column 5)",
  " (in 'string', line 20, column 6 to column 12)",
  " (in 'string', line 22, column 8 to column 30)",
  " (in 'string', line 21, column 23 to line 23, column 7)",
  " (in 'string', line 21, column 6 to line 23, column 7)",
  " (in 'string', line 24, column 6 to column 31)",
  " (in 'string', line 27, column 10 to column 58)",
  " (in 'string', line 29, column 10 to column 28)",
  " (in 'string', line 30, column 10 to column 16)",
  " (in 'string', line 32, column 12 to column 34)",
  " (in 'string', line 31, column 27 to line 33, column 11)",
  " (in 'string', line 31, column 10 to line 33, column 11)",
  " (in 'string', line 37, column 12 to column 63)",
  " (in 'string', line 36, column 18 to line 38, column 11)",
  " (in 'string', line 35, column 12 to column 79)",
  " (in 'string', line 34, column 22 to line 36, column 11)",
  " (in 'string', line 34, column 10 to line 38, column 11)",
  " (in 'string', line 26, column 25 to line 39, column 9)",
  " (in 'string', line 26, column 8 to line 39, column 9)",
  " (in 'string', line 25, column 15 to line 40, column 7)",
  " (in 'string', line 25, column 6 to line 40, column 7)",
  " (in 'string', line 19, column 17 to line 41, column 5)",
  " (in 'string', line 19, column 4 to line 41, column 5)",
  " (in 'string', line 42, column 4 to column 13)",
  " (in 'string', line 2, column 76 to line 43, column 3)",
  " (in 'string', line 46, column 11 to column 12)",
  " (in 'string', line 46, column 4 to column 18)",
  " (in 'string', line 48, column 6 to column 40)",
  " (in 'string', line 47, column 17 to line 48, column 42)",
  " (in 'string', line 47, column 4 to line 48, column 42)",
  " (in 'string', line 49, column 4 to column 15)",
  " (in 'string', line 45, column 57 to line 50, column 3)",
  " (in 'string', line 52, column 4 to column 11)",
  " (in 'string', line 53, column 4 to column 18)",
  " (in 'string', line 54, column 4 to column 17)",
  " (in 'string', line 61, column 6 to column 80)",
  " (in 'string', line 59, column 11 to line 62, column 5)",
  " (in 'string', line 58, column 6 to column 85)",
  " (in 'string', line 56, column 16 to line 59, column 5)",
  " (in 'string', line 56, column 4 to line 62, column 5)",
  " (in 'string', line 63, column 4 to column 13)",
  " (in 'string', line 51, column 62 to line 64, column 3)",
  " (in 'string', line 66, column 4 to column 20)",
  " (in 'string', line 67, column 4 to column 20)",
  " (in 'string', line 68, column 11 to column 12)",
  " (in 'string', line 68, column 13 to column 14)",
  " (in 'string', line 68, column 4 to column 19)",
  " (in 'string', line 69, column 11 to column 12)",
  " (in 'string', line 69, column 4 to column 18)",
  " (in 'string', line 71, column 6 to column 65)",
  " (in 'string', line 70, column 18 to line 72, column 5)",
  " (in 'string', line 70, column 4 to line 72, column 5)",
  " (in 'string', line 73, column 4 to column 17)",
  " (in 'string', line 75, column 6 to column 26)",
  " (in 'string', line 74, column 18 to line 76, column 5)",
  " (in 'string', line 74, column 4 to line 76, column 5)",
  " (in 'string', line 77, column 4 to column 15)",
  " (in 'string', line 65, column 57 to line 78, column 3)",
  " (in 'string', line 81, column 11 to column 14)",
  " (in 'string', line 81, column 15 to column 16)",
  " (in 'string', line 81, column 4 to column 43)",
  " (in 'string', line 82, column 4 to column 15)",
  " (in 'string', line 83, column 4 to column 13)",
  " (in 'string', line 84, column 4 to column 13)",
  " (in 'string', line 85, column 4 to column 14)",
  " (in 'string', line 86, column 4 to column 22)",
  " (in 'string', line 87, column 11 to column 12)",
  " (in 'string', line 87, column 13 to column 14)",
  " (in 'string', line 87, column 4 to column 41)",
  " (in 'string', line 88, column 11 to column 12)",
  " (in 'string', line 88, column 4 to column 37)",
  " (in 'string', line 89, column 4 to column 14)",
  " (in 'string', line 92, column 6 to column 32)",
  " (in 'string', line 94, column 8 to column 26)",
  " (in 'string', line 93, column 24 to line 95, column 7)",
  " (in 'string', line 93, column 6 to line 95, column 7)",
  " (in 'string', line 99, column 12 to column 57)",
  " (in 'string', line 100, column 12 to column 57)",
  " (in 'string', line 101, column 12 to column 77)",
  " (in 'string', line 102, column 12 to column 32)",
  " (in 'string', line 107, column 14 to column 55)",
  " (in 'string', line 108, column 14 to column 36)",
  " (in 'string', line 106, column 19 to line 109, column 13)",
  " (in 'string', line 104, column 14 to column 70)",
  " (in 'string', line 105, column 14 to column 36)",
  " (in 'string', line 103, column 24 to line 106, column 13)",
  " (in 'string', line 103, column 12 to line 109, column 13)",
  " (in 'string', line 98, column 32 to line 110, column 11)",
  " (in 'string', line 98, column 10 to line 110, column 11)",
  " (in 'string', line 97, column 30 to line 111, column 9)",
  " (in 'string', line 97, column 8 to line 111, column 9)",
  " (in 'string', line 96, column 20 to line 112, column 7)",
  " (in 'string', line 96, column 6 to line 112, column 7)",
  " (in 'string', line 114, column 8 to column 84)",
  " (in 'string', line 115, column 8 to column 28)",
  " (in 'string', line 116, column 8 to column 104)",
  " (in 'string', line 113, column 24 to line 117, column 7)",
  " (in 'string', line 113, column 6 to line 117, column 7)",
  " (in 'string', line 118, column 6 to column 82)",
  " (in 'string', line 119, column 6 to column 69)",
  " (in 'string', line 91, column 17 to line 120, column 5)",
  " (in 'string', line 91, column 4 to line 120, column 5)",
  " (in 'string', line 121, column 4 to column 15)",
  " (in 'string', line 80, column 62 to line 122, column 4)",
  " (in 'string', line 125, column 4 to column 21)",
  " (in 'string', line 126, column 4 to column 25)",
  " (in 'string', line 127, column 4 to column 17)",
  " (in 'string', line 128, column 4 to column 12)",
  " (in 'string', line 129, column 4 to column 12)",
  " (in 'string', line 130, column 4 to column 12)",
  " (in 'string', line 131, column 4 to column 15)",
  " (in 'string', line 135, column 6 to column 17)",
  " (in 'string', line 134, column 11 to line 136, column 5)",
  " (in 'string', line 133, column 6 to column 25)",
  " (in 'string', line 132, column 18 to line 134, column 5)",
  " (in 'string', line 132, column 4 to line 136, column 5)",
  " (in 'string', line 137, column 4 to column 16)",
  " (in 'string', line 139, column 6 to column 32)",
  " (in 'string', line 138, column 17 to line 140, column 5)",
  " (in 'string', line 138, column 4 to line 140, column 5)",
  " (in 'string', line 141, column 4 to column 29)",
  " (in 'string', line 143, column 6 to column 75)",
  " (in 'string', line 144, column 6 to column 28)",
  " (in 'string', line 146, column 8 to column 18)",
  " (in 'string', line 145, column 20 to line 147, column 7)",
  " (in 'string', line 145, column 6 to line 147, column 7)",
  " (in 'string', line 142, column 17 to line 148, column 5)",
  " (in 'string', line 142, column 4 to line 148, column 5)",
  " (in 'string', line 150, column 4 to column 51)",
  " (in 'string', line 151, column 4 to column 14)",
  " (in 'string', line 124, column 78 to line 152, column 4)"};
template <typename T1__, typename T2__, typename T3__,
          stan::require_all_t<stan::is_stan_scalar<T1__>,
                              stan::is_stan_scalar<T2__>,
                              stan::is_stan_scalar<T3__>>* = nullptr>
Eigen::Matrix<stan::promote_args_t<T1__, T2__, T3__>,-1,-1>
genChol(const int& n, const T1__& alpha, const T2__& theta,
        const std::vector<T3__>& dists, const int& mod, std::ostream*
        pstream__);
template <typename T0__,
          stan::require_all_t<stan::is_stan_scalar<T0__>>* = nullptr>
Eigen::Matrix<stan::promote_args_t<T0__>,-1,1>
lambda_nD(const std::vector<T0__>& L, const std::vector<int>& m, const int&
          D, std::ostream* pstream__);
template <typename T0__, typename T1__, typename T2__,
          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__>>* = nullptr>
stan::promote_args_t<T0__, T1__, stan::base_type_t<T2__>>
spd_nD(const T0__& sigma, const T1__& phi, const T2__& w_arg__, const int& D,
       const int& mod, std::ostream* pstream__);
template <typename T0__, typename T2__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_eigen_matrix_dynamic<T2__>,
                              stan::is_vt_not_complex<T2__>>* = nullptr>
Eigen::Matrix<stan::promote_args_t<T0__, stan::base_type_t<T2__>>,-1,1>
phi_nD(const std::vector<T0__>& L, const std::vector<int>& m, const T2__&
       x_arg__, std::ostream* pstream__);
template <typename T0__, typename T1__, typename T4__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_stan_scalar<T4__>>* = nullptr>
Eigen::Matrix<stan::promote_args_t<T0__, T1__, T4__>,-1,-1>
getAD(const T0__& alpha, const T1__& theta, const int& M, const int& n,
      const std::vector<T4__>& dists, const std::vector<std::vector<int>>&
      NN, const int& mod, std::ostream* pstream__);
template <bool propto__, typename T0__, typename T1__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_eigen_matrix_dynamic<T1__>,
                              stan::is_vt_not_complex<T1__>>* = nullptr>
stan::promote_args_t<T0__, stan::base_type_t<T1__>>
nngp_split_lpdf(const std::vector<T0__>& u, const T1__& AD_arg__,
                const std::vector<std::vector<int>>& NN, const int& start,
                std::ostream* pstream__);
template <typename T1__, typename T2__, typename T3__,
          stan::require_all_t<stan::is_stan_scalar<T1__>,
                              stan::is_stan_scalar<T2__>,
                              stan::is_stan_scalar<T3__>>*>
Eigen::Matrix<stan::promote_args_t<T1__, T2__, T3__>,-1,-1>
genChol(const int& n, const T1__& alpha, const T2__& theta,
        const std::vector<T3__>& dists, const int& mod, std::ostream*
        pstream__) {
  using local_scalar_t__ = stan::promote_args_t<T1__, T2__, T3__>;
  int current_statement__ = 0;
  static constexpr bool propto__ = true;
  // suppress unused var warning
  (void) propto__;
  local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  // suppress unused var warning
  (void) DUMMY_VAR__;
  try {
    current_statement__ = 250;
    stan::math::validate_non_negative_index("L", "n", n);
    current_statement__ = 251;
    stan::math::validate_non_negative_index("L", "n", n);
    Eigen::Matrix<local_scalar_t__,-1,-1> L =
      Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(n, n, DUMMY_VAR__);
    current_statement__ = 252;
    stan::model::assign(L, stan::math::rep_matrix(0, n, n),
      "assigning variable L");
    local_scalar_t__ s = DUMMY_VAR__;
    local_scalar_t__ dist = DUMMY_VAR__;
    current_statement__ = 255;
    stan::model::assign(L, alpha, "assigning variable L",
      stan::model::index_uni(1), stan::model::index_uni(1));
    int idx = std::numeric_limits<int>::min();
    current_statement__ = 265;
    for (int i = 2; i <= n; ++i) {
      current_statement__ = 257;
      idx = (i - 1);
      current_statement__ = 258;
      dist = stan::model::rvalue(dists, "dists", stan::model::index_uni(idx));
      current_statement__ = 263;
      if (stan::math::logical_eq(mod, 0)) {
        current_statement__ = 261;
        stan::model::assign(L, (alpha *
          stan::math::exp(((-1.0 * (dist * dist)) / (theta * theta)))),
          "assigning variable L", stan::model::index_uni(i),
          stan::model::index_uni(1));
      } else {
        current_statement__ = 259;
        stan::model::assign(L, (alpha *
          stan::math::exp(((-1.0 * dist) / theta))), "assigning variable L",
          stan::model::index_uni(i), stan::model::index_uni(1));
      }
    }
    current_statement__ = 287;
    for (int j = 2; j <= n; ++j) {
      current_statement__ = 266;
      s = 0;
      current_statement__ = 269;
      for (int k = 1; k <= (j - 1); ++k) {
        current_statement__ = 267;
        s = (s +
          (stan::model::rvalue(L, "L", stan::model::index_uni(j),
             stan::model::index_uni(k))
          *
          stan::model::rvalue(L, "L", stan::model::index_uni(j),
            stan::model::index_uni(k))));
      }
      current_statement__ = 270;
      stan::model::assign(L, stan::math::sqrt((alpha - s)),
        "assigning variable L", stan::model::index_uni(j),
        stan::model::index_uni(j));
      current_statement__ = 285;
      if (stan::math::logical_lt(j, n)) {
        current_statement__ = 283;
        for (int i = (j + 1); i <= n; ++i) {
          current_statement__ = 271;
          idx = (((((n - 1) * (j - 1)) - (((j - 2) * (j - 1)) / 2)) + ((i -
            j) - 1)) + 1);
          current_statement__ = 272;
          dist = stan::model::rvalue(dists, "dists",
                   stan::model::index_uni(idx));
          current_statement__ = 273;
          s = 0;
          current_statement__ = 276;
          for (int k = 1; k <= (j - 1); ++k) {
            current_statement__ = 274;
            s = (s +
              (stan::model::rvalue(L, "L", stan::model::index_uni(j),
                 stan::model::index_uni(k))
              *
              stan::model::rvalue(L, "L", stan::model::index_uni(i),
                stan::model::index_uni(k))));
          }
          current_statement__ = 281;
          if (stan::math::logical_eq(mod, 0)) {
            current_statement__ = 279;
            stan::model::assign(L, ((1 /
              stan::model::rvalue(L, "L", stan::model::index_uni(j),
                stan::model::index_uni(j))) * ((alpha *
              stan::math::exp((-(dist * dist) / (theta * theta)))) - s)),
              "assigning variable L", stan::model::index_uni(i),
              stan::model::index_uni(j));
          } else {
            current_statement__ = 277;
            stan::model::assign(L, ((1 /
              stan::model::rvalue(L, "L", stan::model::index_uni(j),
                stan::model::index_uni(j))) * ((alpha *
              stan::math::exp((-dist / theta))) - s)),
              "assigning variable L", stan::model::index_uni(i),
              stan::model::index_uni(j));
          }
        }
      }
    }
    current_statement__ = 288;
    return L;
  } 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__>>*>
Eigen::Matrix<stan::promote_args_t<T0__>,-1,1>
lambda_nD(const std::vector<T0__>& L, const std::vector<int>& m, const int&
          D, std::ostream* pstream__) {
  using local_scalar_t__ = stan::promote_args_t<T0__>;
  int current_statement__ = 0;
  static constexpr bool propto__ = true;
  // suppress unused var warning
  (void) propto__;
  local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  // suppress unused var warning
  (void) DUMMY_VAR__;
  try {
    current_statement__ = 290;
    stan::math::validate_non_negative_index("lam", "D", D);
    Eigen::Matrix<local_scalar_t__,-1,1> lam =
      Eigen::Matrix<local_scalar_t__,-1,1>::Constant(D, DUMMY_VAR__);
    current_statement__ = 294;
    for (int i = 1; i <= D; ++i) {
      current_statement__ = 292;
      stan::model::assign(lam,
        stan::math::pow(
          ((stan::model::rvalue(m, "m", stan::model::index_uni(i)) *
          stan::math::pi()) / (2 *
          stan::model::rvalue(L, "L", stan::model::index_uni(i)))), 2),
        "assigning variable lam", stan::model::index_uni(i));
    }
    current_statement__ = 295;
    return lam;
  } catch (const std::exception& e) {
    stan::lang::rethrow_located(e, locations_array__[current_statement__]);
  }
}
template <typename T0__, typename T1__, typename T2__,
          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::promote_args_t<T0__, T1__, stan::base_type_t<T2__>>
spd_nD(const T0__& sigma, const T1__& phi, const T2__& w_arg__, const int& D,
       const int& mod, std::ostream* pstream__) {
  using local_scalar_t__ = stan::promote_args_t<T0__, T1__,
                             stan::base_type_t<T2__>>;
  int current_statement__ = 0;
  const auto& w = stan::math::to_ref(w_arg__);
  static constexpr bool propto__ = true;
  // suppress unused var warning
  (void) propto__;
  local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  // suppress unused var warning
  (void) DUMMY_VAR__;
  try {
    local_scalar_t__ S = DUMMY_VAR__;
    Eigen::Matrix<local_scalar_t__,-1,1> wsq =
      Eigen::Matrix<local_scalar_t__,-1,1>::Constant(2, DUMMY_VAR__);
    current_statement__ = 299;
    stan::model::assign(wsq, stan::math::elt_multiply(w, w),
      "assigning variable wsq");
    current_statement__ = 304;
    if (stan::math::logical_eq(mod, 0)) {
      current_statement__ = 302;
      S = ((((sigma *
        stan::math::pow(stan::math::sqrt((2 * stan::math::pi())), D)) * phi)
        * phi) *
        stan::math::exp((-0.5 * ((phi * phi) *
          (stan::model::rvalue(wsq, "wsq", stan::model::index_uni(1)) +
          stan::model::rvalue(wsq, "wsq", stan::model::index_uni(2)))))));
    } else {
      current_statement__ = 300;
      S = (((((sigma * 4) * stan::math::pi()) * phi) * phi) *
        stan::math::pow((1 + ((phi * phi) *
          (stan::model::rvalue(wsq, "wsq", stan::model::index_uni(1)) +
          stan::model::rvalue(wsq, "wsq", stan::model::index_uni(2))))), -1.5));
    }
    current_statement__ = 305;
    return S;
  } catch (const std::exception& e) {
    stan::lang::rethrow_located(e, locations_array__[current_statement__]);
  }
}
template <typename T0__, typename T2__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_eigen_matrix_dynamic<T2__>,
                              stan::is_vt_not_complex<T2__>>*>
Eigen::Matrix<stan::promote_args_t<T0__, stan::base_type_t<T2__>>,-1,1>
phi_nD(const std::vector<T0__>& L, const std::vector<int>& m, const T2__&
       x_arg__, std::ostream* pstream__) {
  using local_scalar_t__ = stan::promote_args_t<T0__,
                             stan::base_type_t<T2__>>;
  int current_statement__ = 0;
  const auto& x = stan::math::to_ref(x_arg__);
  static constexpr bool propto__ = true;
  // suppress unused var warning
  (void) propto__;
  local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  // suppress unused var warning
  (void) DUMMY_VAR__;
  try {
    int c = std::numeric_limits<int>::min();
    current_statement__ = 307;
    c = stan::math::cols(x);
    int r = std::numeric_limits<int>::min();
    current_statement__ = 308;
    r = stan::math::rows(x);
    current_statement__ = 309;
    stan::math::validate_non_negative_index("fi", "r", r);
    current_statement__ = 310;
    stan::math::validate_non_negative_index("fi", "c", c);
    Eigen::Matrix<local_scalar_t__,-1,-1> fi =
      Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(r, c, DUMMY_VAR__);
    current_statement__ = 312;
    stan::math::validate_non_negative_index("fi1", "r", r);
    Eigen::Matrix<local_scalar_t__,-1,1> fi1 =
      Eigen::Matrix<local_scalar_t__,-1,1>::Constant(r, DUMMY_VAR__);
    current_statement__ = 316;
    for (int i = 1; i <= c; ++i) {
      current_statement__ = 314;
      stan::model::assign(fi,
        stan::math::multiply((1 /
          stan::math::sqrt(
            stan::model::rvalue(L, "L", stan::model::index_uni(i)))),
          stan::math::sin(
            stan::math::divide(
              stan::math::multiply(
                (stan::model::rvalue(m, "m", stan::model::index_uni(i)) *
                stan::math::pi()),
                stan::math::add(
                  stan::model::rvalue(x, "x", stan::model::index_omni(),
                    stan::model::index_uni(i)),
                  stan::model::rvalue(L, "L", stan::model::index_uni(i)))),
              (2 * stan::model::rvalue(L, "L", stan::model::index_uni(i)))))),
        "assigning variable fi", stan::model::index_omni(),
        stan::model::index_uni(i));
    }
    current_statement__ = 317;
    stan::model::assign(fi1,
      stan::model::rvalue(fi, "fi", stan::model::index_omni(),
        stan::model::index_uni(1)), "assigning variable fi1");
    current_statement__ = 320;
    for (int i = 2; i <= c; ++i) {
      current_statement__ = 318;
      stan::model::assign(fi1,
        stan::math::elt_multiply(stan::model::deep_copy(fi1),
          stan::model::rvalue(fi, "fi", stan::model::index_omni(),
            stan::model::index_uni(i))), "assigning variable fi1");
    }
    current_statement__ = 321;
    return fi1;
  } catch (const std::exception& e) {
    stan::lang::rethrow_located(e, locations_array__[current_statement__]);
  }
}
template <typename T0__, typename T1__, typename T4__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_stan_scalar<T1__>,
                              stan::is_stan_scalar<T4__>>*>
Eigen::Matrix<stan::promote_args_t<T0__, T1__, T4__>,-1,-1>
getAD(const T0__& alpha, const T1__& theta, const int& M, const int& n,
      const std::vector<T4__>& dists, const std::vector<std::vector<int>>&
      NN, const int& mod, std::ostream* pstream__) {
  using local_scalar_t__ = stan::promote_args_t<T0__, T1__, T4__>;
  int current_statement__ = 0;
  static constexpr bool propto__ = true;
  // suppress unused var warning
  (void) propto__;
  local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  // suppress unused var warning
  (void) DUMMY_VAR__;
  try {
    current_statement__ = 323;
    stan::math::validate_non_negative_index("AD", "M + 1", (M + 1));
    current_statement__ = 324;
    stan::math::validate_non_negative_index("AD", "n", n);
    Eigen::Matrix<local_scalar_t__,-1,-1> AD =
      Eigen::Matrix<local_scalar_t__,-1,-1>::Constant((M + 1), n,
        DUMMY_VAR__);
    current_statement__ = 325;
    stan::model::assign(AD, stan::math::rep_matrix(0, (M + 1), n),
      "assigning variable AD");
    int idxlim = std::numeric_limits<int>::min();
    int idx1 = std::numeric_limits<int>::min();
    int idx2 = std::numeric_limits<int>::min();
    local_scalar_t__ dist = DUMMY_VAR__;
    current_statement__ = 330;
    stan::model::assign(AD, alpha, "assigning variable AD",
      stan::model::index_uni((M + 1)), stan::model::index_uni(1));
    current_statement__ = 331;
    stan::math::validate_non_negative_index("smat", "M", M);
    current_statement__ = 332;
    stan::math::validate_non_negative_index("smat", "M", M);
    Eigen::Matrix<local_scalar_t__,-1,-1> smat =
      Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(M, M, DUMMY_VAR__);
    current_statement__ = 333;
    stan::model::assign(smat, stan::math::rep_matrix(0, M, M),
      "assigning variable smat");
    current_statement__ = 334;
    stan::math::validate_non_negative_index("svec", "M", M);
    Eigen::Matrix<local_scalar_t__,-1,1> svec =
      Eigen::Matrix<local_scalar_t__,-1,1>::Constant(M, DUMMY_VAR__);
    current_statement__ = 335;
    stan::model::assign(svec, stan::math::rep_vector(0, M),
      "assigning variable svec");
    int index = std::numeric_limits<int>::min();
    current_statement__ = 366;
    for (int i = 2; i <= n; ++i) {
      current_statement__ = 337;
      idxlim = (stan::math::logical_lte(i, M) ? (i - 1) : M);
      current_statement__ = 340;
      for (int j = 1; j <= idxlim; ++j) {
        current_statement__ = 338;
        stan::model::assign(smat, alpha, "assigning variable smat",
          stan::model::index_uni(j), stan::model::index_uni(j));
      }
      current_statement__ = 357;
      if (stan::math::logical_gt(idxlim, 1)) {
        current_statement__ = 355;
        for (int j = 1; j <= (idxlim - 1); ++j) {
          current_statement__ = 353;
          for (int k = (j + 1); k <= idxlim; ++k) {
            current_statement__ = 341;
            idx1 = (stan::math::logical_lt(
                      stan::model::rvalue(NN, "NN",
                        stan::model::index_uni(j), stan::model::index_uni(i)),
                      stan::model::rvalue(NN, "NN",
                        stan::model::index_uni(k), stan::model::index_uni(i))) ? 
              stan::model::rvalue(NN, "NN", stan::model::index_uni(j),
                stan::model::index_uni(i)) : stan::model::rvalue(NN, "NN",
                                               stan::model::index_uni(k),
                                               stan::model::index_uni(i)));
            current_statement__ = 342;
            idx2 = (stan::math::logical_lt(
                      stan::model::rvalue(NN, "NN",
                        stan::model::index_uni(j), stan::model::index_uni(i)),
                      stan::model::rvalue(NN, "NN",
                        stan::model::index_uni(k), stan::model::index_uni(i))) ? 
              stan::model::rvalue(NN, "NN", stan::model::index_uni(k),
                stan::model::index_uni(i)) : stan::model::rvalue(NN, "NN",
                                               stan::model::index_uni(j),
                                               stan::model::index_uni(i)));
            current_statement__ = 343;
            index = (((((n - 1) * (idx1 - 1)) - (((idx1 - 2) * (idx1 - 1)) /
              2)) + ((idx2 - idx1) - 1)) + 1);
            current_statement__ = 344;
            dist = stan::model::rvalue(dists, "dists",
                     stan::model::index_uni(index));
            current_statement__ = 351;
            if (stan::math::logical_eq(mod, 0)) {
              current_statement__ = 348;
              stan::model::assign(smat, (alpha *
                stan::math::exp(((-1.0 * (dist * dist)) / (theta * theta)))),
                "assigning variable smat", stan::model::index_uni(j),
                stan::model::index_uni(k));
              current_statement__ = 349;
              stan::model::assign(smat,
                stan::model::rvalue(smat, "smat", stan::model::index_uni(j),
                  stan::model::index_uni(k)), "assigning variable smat",
                stan::model::index_uni(k), stan::model::index_uni(j));
            } else {
              current_statement__ = 345;
              stan::model::assign(smat, (alpha *
                stan::math::exp(((-1.0 * dist) / theta))),
                "assigning variable smat", stan::model::index_uni(j),
                stan::model::index_uni(k));
              current_statement__ = 346;
              stan::model::assign(smat,
                stan::model::rvalue(smat, "smat", stan::model::index_uni(j),
                  stan::model::index_uni(k)), "assigning variable smat",
                stan::model::index_uni(k), stan::model::index_uni(j));
            }
          }
        }
      }
      current_statement__ = 362;
      for (int j = 1; j <= idxlim; ++j) {
        current_statement__ = 358;
        index = (((((n - 1) *
          (stan::model::rvalue(NN, "NN", stan::model::index_uni(j),
             stan::model::index_uni(i))
          - 1)) -
          (((stan::model::rvalue(NN, "NN", stan::model::index_uni(j),
               stan::model::index_uni(i))
          - 2) *
          (stan::model::rvalue(NN, "NN", stan::model::index_uni(j),
             stan::model::index_uni(i))
          - 1)) / 2)) + ((i -
          stan::model::rvalue(NN, "NN", stan::model::index_uni(j),
            stan::model::index_uni(i)))
          - 1)) + 1);
        current_statement__ = 359;
        dist = stan::model::rvalue(dists, "dists",
                 stan::model::index_uni(index));
        current_statement__ = 360;
        stan::model::assign(svec, (stan::math::logical_eq(mod, 0) ? (alpha *
          stan::math::exp(((-1.0 * (dist * dist)) / (theta * theta)))) : (alpha
          * stan::math::exp(((-1.0 * dist) / theta)))),
          "assigning variable svec", stan::model::index_uni(j));
      }
      current_statement__ = 363;
      stan::model::assign(AD,
        stan::math::mdivide_left_spd(
          stan::model::rvalue(smat, "smat",
            stan::model::index_min_max(1, idxlim),
            stan::model::index_min_max(1, idxlim)),
          stan::model::rvalue(svec, "svec",
            stan::model::index_min_max(1, idxlim))), "assigning variable AD",
        stan::model::index_min_max(1, idxlim), stan::model::index_uni(i));
      current_statement__ = 364;
      stan::model::assign(AD, (alpha -
        stan::math::dot_product(
          stan::model::rvalue(AD, "AD",
            stan::model::index_min_max(1, idxlim), stan::model::index_uni(i)),
          stan::model::rvalue(svec, "svec",
            stan::model::index_min_max(1, idxlim)))),
        "assigning variable AD", stan::model::index_uni((M + 1)),
        stan::model::index_uni(i));
    }
    current_statement__ = 367;
    return AD;
  } catch (const std::exception& e) {
    stan::lang::rethrow_located(e, locations_array__[current_statement__]);
  }
}
template <bool propto__, typename T0__, typename T1__,
          stan::require_all_t<stan::is_stan_scalar<T0__>,
                              stan::is_eigen_matrix_dynamic<T1__>,
                              stan::is_vt_not_complex<T1__>>*>
stan::promote_args_t<T0__, stan::base_type_t<T1__>>
nngp_split_lpdf(const std::vector<T0__>& u, const T1__& AD_arg__,
                const std::vector<std::vector<int>>& NN, const int& start,
                std::ostream* pstream__) {
  using local_scalar_t__ = stan::promote_args_t<T0__,
                             stan::base_type_t<T1__>>;
  int current_statement__ = 0;
  const auto& AD = stan::math::to_ref(AD_arg__);
  local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  // suppress unused var warning
  (void) DUMMY_VAR__;
  try {
    int n = std::numeric_limits<int>::min();
    current_statement__ = 369;
    n = stan::math::cols(AD);
    int M = std::numeric_limits<int>::min();
    current_statement__ = 370;
    M = (stan::math::rows(AD) - 1);
    local_scalar_t__ logdetD = DUMMY_VAR__;
    local_scalar_t__ qf = DUMMY_VAR__;
    local_scalar_t__ au = DUMMY_VAR__;
    local_scalar_t__ ll = DUMMY_VAR__;
    int idxlim = std::numeric_limits<int>::min();
    current_statement__ = 380;
    if (stan::math::logical_lte(start, M)) {
      current_statement__ = 378;
      idxlim = (start - 1);
    } else {
      current_statement__ = 376;
      idxlim = M;
    }
    current_statement__ = 381;
    logdetD = 0;
    current_statement__ = 384;
    for (int i = 1; i <= n; ++i) {
      current_statement__ = 382;
      logdetD = (logdetD +
        stan::math::log(
          stan::model::rvalue(AD, "AD", stan::model::index_uni((M + 1)),
            stan::model::index_uni(i))));
    }
    current_statement__ = 385;
    qf = ((stan::model::rvalue(u, "u", stan::model::index_uni(1)) *
      stan::model::rvalue(u, "u", stan::model::index_uni(1))) /
      stan::model::rvalue(AD, "AD", stan::model::index_uni((M + 1)),
        stan::model::index_uni(1)));
    current_statement__ = 392;
    for (int i = 2; i <= n; ++i) {
      current_statement__ = 386;
      au = (stan::model::rvalue(u, "u", stan::model::index_uni(i)) -
        stan::math::dot_product(
          stan::model::rvalue(AD, "AD",
            stan::model::index_min_max(1, idxlim), stan::model::index_uni(i)),
          stan::math::to_vector(
            stan::model::rvalue(u, "u",
              stan::model::index_multi(
                stan::model::rvalue(NN, "NN",
                  stan::model::index_min_max(1, idxlim),
                  stan::model::index_uni(i)))))));
      current_statement__ = 387;
      qf = (qf + ((au * au) /
        stan::model::rvalue(AD, "AD", stan::model::index_uni((M + 1)),
          stan::model::index_uni(i))));
      current_statement__ = 390;
      if (stan::math::logical_lt(idxlim, M)) {
        current_statement__ = 388;
        idxlim = (idxlim + 1);
      }
    }
    current_statement__ = 393;
    ll = (((-0.5 * logdetD) - (0.5 * qf)) - ((0.5 * n) *
      stan::math::log((2 * stan::math::pi()))));
    current_statement__ = 394;
    return ll;
  } catch (const std::exception& e) {
    stan::lang::rethrow_located(e, locations_array__[current_statement__]);
  }
}
#include <stan_meta_header.hpp>
class model_rtsbayes final : public model_base_crtp<model_rtsbayes> {
private:
  int mod;
  int approx;
  int is_region;
  int D;
  int Q;
  int Nsample;
  int n_region;
  int nT;
  std::vector<int> y;
  Eigen::Matrix<double,-1,-1> x_grid_data__;
  Eigen::Matrix<double,-1,1> popdens_data__;
  Eigen::Matrix<double,-1,-1> X_data__;
  std::vector<double> prior_lscale;
  std::vector<double> prior_var;
  std::vector<double> prior_linpred_mean;
  std::vector<double> prior_linpred_sd;
  int known_cov;
  double sigma_data;
  double phi_data;
  int n_Q;
  std::vector<int> n_cell;
  std::vector<int> cell_id;
  Eigen::Matrix<double,-1,1> q_weights_data__;
  int M;
  int M_nD;
  std::vector<std::vector<int>> indices;
  std::vector<double> L;
  std::vector<std::vector<int>> NN;
  Eigen::Matrix<double,-1,1> logpopdens_data__;
  Eigen::Matrix<double,-1,-1> L_data_data__;
  std::vector<double> dists;
  Eigen::Matrix<double,-1,-1> PHI_data__;
  std::vector<double> diagSPD_data;
  Eigen::Matrix<double,-1,-1> AD_data_data__;
  int phi_param_1dim__;
  int sigma_param_1dim__;
  int ar_1dim__;
  int f_raw_1dim__;
  int beta_1dim__;
  int beta_2dim__;
  int f_1dim__;
  int Lmat_1dim__;
  int Lmat_2dim__;
  int diagSPD_1dim__;
  int SPD_beta_1dim__;
  int AD_1dim__;
  int AD_2dim__;
  int y_grid_predict_1dim__;
  int region_predict_1dim__;
  int region_mean_predict_1dim__;
  Eigen::Map<Eigen::Matrix<double,-1,-1>> x_grid{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double,-1,1>> popdens{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double,-1,-1>> X{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double,-1,1>> q_weights{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double,-1,1>> logpopdens{nullptr, 0};
  Eigen::Map<Eigen::Matrix<double,-1,-1>> L_data{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double,-1,-1>> PHI{nullptr, 0, 0};
  Eigen::Map<Eigen::Matrix<double,-1,-1>> AD_data{nullptr, 0, 0};
public:
  ~model_rtsbayes() {}
  model_rtsbayes(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_rtsbayes_namespace::model_rtsbayes";
    // 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__ = 147;
      context__.validate_dims("data initialization", "mod", "int",
        std::vector<size_t>{});
      mod = std::numeric_limits<int>::min();
      current_statement__ = 147;
      mod = context__.vals_i("mod")[(1 - 1)];
      current_statement__ = 148;
      context__.validate_dims("data initialization", "approx", "int",
        std::vector<size_t>{});
      approx = std::numeric_limits<int>::min();
      current_statement__ = 148;
      approx = context__.vals_i("approx")[(1 - 1)];
      current_statement__ = 148;
      stan::math::check_greater_or_equal(function__, "approx", approx, 0);
      current_statement__ = 148;
      stan::math::check_less_or_equal(function__, "approx", approx, 2);
      current_statement__ = 149;
      context__.validate_dims("data initialization", "is_region", "int",
        std::vector<size_t>{});
      is_region = std::numeric_limits<int>::min();
      current_statement__ = 149;
      is_region = context__.vals_i("is_region")[(1 - 1)];
      current_statement__ = 150;
      context__.validate_dims("data initialization", "D", "int",
        std::vector<size_t>{});
      D = std::numeric_limits<int>::min();
      current_statement__ = 150;
      D = context__.vals_i("D")[(1 - 1)];
      current_statement__ = 150;
      stan::math::check_greater_or_equal(function__, "D", D, 1);
      current_statement__ = 151;
      context__.validate_dims("data initialization", "Q", "int",
        std::vector<size_t>{});
      Q = std::numeric_limits<int>::min();
      current_statement__ = 151;
      Q = context__.vals_i("Q")[(1 - 1)];
      current_statement__ = 151;
      stan::math::check_greater_or_equal(function__, "Q", Q, 1);
      current_statement__ = 152;
      context__.validate_dims("data initialization", "Nsample", "int",
        std::vector<size_t>{});
      Nsample = std::numeric_limits<int>::min();
      current_statement__ = 152;
      Nsample = context__.vals_i("Nsample")[(1 - 1)];
      current_statement__ = 152;
      stan::math::check_greater_or_equal(function__, "Nsample", Nsample, 1);
      current_statement__ = 153;
      context__.validate_dims("data initialization", "n_region", "int",
        std::vector<size_t>{});
      n_region = std::numeric_limits<int>::min();
      current_statement__ = 153;
      n_region = context__.vals_i("n_region")[(1 - 1)];
      current_statement__ = 153;
      stan::math::check_greater_or_equal(function__, "n_region", n_region, 1);
      current_statement__ = 154;
      context__.validate_dims("data initialization", "nT", "int",
        std::vector<size_t>{});
      nT = std::numeric_limits<int>::min();
      current_statement__ = 154;
      nT = context__.vals_i("nT")[(1 - 1)];
      current_statement__ = 155;
      stan::math::validate_non_negative_index("y",
        "is_region ? n_region * nT : Nsample * nT", (is_region ? (n_region *
        nT) : (Nsample * nT)));
      current_statement__ = 156;
      context__.validate_dims("data initialization", "y", "int",
        std::vector<size_t>{static_cast<size_t>((is_region ? (n_region *
                              nT) : (Nsample * nT)))});
      y = std::vector<int>((is_region ? (n_region * nT) : (Nsample * nT)),
            std::numeric_limits<int>::min());
      current_statement__ = 156;
      y = context__.vals_i("y");
      current_statement__ = 157;
      stan::math::validate_non_negative_index("x_grid", "Nsample", Nsample);
      current_statement__ = 158;
      stan::math::validate_non_negative_index("x_grid", "D", D);
      current_statement__ = 159;
      context__.validate_dims("data initialization", "x_grid", "double",
        std::vector<size_t>{static_cast<size_t>(Nsample),
          static_cast<size_t>(D)});
      x_grid_data__ = Eigen::Matrix<double,-1,-1>::Constant(Nsample, D,
                        std::numeric_limits<double>::quiet_NaN());
      new (&x_grid)
        Eigen::Map<Eigen::Matrix<double,-1,-1>>(x_grid_data__.data(),
        Nsample, D);
      {
        std::vector<local_scalar_t__> x_grid_flat__;
        current_statement__ = 159;
        x_grid_flat__ = context__.vals_r("x_grid");
        current_statement__ = 159;
        pos__ = 1;
        current_statement__ = 159;
        for (int sym1__ = 1; sym1__ <= D; ++sym1__) {
          current_statement__ = 159;
          for (int sym2__ = 1; sym2__ <= Nsample; ++sym2__) {
            current_statement__ = 159;
            stan::model::assign(x_grid, x_grid_flat__[(pos__ - 1)],
              "assigning variable x_grid", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 159;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 160;
      stan::math::validate_non_negative_index("popdens", "Nsample * nT",
        (Nsample * nT));
      current_statement__ = 161;
      context__.validate_dims("data initialization", "popdens", "double",
        std::vector<size_t>{static_cast<size_t>((Nsample * nT))});
      popdens_data__ = Eigen::Matrix<double,-1,1>::Constant((Nsample * nT),
                         std::numeric_limits<double>::quiet_NaN());
      new (&popdens)
        Eigen::Map<Eigen::Matrix<double,-1,1>>(popdens_data__.data(),
        (Nsample * nT));
      {
        std::vector<local_scalar_t__> popdens_flat__;
        current_statement__ = 161;
        popdens_flat__ = context__.vals_r("popdens");
        current_statement__ = 161;
        pos__ = 1;
        current_statement__ = 161;
        for (int sym1__ = 1; sym1__ <= (Nsample * nT); ++sym1__) {
          current_statement__ = 161;
          stan::model::assign(popdens, popdens_flat__[(pos__ - 1)],
            "assigning variable popdens", stan::model::index_uni(sym1__));
          current_statement__ = 161;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 162;
      stan::math::validate_non_negative_index("X", "Nsample * nT", (Nsample *
        nT));
      current_statement__ = 163;
      stan::math::validate_non_negative_index("X", "Q", Q);
      current_statement__ = 164;
      context__.validate_dims("data initialization", "X", "double",
        std::vector<size_t>{static_cast<size_t>((Nsample * nT)),
          static_cast<size_t>(Q)});
      X_data__ = Eigen::Matrix<double,-1,-1>::Constant((Nsample * nT), Q,
                   std::numeric_limits<double>::quiet_NaN());
      new (&X) Eigen::Map<Eigen::Matrix<double,-1,-1>>(X_data__.data(),
        (Nsample * nT), Q);
      {
        std::vector<local_scalar_t__> X_flat__;
        current_statement__ = 164;
        X_flat__ = context__.vals_r("X");
        current_statement__ = 164;
        pos__ = 1;
        current_statement__ = 164;
        for (int sym1__ = 1; sym1__ <= Q; ++sym1__) {
          current_statement__ = 164;
          for (int sym2__ = 1; sym2__ <= (Nsample * nT); ++sym2__) {
            current_statement__ = 164;
            stan::model::assign(X, X_flat__[(pos__ - 1)],
              "assigning variable X", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 164;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 165;
      context__.validate_dims("data initialization", "prior_lscale",
        "double", std::vector<size_t>{static_cast<size_t>(2)});
      prior_lscale = std::vector<double>(2,
                       std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 165;
      prior_lscale = context__.vals_r("prior_lscale");
      current_statement__ = 166;
      context__.validate_dims("data initialization", "prior_var", "double",
        std::vector<size_t>{static_cast<size_t>(2)});
      prior_var = std::vector<double>(2,
                    std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 166;
      prior_var = context__.vals_r("prior_var");
      current_statement__ = 167;
      stan::math::validate_non_negative_index("prior_linpred_mean", "Q", Q);
      current_statement__ = 168;
      context__.validate_dims("data initialization", "prior_linpred_mean",
        "double", std::vector<size_t>{static_cast<size_t>(Q)});
      prior_linpred_mean = std::vector<double>(Q,
                             std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 168;
      prior_linpred_mean = context__.vals_r("prior_linpred_mean");
      current_statement__ = 169;
      stan::math::validate_non_negative_index("prior_linpred_sd", "Q", Q);
      current_statement__ = 170;
      context__.validate_dims("data initialization", "prior_linpred_sd",
        "double", std::vector<size_t>{static_cast<size_t>(Q)});
      prior_linpred_sd = std::vector<double>(Q,
                           std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 170;
      prior_linpred_sd = context__.vals_r("prior_linpred_sd");
      current_statement__ = 171;
      context__.validate_dims("data initialization", "known_cov", "int",
        std::vector<size_t>{});
      known_cov = std::numeric_limits<int>::min();
      current_statement__ = 171;
      known_cov = context__.vals_i("known_cov")[(1 - 1)];
      current_statement__ = 171;
      stan::math::check_greater_or_equal(function__, "known_cov", known_cov,
        0);
      current_statement__ = 171;
      stan::math::check_less_or_equal(function__, "known_cov", known_cov, 1);
      current_statement__ = 172;
      context__.validate_dims("data initialization", "sigma_data", "double",
        std::vector<size_t>{});
      sigma_data = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 172;
      sigma_data = context__.vals_r("sigma_data")[(1 - 1)];
      current_statement__ = 172;
      stan::math::check_greater_or_equal(function__, "sigma_data",
        sigma_data, 0);
      current_statement__ = 173;
      context__.validate_dims("data initialization", "phi_data", "double",
        std::vector<size_t>{});
      phi_data = std::numeric_limits<double>::quiet_NaN();
      current_statement__ = 173;
      phi_data = context__.vals_r("phi_data")[(1 - 1)];
      current_statement__ = 173;
      stan::math::check_greater_or_equal(function__, "phi_data", phi_data, 0);
      current_statement__ = 174;
      context__.validate_dims("data initialization", "n_Q", "int",
        std::vector<size_t>{});
      n_Q = std::numeric_limits<int>::min();
      current_statement__ = 174;
      n_Q = context__.vals_i("n_Q")[(1 - 1)];
      current_statement__ = 175;
      stan::math::validate_non_negative_index("n_cell",
        "is_region ? n_region + 1 : 1", (is_region ? (n_region + 1) : 1));
      current_statement__ = 176;
      context__.validate_dims("data initialization", "n_cell", "int",
        std::vector<size_t>{static_cast<size_t>((is_region ? (n_region +
                              1) : 1))});
      n_cell = std::vector<int>((is_region ? (n_region + 1) : 1),
                 std::numeric_limits<int>::min());
      current_statement__ = 176;
      n_cell = context__.vals_i("n_cell");
      current_statement__ = 176;
      stan::math::check_greater_or_equal(function__, "n_cell", n_cell, 1);
      current_statement__ = 177;
      stan::math::validate_non_negative_index("cell_id",
        "is_region ? n_Q : 1", (is_region ? n_Q : 1));
      current_statement__ = 178;
      context__.validate_dims("data initialization", "cell_id", "int",
        std::vector<size_t>{static_cast<size_t>((is_region ? n_Q : 1))});
      cell_id = std::vector<int>((is_region ? n_Q : 1),
                  std::numeric_limits<int>::min());
      current_statement__ = 178;
      cell_id = context__.vals_i("cell_id");
      current_statement__ = 178;
      stan::math::check_greater_or_equal(function__, "cell_id", cell_id, 1);
      current_statement__ = 179;
      stan::math::validate_non_negative_index("q_weights",
        "is_region ? n_Q : 1", (is_region ? n_Q : 1));
      current_statement__ = 180;
      context__.validate_dims("data initialization", "q_weights", "double",
        std::vector<size_t>{static_cast<size_t>((is_region ? n_Q : 1))});
      q_weights_data__ = Eigen::Matrix<double,-1,1>::Constant((is_region ? n_Q : 1),
                           std::numeric_limits<double>::quiet_NaN());
      new (&q_weights)
        Eigen::Map<Eigen::Matrix<double,-1,1>>(q_weights_data__.data(),
        (is_region ? n_Q : 1));
      {
        std::vector<local_scalar_t__> q_weights_flat__;
        current_statement__ = 180;
        q_weights_flat__ = context__.vals_r("q_weights");
        current_statement__ = 180;
        pos__ = 1;
        current_statement__ = 180;
        for (int sym1__ = 1; sym1__ <= (is_region ? n_Q : 1); ++sym1__) {
          current_statement__ = 180;
          stan::model::assign(q_weights, q_weights_flat__[(pos__ - 1)],
            "assigning variable q_weights", stan::model::index_uni(sym1__));
          current_statement__ = 180;
          pos__ = (pos__ + 1);
        }
      }
      current_statement__ = 181;
      context__.validate_dims("data initialization", "M", "int",
        std::vector<size_t>{});
      M = std::numeric_limits<int>::min();
      current_statement__ = 181;
      M = context__.vals_i("M")[(1 - 1)];
      current_statement__ = 181;
      stan::math::check_greater_or_equal(function__, "M", M, 1);
      current_statement__ = 182;
      context__.validate_dims("data initialization", "M_nD", "int",
        std::vector<size_t>{});
      M_nD = std::numeric_limits<int>::min();
      current_statement__ = 182;
      M_nD = context__.vals_i("M_nD")[(1 - 1)];
      current_statement__ = 182;
      stan::math::check_greater_or_equal(function__, "M_nD", M_nD, 1);
      current_statement__ = 183;
      stan::math::validate_non_negative_index("indices",
        "approx == 1 ? M_nD : 1",
        (stan::math::logical_eq(approx, 1) ? M_nD : 1));
      current_statement__ = 184;
      stan::math::validate_non_negative_index("indices",
        "approx == 1 ? D : 1", (stan::math::logical_eq(approx, 1) ? D : 1));
      current_statement__ = 185;
      context__.validate_dims("data initialization", "indices", "int",
        std::vector<size_t>{static_cast<size_t>(
                              (stan::math::logical_eq(approx, 1) ? M_nD : 1)),
          static_cast<size_t>((stan::math::logical_eq(approx, 1) ? D : 1))});
      indices = std::vector<std::vector<int>>((stan::math::logical_eq(approx,
                                                 1) ? M_nD : 1),
                  std::vector<int>((stan::math::logical_eq(approx, 1) ? D : 1),
                    std::numeric_limits<int>::min()));
      {
        std::vector<int> indices_flat__;
        current_statement__ = 185;
        indices_flat__ = context__.vals_i("indices");
        current_statement__ = 185;
        pos__ = 1;
        current_statement__ = 185;
        for (int sym1__ = 1; sym1__ <=
             (stan::math::logical_eq(approx, 1) ? D : 1); ++sym1__) {
          current_statement__ = 185;
          for (int sym2__ = 1; sym2__ <=
               (stan::math::logical_eq(approx, 1) ? M_nD : 1); ++sym2__) {
            current_statement__ = 185;
            stan::model::assign(indices, indices_flat__[(pos__ - 1)],
              "assigning variable indices", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 185;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 186;
      stan::math::validate_non_negative_index("L", "D", D);
      current_statement__ = 187;
      context__.validate_dims("data initialization", "L", "double",
        std::vector<size_t>{static_cast<size_t>(D)});
      L = std::vector<double>(D, std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 187;
      L = context__.vals_r("L");
      current_statement__ = 188;
      stan::math::validate_non_negative_index("NN", "approx == 2 ? M : 1",
        (stan::math::logical_eq(approx, 2) ? M : 1));
      current_statement__ = 189;
      stan::math::validate_non_negative_index("NN",
        "approx == 2 ? Nsample : 1",
        (stan::math::logical_eq(approx, 2) ? Nsample : 1));
      current_statement__ = 190;
      context__.validate_dims("data initialization", "NN", "int",
        std::vector<size_t>{static_cast<size_t>(
                              (stan::math::logical_eq(approx, 2) ? M : 1)),
          static_cast<size_t>(
            (stan::math::logical_eq(approx, 2) ? Nsample : 1))});
      NN = std::vector<std::vector<int>>((stan::math::logical_eq(approx, 2) ? M : 1),
             std::vector<int>((stan::math::logical_eq(approx, 2) ? Nsample : 1),
               std::numeric_limits<int>::min()));
      {
        std::vector<int> NN_flat__;
        current_statement__ = 190;
        NN_flat__ = context__.vals_i("NN");
        current_statement__ = 190;
        pos__ = 1;
        current_statement__ = 190;
        for (int sym1__ = 1; sym1__ <=
             (stan::math::logical_eq(approx, 2) ? Nsample : 1); ++sym1__) {
          current_statement__ = 190;
          for (int sym2__ = 1; sym2__ <=
               (stan::math::logical_eq(approx, 2) ? M : 1); ++sym2__) {
            current_statement__ = 190;
            stan::model::assign(NN, NN_flat__[(pos__ - 1)],
              "assigning variable NN", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 190;
            pos__ = (pos__ + 1);
          }
        }
      }
      current_statement__ = 191;
      stan::math::validate_non_negative_index("logpopdens", "Nsample * nT",
        (Nsample * nT));
      current_statement__ = 192;
      logpopdens_data__ = Eigen::Matrix<double,-1,1>::Constant((Nsample *
                            nT), std::numeric_limits<double>::quiet_NaN());
      new (&logpopdens)
        Eigen::Map<Eigen::Matrix<double,-1,1>>(logpopdens_data__.data(),
        (Nsample * nT));
      current_statement__ = 192;
      stan::model::assign(logpopdens, stan::math::log(popdens),
        "assigning variable logpopdens");
      current_statement__ = 193;
      stan::math::validate_non_negative_index("L_data",
        "known_cov && approx == 0 ? Nsample : 0",
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 0))) ? Nsample : 0));
      current_statement__ = 194;
      stan::math::validate_non_negative_index("L_data",
        "known_cov && approx == 0 ? Nsample : 0",
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 0))) ? Nsample : 0));
      current_statement__ = 195;
      L_data_data__ = Eigen::Matrix<double,-1,-1>::Constant(((stan::math::primitive_value(
                                                                known_cov) &&
                        stan::math::primitive_value(
                          stan::math::logical_eq(approx, 0))) ? Nsample : 0),
                        ((stan::math::primitive_value(known_cov) &&
                        stan::math::primitive_value(
                          stan::math::logical_eq(approx, 0))) ? Nsample : 0),
                        std::numeric_limits<double>::quiet_NaN());
      new (&L_data)
        Eigen::Map<Eigen::Matrix<double,-1,-1>>(L_data_data__.data(),
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 0))) ? Nsample : 0),
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 0))) ? Nsample : 0));
      current_statement__ = 196;
      stan::math::validate_non_negative_index("dists",
        "approx != 1 ? (Nsample * (Nsample - 1)) %/% 2 : 0",
        (stan::math::logical_neq(approx, 1) ? ((Nsample * (Nsample - 1)) /
        2) : 0));
      current_statement__ = 197;
      dists = std::vector<double>((stan::math::logical_neq(approx, 1) ? ((Nsample
                * (Nsample - 1)) / 2) : 0),
                std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 198;
      stan::math::validate_non_negative_index("PHI",
        "approx == 1 ? Nsample : 0",
        (stan::math::logical_eq(approx, 1) ? Nsample : 0));
      current_statement__ = 199;
      stan::math::validate_non_negative_index("PHI",
        "approx == 1 ? M_nD : 0",
        (stan::math::logical_eq(approx, 1) ? M_nD : 0));
      current_statement__ = 200;
      PHI_data__ = Eigen::Matrix<double,-1,-1>::Constant((stan::math::logical_eq(
                                                            approx, 1) ? Nsample : 0),
                     (stan::math::logical_eq(approx, 1) ? M_nD : 0),
                     std::numeric_limits<double>::quiet_NaN());
      new (&PHI) Eigen::Map<Eigen::Matrix<double,-1,-1>>(PHI_data__.data(),
        (stan::math::logical_eq(approx, 1) ? Nsample : 0),
        (stan::math::logical_eq(approx, 1) ? M_nD : 0));
      current_statement__ = 201;
      stan::math::validate_non_negative_index("diagSPD_data",
        "known_cov && approx == 1 ? M_nD : 0",
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 1))) ? M_nD : 0));
      current_statement__ = 202;
      diagSPD_data = std::vector<double>(((stan::math::primitive_value(
                                             known_cov) &&
                       stan::math::primitive_value(
                         stan::math::logical_eq(approx, 1))) ? M_nD : 0),
                       std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 203;
      stan::math::validate_non_negative_index("AD_data",
        "known_cov && approx == 2 ? M + 1 : 0",
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 2))) ? (M
        + 1) : 0));
      current_statement__ = 204;
      stan::math::validate_non_negative_index("AD_data",
        "known_cov && approx == 2 ? Nsample : 0",
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 2))) ? Nsample : 0));
      current_statement__ = 205;
      AD_data_data__ = Eigen::Matrix<double,-1,-1>::Constant(((stan::math::primitive_value(
                                                                 known_cov)
                         &&
                         stan::math::primitive_value(
                           stan::math::logical_eq(approx, 2))) ? (M +
                         1) : 0), ((stan::math::primitive_value(known_cov) &&
                         stan::math::primitive_value(
                           stan::math::logical_eq(approx, 2))) ? Nsample : 0),
                         std::numeric_limits<double>::quiet_NaN());
      new (&AD_data)
        Eigen::Map<Eigen::Matrix<double,-1,-1>>(AD_data_data__.data(),
        ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 2))) ? (M
        + 1) : 0), ((stan::math::primitive_value(known_cov) &&
        stan::math::primitive_value(stan::math::logical_eq(approx, 2))) ? Nsample : 0));
      current_statement__ = 212;
      if ((stan::math::primitive_value(stan::math::logical_eq(approx, 0)) ||
          stan::math::primitive_value(stan::math::logical_eq(approx, 2)))) {
        current_statement__ = 210;
        for (int i = 1; i <= (Nsample - 1); ++i) {
          current_statement__ = 208;
          for (int j = (i + 1); j <= Nsample; ++j) {
            current_statement__ = 206;
            stan::model::assign(dists,
              stan::math::sqrt(
                (((stan::model::rvalue(x_grid, "x_grid",
                     stan::model::index_uni(i), stan::model::index_uni(1)) -
                stan::model::rvalue(x_grid, "x_grid",
                  stan::model::index_uni(j), stan::model::index_uni(1))) *
                (stan::model::rvalue(x_grid, "x_grid",
                   stan::model::index_uni(i), stan::model::index_uni(1)) -
                stan::model::rvalue(x_grid, "x_grid",
                  stan::model::index_uni(j), stan::model::index_uni(1)))) +
                ((stan::model::rvalue(x_grid, "x_grid",
                    stan::model::index_uni(i), stan::model::index_uni(2)) -
                stan::model::rvalue(x_grid, "x_grid",
                  stan::model::index_uni(j), stan::model::index_uni(2))) *
                (stan::model::rvalue(x_grid, "x_grid",
                   stan::model::index_uni(i), stan::model::index_uni(2)) -
                stan::model::rvalue(x_grid, "x_grid",
                  stan::model::index_uni(j), stan::model::index_uni(2)))))),
              "assigning variable dists",
              stan::model::index_uni((((((Nsample - 1) * (i - 1)) - (((i - 2)
                * (i - 1)) / 2)) + ((j - i) - 1)) + 1)));
          }
        }
      }
      current_statement__ = 232;
      if (stan::math::logical_eq(approx, 0)) {
        current_statement__ = 230;
        if (known_cov) {
          current_statement__ = 228;
          stan::model::assign(L_data,
            genChol(Nsample, sigma_data, phi_data, dists, mod, pstream__),
            "assigning variable L_data");
        }
      } else {
        current_statement__ = 227;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 220;
          for (int m = 1; m <= M_nD; ++m) {
            current_statement__ = 218;
            stan::model::assign(PHI,
              phi_nD(L,
                stan::model::rvalue(indices, "indices",
                  stan::model::index_uni(m), stan::model::index_omni()),
                x_grid, pstream__), "assigning variable PHI",
              stan::model::index_omni(), stan::model::index_uni(m));
          }
          current_statement__ = 225;
          if (known_cov) {
            current_statement__ = 223;
            for (int m = 1; m <= M_nD; ++m) {
              current_statement__ = 221;
              stan::model::assign(diagSPD_data,
                stan::math::sqrt(
                  spd_nD(sigma_data, phi_data,
                    stan::math::sqrt(
                      lambda_nD(L,
                        stan::model::rvalue(indices, "indices",
                          stan::model::index_uni(m),
                          stan::model::index_omni()), D, pstream__)), D, mod,
                    pstream__)), "assigning variable diagSPD_data",
                stan::model::index_uni(m));
            }
          }
        } else {
          current_statement__ = 217;
          if (stan::math::logical_eq(approx, 2)) {
            current_statement__ = 215;
            if (known_cov) {
              current_statement__ = 213;
              stan::model::assign(AD_data,
                getAD(sigma_data, phi_data, M, Nsample, dists, NN, mod,
                  pstream__), "assigning variable AD_data");
            }
          }
        }
      }
      current_statement__ = 233;
      phi_param_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 233;
      phi_param_1dim__ = (known_cov ? 0 : 1);
      current_statement__ = 233;
      stan::math::validate_non_negative_index("phi_param",
        "known_cov ? 0 : 1", phi_param_1dim__);
      current_statement__ = 234;
      sigma_param_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 234;
      sigma_param_1dim__ = (known_cov ? 0 : 1);
      current_statement__ = 234;
      stan::math::validate_non_negative_index("sigma_param",
        "known_cov ? 0 : 1", sigma_param_1dim__);
      current_statement__ = 235;
      stan::math::validate_non_negative_index("gamma", "Q", Q);
      current_statement__ = 236;
      ar_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 236;
      ar_1dim__ = (stan::math::logical_gt(nT, 1) ? 1 : 0);
      current_statement__ = 236;
      stan::math::validate_non_negative_index("ar", "nT > 1 ? 1 : 0",
        ar_1dim__);
      current_statement__ = 237;
      f_raw_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 237;
      f_raw_1dim__ = ((stan::math::primitive_value(
                         stan::math::logical_eq(approx, 0))
        ||
        stan::math::primitive_value(stan::math::logical_eq(approx, 2))) ? (Nsample
        * nT) : 2);
      current_statement__ = 237;
      stan::math::validate_non_negative_index("f_raw",
        "(approx == 0 || approx == 2) ? Nsample * nT : 2", f_raw_1dim__);
      current_statement__ = 238;
      beta_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 238;
      beta_1dim__ = (stan::math::logical_eq(approx, 1) ? M_nD : 0);
      current_statement__ = 238;
      stan::math::validate_non_negative_index("beta",
        "approx == 1 ? M_nD : 0", beta_1dim__);
      current_statement__ = 239;
      beta_2dim__ = std::numeric_limits<int>::min();
      current_statement__ = 239;
      beta_2dim__ = (stan::math::logical_eq(approx, 1) ? nT : 0);
      current_statement__ = 239;
      stan::math::validate_non_negative_index("beta", "approx == 1 ? nT : 0",
        beta_2dim__);
      current_statement__ = 240;
      f_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 240;
      f_1dim__ = (Nsample * nT);
      current_statement__ = 240;
      stan::math::validate_non_negative_index("f", "Nsample * nT", f_1dim__);
      current_statement__ = 241;
      Lmat_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 241;
      Lmat_1dim__ = (stan::math::logical_eq(approx, 0) ? Nsample : 0);
      current_statement__ = 241;
      stan::math::validate_non_negative_index("Lmat",
        "approx == 0 ? Nsample : 0", Lmat_1dim__);
      current_statement__ = 242;
      Lmat_2dim__ = std::numeric_limits<int>::min();
      current_statement__ = 242;
      Lmat_2dim__ = (stan::math::logical_eq(approx, 0) ? Nsample : 0);
      current_statement__ = 242;
      stan::math::validate_non_negative_index("Lmat",
        "approx == 0 ? Nsample : 0", Lmat_2dim__);
      current_statement__ = 243;
      diagSPD_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 243;
      diagSPD_1dim__ = (stan::math::logical_eq(approx, 1) ? M_nD : 0);
      current_statement__ = 243;
      stan::math::validate_non_negative_index("diagSPD",
        "approx == 1 ? M_nD : 0", diagSPD_1dim__);
      current_statement__ = 244;
      SPD_beta_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 244;
      SPD_beta_1dim__ = (stan::math::logical_eq(approx, 1) ? M_nD : 0);
      current_statement__ = 244;
      stan::math::validate_non_negative_index("SPD_beta",
        "approx == 1 ? M_nD : 0", SPD_beta_1dim__);
      current_statement__ = 245;
      AD_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 245;
      AD_1dim__ = (stan::math::logical_eq(approx, 2) ? (M + 1) : 0);
      current_statement__ = 245;
      stan::math::validate_non_negative_index("AD",
        "approx == 2 ? M + 1 : 0", AD_1dim__);
      current_statement__ = 246;
      AD_2dim__ = std::numeric_limits<int>::min();
      current_statement__ = 246;
      AD_2dim__ = (stan::math::logical_eq(approx, 2) ? Nsample : 0);
      current_statement__ = 246;
      stan::math::validate_non_negative_index("AD",
        "approx == 2 ? Nsample : 0", AD_2dim__);
      current_statement__ = 247;
      y_grid_predict_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 247;
      y_grid_predict_1dim__ = (Nsample * nT);
      current_statement__ = 247;
      stan::math::validate_non_negative_index("y_grid_predict",
        "Nsample * nT", y_grid_predict_1dim__);
      current_statement__ = 248;
      region_predict_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 248;
      region_predict_1dim__ = (is_region ? (n_region * nT) : 0);
      current_statement__ = 248;
      stan::math::validate_non_negative_index("region_predict",
        "is_region ? n_region * nT : 0", region_predict_1dim__);
      current_statement__ = 249;
      region_mean_predict_1dim__ = std::numeric_limits<int>::min();
      current_statement__ = 249;
      region_mean_predict_1dim__ = (is_region ? (Nsample * nT) : 0);
      current_statement__ = 249;
      stan::math::validate_non_negative_index("region_mean_predict",
        "is_region ? Nsample * nT : 0", region_mean_predict_1dim__);
    } catch (const std::exception& e) {
      stan::lang::rethrow_located(e, locations_array__[current_statement__]);
    }
    num_params_r__ = phi_param_1dim__ + sigma_param_1dim__ + Q + ar_1dim__ +
      f_raw_1dim__ + (beta_1dim__ * beta_2dim__);
  }
  inline std::string model_name() const final {
    return "model_rtsbayes";
  }
  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_rtsbayes_namespace::log_prob";
    // suppress unused var warning
    (void) function__;
    try {
      std::vector<local_scalar_t__> phi_param =
        std::vector<local_scalar_t__>(phi_param_1dim__, DUMMY_VAR__);
      current_statement__ = 1;
      phi_param = in__.template read_constrain_lb<
                    std::vector<local_scalar_t__>, jacobian__>(1e-05, lp__,
                    phi_param_1dim__);
      std::vector<local_scalar_t__> sigma_param =
        std::vector<local_scalar_t__>(sigma_param_1dim__, DUMMY_VAR__);
      current_statement__ = 2;
      sigma_param = in__.template read_constrain_lb<
                      std::vector<local_scalar_t__>, jacobian__>(1e-05, lp__,
                      sigma_param_1dim__);
      Eigen::Matrix<local_scalar_t__,-1,1> gamma =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(Q, DUMMY_VAR__);
      current_statement__ = 3;
      gamma = in__.template read<Eigen::Matrix<local_scalar_t__,-1,1>>(Q);
      std::vector<local_scalar_t__> ar =
        std::vector<local_scalar_t__>(ar_1dim__, DUMMY_VAR__);
      current_statement__ = 4;
      ar = in__.template read_constrain_lub<std::vector<local_scalar_t__>,
             jacobian__>(-1, 1, lp__, ar_1dim__);
      std::vector<local_scalar_t__> f_raw =
        std::vector<local_scalar_t__>(f_raw_1dim__, DUMMY_VAR__);
      current_statement__ = 5;
      f_raw = in__.template read<std::vector<local_scalar_t__>>(f_raw_1dim__);
      Eigen::Matrix<local_scalar_t__,-1,-1> beta =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(beta_1dim__,
          beta_2dim__, DUMMY_VAR__);
      current_statement__ = 6;
      beta = in__.template read<
               Eigen::Matrix<local_scalar_t__,-1,-1>>(beta_1dim__,
               beta_2dim__);
      Eigen::Matrix<local_scalar_t__,-1,1> f =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(f_1dim__, DUMMY_VAR__);
      local_scalar_t__ sigma = DUMMY_VAR__;
      local_scalar_t__ phi = DUMMY_VAR__;
      Eigen::Matrix<local_scalar_t__,-1,-1> Lmat =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(Lmat_1dim__,
          Lmat_2dim__, DUMMY_VAR__);
      Eigen::Matrix<local_scalar_t__,-1,1> diagSPD =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(diagSPD_1dim__,
          DUMMY_VAR__);
      Eigen::Matrix<local_scalar_t__,-1,1> SPD_beta =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(SPD_beta_1dim__,
          DUMMY_VAR__);
      Eigen::Matrix<local_scalar_t__,-1,-1> AD =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(AD_1dim__, AD_2dim__,
          DUMMY_VAR__);
      current_statement__ = 35;
      if (known_cov) {
        current_statement__ = 28;
        sigma = sigma_data;
        current_statement__ = 29;
        phi = phi_data;
        current_statement__ = 31;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 30;
          stan::model::assign(diagSPD, stan::math::to_vector(diagSPD_data),
            "assigning variable diagSPD");
        }
        current_statement__ = 33;
        if (stan::math::logical_eq(approx, 2)) {
          current_statement__ = 32;
          stan::model::assign(AD, AD_data, "assigning variable AD");
        }
      } else {
        current_statement__ = 17;
        sigma = stan::model::rvalue(sigma_param, "sigma_param",
                  stan::model::index_uni(1));
        current_statement__ = 18;
        phi = stan::model::rvalue(phi_param, "phi_param",
                stan::model::index_uni(1));
        current_statement__ = 23;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 21;
          for (int m = 1; m <= M_nD; ++m) {
            current_statement__ = 19;
            stan::model::assign(diagSPD,
              stan::math::sqrt(
                spd_nD(sigma, phi,
                  stan::math::sqrt(
                    lambda_nD(L,
                      stan::model::rvalue(indices, "indices",
                        stan::model::index_uni(m), stan::model::index_omni()),
                      D, pstream__)), D, mod, pstream__)),
              "assigning variable diagSPD", stan::model::index_uni(m));
          }
        }
        current_statement__ = 26;
        if (stan::math::logical_eq(approx, 2)) {
          current_statement__ = 24;
          stan::model::assign(AD,
            getAD(sigma, phi, M, Nsample, dists, NN, mod, pstream__),
            "assigning variable AD");
        }
      }
      current_statement__ = 80;
      if (stan::math::logical_eq(approx, 0)) {
        current_statement__ = 67;
        if (stan::math::logical_negation(known_cov)) {
          current_statement__ = 65;
          stan::model::assign(Lmat,
            genChol(Nsample, sigma, phi, dists, mod, pstream__),
            "assigning variable Lmat");
        } else {
          current_statement__ = 63;
          stan::model::assign(Lmat, L_data, "assigning variable Lmat");
        }
        current_statement__ = 78;
        for (int t = 1; t <= nT; ++t) {
          current_statement__ = 76;
          if (stan::math::logical_gt(nT, 1)) {
            current_statement__ = 74;
            if (stan::math::logical_eq(t, 1)) {
              current_statement__ = 72;
              stan::model::assign(f,
                stan::math::multiply(
                  stan::math::multiply((1 / (1 -
                    stan::math::pow(
                      stan::model::rvalue(ar, "ar", stan::model::index_uni(1)),
                      2))), Lmat),
                  stan::math::to_vector(
                    stan::model::rvalue(f_raw, "f_raw",
                      stan::model::index_min_max(1, Nsample)))),
                "assigning variable f",
                stan::model::index_min_max(1, Nsample));
            } else {
              current_statement__ = 70;
              stan::model::assign(f,
                stan::math::add(
                  stan::math::multiply(
                    stan::math::multiply(
                      stan::model::rvalue(ar, "ar", stan::model::index_uni(1)),
                      Lmat),
                    stan::model::deep_copy(
                      stan::model::rvalue(f, "f",
                        stan::model::index_min_max(((Nsample * (t - 2)) + 1),
                          ((t - 1) * Nsample))))),
                  stan::math::multiply(Lmat,
                    stan::math::to_vector(
                      stan::model::rvalue(f_raw, "f_raw",
                        stan::model::index_min_max(((Nsample * (t - 1)) + 1),
                          (t * Nsample)))))), "assigning variable f",
                stan::model::index_min_max(((Nsample * (t - 1)) + 1), (t *
                  Nsample)));
            }
          } else {
            current_statement__ = 68;
            stan::model::assign(f,
              stan::math::multiply(Lmat, stan::math::to_vector(f_raw)),
              "assigning variable f");
          }
        }
      } else {
        current_statement__ = 62;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 60;
          for (int t = 1; t <= nT; ++t) {
            current_statement__ = 49;
            stan::model::assign(SPD_beta,
              stan::math::elt_multiply(diagSPD,
                stan::model::rvalue(beta, "beta", stan::model::index_omni(),
                  stan::model::index_uni(t))), "assigning variable SPD_beta");
            current_statement__ = 58;
            if (stan::math::logical_gt(nT, 1)) {
              current_statement__ = 56;
              if (stan::math::logical_eq(t, 1)) {
                current_statement__ = 54;
                stan::model::assign(f,
                  stan::math::multiply(
                    stan::math::multiply((1 / (1 -
                      stan::math::pow(
                        stan::model::rvalue(ar, "ar",
                          stan::model::index_uni(1)), 2))), PHI), SPD_beta),
                  "assigning variable f",
                  stan::model::index_min_max(1, Nsample));
              } else {
                current_statement__ = 52;
                stan::model::assign(f,
                  stan::math::add(
                    stan::math::multiply(
                      stan::model::rvalue(ar, "ar", stan::model::index_uni(1)),
                      stan::model::deep_copy(
                        stan::model::rvalue(f, "f",
                          stan::model::index_min_max(((Nsample * (t - 2)) +
                            1), ((t - 1) * Nsample))))),
                    stan::math::multiply(PHI, SPD_beta)),
                  "assigning variable f",
                  stan::model::index_min_max(((Nsample * (t - 1)) + 1), (t *
                    Nsample)));
              }
            } else {
              current_statement__ = 50;
              stan::model::assign(f, stan::math::multiply(PHI, SPD_beta),
                "assigning variable f",
                stan::model::index_min_max(1, Nsample));
            }
          }
        } else {
          current_statement__ = 48;
          if (stan::math::logical_eq(approx, 2)) {
            current_statement__ = 46;
            for (int t = 1; t <= nT; ++t) {
              current_statement__ = 44;
              if (stan::math::logical_gt(nT, 1)) {
                current_statement__ = 42;
                if (stan::math::logical_eq(t, 1)) {
                  current_statement__ = 40;
                  stan::model::assign(f,
                    stan::math::multiply((1 / (1 -
                      stan::math::pow(
                        stan::model::rvalue(ar, "ar",
                          stan::model::index_uni(1)), 2))),
                      stan::math::to_vector(
                        stan::model::rvalue(f_raw, "f_raw",
                          stan::model::index_min_max(1, Nsample)))),
                    "assigning variable f",
                    stan::model::index_min_max(1, Nsample));
                } else {
                  current_statement__ = 38;
                  stan::model::assign(f,
                    stan::math::add(
                      stan::math::multiply(
                        stan::model::rvalue(ar, "ar",
                          stan::model::index_uni(1)),
                        stan::model::deep_copy(
                          stan::model::rvalue(f, "f",
                            stan::model::index_min_max(((Nsample * (t - 2)) +
                              1), ((t - 1) * Nsample))))),
                      stan::math::to_vector(
                        stan::model::rvalue(f_raw, "f_raw",
                          stan::model::index_min_max(((Nsample * (t - 1)) +
                            1), (t * Nsample))))), "assigning variable f",
                    stan::model::index_min_max(((Nsample * (t - 1)) + 1), (t
                      * Nsample)));
                }
              } else {
                current_statement__ = 36;
                stan::model::assign(f, stan::math::to_vector(f_raw),
                  "assigning variable f");
              }
            }
          }
        }
      }
      current_statement__ = 8;
      stan::math::check_greater_or_equal(function__, "sigma", sigma, 1e-05);
      current_statement__ = 9;
      stan::math::check_greater_or_equal(function__, "phi", phi, 1e-05);
      {
        current_statement__ = 99;
        stan::math::validate_non_negative_index("lambda_r",
          "is_region ? n_region * nT : 0", (is_region ? (n_region * nT) : 0));
        Eigen::Matrix<local_scalar_t__,-1,1> lambda_r =
          Eigen::Matrix<local_scalar_t__,-1,1>::Constant((is_region ? (n_region
            * nT) : 0), DUMMY_VAR__);
        current_statement__ = 101;
        stan::math::validate_non_negative_index("region_mean",
          "is_region ? Nsample * nT : 0", (is_region ? (Nsample * nT) : 0));
        Eigen::Matrix<local_scalar_t__,-1,1> region_mean =
          Eigen::Matrix<local_scalar_t__,-1,1>::Constant((is_region ? (Nsample
            * nT) : 0), DUMMY_VAR__);
        current_statement__ = 106;
        if (is_region) {
          current_statement__ = 103;
          stan::model::assign(lambda_r,
            stan::math::rep_vector(0, (n_region * nT)),
            "assigning variable lambda_r");
          current_statement__ = 104;
          stan::model::assign(region_mean,
            stan::math::rep_vector(0, (Nsample * nT)),
            "assigning variable region_mean");
        }
        current_statement__ = 108;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 107;
          lp_accum__.add(stan::math::normal_lpdf<propto__>(
                           stan::math::to_vector(beta), 0, 1));
        }
        current_statement__ = 112;
        if (stan::math::logical_negation(known_cov)) {
          current_statement__ = 109;
          lp_accum__.add(stan::math::normal_lpdf<propto__>(phi_param,
                           stan::model::rvalue(prior_lscale, "prior_lscale",
                             stan::model::index_uni(1)),
                           stan::model::rvalue(prior_lscale, "prior_lscale",
                             stan::model::index_uni(2))));
          current_statement__ = 110;
          lp_accum__.add(stan::math::normal_lpdf<propto__>(sigma_param,
                           stan::model::rvalue(prior_var, "prior_var",
                             stan::model::index_uni(1)),
                           stan::model::rvalue(prior_var, "prior_var",
                             stan::model::index_uni(2))));
        }
        current_statement__ = 114;
        if (stan::math::logical_gt(nT, 1)) {
          current_statement__ = 113;
          lp_accum__.add(stan::math::normal_lpdf<propto__>(ar, 0, 1));
        }
        current_statement__ = 117;
        for (int q = 1; q <= Q; ++q) {
          current_statement__ = 115;
          lp_accum__.add(stan::math::normal_lpdf<propto__>(
                           stan::model::rvalue(gamma, "gamma",
                             stan::model::index_uni(q)),
                           stan::model::rvalue(prior_linpred_mean,
                             "prior_linpred_mean", stan::model::index_uni(q)),
                           stan::model::rvalue(prior_linpred_sd,
                             "prior_linpred_sd", stan::model::index_uni(q))));
        }
        current_statement__ = 130;
        if (stan::math::logical_eq(approx, 2)) {
          current_statement__ = 128;
          for (int t = 1; t <= nT; ++t) {
            current_statement__ = 126;
            if (stan::math::logical_gt(nT, 1)) {
              current_statement__ = 124;
              if (stan::math::logical_eq(t, 1)) {
                current_statement__ = 122;
                lp_accum__.add(nngp_split_lpdf<false>(
                                 stan::math::to_array_1d(
                                   stan::model::rvalue(f_raw, "f_raw",
                                     stan::model::index_min_max(1, Nsample))),
                                 AD, NN, 1, pstream__));
              } else {
                current_statement__ = 120;
                lp_accum__.add(nngp_split_lpdf<false>(
                                 stan::math::to_array_1d(
                                   stan::model::rvalue(f_raw, "f_raw",
                                     stan::model::index_min_max(((Nsample *
                                       (t - 1)) + 1), (t * Nsample)))), AD,
                                 NN, 1, pstream__));
              }
            } else {
              current_statement__ = 118;
              lp_accum__.add(nngp_split_lpdf<false>(
                               stan::math::to_array_1d(f_raw), AD, NN, 1,
                               pstream__));
            }
          }
        }
        current_statement__ = 146;
        if (is_region) {
          current_statement__ = 133;
          stan::model::assign(region_mean,
            stan::math::add(stan::math::multiply(X, gamma), logpopdens),
            "assigning variable region_mean");
          current_statement__ = 136;
          if (stan::math::logical_eq(approx, 0)) {
            current_statement__ = 134;
            lp_accum__.add(stan::math::std_normal_lpdf<propto__>(f_raw));
          }
          current_statement__ = 143;
          for (int r = 1; r <= n_region; ++r) {
            current_statement__ = 141;
            for (int t = 1; t <= nT; ++t) {
              current_statement__ = 139;
              for (int l = 1; l <=
                   (stan::model::rvalue(n_cell, "n_cell",
                      stan::model::index_uni((r + 1))) -
                   stan::model::rvalue(n_cell, "n_cell",
                     stan::model::index_uni(r))); ++l) {
                current_statement__ = 137;
                stan::model::assign(lambda_r,
                  (stan::model::rvalue(lambda_r, "lambda_r",
                     stan::model::index_uni((r + ((t - 1) * n_region)))) +
                  (stan::model::rvalue(q_weights, "q_weights",
                     stan::model::index_uni(
                       ((stan::model::rvalue(n_cell, "n_cell",
                           stan::model::index_uni(r)) + l) - 1))) *
                  stan::math::exp(
                    (stan::model::rvalue(f, "f",
                       stan::model::index_uni(
                         (stan::model::rvalue(cell_id, "cell_id",
                            stan::model::index_uni(
                              ((stan::model::rvalue(n_cell, "n_cell",
                                  stan::model::index_uni(r)) + l) - 1))) +
                         ((t - 1) * Nsample)))) +
                    stan::model::rvalue(region_mean, "region_mean",
                      stan::model::index_uni(
                        (stan::model::rvalue(cell_id, "cell_id",
                           stan::model::index_uni(
                             ((stan::model::rvalue(n_cell, "n_cell",
                                 stan::model::index_uni(r)) + l) - 1))) + ((t
                        - 1) * Nsample)))))))),
                  "assigning variable lambda_r",
                  stan::model::index_uni((r + ((t - 1) * n_region))));
              }
            }
          }
          current_statement__ = 144;
          lp_accum__.add(stan::math::poisson_lpmf<propto__>(y, lambda_r));
        } else {
          current_statement__ = 131;
          lp_accum__.add(stan::math::poisson_log_lpmf<propto__>(y,
                           stan::math::add(
                             stan::math::add(stan::math::multiply(X, gamma),
                               logpopdens), f)));
        }
      }
    } 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_rtsbayes_namespace::write_array";
    // suppress unused var warning
    (void) function__;
    try {
      std::vector<double> phi_param =
        std::vector<double>(phi_param_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 1;
      phi_param = in__.template read_constrain_lb<
                    std::vector<local_scalar_t__>, jacobian__>(1e-05, lp__,
                    phi_param_1dim__);
      std::vector<double> sigma_param =
        std::vector<double>(sigma_param_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 2;
      sigma_param = in__.template read_constrain_lb<
                      std::vector<local_scalar_t__>, jacobian__>(1e-05, lp__,
                      sigma_param_1dim__);
      Eigen::Matrix<double,-1,1> gamma =
        Eigen::Matrix<double,-1,1>::Constant(Q,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 3;
      gamma = in__.template read<Eigen::Matrix<local_scalar_t__,-1,1>>(Q);
      std::vector<double> ar =
        std::vector<double>(ar_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 4;
      ar = in__.template read_constrain_lub<std::vector<local_scalar_t__>,
             jacobian__>(-1, 1, lp__, ar_1dim__);
      std::vector<double> f_raw =
        std::vector<double>(f_raw_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 5;
      f_raw = in__.template read<std::vector<local_scalar_t__>>(f_raw_1dim__);
      Eigen::Matrix<double,-1,-1> beta =
        Eigen::Matrix<double,-1,-1>::Constant(beta_1dim__, beta_2dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 6;
      beta = in__.template read<
               Eigen::Matrix<local_scalar_t__,-1,-1>>(beta_1dim__,
               beta_2dim__);
      Eigen::Matrix<double,-1,1> f =
        Eigen::Matrix<double,-1,1>::Constant(f_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      double sigma = std::numeric_limits<double>::quiet_NaN();
      double phi = std::numeric_limits<double>::quiet_NaN();
      Eigen::Matrix<double,-1,-1> Lmat =
        Eigen::Matrix<double,-1,-1>::Constant(Lmat_1dim__, Lmat_2dim__,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,1> diagSPD =
        Eigen::Matrix<double,-1,1>::Constant(diagSPD_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,1> SPD_beta =
        Eigen::Matrix<double,-1,1>::Constant(SPD_beta_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,-1> AD =
        Eigen::Matrix<double,-1,-1>::Constant(AD_1dim__, AD_2dim__,
          std::numeric_limits<double>::quiet_NaN());
      out__.write(phi_param);
      out__.write(sigma_param);
      out__.write(gamma);
      out__.write(ar);
      out__.write(f_raw);
      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__ = 35;
      if (known_cov) {
        current_statement__ = 28;
        sigma = sigma_data;
        current_statement__ = 29;
        phi = phi_data;
        current_statement__ = 31;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 30;
          stan::model::assign(diagSPD, stan::math::to_vector(diagSPD_data),
            "assigning variable diagSPD");
        }
        current_statement__ = 33;
        if (stan::math::logical_eq(approx, 2)) {
          current_statement__ = 32;
          stan::model::assign(AD, AD_data, "assigning variable AD");
        }
      } else {
        current_statement__ = 17;
        sigma = stan::model::rvalue(sigma_param, "sigma_param",
                  stan::model::index_uni(1));
        current_statement__ = 18;
        phi = stan::model::rvalue(phi_param, "phi_param",
                stan::model::index_uni(1));
        current_statement__ = 23;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 21;
          for (int m = 1; m <= M_nD; ++m) {
            current_statement__ = 19;
            stan::model::assign(diagSPD,
              stan::math::sqrt(
                spd_nD(sigma, phi,
                  stan::math::sqrt(
                    lambda_nD(L,
                      stan::model::rvalue(indices, "indices",
                        stan::model::index_uni(m), stan::model::index_omni()),
                      D, pstream__)), D, mod, pstream__)),
              "assigning variable diagSPD", stan::model::index_uni(m));
          }
        }
        current_statement__ = 26;
        if (stan::math::logical_eq(approx, 2)) {
          current_statement__ = 24;
          stan::model::assign(AD,
            getAD(sigma, phi, M, Nsample, dists, NN, mod, pstream__),
            "assigning variable AD");
        }
      }
      current_statement__ = 80;
      if (stan::math::logical_eq(approx, 0)) {
        current_statement__ = 67;
        if (stan::math::logical_negation(known_cov)) {
          current_statement__ = 65;
          stan::model::assign(Lmat,
            genChol(Nsample, sigma, phi, dists, mod, pstream__),
            "assigning variable Lmat");
        } else {
          current_statement__ = 63;
          stan::model::assign(Lmat, L_data, "assigning variable Lmat");
        }
        current_statement__ = 78;
        for (int t = 1; t <= nT; ++t) {
          current_statement__ = 76;
          if (stan::math::logical_gt(nT, 1)) {
            current_statement__ = 74;
            if (stan::math::logical_eq(t, 1)) {
              current_statement__ = 72;
              stan::model::assign(f,
                stan::math::multiply(
                  stan::math::multiply((1 / (1 -
                    stan::math::pow(
                      stan::model::rvalue(ar, "ar", stan::model::index_uni(1)),
                      2))), Lmat),
                  stan::math::to_vector(
                    stan::model::rvalue(f_raw, "f_raw",
                      stan::model::index_min_max(1, Nsample)))),
                "assigning variable f",
                stan::model::index_min_max(1, Nsample));
            } else {
              current_statement__ = 70;
              stan::model::assign(f,
                stan::math::add(
                  stan::math::multiply(
                    stan::math::multiply(
                      stan::model::rvalue(ar, "ar", stan::model::index_uni(1)),
                      Lmat),
                    stan::model::deep_copy(
                      stan::model::rvalue(f, "f",
                        stan::model::index_min_max(((Nsample * (t - 2)) + 1),
                          ((t - 1) * Nsample))))),
                  stan::math::multiply(Lmat,
                    stan::math::to_vector(
                      stan::model::rvalue(f_raw, "f_raw",
                        stan::model::index_min_max(((Nsample * (t - 1)) + 1),
                          (t * Nsample)))))), "assigning variable f",
                stan::model::index_min_max(((Nsample * (t - 1)) + 1), (t *
                  Nsample)));
            }
          } else {
            current_statement__ = 68;
            stan::model::assign(f,
              stan::math::multiply(Lmat, stan::math::to_vector(f_raw)),
              "assigning variable f");
          }
        }
      } else {
        current_statement__ = 62;
        if (stan::math::logical_eq(approx, 1)) {
          current_statement__ = 60;
          for (int t = 1; t <= nT; ++t) {
            current_statement__ = 49;
            stan::model::assign(SPD_beta,
              stan::math::elt_multiply(diagSPD,
                stan::model::rvalue(beta, "beta", stan::model::index_omni(),
                  stan::model::index_uni(t))), "assigning variable SPD_beta");
            current_statement__ = 58;
            if (stan::math::logical_gt(nT, 1)) {
              current_statement__ = 56;
              if (stan::math::logical_eq(t, 1)) {
                current_statement__ = 54;
                stan::model::assign(f,
                  stan::math::multiply(
                    stan::math::multiply((1 / (1 -
                      stan::math::pow(
                        stan::model::rvalue(ar, "ar",
                          stan::model::index_uni(1)), 2))), PHI), SPD_beta),
                  "assigning variable f",
                  stan::model::index_min_max(1, Nsample));
              } else {
                current_statement__ = 52;
                stan::model::assign(f,
                  stan::math::add(
                    stan::math::multiply(
                      stan::model::rvalue(ar, "ar", stan::model::index_uni(1)),
                      stan::model::deep_copy(
                        stan::model::rvalue(f, "f",
                          stan::model::index_min_max(((Nsample * (t - 2)) +
                            1), ((t - 1) * Nsample))))),
                    stan::math::multiply(PHI, SPD_beta)),
                  "assigning variable f",
                  stan::model::index_min_max(((Nsample * (t - 1)) + 1), (t *
                    Nsample)));
              }
            } else {
              current_statement__ = 50;
              stan::model::assign(f, stan::math::multiply(PHI, SPD_beta),
                "assigning variable f",
                stan::model::index_min_max(1, Nsample));
            }
          }
        } else {
          current_statement__ = 48;
          if (stan::math::logical_eq(approx, 2)) {
            current_statement__ = 46;
            for (int t = 1; t <= nT; ++t) {
              current_statement__ = 44;
              if (stan::math::logical_gt(nT, 1)) {
                current_statement__ = 42;
                if (stan::math::logical_eq(t, 1)) {
                  current_statement__ = 40;
                  stan::model::assign(f,
                    stan::math::multiply((1 / (1 -
                      stan::math::pow(
                        stan::model::rvalue(ar, "ar",
                          stan::model::index_uni(1)), 2))),
                      stan::math::to_vector(
                        stan::model::rvalue(f_raw, "f_raw",
                          stan::model::index_min_max(1, Nsample)))),
                    "assigning variable f",
                    stan::model::index_min_max(1, Nsample));
                } else {
                  current_statement__ = 38;
                  stan::model::assign(f,
                    stan::math::add(
                      stan::math::multiply(
                        stan::model::rvalue(ar, "ar",
                          stan::model::index_uni(1)),
                        stan::model::deep_copy(
                          stan::model::rvalue(f, "f",
                            stan::model::index_min_max(((Nsample * (t - 2)) +
                              1), ((t - 1) * Nsample))))),
                      stan::math::to_vector(
                        stan::model::rvalue(f_raw, "f_raw",
                          stan::model::index_min_max(((Nsample * (t - 1)) +
                            1), (t * Nsample))))), "assigning variable f",
                    stan::model::index_min_max(((Nsample * (t - 1)) + 1), (t
                      * Nsample)));
                }
              } else {
                current_statement__ = 36;
                stan::model::assign(f, stan::math::to_vector(f_raw),
                  "assigning variable f");
              }
            }
          }
        }
      }
      current_statement__ = 8;
      stan::math::check_greater_or_equal(function__, "sigma", sigma, 1e-05);
      current_statement__ = 9;
      stan::math::check_greater_or_equal(function__, "phi", phi, 1e-05);
      if (emit_transformed_parameters__) {
        out__.write(f);
        out__.write(sigma);
        out__.write(phi);
        out__.write(Lmat);
        out__.write(diagSPD);
        out__.write(SPD_beta);
        out__.write(AD);
      }
      if (stan::math::logical_negation(emit_generated_quantities__)) {
        return ;
      }
      Eigen::Matrix<double,-1,1> y_grid_predict =
        Eigen::Matrix<double,-1,1>::Constant(y_grid_predict_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,1> region_predict =
        Eigen::Matrix<double,-1,1>::Constant(region_predict_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      Eigen::Matrix<double,-1,1> region_mean_predict =
        Eigen::Matrix<double,-1,1>::Constant(region_mean_predict_1dim__,
          std::numeric_limits<double>::quiet_NaN());
      current_statement__ = 98;
      if (stan::math::logical_negation(is_region)) {
        current_statement__ = 96;
        for (int i = 1; i <= (Nsample * nT); ++i) {
          current_statement__ = 94;
          stan::model::assign(y_grid_predict,
            stan::math::exp(
              ((stan::math::multiply(
                  stan::model::rvalue(X, "X", stan::model::index_uni(i),
                    stan::model::index_omni()), gamma) +
              stan::model::rvalue(logpopdens, "logpopdens",
                stan::model::index_uni(i))) +
              stan::model::rvalue(f, "f", stan::model::index_uni(i)))),
            "assigning variable y_grid_predict", stan::model::index_uni(i));
        }
      } else {
        current_statement__ = 81;
        stan::model::assign(region_predict,
          stan::math::rep_vector(0, (n_region * nT)),
          "assigning variable region_predict");
        current_statement__ = 82;
        stan::model::assign(region_mean_predict,
          stan::math::add(stan::math::multiply(X, gamma), logpopdens),
          "assigning variable region_mean_predict");
        current_statement__ = 85;
        for (int i = 1; i <= (Nsample * nT); ++i) {
          current_statement__ = 83;
          stan::model::assign(y_grid_predict,
            stan::math::exp(
              (stan::model::rvalue(f, "f", stan::model::index_uni(i)) +
              stan::model::rvalue(region_mean_predict, "region_mean_predict",
                stan::model::index_uni(i)))),
            "assigning variable y_grid_predict", stan::model::index_uni(i));
        }
        current_statement__ = 92;
        for (int r = 1; r <= n_region; ++r) {
          current_statement__ = 90;
          for (int t = 1; t <= nT; ++t) {
            current_statement__ = 88;
            for (int l = 1; l <=
                 (stan::model::rvalue(n_cell, "n_cell",
                    stan::model::index_uni((r + 1))) -
                 stan::model::rvalue(n_cell, "n_cell",
                   stan::model::index_uni(r))); ++l) {
              current_statement__ = 86;
              stan::model::assign(region_predict,
                (stan::model::rvalue(region_predict, "region_predict",
                   stan::model::index_uni((r + ((t - 1) * n_region)))) +
                (((stan::model::rvalue(popdens, "popdens",
                     stan::model::index_uni((r + ((t - 1) * n_region)))) *
                stan::math::exp(
                  stan::math::multiply(
                    stan::model::rvalue(X, "X",
                      stan::model::index_uni((r + ((t - 1) * n_region))),
                      stan::model::index_omni()), gamma))) *
                stan::model::rvalue(q_weights, "q_weights",
                  stan::model::index_uni(
                    ((stan::model::rvalue(n_cell, "n_cell",
                        stan::model::index_uni(r)) + l) - 1)))) *
                stan::math::exp(
                  (stan::model::rvalue(f, "f",
                     stan::model::index_uni(
                       (stan::model::rvalue(cell_id, "cell_id",
                          stan::model::index_uni(
                            ((stan::model::rvalue(n_cell, "n_cell",
                                stan::model::index_uni(r)) + l) - 1))) + ((t
                       - 1) * Nsample)))) +
                  stan::model::rvalue(region_mean_predict,
                    "region_mean_predict",
                    stan::model::index_uni(
                      (stan::model::rvalue(cell_id, "cell_id",
                         stan::model::index_uni(
                           ((stan::model::rvalue(n_cell, "n_cell",
                               stan::model::index_uni(r)) + l) - 1))) + ((t -
                      1) * Nsample)))))))),
                "assigning variable region_predict",
                stan::model::index_uni((r + ((t - 1) * n_region))));
            }
          }
        }
      }
      out__.write(y_grid_predict);
      out__.write(region_predict);
      out__.write(region_mean_predict);
    } 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;
      std::vector<local_scalar_t__> phi_param =
        std::vector<local_scalar_t__>(phi_param_1dim__, DUMMY_VAR__);
      current_statement__ = 1;
      stan::model::assign(phi_param,
        in__.read<std::vector<local_scalar_t__>>(phi_param_1dim__),
        "assigning variable phi_param");
      out__.write_free_lb(1e-05, phi_param);
      std::vector<local_scalar_t__> sigma_param =
        std::vector<local_scalar_t__>(sigma_param_1dim__, DUMMY_VAR__);
      current_statement__ = 2;
      stan::model::assign(sigma_param,
        in__.read<std::vector<local_scalar_t__>>(sigma_param_1dim__),
        "assigning variable sigma_param");
      out__.write_free_lb(1e-05, sigma_param);
      Eigen::Matrix<local_scalar_t__,-1,1> gamma =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(Q, DUMMY_VAR__);
      current_statement__ = 3;
      stan::model::assign(gamma,
        in__.read<Eigen::Matrix<local_scalar_t__,-1,1>>(Q),
        "assigning variable gamma");
      out__.write(gamma);
      std::vector<local_scalar_t__> ar =
        std::vector<local_scalar_t__>(ar_1dim__, DUMMY_VAR__);
      current_statement__ = 4;
      stan::model::assign(ar,
        in__.read<std::vector<local_scalar_t__>>(ar_1dim__),
        "assigning variable ar");
      out__.write_free_lub(-1, 1, ar);
      std::vector<local_scalar_t__> f_raw =
        std::vector<local_scalar_t__>(f_raw_1dim__, DUMMY_VAR__);
      current_statement__ = 5;
      stan::model::assign(f_raw,
        in__.read<std::vector<local_scalar_t__>>(f_raw_1dim__),
        "assigning variable f_raw");
      out__.write(f_raw);
      Eigen::Matrix<local_scalar_t__,-1,-1> beta =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(beta_1dim__,
          beta_2dim__, DUMMY_VAR__);
      current_statement__ = 6;
      stan::model::assign(beta,
        in__.read<Eigen::Matrix<local_scalar_t__,-1,-1>>(beta_1dim__,
          beta_2dim__), "assigning variable beta");
      out__.write(beta);
    } 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", "phi_param",
        "double", std::vector<size_t>{static_cast<size_t>(phi_param_1dim__)});
      current_statement__ = 2;
      context__.validate_dims("parameter initialization", "sigma_param",
        "double",
        std::vector<size_t>{static_cast<size_t>(sigma_param_1dim__)});
      current_statement__ = 3;
      context__.validate_dims("parameter initialization", "gamma", "double",
        std::vector<size_t>{static_cast<size_t>(Q)});
      current_statement__ = 4;
      context__.validate_dims("parameter initialization", "ar", "double",
        std::vector<size_t>{static_cast<size_t>(ar_1dim__)});
      current_statement__ = 5;
      context__.validate_dims("parameter initialization", "f_raw", "double",
        std::vector<size_t>{static_cast<size_t>(f_raw_1dim__)});
      current_statement__ = 6;
      context__.validate_dims("parameter initialization", "beta", "double",
        std::vector<size_t>{static_cast<size_t>(beta_1dim__),
          static_cast<size_t>(beta_2dim__)});
      int pos__ = std::numeric_limits<int>::min();
      pos__ = 1;
      std::vector<local_scalar_t__> phi_param =
        std::vector<local_scalar_t__>(phi_param_1dim__, DUMMY_VAR__);
      current_statement__ = 1;
      phi_param = context__.vals_r("phi_param");
      out__.write_free_lb(1e-05, phi_param);
      std::vector<local_scalar_t__> sigma_param =
        std::vector<local_scalar_t__>(sigma_param_1dim__, DUMMY_VAR__);
      current_statement__ = 2;
      sigma_param = context__.vals_r("sigma_param");
      out__.write_free_lb(1e-05, sigma_param);
      Eigen::Matrix<local_scalar_t__,-1,1> gamma =
        Eigen::Matrix<local_scalar_t__,-1,1>::Constant(Q, DUMMY_VAR__);
      {
        std::vector<local_scalar_t__> gamma_flat__;
        current_statement__ = 3;
        gamma_flat__ = context__.vals_r("gamma");
        current_statement__ = 3;
        pos__ = 1;
        current_statement__ = 3;
        for (int sym1__ = 1; sym1__ <= Q; ++sym1__) {
          current_statement__ = 3;
          stan::model::assign(gamma, gamma_flat__[(pos__ - 1)],
            "assigning variable gamma", stan::model::index_uni(sym1__));
          current_statement__ = 3;
          pos__ = (pos__ + 1);
        }
      }
      out__.write(gamma);
      std::vector<local_scalar_t__> ar =
        std::vector<local_scalar_t__>(ar_1dim__, DUMMY_VAR__);
      current_statement__ = 4;
      ar = context__.vals_r("ar");
      out__.write_free_lub(-1, 1, ar);
      std::vector<local_scalar_t__> f_raw =
        std::vector<local_scalar_t__>(f_raw_1dim__, DUMMY_VAR__);
      current_statement__ = 5;
      f_raw = context__.vals_r("f_raw");
      out__.write(f_raw);
      Eigen::Matrix<local_scalar_t__,-1,-1> beta =
        Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(beta_1dim__,
          beta_2dim__, DUMMY_VAR__);
      {
        std::vector<local_scalar_t__> beta_flat__;
        current_statement__ = 6;
        beta_flat__ = context__.vals_r("beta");
        current_statement__ = 6;
        pos__ = 1;
        current_statement__ = 6;
        for (int sym1__ = 1; sym1__ <= beta_2dim__; ++sym1__) {
          current_statement__ = 6;
          for (int sym2__ = 1; sym2__ <= beta_1dim__; ++sym2__) {
            current_statement__ = 6;
            stan::model::assign(beta, beta_flat__[(pos__ - 1)],
              "assigning variable beta", stan::model::index_uni(sym2__),
              stan::model::index_uni(sym1__));
            current_statement__ = 6;
            pos__ = (pos__ + 1);
          }
        }
      }
      out__.write(beta);
    } 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>{"phi_param", "sigma_param", "gamma",
                "ar", "f_raw", "beta"};
    if (emit_transformed_parameters__) {
      std::vector<std::string>
        temp{"f", "sigma", "phi", "Lmat", "diagSPD", "SPD_beta", "AD"};
      names__.reserve(names__.size() + temp.size());
      names__.insert(names__.end(), temp.begin(), temp.end());
    }
    if (emit_generated_quantities__) {
      std::vector<std::string>
        temp{"y_grid_predict", "region_predict", "region_mean_predict"};
      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>(
                                                                    phi_param_1dim__)},
                std::vector<size_t>{static_cast<size_t>(sigma_param_1dim__)},
                std::vector<size_t>{static_cast<size_t>(Q)},
                std::vector<size_t>{static_cast<size_t>(ar_1dim__)},
                std::vector<size_t>{static_cast<size_t>(f_raw_1dim__)},
                std::vector<size_t>{static_cast<size_t>(beta_1dim__),
                  static_cast<size_t>(beta_2dim__)}};
    if (emit_transformed_parameters__) {
      std::vector<std::vector<size_t>>
        temp{std::vector<size_t>{static_cast<size_t>(f_1dim__)},
             std::vector<size_t>{}, std::vector<size_t>{},
             std::vector<size_t>{static_cast<size_t>(Lmat_1dim__),
               static_cast<size_t>(Lmat_2dim__)},
             std::vector<size_t>{static_cast<size_t>(diagSPD_1dim__)},
             std::vector<size_t>{static_cast<size_t>(SPD_beta_1dim__)},
             std::vector<size_t>{static_cast<size_t>(AD_1dim__),
               static_cast<size_t>(AD_2dim__)}};
      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>(y_grid_predict_1dim__)},
             std::vector<size_t>{static_cast<size_t>(region_predict_1dim__)},
             std::vector<size_t>{static_cast<size_t>(
                                   region_mean_predict_1dim__)}};
      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__ <= phi_param_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "phi_param" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= sigma_param_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "sigma_param" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= Q; ++sym1__) {
      param_names__.emplace_back(std::string() + "gamma" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= ar_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "ar" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= f_raw_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "f_raw" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= beta_2dim__; ++sym1__) {
      for (int sym2__ = 1; sym2__ <= beta_1dim__; ++sym2__) {
        param_names__.emplace_back(std::string() + "beta" + '.' +
          std::to_string(sym2__) + '.' + std::to_string(sym1__));
      }
    }
    if (emit_transformed_parameters__) {
      for (int sym1__ = 1; sym1__ <= f_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "f" + '.' +
          std::to_string(sym1__));
      }
      param_names__.emplace_back(std::string() + "sigma");
      param_names__.emplace_back(std::string() + "phi");
      for (int sym1__ = 1; sym1__ <= Lmat_2dim__; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= Lmat_1dim__; ++sym2__) {
          param_names__.emplace_back(std::string() + "Lmat" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= diagSPD_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "diagSPD" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= SPD_beta_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "SPD_beta" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= AD_2dim__; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= AD_1dim__; ++sym2__) {
          param_names__.emplace_back(std::string() + "AD" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
    }
    if (emit_generated_quantities__) {
      for (int sym1__ = 1; sym1__ <= y_grid_predict_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "y_grid_predict" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= region_predict_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "region_predict" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= region_mean_predict_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "region_mean_predict" +
          '.' + 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__ <= phi_param_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "phi_param" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= sigma_param_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "sigma_param" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= Q; ++sym1__) {
      param_names__.emplace_back(std::string() + "gamma" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= ar_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "ar" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= f_raw_1dim__; ++sym1__) {
      param_names__.emplace_back(std::string() + "f_raw" + '.' +
        std::to_string(sym1__));
    }
    for (int sym1__ = 1; sym1__ <= beta_2dim__; ++sym1__) {
      for (int sym2__ = 1; sym2__ <= beta_1dim__; ++sym2__) {
        param_names__.emplace_back(std::string() + "beta" + '.' +
          std::to_string(sym2__) + '.' + std::to_string(sym1__));
      }
    }
    if (emit_transformed_parameters__) {
      for (int sym1__ = 1; sym1__ <= f_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "f" + '.' +
          std::to_string(sym1__));
      }
      param_names__.emplace_back(std::string() + "sigma");
      param_names__.emplace_back(std::string() + "phi");
      for (int sym1__ = 1; sym1__ <= Lmat_2dim__; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= Lmat_1dim__; ++sym2__) {
          param_names__.emplace_back(std::string() + "Lmat" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
      for (int sym1__ = 1; sym1__ <= diagSPD_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "diagSPD" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= SPD_beta_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "SPD_beta" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= AD_2dim__; ++sym1__) {
        for (int sym2__ = 1; sym2__ <= AD_1dim__; ++sym2__) {
          param_names__.emplace_back(std::string() + "AD" + '.' +
            std::to_string(sym2__) + '.' + std::to_string(sym1__));
        }
      }
    }
    if (emit_generated_quantities__) {
      for (int sym1__ = 1; sym1__ <= y_grid_predict_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "y_grid_predict" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= region_predict_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "region_predict" + '.' +
          std::to_string(sym1__));
      }
      for (int sym1__ = 1; sym1__ <= region_mean_predict_1dim__; ++sym1__) {
        param_names__.emplace_back(std::string() + "region_mean_predict" +
          '.' + std::to_string(sym1__));
      }
    }
  }
  inline std::string get_constrained_sizedtypes() const {
    return std::string("[{\"name\":\"phi_param\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(phi_param_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"sigma_param\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(sigma_param_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"gamma\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(Q) + "},\"block\":\"parameters\"},{\"name\":\"ar\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(ar_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"f_raw\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(f_raw_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"beta\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(beta_1dim__) + ",\"cols\":" + std::to_string(beta_2dim__) + "},\"block\":\"parameters\"},{\"name\":\"f\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(f_1dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"sigma\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"phi\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"Lmat\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(Lmat_1dim__) + ",\"cols\":" + std::to_string(Lmat_2dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"diagSPD\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(diagSPD_1dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"SPD_beta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(SPD_beta_1dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"AD\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(AD_1dim__) + ",\"cols\":" + std::to_string(AD_2dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"y_grid_predict\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(y_grid_predict_1dim__) + "},\"block\":\"generated_quantities\"},{\"name\":\"region_predict\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(region_predict_1dim__) + "},\"block\":\"generated_quantities\"},{\"name\":\"region_mean_predict\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(region_mean_predict_1dim__) + "},\"block\":\"generated_quantities\"}]");
  }
  inline std::string get_unconstrained_sizedtypes() const {
    return std::string("[{\"name\":\"phi_param\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(phi_param_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"sigma_param\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(sigma_param_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"gamma\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(Q) + "},\"block\":\"parameters\"},{\"name\":\"ar\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(ar_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"f_raw\",\"type\":{\"name\":\"array\",\"length\":" + std::to_string(f_raw_1dim__) + ",\"element_type\":{\"name\":\"real\"}},\"block\":\"parameters\"},{\"name\":\"beta\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(beta_1dim__) + ",\"cols\":" + std::to_string(beta_2dim__) + "},\"block\":\"parameters\"},{\"name\":\"f\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(f_1dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"sigma\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"phi\",\"type\":{\"name\":\"real\"},\"block\":\"transformed_parameters\"},{\"name\":\"Lmat\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(Lmat_1dim__) + ",\"cols\":" + std::to_string(Lmat_2dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"diagSPD\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(diagSPD_1dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"SPD_beta\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(SPD_beta_1dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"AD\",\"type\":{\"name\":\"matrix\",\"rows\":" + std::to_string(AD_1dim__) + ",\"cols\":" + std::to_string(AD_2dim__) + "},\"block\":\"transformed_parameters\"},{\"name\":\"y_grid_predict\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(y_grid_predict_1dim__) + "},\"block\":\"generated_quantities\"},{\"name\":\"region_predict\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(region_predict_1dim__) + "},\"block\":\"generated_quantities\"},{\"name\":\"region_mean_predict\",\"type\":{\"name\":\"vector\",\"length\":" + std::to_string(region_mean_predict_1dim__) + "},\"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__ = (((((phi_param_1dim__ + sigma_param_1dim__) +
      Q) + ar_1dim__) + f_raw_1dim__) + (beta_1dim__ * beta_2dim__));
    const size_t num_transformed = emit_transformed_parameters *
      (((((((f_1dim__ + 1) + 1) + (Lmat_1dim__ * Lmat_2dim__)) +
      diagSPD_1dim__) + SPD_beta_1dim__) + (AD_1dim__ * AD_2dim__)));
    const size_t num_gen_quantities = emit_generated_quantities *
      (((y_grid_predict_1dim__ + region_predict_1dim__) +
      region_mean_predict_1dim__));
    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__ = (((((phi_param_1dim__ + sigma_param_1dim__) +
      Q) + ar_1dim__) + f_raw_1dim__) + (beta_1dim__ * beta_2dim__));
    const size_t num_transformed = emit_transformed_parameters *
      (((((((f_1dim__ + 1) + 1) + (Lmat_1dim__ * Lmat_2dim__)) +
      diagSPD_1dim__) + SPD_beta_1dim__) + (AD_1dim__ * AD_2dim__)));
    const size_t num_gen_quantities = emit_generated_quantities *
      (((y_grid_predict_1dim__ + region_predict_1dim__) +
      region_mean_predict_1dim__));
    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_rtsbayes_namespace::model_rtsbayes;
#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_rtsbayes_namespace::profiles__;
}
#endif
#endif
