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