diff --git a/CHANGES.md b/CHANGES.md index 208041d3f8..a15582a946 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,8 @@ Grammars: - enh(js/ts) improve `CLASS_REFERENCE` (#3411) [Josh Goebel][] - enh(nsis) Update defines pattern to allow `!` (#3417) [idleberg][] - enh(nsis) Update language strings pattern to allow `!` (#3420) [idleberg][] +- fix(stan) Updated for Stan 2.28 and other misc. improvements (#3410) +- enh(nsis) Update variables pattern (#3416) [idleberg][] - fix(clojure) Several issues with Clojure highlighting (#3397) [Björn Ebbinghaus][] - fix(clojure) `comment` macro catches more than it should (#3395) - fix(clojure) `$` in symbol breaks highlighting @@ -35,7 +37,6 @@ Grammars: - enh(clojure) Add `regex` mode to regex literal - fix(clojure) Remove inconsistent/broken highlighting for metadata - enh(clojure) Add `punctuation` mode for commas. - - enh(nsis) Update variables pattern (#3416) [idleberg][] Developer Tools: diff --git a/src/languages/stan.js b/src/languages/stan.js index 3bd657282b..1636359f22 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -1,12 +1,13 @@ /* Language: Stan Description: The Stan probabilistic programming language -Author: Jeffrey B. Arnold +Author: Sean Pinkney Website: http://mc-stan.org/ Category: scientific */ export default function(hljs) { + const regex = hljs.regex; // variable names cannot conflict with block identifiers const BLOCKS = [ 'functions', @@ -17,6 +18,7 @@ export default function(hljs) { 'transformed', 'generated' ]; + const STATEMENTS = [ 'for', 'in', @@ -27,16 +29,10 @@ export default function(hljs) { 'continue', 'return' ]; - const SPECIAL_FUNCTIONS = [ - 'print', - 'reject', - 'increment_log_prob|10', - 'integrate_ode|10', - 'integrate_ode_rk45|10', - 'integrate_ode_bdf|10', - 'algebra_solver' - ]; - const VAR_TYPES = [ + + const TYPES = [ + 'array', + 'complex', 'int', 'real', 'vector', @@ -52,13 +48,24 @@ export default function(hljs) { 'cov_matrix|10', 'void' ]; + + // to get the functions list + // clone the [stan-docs repo](https://github.com/stan-dev/docs) + // then cd into it and run this bash script https://gist.github.com/joshgoebel/dcd33f82d4059a907c986049893843cf + // + // the output files are + // distributions_quoted.txt + // functions_quoted.txt + const FUNCTIONS = [ 'Phi', 'Phi_approx', 'abs', 'acos', 'acosh', + 'add_diag', 'algebra_solver', + 'algebra_solver_newton', 'append_array', 'append_col', 'append_row', @@ -67,56 +74,21 @@ export default function(hljs) { 'atan', 'atan2', 'atanh', - 'bernoulli_cdf', - 'bernoulli_lccdf', - 'bernoulli_lcdf', - 'bernoulli_logit_lpmf', - 'bernoulli_logit_rng', - 'bernoulli_lpmf', - 'bernoulli_rng', 'bessel_first_kind', 'bessel_second_kind', - 'beta_binomial_cdf', - 'beta_binomial_lccdf', - 'beta_binomial_lcdf', - 'beta_binomial_lpmf', - 'beta_binomial_rng', - 'beta_cdf', - 'beta_lccdf', - 'beta_lcdf', - 'beta_lpdf', - 'beta_rng', 'binary_log_loss', - 'binomial_cdf', 'binomial_coefficient_log', - 'binomial_lccdf', - 'binomial_lcdf', - 'binomial_logit_lpmf', - 'binomial_lpmf', - 'binomial_rng', 'block', - 'categorical_logit_lpmf', - 'categorical_logit_rng', - 'categorical_lpmf', - 'categorical_rng', - 'cauchy_cdf', - 'cauchy_lccdf', - 'cauchy_lcdf', - 'cauchy_lpdf', - 'cauchy_rng', 'cbrt', 'ceil', - 'chi_square_cdf', - 'chi_square_lccdf', - 'chi_square_lcdf', - 'chi_square_lpdf', - 'chi_square_rng', + 'chol2inv', 'cholesky_decompose', 'choose', 'col', 'cols', 'columns_dot_product', 'columns_dot_self', + 'conj', 'cos', 'cosh', 'cov_exp_quad', @@ -134,34 +106,16 @@ export default function(hljs) { 'diagonal', 'digamma', 'dims', - 'dirichlet_lpdf', - 'dirichlet_rng', 'distance', 'dot_product', 'dot_self', - 'double_exponential_cdf', - 'double_exponential_lccdf', - 'double_exponential_lcdf', - 'double_exponential_lpdf', - 'double_exponential_rng', - 'e', 'eigenvalues_sym', 'eigenvectors_sym', 'erf', 'erfc', 'exp', 'exp2', - 'exp_mod_normal_cdf', - 'exp_mod_normal_lccdf', - 'exp_mod_normal_lcdf', - 'exp_mod_normal_lpdf', - 'exp_mod_normal_rng', 'expm1', - 'exponential_cdf', - 'exponential_lccdf', - 'exponential_lcdf', - 'exponential_lpdf', - 'exponential_rng', 'fabs', 'falling_factorial', 'fdim', @@ -170,94 +124,68 @@ export default function(hljs) { 'fmax', 'fmin', 'fmod', - 'frechet_cdf', - 'frechet_lccdf', - 'frechet_lcdf', - 'frechet_lpdf', - 'frechet_rng', - 'gamma_cdf', - 'gamma_lccdf', - 'gamma_lcdf', - 'gamma_lpdf', 'gamma_p', 'gamma_q', - 'gamma_rng', - 'gaussian_dlm_obs_lpdf', + 'generalized_inverse', + 'get_imag', 'get_lp', - 'gumbel_cdf', - 'gumbel_lccdf', - 'gumbel_lcdf', - 'gumbel_lpdf', - 'gumbel_rng', + 'get_real', 'head', - 'hypergeometric_lpmf', - 'hypergeometric_rng', + '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', 'inv', 'inv_Phi', - 'inv_chi_square_cdf', - 'inv_chi_square_lccdf', - 'inv_chi_square_lcdf', - 'inv_chi_square_lpdf', - 'inv_chi_square_rng', 'inv_cloglog', - 'inv_gamma_cdf', - 'inv_gamma_lccdf', - 'inv_gamma_lcdf', - 'inv_gamma_lpdf', - 'inv_gamma_rng', 'inv_logit', 'inv_sqrt', 'inv_square', - 'inv_wishart_lpdf', - 'inv_wishart_rng', 'inverse', 'inverse_spd', 'is_inf', 'is_nan', + 'lambert_w0', + 'lambert_wm1', 'lbeta', 'lchoose', + 'ldexp', 'lgamma', - 'lkj_corr_cholesky_lpdf', - 'lkj_corr_cholesky_rng', - 'lkj_corr_lpdf', - 'lkj_corr_rng', + 'linspaced_array', + 'linspaced_int_array', + 'linspaced_row_vector', + 'linspaced_vector', 'lmgamma', 'lmultiply', 'log', - 'log10', 'log1m', 'log1m_exp', 'log1m_inv_logit', 'log1p', 'log1p_exp', - 'log2', 'log_determinant', 'log_diff_exp', 'log_falling_factorial', 'log_inv_logit', + 'log_inv_logit_diff', 'log_mix', + 'log_modified_bessel_first_kind', 'log_rising_factorial', 'log_softmax', 'log_sum_exp', - 'logistic_cdf', - 'logistic_lccdf', - 'logistic_lcdf', - 'logistic_lpdf', - 'logistic_rng', 'logit', - 'lognormal_cdf', - 'lognormal_lccdf', - 'lognormal_lcdf', - 'lognormal_lpdf', - 'lognormal_rng', 'machine_precision', + 'map_rect', 'matrix_exp', + 'matrix_exp_multiply', + 'matrix_power', 'max', 'mdivide_left_spd', 'mdivide_left_tri_low', @@ -267,120 +195,80 @@ export default function(hljs) { 'min', 'modified_bessel_first_kind', 'modified_bessel_second_kind', - 'multi_gp_cholesky_lpdf', - 'multi_gp_lpdf', - 'multi_normal_cholesky_lpdf', - 'multi_normal_cholesky_rng', - 'multi_normal_lpdf', - 'multi_normal_prec_lpdf', - 'multi_normal_rng', - 'multi_student_t_lpdf', - 'multi_student_t_rng', - 'multinomial_lpmf', - 'multinomial_rng', 'multiply_log', 'multiply_lower_tri_self_transpose', - 'neg_binomial_2_cdf', - 'neg_binomial_2_lccdf', - 'neg_binomial_2_lcdf', - 'neg_binomial_2_log_lpmf', - 'neg_binomial_2_log_rng', - 'neg_binomial_2_lpmf', - 'neg_binomial_2_rng', - 'neg_binomial_cdf', - 'neg_binomial_lccdf', - 'neg_binomial_lcdf', - 'neg_binomial_lpmf', - 'neg_binomial_rng', 'negative_infinity', - 'normal_cdf', - 'normal_lccdf', - 'normal_lcdf', - 'normal_lpdf', - 'normal_rng', + 'norm', 'not_a_number', 'num_elements', - 'ordered_logistic_lpmf', - 'ordered_logistic_rng', + 'ode_adams', + 'ode_adams_tol', + 'ode_adjoint_tol_ctl', + 'ode_bdf', + 'ode_bdf_tol', + 'ode_ckrk', + 'ode_ckrk_tol', + 'ode_rk45', + 'ode_rk45_tol', + 'one_hot_array', + 'one_hot_int_array', + 'one_hot_row_vector', + 'one_hot_vector', + 'ones_array', + 'ones_int_array', + 'ones_row_vector', + 'ones_vector', 'owens_t', - 'pareto_cdf', - 'pareto_lccdf', - 'pareto_lcdf', - 'pareto_lpdf', - 'pareto_rng', - 'pareto_type_2_cdf', - 'pareto_type_2_lccdf', - 'pareto_type_2_lcdf', - 'pareto_type_2_lpdf', - 'pareto_type_2_rng', - 'pi', - 'poisson_cdf', - 'poisson_lccdf', - 'poisson_lcdf', - 'poisson_log_lpmf', - 'poisson_log_rng', - 'poisson_lpmf', - 'poisson_rng', + 'polar', 'positive_infinity', 'pow', 'print', 'prod', + 'proj', 'qr_Q', 'qr_R', + 'qr_thin_Q', + 'qr_thin_R', 'quad_form', 'quad_form_diag', 'quad_form_sym', + 'quantile', 'rank', - 'rayleigh_cdf', - 'rayleigh_lccdf', - 'rayleigh_lcdf', - 'rayleigh_lpdf', - 'rayleigh_rng', + 'reduce_sum', 'reject', 'rep_array', 'rep_matrix', 'rep_row_vector', 'rep_vector', + 'reverse', 'rising_factorial', 'round', 'row', 'rows', 'rows_dot_product', 'rows_dot_self', - 'scaled_inv_chi_square_cdf', - 'scaled_inv_chi_square_lccdf', - 'scaled_inv_chi_square_lcdf', - 'scaled_inv_chi_square_lpdf', - 'scaled_inv_chi_square_rng', + 'scale_matrix_exp_multiply', 'sd', 'segment', 'sin', 'singular_values', 'sinh', 'size', - 'skew_normal_cdf', - 'skew_normal_lccdf', - 'skew_normal_lcdf', - 'skew_normal_lpdf', - 'skew_normal_rng', 'softmax', 'sort_asc', 'sort_desc', 'sort_indices_asc', 'sort_indices_desc', 'sqrt', - 'sqrt2', 'square', 'squared_distance', 'step', - 'student_t_cdf', - 'student_t_lccdf', - 'student_t_lcdf', - 'student_t_lpdf', - 'student_t_rng', 'sub_col', 'sub_row', 'sum', + 'svd_U', + 'svd_V', + 'symmetrize_from_lower_tri', 'tail', 'tan', 'tanh', @@ -389,6 +277,7 @@ export default function(hljs) { 'tgamma', 'to_array_1d', 'to_array_2d', + 'to_complex', 'to_matrix', 'to_row_vector', 'to_vector', @@ -397,35 +286,29 @@ export default function(hljs) { 'trace_quad_form', 'trigamma', 'trunc', - 'uniform_cdf', - 'uniform_lccdf', - 'uniform_lcdf', - 'uniform_lpdf', - 'uniform_rng', + 'uniform_simplex', 'variance', - 'von_mises_lpdf', - 'von_mises_rng', - 'weibull_cdf', - 'weibull_lccdf', - 'weibull_lcdf', - 'weibull_lpdf', - 'weibull_rng', - 'wiener_lpdf', - 'wishart_lpdf', - 'wishart_rng' + 'zeros_array', + 'zeros_int_array', + 'zeros_row_vector' ]; + const DISTRIBUTIONS = [ 'bernoulli', 'bernoulli_logit', + 'bernoulli_logit_glm', 'beta', 'beta_binomial', + 'beta_proportion', 'binomial', 'binomial_logit', 'categorical', 'categorical_logit', + 'categorical_logit_glm', 'cauchy', 'chi_square', 'dirichlet', + 'discrete_range', 'double_exponential', 'exp_mod_normal', 'exponential', @@ -433,6 +316,7 @@ export default function(hljs) { 'gamma', 'gaussian_dlm_obs', 'gumbel', + 'hmm_latent', 'hypergeometric', 'inv_chi_square', 'inv_gamma', @@ -448,18 +332,26 @@ export default function(hljs) { 'multi_normal_prec', 'multi_student_t', 'multinomial', + 'multinomial_logit', 'neg_binomial', 'neg_binomial_2', 'neg_binomial_2_log', + 'neg_binomial_2_log_glm', 'normal', + 'normal_id_glm', 'ordered_logistic', + 'ordered_logistic_glm', + 'ordered_probit', 'pareto', 'pareto_type_2', 'poisson', 'poisson_log', + 'poisson_log_glm', 'rayleigh', 'scaled_inv_chi_square', + 'skew_double_exponential', 'skew_normal', + 'std_normal', 'student_t', 'uniform', 'von_mises', @@ -475,7 +367,7 @@ export default function(hljs) { relevance: 0, contains: [ { - className: 'doctag', + scope: 'doctag', match: /@(return|param)/ } ] @@ -483,27 +375,33 @@ export default function(hljs) { ); const INCLUDE = { - className: 'meta', - begin: /^#include\b/, + scope: 'meta', + begin: /#include\b/, end: /$/, - relevance: 0, // relevance comes from keywords - keywords: "include", contains: [ { - match: /[a-z][a-z-.]+/, - className: "string" + match: /[a-z][a-z-._]+/, + scope: 'string' }, hljs.C_LINE_COMMENT_MODE ] }; + const RANGE_CONSTRAINTS = [ + "lower", + "upper", + "offset", + "multiplier" + ]; + return { name: 'Stan', aliases: [ 'stanfuncs' ], keywords: { $pattern: hljs.IDENT_RE, title: BLOCKS, - keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS), + type: TYPES, + keyword: STATEMENTS, built_in: FUNCTIONS }, contains: [ @@ -512,41 +410,81 @@ export default function(hljs) { hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, { - // hack: in range constraints, lower must follow "<" - begin: /<\s*lower\s*=/, - keywords: 'lower' + scope: 'built_in', + match: /\s(pi|e|sqrt2|log2|log10)(?=\()/, + relevance: 0 + }, + { + match: regex.concat(/[<,]\s*/, regex.either(...RANGE_CONSTRAINTS), /\s*=/), + keywords: RANGE_CONSTRAINTS }, { - // hack: in range constraints, upper must follow either , or < - // or - begin: /[<,]\s*upper\s*=/, - keywords: 'upper' + scope: 'keyword', + match: /\btarget(?=\s*\+=)/, }, { - className: 'keyword', - begin: /\btarget\s*\+=/ + // highlights the 'T' in T[,] for only Stan language distributrions + match: [ + /~\s*/, + regex.either(...DISTRIBUTIONS), + /(?:\(\))/, + /\s*T(?=\s*\[)/ + ], + scope: { + 2: "built_in", + 4: "keyword" + } }, { - begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(', - keywords: DISTRIBUTIONS + // highlights distributions that end with special endings + scope: 'built_in', + keywords: DISTRIBUTIONS, + begin: regex.concat(/\w*/, regex.either(...DISTRIBUTIONS), /(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/) }, { - className: 'number', - variants: [ - { - begin: /\b\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/ - }, - { - begin: /\.\d+(?:[eE][+-]?\d+)?\b/ - } + // highlights distributions after ~ + begin: [ + /~/, + /\s*/, + regex.concat(regex.either(...DISTRIBUTIONS), /(?=\s*[\(.*\)])/) ], - relevance: 0 + scope: { 3: "built_in" } + }, + { + // highlights user defined distributions after ~ + begin: [ + /~/, + /\s*\w+(?=\s*[\(.*\)])/, + '(?!.*/\b(' + regex.either(...DISTRIBUTIONS) + ')\b)' + ], + scope: { 2: "title.function" } + }, + { + // highlights user defined distributions with special endings + scope: 'title.function', + begin: /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ }, { - className: 'string', - begin: '"', - end: '"', + scope: 'number', + match: regex.concat( + // Comes from @RunDevelopment accessed 11/29/2021 at + // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 + + // start of big noncapture group which + // 1. gets numbers that are by themselves + // 2. numbers that are separated by _ + // 3. numbers that are separted by . + /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/, + // grabs scientific notation + // grabs complex numbers with i + /(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/ + ), relevance: 0 + }, + { + scope: 'string', + begin: /"/, + end: /"/ } ] }; diff --git a/test/detect/stan/default.txt b/test/detect/stan/default.txt index 99e56c45ec..a4b42f332e 100644 --- a/test/detect/stan/default.txt +++ b/test/detect/stan/default.txt @@ -1,39 +1,40 @@ -// Multivariate Regression Example -// Taken from stan-reference-2.8.0.pdf p.66 - +functions { + #include normal_copula.stanfunctions +} data { - int N; // num individuals - int K; // num ind predictors - int J; // num groups - int L; // num group predictors - int jj[N]; // group for individual - matrix[N,K] x; // individual predictors - row_vector[L] u[J]; // group predictors - vector[N] y; // outcomes + int N; + int K; + matrix[N, K] x; } +transformed data { + complex zi = 1+3.14i; + zi = zi * 0i; + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); +}› parameters { - corr_matrix[K] Omega; // prior correlation - vector[K] tau; // prior scale - matrix[L,K] gamma; // group coeffs - vector[K] beta[J]; // indiv coeffs by group - real sigma; // prediction error scale + array[K - 1] real mu; + array[K + 1] real sigma; + cholesky_factor_corr[K] L; } model { - tau ~ cauchy(0,2.5); - Omega ~ lkj_corr(2); - to_vector(gamma) ~ normal(0, 5); - { - row_vector[K] u_gamma[J]; - for (j in 1:J) - u_gamma[j] <- u[j] * gamma; - beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau)); - } + 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]); + { - vector[N] x_beta_jj; - for (n in 1:N) - x_beta_jj[n] <- x[n] * beta[jj[n]]; - y ~ normal(x_beta_jj, sigma); + matrix[K, N] y; + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + } + y ~ multi_normal(L); + } } - -# Note: Octothorpes indicate comments, too! +generated quantities { + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); +} \ No newline at end of file diff --git a/test/markup/stan/default.expect.txt b/test/markup/stan/default.expect.txt index 6a32c43433..aeb9552aa5 100644 --- a/test/markup/stan/default.expect.txt +++ b/test/markup/stan/default.expect.txt @@ -1,39 +1,40 @@ -// Multivariate Regression Example -// Taken from stan-reference-2.8.0.pdf p.66 - +functions { + #include normal_copula.stanfunctions +} data { - int<lower=0> N; // num individuals - int<lower=1> K; // num ind predictors - int<lower=1> J; // num groups - int<lower=1> L; // num group predictors - int<lower=1,upper=J> jj[N]; // group for individual - matrix[N,K] x; // individual predictors - row_vector[L] u[J]; // group predictors - vector[N] y; // outcomes + int<lower=0> N; + int K; + matrix[N, K] x; } +transformed data { + complex zi = 1+3.14i; + zi = zi * 0i; + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); +}› parameters { - corr_matrix[K] Omega; // prior correlation - vector<lower=0>[K] tau; // prior scale - matrix[L,K] gamma; // group coeffs - vector[K] beta[J]; // indiv coeffs by group - real<lower=0> sigma; // prediction error scale + array[K - 1] real mu; + array[K + 1] real<lower=0> sigma; + cholesky_factor_corr[K] L; } model { - tau ~ cauchy(0,2.5); - Omega ~ lkj_corr(2); - to_vector(gamma) ~ normal(0, 5); - { - row_vector[K] u_gamma[J]; - for (j in 1:J) - u_gamma[j] <- u[j] * gamma; - beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau)); - } + 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]); + { - vector[N] x_beta_jj; - for (n in 1:N) - x_beta_jj[n] <- x[n] * beta[jj[n]]; - y ~ normal(x_beta_jj, sigma); + matrix[K, N] y; + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + } + y ~ multi_normal(L); + } } - -# Note: Octothorpes indicate comments, too! +generated quantities { + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); +} \ No newline at end of file diff --git a/test/markup/stan/default.txt b/test/markup/stan/default.txt index 99e56c45ec..a4b42f332e 100644 --- a/test/markup/stan/default.txt +++ b/test/markup/stan/default.txt @@ -1,39 +1,40 @@ -// Multivariate Regression Example -// Taken from stan-reference-2.8.0.pdf p.66 - +functions { + #include normal_copula.stanfunctions +} data { - int N; // num individuals - int K; // num ind predictors - int J; // num groups - int L; // num group predictors - int jj[N]; // group for individual - matrix[N,K] x; // individual predictors - row_vector[L] u[J]; // group predictors - vector[N] y; // outcomes + int N; + int K; + matrix[N, K] x; } +transformed data { + complex zi = 1+3.14i; + zi = zi * 0i; + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); +}› parameters { - corr_matrix[K] Omega; // prior correlation - vector[K] tau; // prior scale - matrix[L,K] gamma; // group coeffs - vector[K] beta[J]; // indiv coeffs by group - real sigma; // prediction error scale + array[K - 1] real mu; + array[K + 1] real sigma; + cholesky_factor_corr[K] L; } model { - tau ~ cauchy(0,2.5); - Omega ~ lkj_corr(2); - to_vector(gamma) ~ normal(0, 5); - { - row_vector[K] u_gamma[J]; - for (j in 1:J) - u_gamma[j] <- u[j] * gamma; - beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau)); - } + 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]); + { - vector[N] x_beta_jj; - for (n in 1:N) - x_beta_jj[n] <- x[n] * beta[jj[n]]; - y ~ normal(x_beta_jj, sigma); + matrix[K, N] y; + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + } + y ~ multi_normal(L); + } } - -# Note: Octothorpes indicate comments, too! +generated quantities { + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); +} \ No newline at end of file