From db03ecaaad3fdad064b5bb90532efa811022ef48 Mon Sep 17 00:00:00 2001 From: spinkney Date: Sat, 10 Aug 2024 07:55:00 -0400 Subject: [PATCH 1/2] add transform --- .../Exponential/Exponential_functions.stan | 21 +++++++++++++++++++ .../Exponential_parameters_log_simplex.stan | 7 +++++++ .../Exponential_parameters_simplex.stan | 6 ++++++ 3 files changed, 34 insertions(+) create mode 100644 simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan create mode 100644 simplex_transforms/stan/transforms/Exponential/Exponential_parameters_log_simplex.stan create mode 100644 simplex_transforms/stan/transforms/Exponential/Exponential_parameters_simplex.stan diff --git a/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan b/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan new file mode 100644 index 0000000..e91092c --- /dev/null +++ b/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan @@ -0,0 +1,21 @@ +vector exponential_simplex_constrain_lp(vector y) { + int N = rows(y); + vector[N] x_pos = y - log1p_exp(-y); + real r = log_sum_exp(x_pos); + vector[N] x = exp(x_pos - r); + target += x_pos; + target += std_normal_lpdf(r - log(N)); + target += sum(y) - N * r; + return x; +} + +vector exponential_log_simplex_constrain_lp(vector y) { + int N = rows(y); + vector[N] x = y - log1p_exp(-y); + real r = log_sum_exp(x); + vector[N] log_x = x - r; + target += x; + target += std_normal_lpdf(r - log(N)); + target += log_x[N]; + return log_x; +} diff --git a/simplex_transforms/stan/transforms/Exponential/Exponential_parameters_log_simplex.stan b/simplex_transforms/stan/transforms/Exponential/Exponential_parameters_log_simplex.stan new file mode 100644 index 0000000..fc7a88b --- /dev/null +++ b/simplex_transforms/stan/transforms/Exponential/Exponential_parameters_log_simplex.stan @@ -0,0 +1,7 @@ +parameters { + vector[N] y; +} +transformed parameters { + vector[N] log_x = exponential_log_simplex_constrain_lp(y); + simplex[N] x = exp(log_x); +} diff --git a/simplex_transforms/stan/transforms/Exponential/Exponential_parameters_simplex.stan b/simplex_transforms/stan/transforms/Exponential/Exponential_parameters_simplex.stan new file mode 100644 index 0000000..6560313 --- /dev/null +++ b/simplex_transforms/stan/transforms/Exponential/Exponential_parameters_simplex.stan @@ -0,0 +1,6 @@ +parameters { + vector[N] y; +} +transformed parameters { + simplex[N] x = exponential_simplex_constrain_lp(y); +} From 74d8a32de1e0491e05a6d3718fc2d8c6e6b448ab Mon Sep 17 00:00:00 2001 From: spinkney Date: Mon, 12 Aug 2024 17:28:14 -0400 Subject: [PATCH 2/2] Update Exponential_functions.stan --- .../transforms/Exponential/Exponential_functions.stan | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan b/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan index e91092c..65fea1e 100644 --- a/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan +++ b/simplex_transforms/stan/transforms/Exponential/Exponential_functions.stan @@ -1,11 +1,10 @@ vector exponential_simplex_constrain_lp(vector y) { int N = rows(y); - vector[N] x_pos = y - log1p_exp(-y); + vector[N] z = y - log1p_exp(-y); real r = log_sum_exp(x_pos); vector[N] x = exp(x_pos - r); - target += x_pos; target += std_normal_lpdf(r - log(N)); - target += sum(y) - N * r; + target += sum(y); return x; } @@ -14,8 +13,7 @@ vector exponential_log_simplex_constrain_lp(vector y) { vector[N] x = y - log1p_exp(-y); real r = log_sum_exp(x); vector[N] log_x = x - r; - target += x; target += std_normal_lpdf(r - log(N)); - target += log_x[N]; + target += log_x[N] - N * r; return log_x; }