stan.js 11 KB

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