forked from blei-lab/hlda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyperparameter.c
executable file
·51 lines (38 loc) · 1.24 KB
/
hyperparameter.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
#include "hyperparameter.h"
/*
(p 585 in escobar and west)
function of a,b,k,n,\alpha
1. sample \eta ~ B(\alpha + 1, n)
where n = the number of documents allocated to me
2. sample c ~ Bern(\pi)
where \pi/(1-\pi) = a+k-1 / n(b-log(\eta))
3. if c = 0 then
sample \alpha' ~ G(a + k, b - log(\eta))
else
sample \alpha' ~ G(a + k - 1, b - log(\eta))
*/
double gibbs_sample_DP_scaling(double alpha, // current alpha
double shape, // Gamma shape parameter
double scale, // Gamma scale parameter
int k, // number of components
int n) // number of data points
{
printf("alpha=%g\nshape=%g\nscale=%g\nk=%d\nn=%d\n",
alpha, shape, scale, k, n);
double eta = rbeta(alpha + 1, (double) n);
double pi = shape + k - 1;
double rate = 1.0/scale - log(eta);
pi = pi / (pi + rate * n);
int c = rbernoulli(pi);
double alpha_new = 0;
if (c == 0)
{
alpha_new = rgamma(shape + k - 1, 1.0/rate);
}
else
{
alpha_new = rgamma(shape + k, 1.0/rate);
}
printf("-----\nnew alpha=%g\n-----\n", alpha_new);
return(alpha_new);
}