1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
<beginfold id='1'>/*</beginfold id='1'> Stan Highlighting Example
This file contains a syntatically correct but nonsensical Stan program that
includes almost every feature of the language needed to validate syntax
highlighters. It will compile (as of Stan 2.17.1), but it does nothing
useful.
Author: Jeffrey Arnold <jeffrey.anold@gmail.com>
Copyright: Jeffrey Arnold (2018)
License: MIT
<endfold id='1'>*/</endfold id='1'>
// line comment
# deprecated line comment
functions <beginfold id='2'>{</beginfold id='2'>
#include stuff.stan
#include "morestuff.stan"
#include 'moststuff.stan'
#include <evenmorestuff.stan>
// declarations
void oof(real x);
// definitions
// return types
void oof(real x) <beginfold id='2'>{</beginfold id='2'>
print("print ", x);
<endfold id='2'>}</endfold id='2'>
<beginfold id='1'>/*</beginfold id='1'>
@param x A number
@return x + 1
<endfold id='1'>*/</endfold id='1'>
real foo(real x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
int bar(int x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
vector baz(vector x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
row_vector qux(row_vector x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
matrix quux(matrix x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
// numbers of arguments
void corge() <beginfold id='2'>{</beginfold id='2'>
print("no parameters");
<endfold id='2'>}</endfold id='2'>
void grault(int a, real b, vector c, row_vector d, matrix f) <beginfold id='2'>{</beginfold id='2'>
print("many parameters");
<endfold id='2'>}</endfold id='2'>
void garply(real a, real[] b, real[,] c, real[,,] d) <beginfold id='2'>{</beginfold id='2'>
print("array arguments");
<endfold id='2'>}</endfold id='2'>
// array return types
int[] waldo(int[] x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
int[,] fred(int[,] x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
int[,,] plough(int[,,] x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
// data only function argument
real plugh(data real x) <beginfold id='2'>{</beginfold id='2'>
return x;
<endfold id='2'>}</endfold id='2'>
// ode function
real[] ode_func(real a, real[] b, real[] c, real[] d, int[] e) <beginfold id='2'>{</beginfold id='2'>
return b;
<endfold id='2'>}</endfold id='2'>
<endfold id='2'>}</endfold id='2'>
data <beginfold id='2'>{</beginfold id='2'>
// non-int variable types
int x_int;
real x_real;
real y_real;
vector[1] x_vector;
ordered[1] x_ordered;
positive_ordered[1] x_positive_ordered;
simplex[1] x_simplex;
unit_vector[1] x_unit_vector;
row_vector[1] x_row_vector;
matrix[1, 1] x_matrix;
cholesky_factor_corr[2] x_cholesky_factor_corr;
cholesky_factor_cov[2] x_cholesky_factor_cov;
cholesky_factor_cov[2, 3] x_cholesky_factor_cov_2;
corr_matrix[2] x_corr_matrix;
cov_matrix[2] x_cov_matrix;
// range constraints
real<lower = 0., upper = 1.> alpha;
real<lower = 0.> bravo;
real<upper = 1.> charlie;
// arrays
int echo[1];
int foxtrot[1, 1];
int golf[1, 1, 1];
// identifier with all valid letters
real abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789;
// hard pattern
real<lower = (bravo < charlie), upper = (bravo > charlie)> ranger;
// identifier patterns
real a;
real a3;
real a_3;
real Sigma;
real my_cpp_style_variable;
real myCamelCaseVariable;
real abcdefghijklmnojk;
// names beginning with keywords
real iffffff;
real whilest;
// name ending with truncation
real fooT;
// new array syntax
array [N] real foo_new;
<endfold id='2'>}</endfold id='2'>
transformed data <beginfold id='2'>{</beginfold id='2'>
// declaration and assignment
int india = 1;
real romeo = 1.0;
row_vector[2] victor = [1, 2];
matrix[2, 2] mike = [[1, 2], [3, 4]];
real sierra[2] = <beginfold id='2'>{</beginfold id='2'>1., 2.<endfold id='2'>}</endfold id='2'>;
complex zulu = 3+4.1i;
<endfold id='2'>}</endfold id='2'>
parameters <beginfold id='2'>{</beginfold id='2'>
real hotel;
real<offset = 0., multiplier = 1.> alpha;
sum_to_zero_vector[3] zero_sum_game;
<endfold id='2'>}</endfold id='2'>
transformed parameters <beginfold id='2'>{</beginfold id='2'>
real juliette;
juliette = hotel * 2.;
// increment log-determinant of the jacobian
jacobian += 1.;
<endfold id='2'>}</endfold id='2'>
model <beginfold id='2'>{</beginfold id='2'>
real x;
int k;
vector[2] y = [1., 1.]';
matrix[2, 2] A = [[1., 1.], [1., 1.]];
real odeout[2, 2];
real algout[2, 2];
// if else statements
if (x_real < 0) x = 0.;
if (x_real < 0) <beginfold id='2'>{</beginfold id='2'>
x = 0.;
<endfold id='2'>}</endfold id='2'>
if (x_real < 0) x = 0.;
else x = 1.;
if (x_real < 0) <beginfold id='2'>{</beginfold id='2'>
x = 0.;
<endfold id='2'>}</endfold id='2'> else <beginfold id='2'>{</beginfold id='2'>
x = 1.;
<endfold id='2'>}</endfold id='2'>
if (x_real < 0) x = 0.;
else if (x_real > 1) x = 1.;
else x = 0.5;
if (x_real < 0) <beginfold id='2'>{</beginfold id='2'>
x = 0.;
<endfold id='2'>}</endfold id='2'> else if (x_real > 1) <beginfold id='2'>{</beginfold id='2'>
x = 1.;
<endfold id='2'>}</endfold id='2'> else <beginfold id='2'>{</beginfold id='2'>
x = 0.5;
<endfold id='2'>}</endfold id='2'>
// for loops
for (i in 1:5) <beginfold id='2'>{</beginfold id='2'>
print("i = ", i);
<endfold id='2'>}</endfold id='2'>
// for (j in echo) {
// print("j = ", j);
// }
// while loop
while (1) <beginfold id='2'>{</beginfold id='2'>
break;
continue;
<endfold id='2'>}</endfold id='2'>
// reject statement
reject("reject statment ", x_real);
// print statement
print("print statement ", x_real);
print("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_~@#$%^&*`'-+={}[].,;: ");
// fatal_error statement (hard exit)
fatal_error("fatal error statement", x_real);
// increment log probability statements;
target += 1.;
// valid integer literals
k = 0;
k = 1;
k = -1;
k = 256;
k = -127098;
k = 007;
// valid real literals
x = 0.0;
x = 1.0;
x = 3.14;
x = 003.14;
x = -217.9387;
x = 0.123;
x = .123;
x = 1.;
x = -0.123;
x = -.123;
x = -1.;
x = 12e34;
x = 12E34;
x = 12.e34;
x = 12.E34;
x = 12.0e34;
x = 12.0E34;
x = .1e34;
x = .1E34;
x = -12e34;
x = -12E34;
x = -12.e34;
x = -12.E34;
x = -12.0e34;
x = -12.0E34;
x = -.1e34;
x = -.1E34;
x = 12e-34;
x = 12E-34;
x = 12.e-34;
x = 12.E-34;
x = 12.0e-34;
x = 12.0E-34;
x = .1e-34;
x = .1E-34;
x = -12e-34;
x = -12E-34;
x = -12.e-34;
x = -12.E-34;
x = -12.0e-34;
x = -12.0E-34;
x = -.1e-34;
x = -.1E-34;
x = 12e+34;
x = 12E+34;
x = 12.e+34;
x = 12.E+34;
x = 12.0e+34;
x = 12.0E+34;
x = .1e+34;
x = .1E+34;
x = -12e+34;
x = -12E+34;
x = -12.e+34;
x = -12.E+34;
x = -12.0e+34;
x = -12.0E+34;
x = -.1e+34;
x = -.1E+34;
// imaginary literals
complex z = 3 + 3i;
z = 2.3i;
z = 3.4e10i;
z = 0i;
// assignment statements
x = 1;
x += 1.;
x -= 1.;
x *= 1.;
x /= 1.;
y .*= x_vector;
y ./= x_vector;
// operators
x = x_real && 1;
x = x_real || 1;
x = x_real < 1.;
x = x_real <= 1.;
x = x_real > 1.;
x = x_real >= 1.;
x = x_real + 1.;
x = x_real - 1.;
x = x_real * 1.;
x = x_real / 1.;
x = x_real ^ 2.;
x = x_real % 2;
x = !x_real;
x = +x_real;
x = -x_real;
x = x_int ? x_real : 0.;
y = x_row_vector';
y = x_matrix \ x_vector;
y = x_vector .* x_vector;
y = x_vector ./ x_vector;
// parenthized expression
x = (x_real + x_real);
// block statement
<beginfold id='2'>{</beginfold id='2'>
real z;
z = 1.;
<endfold id='2'>}</endfold id='2'>
profile("profile-test") <beginfold id='2'>{</beginfold id='2'>
real z;
z = 1.;
<endfold id='2'>}</endfold id='2'>
// built-in functions
x = log(1.);
x = exp(1.);
// non-built-in function
x = foo(1.);
// constants and nullary functions
x = machine_precision();
x = pi();
x = e();
x = sqrt2();
x = log2();
x = log10();
// special values
x = not_a_number();
x = positive_infinity();
x = negative_infinity();
x = machine_precision();
// log probability
x = target();
// sampling statement
x_real ~ normal(0., 1.);
// truncation
x_real ~ normal(0., 1.) T[-1., 1.];
x_real ~ normal(0., 1.) T[, 1.];
x_real ~ normal(0., 1.) T[-1., ];
x_real ~ normal(0., 1.) T[ , ];
// transformation on lhs of sampling
log(x_real) ~ normal(0., 1.);
// lhs indexes
y[1] = 1.;
A[1, 2] = 1.;
A[1][2] = 1.;
// special functions
odeout = integrate_ode(ode_func, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, x_real, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>0<endfold id='2'>}</endfold id='2'>);
odeout = integrate_ode_bdf(ode_func, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, x_real, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>0<endfold id='2'>}</endfold id='2'>,
x_real, x_real, x_int);
odeout = integrate_ode_rk45(ode_func, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, x_real, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>1.<endfold id='2'>}</endfold id='2'>, <beginfold id='2'>{</beginfold id='2'>0<endfold id='2'>}</endfold id='2'>,
x_real, x_real, x_int);
// algout = algebra_solver(algebra_func, x_vector, x_vector, {1.}, {0});
// distribution functions
x = normal_lpdf(0.5 | 0., 1.);
x = normal_cdf(0.5, 0., 1.);
x = normal_lcdf(0.5 | 0., 1.);
x = normal_lccdf(0.5 | 0., 1.);
x = binomial_lpmf(1 | 2, 0.5);
// deprecated features
foo <- 1;
increment_log_prob(0.0);
y_hat = integrate_ode(sho, y0, t0, ts, theta, x_r, x_i);
x = get_lp();
x = multiply_log(1.0, 1.0);
x = binomial_coefficient_log(1.0, 1.0);
// deprecated distribution functions versions
x = normal_log(0.5, 0.0, 1.0);
x = normal_cdf_log(0.5, 0.0, 1.0);
x = normal_ccdf_log(0.5, 0.0, 1.0);
<endfold id='2'>}</endfold id='2'>
generated quantities <beginfold id='2'>{</beginfold id='2'>
real Y;
// rng function
Y = normal_rng(0., 1.);
tuple(real, int) tupl = (1.5, 2);
complex_matrix C_mike = mike;
<endfold id='2'>}</endfold id='2'>
|