stan.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. var BLOCKS = [
  11. 'functions',
  12. 'model',
  13. 'data',
  14. 'parameters',
  15. 'quantities',
  16. 'transformed',
  17. 'generated'
  18. ];
  19. var STATEMENTS = [
  20. 'for',
  21. 'in',
  22. 'if',
  23. 'else',
  24. 'while',
  25. 'break',
  26. 'continue',
  27. 'return'
  28. ];
  29. var 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. var 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. var FUNCTIONS = [
  55. 'Phi', 'Phi_approx', 'abs', 'acos', 'acosh', 'algebra_solver', 'append_array',
  56. 'append_col', 'append_row', 'asin', 'asinh', 'atan', 'atan2', 'atanh',
  57. 'bernoulli_cdf', 'bernoulli_lccdf', 'bernoulli_lcdf', 'bernoulli_logit_lpmf',
  58. 'bernoulli_logit_rng', 'bernoulli_lpmf', 'bernoulli_rng', 'bessel_first_kind',
  59. 'bessel_second_kind', 'beta_binomial_cdf', 'beta_binomial_lccdf',
  60. 'beta_binomial_lcdf', 'beta_binomial_lpmf', 'beta_binomial_rng', 'beta_cdf',
  61. 'beta_lccdf', 'beta_lcdf', 'beta_lpdf', 'beta_rng', 'binary_log_loss',
  62. 'binomial_cdf', 'binomial_coefficient_log', 'binomial_lccdf', 'binomial_lcdf',
  63. 'binomial_logit_lpmf', 'binomial_lpmf', 'binomial_rng', 'block',
  64. 'categorical_logit_lpmf', 'categorical_logit_rng', 'categorical_lpmf',
  65. 'categorical_rng', 'cauchy_cdf', 'cauchy_lccdf', 'cauchy_lcdf', 'cauchy_lpdf',
  66. 'cauchy_rng', 'cbrt', 'ceil', 'chi_square_cdf', 'chi_square_lccdf',
  67. 'chi_square_lcdf', 'chi_square_lpdf', 'chi_square_rng', 'cholesky_decompose',
  68. 'choose', 'col', 'cols', 'columns_dot_product', 'columns_dot_self', 'cos',
  69. 'cosh', 'cov_exp_quad', 'crossprod', 'csr_extract_u', 'csr_extract_v',
  70. 'csr_extract_w', 'csr_matrix_times_vector', 'csr_to_dense_matrix',
  71. 'cumulative_sum', 'determinant', 'diag_matrix', 'diag_post_multiply',
  72. 'diag_pre_multiply', 'diagonal', 'digamma', 'dims', 'dirichlet_lpdf',
  73. 'dirichlet_rng', 'distance', 'dot_product', 'dot_self',
  74. 'double_exponential_cdf', 'double_exponential_lccdf', 'double_exponential_lcdf',
  75. 'double_exponential_lpdf', 'double_exponential_rng', 'e', 'eigenvalues_sym',
  76. 'eigenvectors_sym', 'erf', 'erfc', 'exp', 'exp2', 'exp_mod_normal_cdf',
  77. 'exp_mod_normal_lccdf', 'exp_mod_normal_lcdf', 'exp_mod_normal_lpdf',
  78. 'exp_mod_normal_rng', 'expm1', 'exponential_cdf', 'exponential_lccdf',
  79. 'exponential_lcdf', 'exponential_lpdf', 'exponential_rng', 'fabs',
  80. 'falling_factorial', 'fdim', 'floor', 'fma', 'fmax', 'fmin', 'fmod',
  81. 'frechet_cdf', 'frechet_lccdf', 'frechet_lcdf', 'frechet_lpdf', 'frechet_rng',
  82. 'gamma_cdf', 'gamma_lccdf', 'gamma_lcdf', 'gamma_lpdf', 'gamma_p', 'gamma_q',
  83. 'gamma_rng', 'gaussian_dlm_obs_lpdf', 'get_lp', 'gumbel_cdf', 'gumbel_lccdf',
  84. 'gumbel_lcdf', 'gumbel_lpdf', 'gumbel_rng', 'head', 'hypergeometric_lpmf',
  85. 'hypergeometric_rng', 'hypot', 'inc_beta', 'int_step', 'integrate_ode',
  86. 'integrate_ode_bdf', 'integrate_ode_rk45', 'inv', 'inv_Phi',
  87. 'inv_chi_square_cdf', 'inv_chi_square_lccdf', 'inv_chi_square_lcdf',
  88. 'inv_chi_square_lpdf', 'inv_chi_square_rng', 'inv_cloglog', 'inv_gamma_cdf',
  89. 'inv_gamma_lccdf', 'inv_gamma_lcdf', 'inv_gamma_lpdf', 'inv_gamma_rng',
  90. 'inv_logit', 'inv_sqrt', 'inv_square', 'inv_wishart_lpdf', 'inv_wishart_rng',
  91. 'inverse', 'inverse_spd', 'is_inf', 'is_nan', 'lbeta', 'lchoose', 'lgamma',
  92. 'lkj_corr_cholesky_lpdf', 'lkj_corr_cholesky_rng', 'lkj_corr_lpdf',
  93. 'lkj_corr_rng', 'lmgamma', 'lmultiply', 'log', 'log10', 'log1m', 'log1m_exp',
  94. 'log1m_inv_logit', 'log1p', 'log1p_exp', 'log2', 'log_determinant',
  95. 'log_diff_exp', 'log_falling_factorial', 'log_inv_logit', 'log_mix',
  96. 'log_rising_factorial', 'log_softmax', 'log_sum_exp', 'logistic_cdf',
  97. 'logistic_lccdf', 'logistic_lcdf', 'logistic_lpdf', 'logistic_rng', 'logit',
  98. 'lognormal_cdf', 'lognormal_lccdf', 'lognormal_lcdf', 'lognormal_lpdf',
  99. 'lognormal_rng', 'machine_precision', 'matrix_exp', 'max', 'mdivide_left_spd',
  100. 'mdivide_left_tri_low', 'mdivide_right_spd', 'mdivide_right_tri_low', 'mean',
  101. 'min', 'modified_bessel_first_kind', 'modified_bessel_second_kind',
  102. 'multi_gp_cholesky_lpdf', 'multi_gp_lpdf', 'multi_normal_cholesky_lpdf',
  103. 'multi_normal_cholesky_rng', 'multi_normal_lpdf', 'multi_normal_prec_lpdf',
  104. 'multi_normal_rng', 'multi_student_t_lpdf', 'multi_student_t_rng',
  105. 'multinomial_lpmf', 'multinomial_rng', 'multiply_log',
  106. 'multiply_lower_tri_self_transpose', 'neg_binomial_2_cdf',
  107. 'neg_binomial_2_lccdf', 'neg_binomial_2_lcdf', 'neg_binomial_2_log_lpmf',
  108. 'neg_binomial_2_log_rng', 'neg_binomial_2_lpmf', 'neg_binomial_2_rng',
  109. 'neg_binomial_cdf', 'neg_binomial_lccdf', 'neg_binomial_lcdf',
  110. 'neg_binomial_lpmf', 'neg_binomial_rng', 'negative_infinity', 'normal_cdf',
  111. 'normal_lccdf', 'normal_lcdf', 'normal_lpdf', 'normal_rng', 'not_a_number',
  112. 'num_elements', 'ordered_logistic_lpmf', 'ordered_logistic_rng', 'owens_t',
  113. 'pareto_cdf', 'pareto_lccdf', 'pareto_lcdf', 'pareto_lpdf', 'pareto_rng',
  114. 'pareto_type_2_cdf', 'pareto_type_2_lccdf', 'pareto_type_2_lcdf',
  115. 'pareto_type_2_lpdf', 'pareto_type_2_rng', 'pi', 'poisson_cdf', 'poisson_lccdf',
  116. 'poisson_lcdf', 'poisson_log_lpmf', 'poisson_log_rng', 'poisson_lpmf',
  117. 'poisson_rng', 'positive_infinity', 'pow', 'print', 'prod', 'qr_Q', 'qr_R',
  118. 'quad_form', 'quad_form_diag', 'quad_form_sym', 'rank', 'rayleigh_cdf',
  119. 'rayleigh_lccdf', 'rayleigh_lcdf', 'rayleigh_lpdf', 'rayleigh_rng', 'reject',
  120. 'rep_array', 'rep_matrix', 'rep_row_vector', 'rep_vector', 'rising_factorial',
  121. 'round', 'row', 'rows', 'rows_dot_product', 'rows_dot_self',
  122. 'scaled_inv_chi_square_cdf', 'scaled_inv_chi_square_lccdf',
  123. 'scaled_inv_chi_square_lcdf', 'scaled_inv_chi_square_lpdf',
  124. 'scaled_inv_chi_square_rng', 'sd', 'segment', 'sin', 'singular_values', 'sinh',
  125. 'size', 'skew_normal_cdf', 'skew_normal_lccdf', 'skew_normal_lcdf',
  126. 'skew_normal_lpdf', 'skew_normal_rng', 'softmax', 'sort_asc', 'sort_desc',
  127. 'sort_indices_asc', 'sort_indices_desc', 'sqrt', 'sqrt2', 'square',
  128. 'squared_distance', 'step', 'student_t_cdf', 'student_t_lccdf',
  129. 'student_t_lcdf', 'student_t_lpdf', 'student_t_rng', 'sub_col', 'sub_row',
  130. 'sum', 'tail', 'tan', 'tanh', 'target', 'tcrossprod', 'tgamma', 'to_array_1d',
  131. 'to_array_2d', 'to_matrix', 'to_row_vector', 'to_vector', 'trace',
  132. 'trace_gen_quad_form', 'trace_quad_form', 'trigamma', 'trunc', 'uniform_cdf',
  133. 'uniform_lccdf', 'uniform_lcdf', 'uniform_lpdf', 'uniform_rng', 'variance',
  134. 'von_mises_lpdf', 'von_mises_rng', 'weibull_cdf', 'weibull_lccdf',
  135. 'weibull_lcdf', 'weibull_lpdf', 'weibull_rng', 'wiener_lpdf', 'wishart_lpdf',
  136. 'wishart_rng'
  137. ];
  138. var DISTRIBUTIONS = [
  139. 'bernoulli', 'bernoulli_logit', 'beta', 'beta_binomial', 'binomial',
  140. 'binomial_logit', 'categorical', 'categorical_logit', 'cauchy', 'chi_square',
  141. 'dirichlet', 'double_exponential', 'exp_mod_normal', 'exponential', 'frechet',
  142. 'gamma', 'gaussian_dlm_obs', 'gumbel', 'hypergeometric', 'inv_chi_square',
  143. 'inv_gamma', 'inv_wishart', 'lkj_corr', 'lkj_corr_cholesky', 'logistic',
  144. 'lognormal', 'multi_gp', 'multi_gp_cholesky', 'multi_normal',
  145. 'multi_normal_cholesky', 'multi_normal_prec', 'multi_student_t', 'multinomial',
  146. 'neg_binomial', 'neg_binomial_2', 'neg_binomial_2_log', 'normal',
  147. 'ordered_logistic', 'pareto', 'pareto_type_2', 'poisson', 'poisson_log',
  148. 'rayleigh', 'scaled_inv_chi_square', 'skew_normal', 'student_t', 'uniform',
  149. 'von_mises', 'weibull', 'wiener', 'wishart'
  150. ];
  151. return {
  152. name: 'Stan',
  153. aliases: ['stanfuncs'],
  154. keywords: {
  155. $pattern: hljs.IDENT_RE,
  156. title: BLOCKS.join(' '),
  157. keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS).join(' '),
  158. built_in: FUNCTIONS.join(' ')
  159. },
  160. contains: [
  161. hljs.C_LINE_COMMENT_MODE,
  162. hljs.COMMENT(
  163. /#/,
  164. /$/,
  165. {
  166. relevance: 0,
  167. keywords: {
  168. 'meta-keyword': 'include'
  169. }
  170. }
  171. ),
  172. hljs.COMMENT(
  173. /\/\*/,
  174. /\*\//,
  175. {
  176. relevance: 0,
  177. // highlight doc strings mentioned in Stan reference
  178. contains: [
  179. {
  180. className: 'doctag',
  181. begin: /@(return|param)/
  182. }
  183. ]
  184. }
  185. ),
  186. {
  187. // hack: in range constraints, lower must follow "<"
  188. begin: /<\s*lower\s*=/,
  189. keywords: 'lower'
  190. },
  191. {
  192. // hack: in range constraints, upper must follow either , or <
  193. // <lower = ..., upper = ...> or <upper = ...>
  194. begin: /[<,]\s*upper\s*=/,
  195. keywords: 'upper'
  196. },
  197. {
  198. className: 'keyword',
  199. begin: /\btarget\s*\+=/,
  200. relevance: 10
  201. },
  202. {
  203. begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(',
  204. keywords: DISTRIBUTIONS.join(' ')
  205. },
  206. {
  207. className: 'number',
  208. variants: [
  209. {
  210. begin: /\b\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/
  211. },
  212. {
  213. begin: /\.\d+(?:[eE][+-]?\d+)?\b/
  214. }
  215. ],
  216. relevance: 0
  217. },
  218. {
  219. className: 'string',
  220. begin: '"',
  221. end: '"',
  222. relevance: 0
  223. }
  224. ]
  225. }
  226. }
  227. module.exports = stan;