From c5c2f3475311faecf2d3618cab9d0f06a8d54aa2 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Wed, 27 Sep 2023 16:45:58 -0400 Subject: [PATCH 1/2] multi logit normal density --- target_densities/multi-logit-normal.stan | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 target_densities/multi-logit-normal.stan diff --git a/target_densities/multi-logit-normal.stan b/target_densities/multi-logit-normal.stan new file mode 100644 index 0000000..03c3365 --- /dev/null +++ b/target_densities/multi-logit-normal.stan @@ -0,0 +1,15 @@ +/** + * Return the multivariate logistic normal density for the specified log simplex. + * + * See: https://en.wikipedia.org/wiki/Logit-normal_distribution#Multivariate_generalization + * + * @param theta a simplex (N rows) + * @param mu location of normal (N-1 rows) + * @param L_Sigma Cholesky factor of covariance (N-1 rows, N-1 cols) + */ +real multi_logit_normal_cholesky_lpdf(vector theta, vector mu, matrix L_Sigma) { + real lp; + lp += sum(-log(theta)); + lp += multi_normal_cholesky_lpdf(log(theta[1:N-1] / theta[N]) | mu, L_Sigma); + return lp; +} From 544c4a06884a9bf114658ca4951fc18d4db0f5eb Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Fri, 29 Sep 2023 08:18:48 -0400 Subject: [PATCH 2/2] log logistic-normal --- target_densities/multi-logit-normal.stan | 45 ++++++++++++++++-------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/target_densities/multi-logit-normal.stan b/target_densities/multi-logit-normal.stan index 03c3365..af97e42 100644 --- a/target_densities/multi-logit-normal.stan +++ b/target_densities/multi-logit-normal.stan @@ -1,15 +1,30 @@ -/** - * Return the multivariate logistic normal density for the specified log simplex. - * - * See: https://en.wikipedia.org/wiki/Logit-normal_distribution#Multivariate_generalization - * - * @param theta a simplex (N rows) - * @param mu location of normal (N-1 rows) - * @param L_Sigma Cholesky factor of covariance (N-1 rows, N-1 cols) - */ -real multi_logit_normal_cholesky_lpdf(vector theta, vector mu, matrix L_Sigma) { - real lp; - lp += sum(-log(theta)); - lp += multi_normal_cholesky_lpdf(log(theta[1:N-1] / theta[N]) | mu, L_Sigma); - return lp; -} +// PASTE INTO functions { ... } to define densities + + /** + * Return the multivariate logistic normal density for the specified log simplex. + * + * See: https://en.wikipedia.org/wiki/Logit-normal_distribution#Multivariate_generalization + * + * @param theta a log simplex (N rows) + * @param mu location of normal (N-1 rows) + * @param L_Sigma Cholesky factor of covariance (N-1 rows, N-1 cols) + */ + real multi_log_logit_normal_cholesky_lpdf(vector log_theta, vector mu, matrix L_Sigma) { + int N = rows(log_theta); + return multi_normal_cholesky_lpdf(log_theta[1:N-1] - log_theta[N] | mu, L_Sigma); + } + + /** + * Return the multivariate logistic normal density for the specified simplex. + * + * See: https://en.wikipedia.org/wiki/Logit-normal_distribution#Multivariate_generalization + * + * @param theta a simplex (N rows) + * @param mu location of normal (N-1 rows) + * @param L_Sigma Cholesky factor of covariance (N-1 rows, N-1 cols) + */ + real multi_logit_normal_cholesky_lpdf(vector theta, vector mu, matrix L_Sigma) { + vector[rows(theta)] log_theta = log(theta); + return sum(-log_theta) + + multi_log_logit_normal_cholesky_lpdf(log_theta | mu, L_Sigma); + }