-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcoupled_ordered_dirichlet.stan
More file actions
164 lines (133 loc) · 3.52 KB
/
Copy pathcoupled_ordered_dirichlet.stan
File metadata and controls
164 lines (133 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// coupled_ordered_dirichlet.stan
//
// Coupled hurdle-inclusion ordered Dirichlet regression.
//
// Allows:
// y_i = all zeros
// component-level zeros
// component-level ones
// Dirichlet allocation over active components
//
// Key idea:
// z_ik ~ Bernoulli(rho_ik)
// A_i = {k : z_ik = 1}
// A_i = empty -> y_i = all zeros
// A_i nonempty -> y_i lies on simplex over A_i
//
// No conditioning constant is needed because the empty active set is allowed.
functions {
real coupled_ordered_dirichlet_lpdf(vector y,
vector theta,
real phi,
real tau,
real a_rho,
real eps) {
int K = num_elements(y);
real total = sum(y);
vector[K] mu = softmax(theta);
real lp_A = 0;
// Case 1: all-zero row.
if (total <= eps) {
for (k in 1:K) {
real eta_k = a_rho * theta[k] - tau;
lp_A += bernoulli_logit_lpmf(0 | eta_k);
}
return lp_A;
}
// Case 2: nonzero row.
{
vector[K] y_norm = y / total;
int A_size = 0;
array[K] int active;
for (k in 1:K) {
real eta_k = a_rho * theta[k] - tau;
if (y_norm[k] > eps) {
A_size += 1;
active[A_size] = k;
lp_A += bernoulli_logit_lpmf(1 | eta_k);
} else {
lp_A += bernoulli_logit_lpmf(0 | eta_k);
}
}
// This should not happen because total > eps,
// but keep as a guard.
if (A_size == 0) {
return negative_infinity();
}
// Singleton active set -> vertex of simplex.
// Dirichlet part is degenerate and contributes 0.
if (A_size == 1) {
return lp_A;
}
// Multiple active components -> Dirichlet over active face.
{
vector[A_size] y_A;
vector[A_size] alpha_A;
real mu_sum = 0;
for (a in 1:A_size) {
mu_sum += mu[active[a]];
}
for (a in 1:A_size) {
int k = active[a];
y_A[a] = y_norm[k];
alpha_A[a] = phi * mu[k] / mu_sum;
}
return lp_A + dirichlet_lpdf(y_A | alpha_A);
}
}
}
}
data {
int<lower=1> N;
int<lower=2> K;
int<lower=1> P;
matrix[N, P] X;
// y can include all-zero rows.
array[N] vector<lower=0>[K] y;
real<lower=0> eps;
}
parameters {
// Baseline category K fixed to zero for softmax identification.
matrix[P, K - 1] beta_raw;
// Shared component-inclusion threshold.
real tau;
// Inclusion scale. Positive version recommended for interpretation.
real<lower=0> a_rho;
// Precision model.
vector[P] delta;
}
transformed parameters {
matrix[P, K] beta;
for (k in 1:(K - 1)) {
beta[, k] = beta_raw[, k];
}
beta[, K] = rep_vector(0, P);
}
model {
// Priors.
to_vector(beta_raw) ~ normal(0, 1);
tau ~ normal(0, 2);
a_rho ~ lognormal(0, 0.5);
delta ~ normal(0, 1);
for (i in 1:N) {
vector[K] theta_i;
real phi_i;
theta_i = beta' * to_vector(X[i]');
phi_i = exp(dot_product(to_vector(X[i]'), delta));
target += coupled_ordered_dirichlet_lpdf(
y[i] | theta_i, phi_i, tau, a_rho, eps
);
}
}
generated quantities {
array[N] real log_lik;
for (i in 1:N) {
vector[K] theta_i;
real phi_i;
theta_i = beta' * to_vector(X[i]');
phi_i = exp(dot_product(to_vector(X[i]'), delta));
log_lik[i] = coupled_ordered_dirichlet_lpdf(
y[i] | theta_i, phi_i, tau, a_rho, eps
);
}
}