comparison sample.cpp @ 268:30a2a45f2b70 sampling

Write y / yinv functions (using the GNU Scientific Library); use them to estimate sigma^2 and d. (Results questionable as yet.)
author mas01cr
date Mon, 16 Jun 2008 11:15:15 +0000
parents 4ffa05f25a00
children dcbb30790b30
comparison
equal deleted inserted replaced
267:40a93d5d462c 268:30a2a45f2b70
1 #include "audioDB.h" 1 #include "audioDB.h"
2
3 #include <gsl/gsl_sf.h>
4
5 static double yfun(double d) {
6 return gsl_sf_log(d) - gsl_sf_psi(d);
7 }
8
9 static double yinv(double y) {
10 double a = 1.0e-5;
11 double b = 1000.0;
12
13 double ay = yfun(a);
14 double by = yfun(b);
15
16 double c, cy;
17
18 /* FIXME: simple binary search */
19 while ((b - a) > 1.0e-5) {
20 c = (a + b) / 2;
21 cy = yfun(c);
22 if (cy > y) {
23 a = c;
24 ay = cy;
25 } else {
26 b = c;
27 by = cy;
28 }
29 }
30
31 return c;
32 }
2 33
3 unsigned audioDB::random_track(unsigned *propTable, unsigned total) { 34 unsigned audioDB::random_track(unsigned *propTable, unsigned total) {
4 /* FIXME: make this O(1) by using the alias-rejection method, or 35 /* FIXME: make this O(1) by using the alias-rejection method, or
5 some other sensible method of sampling from a discrete 36 some other sensible method of sampling from a discrete
6 distribution. */ 37 distribution. */
104 } else { 135 } else {
105 VERB_LOG(1, "infinity found: %f %f %f\n", v1norm, v2norm, v1v2); 136 VERB_LOG(1, "infinity found: %f %f %f\n", v1norm, v2norm, v1v2);
106 } 137 }
107 } 138 }
108 139
140 double sigma2 = (sumdist / (sequenceLength * dbH->dim * 1037));
141 double d = 2 * yinv(log(sumdist/1037) - sumlogdist/1037);
142
109 std::cout << "Summary statistics" << std::endl; 143 std::cout << "Summary statistics" << std::endl;
110 std::cout << "number of samples: " << 1037 << std::endl; 144 std::cout << "number of samples: " << 1037 << std::endl;
111 std::cout << "sum of distances (S): " << sumdist << std::endl; 145 std::cout << "sum of distances (S): " << sumdist << std::endl;
112 std::cout << "sum of log distances (L): " << sumlogdist << std::endl; 146 std::cout << "sum of log distances (L): " << sumlogdist << std::endl;
147 std::cout << std::endl;
148 std::cout << "Estimated parameters" << std::endl;
149 std::cout << "sigma^2: " << sigma2 << std::endl;
150 std::cout << "d: " << d << std::endl;
151 std::cout << "check: " << yfun(d/2) << std::endl;
113 152
114 /* FIXME: we'll also want some summary statistics based on 153 /* FIXME: we'll also want some summary statistics based on
115 propTable, for the minimum-of-X estimate */ 154 propTable, for the minimum-of-X estimate */
116 155
117 delete[] propTable; 156 delete[] propTable;