-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDir.java
155 lines (138 loc) · 3.49 KB
/
MDir.java
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
/*
* This program implements the modified Dirichlet distribution and its mode
* finding algorithm as described in:
*
* Kewei Tu, "Modified Dirichlet Distribution: Allowing Negative Parameters to
* Induce Stronger Sparsity". In the Conference on Empirical Methods in Natural
* Language Processing (EMNLP 2016).
*
* If you use this code in your research, please cite the publication.
*/
package cn.edu.shanghaitech.ai.mdir;
/**
* Modified Dirichlet distribution. It is an extension of the Dirichlet
* distribution that allows the parameters to be negative. It not only induces
* much stronger sparsity, but also simultaneously performs smoothing. It is
* still conjugate to the multinomial distribution.
*
* @author Kewei Tu
*
*/
public class MDir {
protected int dim;
/**
* c_i = alpha_i - 1
*/
protected double[] c;
protected double epsilon;
protected double[] mode;
public MDir(int dim, double[] alpha, double epsilon) {
this.dim = dim;
c = new double[dim];
if (alpha != null)
setAlpha(alpha);
double maxEps = 1.0 / dim;
if (epsilon > maxEps) {
System.err
.println("[mDir Warning] MDir(): espilon is too large, reset to the max value.");
this.epsilon = maxEps;
} else if (epsilon < 0) {
System.err
.println("[mDir Warning] MDir(): espilon is negative, reset to 0.");
this.epsilon = 0;
} else
this.epsilon = epsilon;
}
public void setAlpha(double[] alpha) {
for (int i = 0; i < dim; i++) {
c[i] = alpha[i] - 1;
}
mode = null;
}
public double[] getMode() {
if (mode != null)
return mode;
mode = new double[dim];
double sum = 0;
int nEps = 0;
for (int i = 0; i < dim; i++) {
if (c[i] > 0) {
mode[i] = c[i];
sum += mode[i];
} else {
mode[i] = -1;
nEps++;
}
}
// if c<=0 for all i
if (nEps == dim) {
int iMax = 0;
double max = c[0];
mode[0] = epsilon;
boolean multiModes = false;
for (int i = 1; i < dim; i++) {
if (c[i] > max) {
max = c[i];
iMax = i;
multiModes = false;
} else if (c[i] == max)
multiModes = true;
mode[i] = epsilon;
}
mode[iMax] = 1 - epsilon * (dim - 1);
// warn if there are multiple equivalent modes
if (multiModes)
System.err
.println("[mDir Warning] getMode(): multiple modes, returning one of them.");
return mode;
}
boolean done;
do {
double x = (1 - epsilon * nEps) / sum;
sum = 0;
done = true;
for (int i = 0; i < dim; i++) {
if (mode[i] != -1) {
mode[i] *= x;
if (mode[i] < epsilon) {
mode[i] = -1;
nEps++;
done = false;
} else {
sum += mode[i];
}
}
}
} while (!done);
for (int i = 0; i < dim; i++) {
if (mode[i] == -1)
mode[i] = epsilon;
}
return mode;
}
/**
* @return the unnormalized log probability at the mode
*/
public double getLogProbAtMode() {
if (mode == null)
getMode();
double logProbAtMode = 0;
for (int i = 0; i < dim; i++) {
logProbAtMode += c[i] * Math.log(mode[i]);
}
return logProbAtMode;
}
/**
* @param point
* @return the unnormalized log probability at the point
*/
public double getLogProb(double[] point) {
double logProbAtMode = 0;
for (int i = 0; i < dim; i++) {
if (c[i] != 0 || point[i] != 0)
logProbAtMode += c[i] * Math.log(point[i]);
// otherwise, it is 0 * log 0, which should be 0
}
return logProbAtMode;
}
}