]> git.sesse.net Git - foosball/blob - foosrank.cpp
Adjusted initial parameters for maximum prediction power; in particular,
[foosball] / foosrank.cpp
1 #include <cstdio>
2 #include <cmath>
3 #include <cassert>
4
5 #include <vector>
6 #include <algorithm>
7
8 #include <complex>
9 #include <fftw3.h>
10
11 #define USE_LOGISTIC_DISTRIBUTION 0
12
13 // step sizes
14 static const double int_step_size = 75.0;
15
16 // rating constant (see below)
17 static const double rating_constant = 455.0;
18
19 using namespace std;
20
21 static double prob_score_real(int k, int a, double binomial, double rd_norm);
22 static double prodai(int k, int a);
23 static double fac(int x);
24
25 #if USE_LOGISTIC_DISTRIBUTION
26 // sech²(x)
27 static double sech2(double x)
28 {
29         double e = exp(2.0 * x);
30         return 4.0 * e / ((e+1.0) * (e+1.0));
31 }
32 #endif
33
34 #if 0
35 // probability of match ending k-a (k>a) when winnerR - loserR = RD
36 //
37 //   +inf  
38 //     / 
39 //    |
40 //    | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
41 //    |
42 //   /
43 // -inf
44 //
45 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
46 //
47 // The constant of 455 is chosen carefully so to match with the
48 // Glicko/Bradley-Terry assumption that a player rated 400 points over
49 // his/her opponent will win with a probability of 10/11 =~ 0.90909. 
50 //
51 static double prob_score(int k, int a, double rd)
52 {
53         return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant);
54 }
55 #endif
56
57 // computes x^a, probably more efficiently than pow(x, a) (but requires that a
58 // is n unsigned integer)
59 static double intpow(double x, unsigned a)
60 {
61         double result = 1.0;
62
63         while (a > 0) {
64                 if (a & 1) {
65                         result *= x;
66                 }
67                 a >>= 1;
68                 x *= x;
69         }
70
71         return result;
72 }
73
74 // Same, but takes in binomial(a+k-1, k-1) as an argument in
75 // addition to a. Faster if you already have that precomputed, and assumes rd
76 // is already divided by 455.
77 static double prob_score_real(int k, int a, double binomial, double rd_norm)
78 {
79         double nom = binomial * intpow(pow(2.0, rd_norm), a); 
80         double denom = intpow(1.0 + pow(2.0, rd_norm), k+a);
81         return nom/denom;
82 }
83
84 // Calculates Product(a+i, i=1..k-1) (see above).
85 static double prodai(int k, int a)
86 {
87         double prod = 1.0;
88         for (int i = 1; i < k; ++i)
89                 prod *= (a+i);
90         return prod;
91 }
92
93 static double fac(int x)
94 {
95         double prod = 1.0;
96         for (int i = 2; i <= x; ++i)
97                 prod *= i;
98         return prod;
99 }
100
101 static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector<pair<double, double> > *result)
102 {
103         double binomial_precompute = prodai(k, a) / fac(k-1);
104         winfac /= rating_constant;
105
106         int sz = (6000.0 - 0.0) / int_step_size;
107         double h = (6000.0 - 0.0) / sz;
108
109         static bool inited = false;
110         static fftw_plan f1, f2, b;
111         static complex<double> *func1, *func2, *res;
112
113         if (!inited) {
114                 func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
115                 func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
116                 res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
117                 f1 = fftw_plan_dft_1d(sz*2,
118                         reinterpret_cast<fftw_complex*>(func1),
119                         reinterpret_cast<fftw_complex*>(func1),
120                         FFTW_FORWARD,
121                         FFTW_MEASURE);
122                 f2 = fftw_plan_dft_1d(sz*2,
123                         reinterpret_cast<fftw_complex*>(func2),
124                         reinterpret_cast<fftw_complex*>(func2),
125                         FFTW_FORWARD,
126                         FFTW_MEASURE);
127                 b = fftw_plan_dft_1d(sz*2,
128                         reinterpret_cast<fftw_complex*>(res),
129                         reinterpret_cast<fftw_complex*>(res),
130                         FFTW_BACKWARD,
131                         FFTW_MEASURE);
132                 inited = true;
133         }
134         
135         // start off by zero
136         for (int i = 0; i < sz*2; ++i) {
137                 func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0;
138         }
139
140 #if USE_LOGISTIC_DISTRIBUTION
141         double invsigma2 = 1.0 / sigma2;
142 #else
143         double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
144 #endif
145         for (int i = 0; i < sz; ++i) {
146                 double x1 = 0.0 + h*i;
147
148                 // opponent's pdf
149 #if USE_LOGISTIC_DISTRIBUTION
150                 double z = (x1 - mu2) * invsigma2;
151                 func1[i].real() = sech2(0.5 * z);
152 #else
153                 double z = (x1 - mu2) * invsq2sigma2;
154                 func1[i].real() = exp(-z*z);
155 #endif
156
157                 double x2 = -3000.0 + h*i;
158                 func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
159         }
160
161         result->reserve(sz*2);
162
163         // convolve
164         fftw_execute(f1);
165         fftw_execute(f2);
166         for (int i = 0; i < sz*2; ++i) {
167                 res[i] = func1[i] * func2[i];
168         }
169         fftw_execute(b);
170
171         result->reserve(sz);
172         for (int i = 0; i < sz; ++i) {
173                 double r1 = i*h;
174                 result->push_back(make_pair(r1, abs(res[i])));
175         }
176 }
177
178 // normalize the curve so we know that A ~= 1
179 static void normalize(vector<pair<double, double> > *curve)
180 {
181         double peak = 0.0;
182         for (vector<pair<double, double> >::const_iterator i = curve->begin(); i != curve->end(); ++i) {
183                 peak = max(peak, i->second);
184         }
185
186         double invpeak = 1.0 / peak;
187         for (vector<pair<double, double> >::iterator i = curve->begin(); i != curve->end(); ++i) {
188                 i->second *= invpeak;
189         }
190 }
191
192 // computes matA^T * matB
193 static void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
194                           double *matB, unsigned bh, unsigned bw,
195                           double *result)
196 {
197         assert(ah == bh);
198         for (unsigned y = 0; y < bw; ++y) {
199                 for (unsigned x = 0; x < aw; ++x) {
200                         double sum = 0.0;
201                         for (unsigned c = 0; c < ah; ++c) {
202                                 sum += matA[x*ah + c] * matB[y*bh + c];
203                         }
204                         result[y*bw + x] = sum;
205                 }
206         }
207 }
208
209 // solves Ax = B by Gauss-Jordan elimination, where A is an NxN matrix,
210 // x is a column vector of length N and B is a row vector of length N.
211 // Destroys its input in the process.
212 template<int N>
213 static void solve_matrix(double *A, double *x, double *B)
214 {
215         for (int i = 0; i < N; ++i) {
216                 for (int j = i+1; j < N; ++j) {
217                         // row j -= row i * (a[i,j] / a[i,i])
218                         double f = A[j+i*N] / A[i+i*N];
219
220                         A[j+i*N] = 0.0;
221                         for (int k = i+1; k < N; ++k) {
222                                 A[j+k*N] -= A[i+k*N] * f;
223                         }
224
225                         B[j] -= B[i] * f;
226                 }
227         }
228
229         // back-substitute
230         for (int i = N; i --> 0; ) {
231                 for (int j = i; j --> 0; ) {
232                         // row j -= row i * (a[j,j] / a[j,i])
233                         double f = A[i+j*N] / A[j+j*N];
234                         
235                         // A[j+i*N] = 0.0;
236                         B[j] -= B[i] * f;
237                 }
238         }
239
240         // normalize
241         for (int i = 0; i < N; ++i) {
242                 x[i] = B[i] / A[i+i*N];
243         }
244 }
245
246 // Give an OK starting estimate for the least squares, by numerical integration
247 // of statistical moments.
248 static void estimate_musigma(const vector<pair<double, double> > &curve, double *mu_result, double *sigma_result)
249 {
250         double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
251
252         double area = curve.front().second;
253         double ex = curve.front().first * curve.front().second;
254         double ex2 = curve.front().first * curve.front().first * curve.front().second;
255
256         for (unsigned i = 1; i < curve.size() - 1; i += 2) {
257                 double x = curve[i].first;
258                 double y = curve[i].second;
259                 area += 4.0 * y;
260                 ex += 4.0 * x * y;
261                 ex2 += 4.0 * x * x * y;
262         }
263         for (unsigned i = 2; i < curve.size() - 1; i += 2) {
264                 double x = curve[i].first;
265                 double y = curve[i].second;
266                 area += 2.0 * y;
267                 ex += 2.0 * x * y;
268                 ex2 += 2.0 * x * x * y;
269         }
270         
271         area += curve.back().second;
272         ex += curve.back().first * curve.back().second;
273         ex2 += curve.back().first * curve.back().first * curve.back().second;
274
275         area = (h/3.0) * area;
276         ex = (h/3.0) * ex / area;
277         ex2 = (h/3.0) * ex2 / area;
278
279         *mu_result = ex;
280         *sigma_result = sqrt(ex2 - ex * ex);
281 }
282         
283 // Find best fit of the data in curves to a Gaussian pdf, based on the
284 // given initial estimates. Works by nonlinear least squares, iterating
285 // until we're below a certain threshold.
286 //
287 // Note that the algorithm blows up quite hard if the initial estimate is
288 // not good enough. Use estimate_musigma to get a reasonable starting
289 // estimate.
290 static void least_squares(const vector<pair<double, double> > &curve, double mu1, double sigma1, double *mu_result, double *sigma_result)
291 {
292         double A = 1.0;
293         double mu = mu1;
294         double sigma = sigma1;
295
296         // column-major
297         double matA[curve.size() * 3];  // N x 3
298         double dbeta[curve.size()];     // N x 1
299
300         // A^T * A: 3xN * Nx3 = 3x3
301         double matATA[3*3];
302
303         // A^T * dβ: 3xN * Nx1 = 3x1
304         double matATdb[3];
305
306         double dlambda[3];
307
308         for ( ;; ) {
309                 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
310
311                 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
312                 for (unsigned i = 0; i < curve.size(); ++i) {
313                         double x = curve[i].first;
314
315 #if USE_LOGISTIC_DISTRIBUTION
316                         // df/dA(x_i)
317                         matA[i + 0 * curve.size()] = sech2(0.5 * (x-mu)/sigma);
318
319                         // df/dµ(x_i)
320                         matA[i + 1 * curve.size()] = A * matA[i + 0 * curve.size()]
321                                 * tanh(0.5 * (x-mu)/sigma) / sigma;
322
323                         // df/dσ(x_i)
324                         matA[i + 2 * curve.size()] = 
325                                 matA[i + 1 * curve.size()] * (x-mu)/sigma;
326 #else
327                         // df/dA(x_i)
328                         matA[i + 0 * curve.size()] = 
329                                 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
330
331                         // df/dµ(x_i)
332                         matA[i + 1 * curve.size()] =
333                                 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
334
335                         // df/dσ(x_i)
336                         matA[i + 2 * curve.size()] = 
337                                 matA[i + 1 * curve.size()] * (x-mu)/sigma;
338 #endif
339                 }
340
341                 // find dβ
342                 for (unsigned i = 0; i < curve.size(); ++i) {
343                         double x = curve[i].first;
344                         double y = curve[i].second;
345
346 #if USE_LOGISTIC_DISTRIBUTION
347                         dbeta[i] = y - A * sech2(0.5 * (x-mu)/sigma);
348 #else
349                         dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
350 #endif
351                 }
352
353                 // compute a and b
354                 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
355                 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
356
357                 // solve
358                 solve_matrix<3>(matATA, dlambda, matATdb);
359
360                 A += dlambda[0];
361                 mu += dlambda[1];
362                 sigma += dlambda[2];
363
364                 // terminate when we're down to three digits
365                 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
366                         break;
367         }
368
369         *mu_result = mu;
370         *sigma_result = sigma;
371 }
372
373 void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double *mu, double *sigma, double *probability)
374 {
375         vector<pair<double, double> > curve;
376
377         if (score1 > score2) {
378                 compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, &curve);
379         } else {
380                 compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, &curve);
381         }
382
383         // multiply in the gaussian
384         for (unsigned i = 0; i < curve.size(); ++i) {
385                 double r1 = curve[i].first;
386
387                 // my pdf
388                 double z = (r1 - mu1) / sigma1;
389 #if USE_LOGISTIC_DISTRIBUTION
390                 curve[i].second *= sech2(0.5 * z);
391 #else
392                 double gaussian = exp(-(z*z/2.0));
393                 curve[i].second *= gaussian;
394 #endif
395         }
396         
397         // Compute the overall probability of the given result, by integrating
398         // the entire resulting pdf. Note that since we're actually evaluating
399         // a double integral, we'll need to multiply by h² instead of h.
400         {
401                 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
402                 double sum = curve.front().second;
403                 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
404                         sum += 4.0 * curve[i].second;
405                 }
406                 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
407                         sum += 2.0 * curve[i].second;
408                 }
409                 sum += curve.back().second;
410                 sum *= h * h / 3.0;
411         
412                 // FFT convolution multiplication factor (FFTW computes unnormalized
413                 // transforms)
414                 sum /= (curve.size() * 2);      
415
416                 // pdf normalization factors
417 #if USE_LOGISTIC_DISTRIBUTION
418                 sum /= (sigma1 * 4.0);
419                 sum /= (sigma2 * 4.0);
420 #else
421                 sum /= (sigma1 * sqrt(2.0 * M_PI));
422                 sum /= (sigma2 * sqrt(2.0 * M_PI));
423 #endif
424
425                 *probability = sum;
426         }
427
428         double mu_est, sigma_est;
429         normalize(&curve);
430         estimate_musigma(curve, &mu_est, &sigma_est);
431         least_squares(curve, mu_est, sigma_est, mu, sigma);
432 }
433
434 static void compute_new_double_rating(double mu1, double sigma1, double mu2, double sigma2, double mu3, double sigma3, double mu4, double sigma4, int score1, int score2, double *mu, double *sigma, double *probability)
435 {
436         vector<pair<double, double> > curve, newcurve;
437         double mu_t = mu3 + mu4;
438         double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
439                         
440         if (score1 > score2) {
441                 compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, &curve);
442         } else {
443                 compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, &curve);
444         }
445
446         newcurve.reserve(curve.size());
447
448         // iterate over r1
449         double h = 3000.0 / curve.size();
450         for (unsigned i = 0; i < curve.size(); ++i) {
451                 double sum = 0.0;
452
453                 // could be anything, but this is a nice start
454                 //double r1 = curve[i].first;
455                 double r1 = i * h;
456
457                 // iterate over r2
458 #if USE_LOGISTIC_DISTRIBUTION
459                 double invsigma2 = 1.0 / sigma2;
460 #else
461                 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
462 #endif
463                 for (unsigned j = 0; j < curve.size(); ++j) {
464                         double r1plusr2 = curve[j].first;
465                         double r2 = r1plusr2 - r1;
466
467 #if USE_LOGISTIC_DISTRIBUTION
468                         double z = (r2 - mu2) * invsigma2;
469                         double gaussian = sech2(0.5 * z);
470 #else   
471                         double z = (r2 - mu2) * invsq2sigma2;
472                         double gaussian = exp(-z*z);
473 #endif
474                         sum += curve[j].second * gaussian;
475                 }
476
477 #if USE_LOGISTIC_DISTRIBUTION
478                 double z = (r1 - mu1) / sigma1;
479                 double gaussian = sech2(0.5 * z);
480 #else
481                 double z = (r1 - mu1) / sigma1;
482                 double gaussian = exp(-(z*z/2.0));
483 #endif
484                 newcurve.push_back(make_pair(r1, gaussian * sum));
485         }
486
487         // Compute the overall probability of the given result, by integrating
488         // the entire resulting pdf. Note that since we're actually evaluating
489         // a triple integral, we'll need to multiply by 4h³ (no idea where the
490         // 4 factor comes from, probably from the 0..6000 range somehow) instead
491         // of h.
492         {
493                 double h = (newcurve.back().first - newcurve.front().first) / (newcurve.size() - 1);
494                 double sum = newcurve.front().second;
495                 for (unsigned i = 1; i < newcurve.size() - 1; i += 2) {
496                         sum += 4.0 * newcurve[i].second;
497                 }
498                 for (unsigned i = 2; i < newcurve.size() - 1; i += 2) {
499                         sum += 2.0 * newcurve[i].second;
500                 }
501                 sum += newcurve.back().second;
502
503                 sum *= 4.0 * h * h * h / 3.0;
504         
505                 // FFT convolution multiplication factor (FFTW computes unnormalized
506                 // transforms)
507                 sum /= (newcurve.size() * 2);   
508
509                 // pdf normalization factors
510 #if USE_LOGISTIC_DISTRIBUTION
511                 sum /= (sigma1 * 4.0);
512                 sum /= (sigma2 * 4.0);
513                 sum /= (sigma_t * 4.0);
514 #else
515                 sum /= (sigma1 * sqrt(2.0 * M_PI));
516                 sum /= (sigma2 * sqrt(2.0 * M_PI));
517                 sum /= (sigma_t * sqrt(2.0 * M_PI));
518 #endif
519
520                 *probability = sum;
521         }
522
523         double mu_est, sigma_est;
524         normalize(&newcurve);
525         estimate_musigma(newcurve, &mu_est, &sigma_est);
526         least_squares(newcurve, mu_est, sigma_est, mu, sigma);
527 }
528
529 int main(int argc, char **argv)
530 {
531         FILE *fp = fopen("fftw-wisdom", "rb");
532         if (fp != NULL) {
533                 fftw_import_wisdom_from_file(fp);
534                 fclose(fp);
535         }
536
537         double mu1 = atof(argv[1]);
538         double sigma1 = atof(argv[2]);
539         double mu2 = atof(argv[3]);
540         double sigma2 = atof(argv[4]);
541
542         if (argc > 10) {
543                 double mu3 = atof(argv[5]);
544                 double sigma3 = atof(argv[6]);
545                 double mu4 = atof(argv[7]);
546                 double sigma4 = atof(argv[8]);
547                 int score1 = atoi(argv[9]);
548                 int score2 = atoi(argv[10]);
549                 double mu, sigma, probability;
550                 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, &mu, &sigma, &probability);
551                 printf("%f %f %f\n", mu, sigma, probability);
552         } else if (argc > 8) {
553                 double mu3 = atof(argv[5]);
554                 double sigma3 = atof(argv[6]);
555                 double mu4 = atof(argv[7]);
556                 double sigma4 = atof(argv[8]);
557                 int k = atoi(argv[9]);
558
559                 // assess all possible scores
560                 for (int i = 0; i < k; ++i) {
561                         double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
562                         double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
563                         double probability;
564                         compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, &newmu1_1, &newsigma1_1, &probability);
565                         compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, &newmu1_2, &newsigma1_2, &probability);
566                         compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, &newmu2_1, &newsigma2_1, &probability);
567                         compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, &newmu2_2, &newsigma2_2, &probability);
568                         printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
569                                 k, i, probability, newmu1_1-mu1, newmu1_2-mu2,
570                                 newmu2_1-mu3, newmu2_2-mu4);
571                 }
572                 for (int i = k; i --> 0; ) {
573                         double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
574                         double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
575                         double probability;
576                         compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, &newmu1_1, &newsigma1_1, &probability);
577                         compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, &newmu1_2, &newsigma1_2, &probability);
578                         compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, &newmu2_1, &newsigma2_1, &probability);
579                         compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, &newmu2_2, &newsigma2_2, &probability);
580                         printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
581                                 i, k, probability, newmu1_1-mu1, newmu1_2-mu2,
582                                 newmu2_1-mu3, newmu2_2-mu4);
583                 }
584         } else if (argc > 6) {
585                 int score1 = atoi(argv[5]);
586                 int score2 = atoi(argv[6]);
587                 double mu, sigma, probability;
588                 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, &mu, &sigma, &probability);
589
590                 printf("%f %f %f\n", mu, sigma, probability);
591         } else {
592                 int k = atoi(argv[5]);
593
594                 // assess all possible scores
595                 for (int i = 0; i < k; ++i) {
596                         double newmu1, newmu2, newsigma1, newsigma2, probability;
597                         compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, &newmu1, &newsigma1, &probability);
598                         compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, &newmu2, &newsigma2, &probability);
599                         printf("%u-%u,%f,%+f,%+f\n",
600                                 k, i, probability, newmu1-mu1, newmu2-mu2);
601                 }
602                 for (int i = k; i --> 0; ) {
603                         double newmu1, newmu2, newsigma1, newsigma2, probability;
604                         compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, &newmu1, &newsigma1, &probability);
605                         compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, &newmu2, &newsigma2, &probability);
606                         printf("%u-%u,%f,%+f,%+f\n",
607                                 i, k, probability, newmu1-mu1, newmu2-mu2);
608                 }
609         }
610         
611         fp = fopen("fftw-wisdom", "wb");
612         if (fp != NULL) {
613                 fftw_export_wisdom_to_file(fp);
614                 fclose(fp);
615         }
616 }
617