Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Stan syntax to version 2.33 #3859

Merged
merged 2 commits into from Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -40,6 +40,7 @@ Core Grammars:
- fix(reasonml) simplify syntax and align it with ocaml [jchavarri][]
- fix(swift) `warn_unqualified_access` is an attribute [Bradley Mackey][]
- enh(swift) macro attributes are highlighted as keywords [Bradley Mackey][]
- enh(stan) updated for version 2.33 (#3859) [Brian Ward][]

Dev tool:

Expand All @@ -62,6 +63,7 @@ Dev tool:
[aana-h2]: https://github.com/aana-h2
[Nicholas Thompson]: https://github.com/NAThompson
[Yasith Deelaka]: https://github.com/YasithD
[Brian Ward]: https://github.com/WardBrian


## Version 11.8.0
Expand Down
58 changes: 43 additions & 15 deletions src/languages/stan.js
Expand Up @@ -32,16 +32,20 @@ export default function(hljs) {

const TYPES = [
'array',
'tuple',
'complex',
'int',
'real',
'vector',
'complex_vector',
'ordered',
'positive_ordered',
'simplex',
'unit_vector',
'row_vector',
'complex_row_vector',
'matrix',
'complex_matrix',
'cholesky_factor_corr|10',
'cholesky_factor_cov|10',
'corr_matrix|10',
Expand All @@ -58,8 +62,6 @@ export default function(hljs) {
// functions_quoted.txt

const FUNCTIONS = [
'Phi',
'Phi_approx',
'abs',
'acos',
'acosh',
Expand All @@ -77,7 +79,6 @@ export default function(hljs) {
'bessel_first_kind',
'bessel_second_kind',
'binary_log_loss',
'binomial_coefficient_log',
'block',
'cbrt',
'ceil',
Expand All @@ -88,37 +89,48 @@ export default function(hljs) {
'cols',
'columns_dot_product',
'columns_dot_self',
'complex_schur_decompose',
'complex_schur_decompose_t',
'complex_schur_decompose_u',
'conj',
'cos',
'cosh',
'cov_exp_quad',
'crossprod',
'csr_extract',
'csr_extract_u',
'csr_extract_v',
'csr_extract_w',
'csr_matrix_times_vector',
'csr_to_dense_matrix',
'cumulative_sum',
'dae',
'dae_tol',
'determinant',
'diag_matrix',
'diagonal',
'diag_post_multiply',
'diag_pre_multiply',
'diagonal',
'digamma',
'dims',
'distance',
'dot_product',
'dot_self',
'eigendecompose',
'eigendecompose_sym',
'eigenvalues',
'eigenvalues_sym',
'eigenvectors',
'eigenvectors_sym',
'erf',
'erfc',
'exp',
'exp2',
'expm1',
'fabs',
'falling_factorial',
'fdim',
'fft',
'fft2',
'floor',
'fma',
'fmax',
Expand All @@ -128,28 +140,31 @@ export default function(hljs) {
'gamma_q',
'generalized_inverse',
'get_imag',
'get_lp',
'get_real',
'head',
'hmm_hidden_state_prob',
'hmm_marginal',
'hypot',
'identity_matrix',
'inc_beta',
'int_step',
'integrate_1d',
'integrate_ode',
'integrate_ode_adams',
'integrate_ode_bdf',
'integrate_ode_rk45',
'int_step',
'inv',
'inv_Phi',
'inv_cloglog',
'inv_erfc',
'inverse',
'inverse_spd',
'inv_fft',
'inv_fft2',
'inv_inc_beta',
'inv_logit',
'inv_Phi',
'inv_sqrt',
'inv_square',
'inverse',
'inverse_spd',
'is_inf',
'is_nan',
'lambert_w0',
Expand All @@ -175,12 +190,12 @@ export default function(hljs) {
'log_falling_factorial',
'log_inv_logit',
'log_inv_logit_diff',
'logit',
'log_mix',
'log_modified_bessel_first_kind',
'log_rising_factorial',
'log_softmax',
'log_sum_exp',
'logit',
'machine_precision',
'map_rect',
'matrix_exp',
Expand All @@ -195,10 +210,11 @@ export default function(hljs) {
'min',
'modified_bessel_first_kind',
'modified_bessel_second_kind',
'multiply_log',
'multiply_lower_tri_self_transpose',
'negative_infinity',
'norm',
'norm1',
'norm2',
'not_a_number',
'num_elements',
'ode_adams',
Expand All @@ -219,14 +235,18 @@ export default function(hljs) {
'ones_row_vector',
'ones_vector',
'owens_t',
'Phi',
'Phi_approx',
'polar',
'positive_infinity',
'pow',
'print',
'prod',
'proj',
'qr',
'qr_Q',
'qr_R',
'qr_thin',
'qr_thin_Q',
'qr_thin_R',
'quad_form',
Expand Down Expand Up @@ -266,6 +286,7 @@ export default function(hljs) {
'sub_col',
'sub_row',
'sum',
'svd',
'svd_U',
'svd_V',
'symmetrize_from_lower_tri',
Expand All @@ -278,6 +299,7 @@ export default function(hljs) {
'to_array_1d',
'to_array_2d',
'to_complex',
'to_int',
'to_matrix',
'to_row_vector',
'to_vector',
Expand Down Expand Up @@ -321,18 +343,22 @@ export default function(hljs) {
'inv_chi_square',
'inv_gamma',
'inv_wishart',
'inv_wishart_cholesky',
'lkj_corr',
'lkj_corr_cholesky',
'logistic',
'loglogistic',
'lognormal',
'multi_gp',
'multi_gp_cholesky',
'multinomial',
'multinomial_logit',
'multi_normal',
'multi_normal_cholesky',
'multi_normal_prec',
'multi_student_cholesky_t',
'multi_student_t',
'multinomial',
'multinomial_logit',
'multi_student_t_cholesky',
'neg_binomial',
'neg_binomial_2',
'neg_binomial_2_log',
Expand All @@ -352,12 +378,14 @@ export default function(hljs) {
'skew_double_exponential',
'skew_normal',
'std_normal',
'std_normal_log',
'student_t',
'uniform',
'von_mises',
'weibull',
'wiener',
'wishart'
'wishart',
'wishart_cholesky'
];

const BLOCK_COMMENT = hljs.COMMENT(
Expand Down
6 changes: 4 additions & 2 deletions test/detect/stan/default.txt
Expand Up @@ -5,6 +5,7 @@ data {
int<lower=0> N;
int K;
matrix[N, K] x;
complex_row_vector[K] z;
}
transformed data {
complex zi = 1+3.14i;
Expand All @@ -16,13 +17,14 @@ parameters {
array[K - 1] real mu;
array[K + 1] real<lower=0> sigma;
cholesky_factor_corr[K] L;
tuple(real, tuple(real, real)) t;
}
model {
target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]);
target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]);
target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]);
target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]);

{
matrix[K, N] y;
for (n in 1 : N) {
Expand All @@ -37,4 +39,4 @@ model {
}
generated quantities {
matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L);
}
}
6 changes: 4 additions & 2 deletions test/markup/stan/default.expect.txt
Expand Up @@ -5,6 +5,7 @@
<span class="hljs-type">int</span>&lt;<span class="hljs-keyword">lower</span>=<span class="hljs-number">0</span>&gt; N;
<span class="hljs-type">int</span> K;
<span class="hljs-type">matrix</span>[N, K] x;
<span class="hljs-type">complex_row_vector</span>[K] z;
}
<span class="hljs-title">transformed</span> <span class="hljs-title">data</span> {
<span class="hljs-type">complex</span> zi = <span class="hljs-number">1</span>+<span class="hljs-number">3.14i</span>;
Expand All @@ -16,13 +17,14 @@
<span class="hljs-type">array</span>[K - <span class="hljs-number">1</span>] <span class="hljs-type">real</span> mu;
<span class="hljs-type">array</span>[K + <span class="hljs-number">1</span>] <span class="hljs-type">real</span>&lt;<span class="hljs-keyword">lower</span>=<span class="hljs-number">0</span>&gt; sigma;
<span class="hljs-type">cholesky_factor_corr</span>[K] L;
<span class="hljs-type">tuple</span>(<span class="hljs-type">real</span>, <span class="hljs-type">tuple</span>(<span class="hljs-type">real</span>, <span class="hljs-type">real</span>)) t;
}
<span class="hljs-title">model</span> {
<span class="hljs-keyword">target</span> += <span class="hljs-built_in">normal_lpdf</span>(x[ : , <span class="hljs-number">1</span>] | mu[<span class="hljs-number">1</span>], sigma[<span class="hljs-number">1</span>]);
<span class="hljs-keyword">target</span> += <span class="hljs-built_in">gumbel_lpdf</span>(x[ : , <span class="hljs-number">2</span>] | mu[<span class="hljs-number">2</span>], sigma[<span class="hljs-number">2</span>]);
<span class="hljs-keyword">target</span> += <span class="hljs-built_in">lognormal_lpdf</span>(x[ : , <span class="hljs-number">3</span>] | mu[<span class="hljs-number">3</span>], sigma[<span class="hljs-number">3</span>]);
<span class="hljs-keyword">target</span> += <span class="hljs-built_in">weibull_lpdf</span>(x[ : , <span class="hljs-number">4</span>] | sigma[<span class="hljs-number">4</span>], sigma[<span class="hljs-number">5</span>]);

{
<span class="hljs-type">matrix</span>[K, N] y;
<span class="hljs-keyword">for</span> (n <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> : N) {
Expand All @@ -37,4 +39,4 @@
}
<span class="hljs-title">generated</span> <span class="hljs-title">quantities</span> {
<span class="hljs-type">matrix</span>[K, K] Sigma = <span class="hljs-built_in">multiply_lower_tri_self_transpose</span>(L);
}
}
6 changes: 4 additions & 2 deletions test/markup/stan/default.txt
Expand Up @@ -5,6 +5,7 @@ data {
int<lower=0> N;
int K;
matrix[N, K] x;
complex_row_vector[K] z;
}
transformed data {
complex zi = 1+3.14i;
Expand All @@ -16,13 +17,14 @@ parameters {
array[K - 1] real mu;
array[K + 1] real<lower=0> sigma;
cholesky_factor_corr[K] L;
tuple(real, tuple(real, real)) t;
}
model {
target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]);
target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]);
target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]);
target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]);

{
matrix[K, N] y;
for (n in 1 : N) {
Expand All @@ -37,4 +39,4 @@ model {
}
generated quantities {
matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L);
}
}