From db03ecaaad3fdad064b5bb90532efa811022ef48 Mon Sep 17 00:00:00 2001 From: spinkney Date: Sat, 10 Aug 2024 07:55:00 -0400 Subject: [PATCH] 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); +}