-
Notifications
You must be signed in to change notification settings - Fork 27
/
esl_dirichlet.c
372 lines (319 loc) · 10.8 KB
/
esl_dirichlet.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Dirichlet and Beta densities.
*
* Contents:
* 1. Dirichlet likelihood functions
* 2. Sampling from Dirichlets
* 3. Unit tests
* 4. Test driver
* 5. Example
*
* See also:
* esl_mixdchlet : mixture Dirichlets
* esl_gamma: Gamma densities
*/
#include <esl_config.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "easel.h"
#include "esl_fileparser.h"
#include "esl_minimizer.h"
#include "esl_random.h"
#include "esl_stats.h"
#include "esl_vectorops.h"
#include "esl_dirichlet.h"
/*****************************************************************
*# 1. Dirichlet likelihood functions
*****************************************************************/
/* Function: esl_dirichlet_logpdf()
*
* Purpose: Given Dirichlet parameter vector <alpha> and a probability
* vector <p>, both of cardinality <K>; return
* $\log P(p \mid alpha)$.
*
* Returns: $\log P(p \mid alpha)$.
*
* Xref: Sjolander (1996) appendix, lemma 2.
*/
double
esl_dirichlet_logpdf(double *p, double *alpha, int K)
{
double sum; /* for Gammln(|alpha|) in Z */
double logp; /* RETURN: log P(p|alpha) */
double val;
int a;
sum = logp = 0.0;
for (a = 0; a < K; a++)
if (p[a] > 0.0) /* any param that is == 0.0 doesn't exist */
{
esl_stats_LogGamma(alpha[a], &val);
logp -= val;
logp += (alpha[a]-1.0) * log(p[a]);
sum += alpha[a];
}
esl_stats_LogGamma(sum, &val); // esl_stats_LogGamma() can only fail for x < 0; here sum > 0
logp += val;
return logp;
}
/* Function: esl_dirichlet_logpdf_c()
*
* Purpose: Given an observed count vector $c[0..K-1]$,
* and a simple Dirichlet density parameterized by
* $\alpha[0..K-1]$;
* return $\log P(c \mid \alpha)$.
*
* This is $\int P(c \mid p) P(p \mid \alpha) dp$,
* an integral that can be solved analytically.
*
* Args: c - count vector, [0..K-1]
* alpha - Dirichlet parameters, [0..K-1]
* K - size of c, alpha vectors
*
* Returns: <eslOK> on success, and puts result $\log P(c \mid \alpha)$
* in <ret_answer>.
*/
double
esl_dirichlet_logpdf_c(double *c, double *alpha, int K)
{
double logp;
double sum1, sum2, sum3;
double a1, a2, a3;
int a;
sum1 = sum2 = sum3 = logp = 0.0;
for (a = 0; a < K; a++)
{
sum1 += c[a] + alpha[a];
sum2 += alpha[a];
sum3 += c[a];
esl_stats_LogGamma(alpha[a] + c[a], &a1);
esl_stats_LogGamma(c[a] + 1., &a2);
esl_stats_LogGamma(alpha[a], &a3);
logp += a1 - a2 - a3;
}
esl_stats_LogGamma(sum1, &a1);
esl_stats_LogGamma(sum2, &a2);
esl_stats_LogGamma(sum3 + 1., &a3);
logp += a2 + a3 - a1;
return logp;
}
/*----------- end, Dirichlet likelihood functions ---------------*/
/*****************************************************************
*# 2. Sampling from Dirichlets
*****************************************************************/
/* Function: esl_dirichlet_DSample()
*
* Purpose: Given a Dirichlet density parameterized by $\alpha[0..K-1]$,
* sample a probability vector $p[0..K-1]$ from
* $P(p \mid \alpha)$.
*
* Args: r - random number generation object
* alpha - parameters of Dirichlet density [0..K-1]
* K - vector size
* p - RETURN: sampled probability vector
* (caller allocates 0..K-1).
*
* Returns: <eslOK>, and <p> will contain the sampled vector.
*/
int
esl_dirichlet_DSample(ESL_RANDOMNESS *r, double *alpha, int K, double *p)
{
int x;
for (x = 0; x < K; x++)
p[x] = esl_rnd_Gamma(r, alpha[x]);
esl_vec_DNorm(p, K);
return eslOK;
}
/* Function: esl_dirichlet_FSample()
*
* Purpose: Same as <esl_dirichlet_DSample()>, except it
* works in single-precision floats, not doubles.
*/
int
esl_dirichlet_FSample(ESL_RANDOMNESS *r, float *alpha, int K, float *p)
{
int x;
for (x = 0; x < K; x++)
p[x] = (float) esl_rnd_Gamma(r, (double) alpha[x]);
esl_vec_FNorm(p, K);
return eslOK;
}
/* Function: esl_dirichlet_DSampleUniform()
*
* Purpose: Sample a probability vector $p[0..K-1]$ uniformly, by
* sampling from a Dirichlet of $\alpha_i = 1.0 \forall i$.
*
* Args: r - source of random numbers
* K - vector size
* p - RETURN: sampled prob vector, caller alloc'ed 0..K-1
*
* Returns: <eslOK>, and <p> will contain the sampled vector.
*
* Throws: (no abnormal error conditions)
*/
int
esl_dirichlet_DSampleUniform(ESL_RANDOMNESS *r, int K, double *p)
{
int x;
for (x = 0; x < K; x++)
p[x] = esl_rnd_Gamma(r, 1.0);
esl_vec_DNorm(p, K);
return eslOK;
}
/* Function: esl_dirichlet_FSampleUniform()
*
* Purpose: Same as <esl_dirichlet_DSampleUniform()>, except it
* works in single-precision floats, not doubles.
*/
int
esl_dirichlet_FSampleUniform(ESL_RANDOMNESS *r, int K, float *p)
{
int x;
for (x = 0; x < K; x++)
p[x] = (float) esl_rnd_Gamma(r, 1.0);
esl_vec_FNorm(p, K);
return eslOK;
}
/* Function: esl_dirichlet_SampleBeta()
*
* Purpose: Samples from a Beta(theta1, theta2) density, leaves answer
* in <ret_answer>. (Special case of sampling Dirichlet.)
*
* Returns: <eslOK>.
*/
int
esl_dirichlet_SampleBeta(ESL_RANDOMNESS *r, double theta1, double theta2, double *ret_answer)
{
double p, q;
p = esl_rnd_Gamma(r, theta1);
q = esl_rnd_Gamma(r, theta2);
*ret_answer = p / (p+q);
return eslOK;
}
/*-------------- end, sampling from Dirichlets -------------------*/
/*****************************************************************
* 3. Unit tests
*****************************************************************/
#ifdef eslDIRICHLET_TESTDRIVE
/* utest_uniformity()
* Tests that probability vectors sampled from a uniform Dirichlet
* density indeed have uniform probabilities.
*/
static void
utest_uniformity(ESL_RANDOMNESS *rng)
{
char msg[] = "esl_dirichlet unifomity test failed";
int K = 4;
int N = 100; // number of counts to collect
double tol = 1e-6;
double alpha[K], p[K], c[K];
double logp, prv_logp;
double logpc, prv_logpc;
int i,j;
esl_vec_DSet(alpha, K, 1.0); // uniform density P(p | alpha)
for (i = 0; i < 20; i++)
{
esl_dirichlet_DSample(rng, alpha, 4, p);
esl_vec_DSet(c, K, 0.);
for (j = 0; j < N; j++)
c[ esl_rnd_DChoose(rng, p, K) ] += 1.;
logp = esl_dirichlet_logpdf(p, alpha, 4);
if (! isfinite(logp)) esl_fatal(msg); // PDF is a density; can't test for <= 1.0
if (i > 0 && esl_DCompare_old(logp, prv_logp, tol) != eslOK) esl_fatal(msg);
logpc = esl_dirichlet_logpdf_c(c, alpha, K);
if (! isfinite(logpc)) esl_fatal(msg);
if (i > 0 && esl_DCompare_old(logpc, prv_logpc, tol) != eslOK) esl_fatal(msg);
prv_logp = logp;
prv_logpc = logpc;
}
}
#endif // eslDIRICHLET_TESTDRIVE
/*****************************************************************
* 4. Test driver
*****************************************************************/
#ifdef eslDIRICHLET_TESTDRIVE
#include "easel.h"
#include "esl_getopts.h"
#include "esl_random.h"
static ESL_OPTIONS options[] = {
/* name type default env range toggles reqs incomp help docgroup*/
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
static char usage[] = "[-options]";
static char banner[] = "test driver for dirichlet module";
int
main(int argc, char **argv)
{
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
fprintf(stderr, "## %s\n", argv[0]);
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
utest_uniformity(rng);
fprintf(stderr, "# status = ok\n");
esl_randomness_Destroy(rng);
esl_getopts_Destroy(go);
return 0;
}
#endif // eslDIRICHLET_TESTDRIVE
/*****************************************************************
* 5. Example
*****************************************************************/
#ifdef eslDIRICHLET_EXAMPLE
#include "easel.h"
#include "esl_getopts.h"
#include "esl_random.h"
#include "esl_vectorops.h"
#include "esl_dirichlet.h"
static ESL_OPTIONS options[] = {
/* name type default env range toggles reqs incomp help docgroup*/
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
static char usage[] = "[-options]";
static char banner[] = "examples of using dirichlet module";
int
main(int argc, char **argv)
{
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
int K = 4;
double *alpha1 = malloc(sizeof(double) * K);
double *alpha2 = malloc(sizeof(double) * K);
double *p = malloc(sizeof(double) * K);
double *c = malloc(sizeof(double) * K);
int sumc = 1000;
int N = 1000;
double logp1, logp2;
int j,a;
esl_vec_DSet(alpha1, K, 1.0); // alpha1 is a flat prior
esl_vec_DSet(alpha2, K, 10.0); // alpha2 is a peaked prior, with modes p_i = 1/K
/* Sample probability & count vector from Dirichlet 1.
* Calculate its log probability under both.
* Output p vector, c vector, and the two log P's.
* Since Dirichlet 1 is a uniform distribution, first logp is always the same.
*/
while (N--)
{
esl_dirichlet_DSample(rng, alpha1, K, p);
esl_vec_DSet(c, K, 0.);
for (j = 0; j < sumc; j++) c[esl_rnd_DChoose(rng, p, K)] += 1.;
logp1 = esl_dirichlet_logpdf_c(c, alpha1, K);
logp2 = esl_dirichlet_logpdf_c(c, alpha2, K);
printf("[ ");
for (a = 0; a < K; a++) printf("%6.4f ", p[a]);
printf("] [ ");
for (a = 0; a < K; a++) printf("%6.0f ", c[a]);
printf("] %10.4g %10.4g\n", logp1, logp2);
}
free(alpha1);
free(alpha2);
free(p);
free(c);
esl_randomness_Destroy(rng);
esl_getopts_Destroy(go);
return 0;
}
#endif //eslDIRICHLET_EXAMPLE