2
2
#include < gsl/gsl_blas.h>
3
3
#include < gsl/gsl_sf_lambert.h>
4
4
#include < vector>
5
+ #include < math.h>
5
6
6
7
class Flim {
7
8
// Actual parameters.
@@ -40,7 +41,7 @@ class Flim {
40
41
empirical_pair_(N, N),
41
42
empirical_singleton_(N) {
42
43
gsl_matrix_float_set_zero (lambda_);
43
- gsl_vector_float_set_zero (ones_);
44
+ gsl_vector_float_set_all (ones_, 1.0 );
44
45
}
45
46
46
47
~Flim () {
@@ -108,7 +109,6 @@ class Flim {
108
109
initializeKappa (num_documents);
109
110
}
110
111
111
-
112
112
float sigmoid (float x) {
113
113
return 1.0 / (1.0 + exp (-x));
114
114
}
@@ -130,19 +130,66 @@ class Flim {
130
130
return exp (p11) / (exp (p11) + exp (p10) + exp (p01) + 1 );
131
131
}
132
132
133
- void optimizeAll () {
133
+ double optimizeAll () {
134
+ double total_delta = 0.0 ;
134
135
for (int x = 0 ; x < lambda_.nrow (); ++x) {
135
136
for (int y = 0 ; y < x; ++y) {
136
- optimizeLambda (y, x);
137
+ total_delta += optimizeLambda (y, x);
137
138
}
138
139
}
140
+ return total_delta;
139
141
}
140
142
141
- void optimizeLambda (unsigned int x, unsigned int y) {
143
+ /* *
144
+ * Proof of lemma 3.2.1:
145
+ *
146
+ * x = a - b e^x
147
+ *
148
+ * iff
149
+ *
150
+ * x = a - W(be^a)
151
+ * => W(be^a) = a - x
152
+ * => (a-x) exp(a - x) = be^a
153
+ *
154
+ */
155
+
156
+ double optimizeLambda (unsigned int x, unsigned int y) {
157
+ #ifdef NEW_WAY
142
158
double A = getComputedExpectation (x, y) * exp (-lambda_ (x, y));
143
159
double B = 2 * beta2_;
144
160
double C = empirical_pair_ (x, y) - beta1_;
161
+ #else
162
+ double A = getComputedExpectation (x, y);
163
+ double B = 2 * beta2_;
164
+ double C = empirical_pair_ (x, y) - beta1_ - 2 * beta2_ * lambda_ (x, y);
165
+ #endif
166
+
167
+ // #define DEBUGGING_NOISE
168
+ #ifdef DEBUGGING_NOISE
169
+ if (empirical_pair_ (x, y) > 0 && rand () < RAND_MAX / 1000 ) {
170
+ double overcount_x = lambda_ (x,y) * singleton_expectation_[y];
171
+ double overcount_y = lambda_ (x,y) * singleton_expectation_[x];
145
172
173
+ double p10 = q_lambda_[x] + kappa_[x] - overcount_x;
174
+ double p01 = q_lambda_[y] + kappa_[y] - overcount_y;
175
+
176
+ double p11 = estimates_ (x,y) - overcount_x - overcount_y;
177
+
178
+ std::cout << getComputedExpectation (x, y) << " "
179
+ << empirical_pair_ (x, y) << " "
180
+ << kappa_[x] << " "
181
+ << kappa_[y] << " "
182
+ << lambda_ (x,y) << " "
183
+ << x << " " << y << " "
184
+ << q_lambda_[x] << " "
185
+ << q_lambda_[y] << " "
186
+ << estimates_ (x,y) << " "
187
+ << overcount_x << " " << overcount_y << " "
188
+ << p10 << " " << p01 << " " << p11 << " "
189
+ << std::endl;
190
+ }
191
+ #endif
192
+
146
193
double delta1 = 0 ;
147
194
double delta2 = 0 ;
148
195
if (beta2_ == 0 ) {
@@ -171,8 +218,14 @@ class Flim {
171
218
new_lambda = 0 ;
172
219
}
173
220
221
+ double delta = fabs (new_lambda - lambda_ (x,y));
222
+ if (delta - delta != 0 ) {
223
+ delta = 0.0 ;
224
+ }
225
+
174
226
lambda_ (x,y) = new_lambda;
175
227
lambda_ (y,x) = new_lambda;
228
+ return delta;
176
229
}
177
230
178
231
RcppGSL::matrix<float > getLambda () {
0 commit comments