UNPKG

11.1 kBJavaScriptView Raw
1/*
2Language: Stan
3Description: The Stan probabilistic programming language
4Author: Jeffrey B. Arnold <jeffrey.arnold@gmail.com>
5Website: http://mc-stan.org/
6Category: scientific
7*/
8
9function stan(hljs) {
10 // variable names cannot conflict with block identifiers
11 const BLOCKS = [
12 'functions',
13 'model',
14 'data',
15 'parameters',
16 'quantities',
17 'transformed',
18 'generated'
19 ];
20 const STATEMENTS = [
21 'for',
22 'in',
23 'if',
24 'else',
25 'while',
26 'break',
27 'continue',
28 'return'
29 ];
30 const SPECIAL_FUNCTIONS = [
31 'print',
32 'reject',
33 'increment_log_prob|10',
34 'integrate_ode|10',
35 'integrate_ode_rk45|10',
36 'integrate_ode_bdf|10',
37 'algebra_solver'
38 ];
39 const VAR_TYPES = [
40 'int',
41 'real',
42 'vector',
43 'ordered',
44 'positive_ordered',
45 'simplex',
46 'unit_vector',
47 'row_vector',
48 'matrix',
49 'cholesky_factor_corr|10',
50 'cholesky_factor_cov|10',
51 'corr_matrix|10',
52 'cov_matrix|10',
53 'void'
54 ];
55 const FUNCTIONS = [
56 'Phi',
57 'Phi_approx',
58 'abs',
59 'acos',
60 'acosh',
61 'algebra_solver',
62 'append_array',
63 'append_col',
64 'append_row',
65 'asin',
66 'asinh',
67 'atan',
68 'atan2',
69 'atanh',
70 'bernoulli_cdf',
71 'bernoulli_lccdf',
72 'bernoulli_lcdf',
73 'bernoulli_logit_lpmf',
74 'bernoulli_logit_rng',
75 'bernoulli_lpmf',
76 'bernoulli_rng',
77 'bessel_first_kind',
78 'bessel_second_kind',
79 'beta_binomial_cdf',
80 'beta_binomial_lccdf',
81 'beta_binomial_lcdf',
82 'beta_binomial_lpmf',
83 'beta_binomial_rng',
84 'beta_cdf',
85 'beta_lccdf',
86 'beta_lcdf',
87 'beta_lpdf',
88 'beta_rng',
89 'binary_log_loss',
90 'binomial_cdf',
91 'binomial_coefficient_log',
92 'binomial_lccdf',
93 'binomial_lcdf',
94 'binomial_logit_lpmf',
95 'binomial_lpmf',
96 'binomial_rng',
97 'block',
98 'categorical_logit_lpmf',
99 'categorical_logit_rng',
100 'categorical_lpmf',
101 'categorical_rng',
102 'cauchy_cdf',
103 'cauchy_lccdf',
104 'cauchy_lcdf',
105 'cauchy_lpdf',
106 'cauchy_rng',
107 'cbrt',
108 'ceil',
109 'chi_square_cdf',
110 'chi_square_lccdf',
111 'chi_square_lcdf',
112 'chi_square_lpdf',
113 'chi_square_rng',
114 'cholesky_decompose',
115 'choose',
116 'col',
117 'cols',
118 'columns_dot_product',
119 'columns_dot_self',
120 'cos',
121 'cosh',
122 'cov_exp_quad',
123 'crossprod',
124 'csr_extract_u',
125 'csr_extract_v',
126 'csr_extract_w',
127 'csr_matrix_times_vector',
128 'csr_to_dense_matrix',
129 'cumulative_sum',
130 'determinant',
131 'diag_matrix',
132 'diag_post_multiply',
133 'diag_pre_multiply',
134 'diagonal',
135 'digamma',
136 'dims',
137 'dirichlet_lpdf',
138 'dirichlet_rng',
139 'distance',
140 'dot_product',
141 'dot_self',
142 'double_exponential_cdf',
143 'double_exponential_lccdf',
144 'double_exponential_lcdf',
145 'double_exponential_lpdf',
146 'double_exponential_rng',
147 'e',
148 'eigenvalues_sym',
149 'eigenvectors_sym',
150 'erf',
151 'erfc',
152 'exp',
153 'exp2',
154 'exp_mod_normal_cdf',
155 'exp_mod_normal_lccdf',
156 'exp_mod_normal_lcdf',
157 'exp_mod_normal_lpdf',
158 'exp_mod_normal_rng',
159 'expm1',
160 'exponential_cdf',
161 'exponential_lccdf',
162 'exponential_lcdf',
163 'exponential_lpdf',
164 'exponential_rng',
165 'fabs',
166 'falling_factorial',
167 'fdim',
168 'floor',
169 'fma',
170 'fmax',
171 'fmin',
172 'fmod',
173 'frechet_cdf',
174 'frechet_lccdf',
175 'frechet_lcdf',
176 'frechet_lpdf',
177 'frechet_rng',
178 'gamma_cdf',
179 'gamma_lccdf',
180 'gamma_lcdf',
181 'gamma_lpdf',
182 'gamma_p',
183 'gamma_q',
184 'gamma_rng',
185 'gaussian_dlm_obs_lpdf',
186 'get_lp',
187 'gumbel_cdf',
188 'gumbel_lccdf',
189 'gumbel_lcdf',
190 'gumbel_lpdf',
191 'gumbel_rng',
192 'head',
193 'hypergeometric_lpmf',
194 'hypergeometric_rng',
195 'hypot',
196 'inc_beta',
197 'int_step',
198 'integrate_ode',
199 'integrate_ode_bdf',
200 'integrate_ode_rk45',
201 'inv',
202 'inv_Phi',
203 'inv_chi_square_cdf',
204 'inv_chi_square_lccdf',
205 'inv_chi_square_lcdf',
206 'inv_chi_square_lpdf',
207 'inv_chi_square_rng',
208 'inv_cloglog',
209 'inv_gamma_cdf',
210 'inv_gamma_lccdf',
211 'inv_gamma_lcdf',
212 'inv_gamma_lpdf',
213 'inv_gamma_rng',
214 'inv_logit',
215 'inv_sqrt',
216 'inv_square',
217 'inv_wishart_lpdf',
218 'inv_wishart_rng',
219 'inverse',
220 'inverse_spd',
221 'is_inf',
222 'is_nan',
223 'lbeta',
224 'lchoose',
225 'lgamma',
226 'lkj_corr_cholesky_lpdf',
227 'lkj_corr_cholesky_rng',
228 'lkj_corr_lpdf',
229 'lkj_corr_rng',
230 'lmgamma',
231 'lmultiply',
232 'log',
233 'log10',
234 'log1m',
235 'log1m_exp',
236 'log1m_inv_logit',
237 'log1p',
238 'log1p_exp',
239 'log2',
240 'log_determinant',
241 'log_diff_exp',
242 'log_falling_factorial',
243 'log_inv_logit',
244 'log_mix',
245 'log_rising_factorial',
246 'log_softmax',
247 'log_sum_exp',
248 'logistic_cdf',
249 'logistic_lccdf',
250 'logistic_lcdf',
251 'logistic_lpdf',
252 'logistic_rng',
253 'logit',
254 'lognormal_cdf',
255 'lognormal_lccdf',
256 'lognormal_lcdf',
257 'lognormal_lpdf',
258 'lognormal_rng',
259 'machine_precision',
260 'matrix_exp',
261 'max',
262 'mdivide_left_spd',
263 'mdivide_left_tri_low',
264 'mdivide_right_spd',
265 'mdivide_right_tri_low',
266 'mean',
267 'min',
268 'modified_bessel_first_kind',
269 'modified_bessel_second_kind',
270 'multi_gp_cholesky_lpdf',
271 'multi_gp_lpdf',
272 'multi_normal_cholesky_lpdf',
273 'multi_normal_cholesky_rng',
274 'multi_normal_lpdf',
275 'multi_normal_prec_lpdf',
276 'multi_normal_rng',
277 'multi_student_t_lpdf',
278 'multi_student_t_rng',
279 'multinomial_lpmf',
280 'multinomial_rng',
281 'multiply_log',
282 'multiply_lower_tri_self_transpose',
283 'neg_binomial_2_cdf',
284 'neg_binomial_2_lccdf',
285 'neg_binomial_2_lcdf',
286 'neg_binomial_2_log_lpmf',
287 'neg_binomial_2_log_rng',
288 'neg_binomial_2_lpmf',
289 'neg_binomial_2_rng',
290 'neg_binomial_cdf',
291 'neg_binomial_lccdf',
292 'neg_binomial_lcdf',
293 'neg_binomial_lpmf',
294 'neg_binomial_rng',
295 'negative_infinity',
296 'normal_cdf',
297 'normal_lccdf',
298 'normal_lcdf',
299 'normal_lpdf',
300 'normal_rng',
301 'not_a_number',
302 'num_elements',
303 'ordered_logistic_lpmf',
304 'ordered_logistic_rng',
305 'owens_t',
306 'pareto_cdf',
307 'pareto_lccdf',
308 'pareto_lcdf',
309 'pareto_lpdf',
310 'pareto_rng',
311 'pareto_type_2_cdf',
312 'pareto_type_2_lccdf',
313 'pareto_type_2_lcdf',
314 'pareto_type_2_lpdf',
315 'pareto_type_2_rng',
316 'pi',
317 'poisson_cdf',
318 'poisson_lccdf',
319 'poisson_lcdf',
320 'poisson_log_lpmf',
321 'poisson_log_rng',
322 'poisson_lpmf',
323 'poisson_rng',
324 'positive_infinity',
325 'pow',
326 'print',
327 'prod',
328 'qr_Q',
329 'qr_R',
330 'quad_form',
331 'quad_form_diag',
332 'quad_form_sym',
333 'rank',
334 'rayleigh_cdf',
335 'rayleigh_lccdf',
336 'rayleigh_lcdf',
337 'rayleigh_lpdf',
338 'rayleigh_rng',
339 'reject',
340 'rep_array',
341 'rep_matrix',
342 'rep_row_vector',
343 'rep_vector',
344 'rising_factorial',
345 'round',
346 'row',
347 'rows',
348 'rows_dot_product',
349 'rows_dot_self',
350 'scaled_inv_chi_square_cdf',
351 'scaled_inv_chi_square_lccdf',
352 'scaled_inv_chi_square_lcdf',
353 'scaled_inv_chi_square_lpdf',
354 'scaled_inv_chi_square_rng',
355 'sd',
356 'segment',
357 'sin',
358 'singular_values',
359 'sinh',
360 'size',
361 'skew_normal_cdf',
362 'skew_normal_lccdf',
363 'skew_normal_lcdf',
364 'skew_normal_lpdf',
365 'skew_normal_rng',
366 'softmax',
367 'sort_asc',
368 'sort_desc',
369 'sort_indices_asc',
370 'sort_indices_desc',
371 'sqrt',
372 'sqrt2',
373 'square',
374 'squared_distance',
375 'step',
376 'student_t_cdf',
377 'student_t_lccdf',
378 'student_t_lcdf',
379 'student_t_lpdf',
380 'student_t_rng',
381 'sub_col',
382 'sub_row',
383 'sum',
384 'tail',
385 'tan',
386 'tanh',
387 'target',
388 'tcrossprod',
389 'tgamma',
390 'to_array_1d',
391 'to_array_2d',
392 'to_matrix',
393 'to_row_vector',
394 'to_vector',
395 'trace',
396 'trace_gen_quad_form',
397 'trace_quad_form',
398 'trigamma',
399 'trunc',
400 'uniform_cdf',
401 'uniform_lccdf',
402 'uniform_lcdf',
403 'uniform_lpdf',
404 'uniform_rng',
405 'variance',
406 'von_mises_lpdf',
407 'von_mises_rng',
408 'weibull_cdf',
409 'weibull_lccdf',
410 'weibull_lcdf',
411 'weibull_lpdf',
412 'weibull_rng',
413 'wiener_lpdf',
414 'wishart_lpdf',
415 'wishart_rng'
416 ];
417 const DISTRIBUTIONS = [
418 'bernoulli',
419 'bernoulli_logit',
420 'beta',
421 'beta_binomial',
422 'binomial',
423 'binomial_logit',
424 'categorical',
425 'categorical_logit',
426 'cauchy',
427 'chi_square',
428 'dirichlet',
429 'double_exponential',
430 'exp_mod_normal',
431 'exponential',
432 'frechet',
433 'gamma',
434 'gaussian_dlm_obs',
435 'gumbel',
436 'hypergeometric',
437 'inv_chi_square',
438 'inv_gamma',
439 'inv_wishart',
440 'lkj_corr',
441 'lkj_corr_cholesky',
442 'logistic',
443 'lognormal',
444 'multi_gp',
445 'multi_gp_cholesky',
446 'multi_normal',
447 'multi_normal_cholesky',
448 'multi_normal_prec',
449 'multi_student_t',
450 'multinomial',
451 'neg_binomial',
452 'neg_binomial_2',
453 'neg_binomial_2_log',
454 'normal',
455 'ordered_logistic',
456 'pareto',
457 'pareto_type_2',
458 'poisson',
459 'poisson_log',
460 'rayleigh',
461 'scaled_inv_chi_square',
462 'skew_normal',
463 'student_t',
464 'uniform',
465 'von_mises',
466 'weibull',
467 'wiener',
468 'wishart'
469 ];
470
471 const BLOCK_COMMENT = hljs.COMMENT(
472 /\/\*/,
473 /\*\//,
474 {
475 relevance: 0,
476 contains: [
477 {
478 className: 'doctag',
479 match: /@(return|param)/
480 }
481 ]
482 }
483 );
484
485 const INCLUDE = {
486 className: 'meta',
487 begin: /^#include\b/,
488 end: /$/,
489 relevance: 0, // relevance comes from keywords
490 keywords: "include",
491 contains: [
492 {
493 match: /[a-z][a-z-.]+/,
494 className: "string"
495 },
496 hljs.C_LINE_COMMENT_MODE
497 ]
498 };
499
500 return {
501 name: 'Stan',
502 aliases: [ 'stanfuncs' ],
503 keywords: {
504 $pattern: hljs.IDENT_RE,
505 title: BLOCKS,
506 keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS),
507 built_in: FUNCTIONS
508 },
509 contains: [
510 hljs.C_LINE_COMMENT_MODE,
511 INCLUDE,
512 hljs.HASH_COMMENT_MODE,
513 BLOCK_COMMENT,
514 {
515 // hack: in range constraints, lower must follow "<"
516 begin: /<\s*lower\s*=/,
517 keywords: 'lower'
518 },
519 {
520 // hack: in range constraints, upper must follow either , or <
521 // <lower = ..., upper = ...> or <upper = ...>
522 begin: /[<,]\s*upper\s*=/,
523 keywords: 'upper'
524 },
525 {
526 className: 'keyword',
527 begin: /\btarget\s*\+=/
528 },
529 {
530 begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(',
531 keywords: DISTRIBUTIONS
532 },
533 {
534 className: 'number',
535 variants: [
536 {
537 begin: /\b\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/
538 },
539 {
540 begin: /\.\d+(?:[eE][+-]?\d+)?\b/
541 }
542 ],
543 relevance: 0
544 },
545 {
546 className: 'string',
547 begin: '"',
548 end: '"',
549 relevance: 0
550 }
551 ]
552 };
553}
554
555export { stan as default };