Fitting: better initial guess
authorHeikki Levanto <heikki@indexdata.dk>
Wed, 18 Dec 2013 15:18:22 +0000 (16:18 +0100)
committerHeikki Levanto <heikki@indexdata.dk>
Wed, 18 Dec 2013 15:18:22 +0000 (16:18 +0100)
The two test cases now converge to within two decimals in 19 and 46
iterations, which must be good enough. The results look reasonable.

src/relevance.c
test/test_rank_12.res

index 4cbf7f2..02dc971 100644 (file)
@@ -165,8 +165,8 @@ static void setup_norm_record( struct relevance *rel,  struct record_cluster *cl
         } else {
             if ( rp->score > norm->max )
                 norm->max = rp->score;
-            if ( rp->score < norm->min && abs(rp->score) < 1e-6 )
-                norm->min = rp->score;  // skip zeroes
+            if ( rp->score < norm->min )
+                norm->min = rp->score; 
         }
     }
 }
@@ -190,14 +190,15 @@ static double squaresum( struct norm_record *rp, double a, double b)
 static void normalize_scores(struct relevance *rel)
 {
     const int maxiterations = 1000;
-    const double enough = 1000.0;  // sets the number of decimals we are happy with
+    const double enough = 100.0;  // sets the number of decimals we are happy with
     const double stepchange = 0.5; // reduction of the step size when finding middle
       // 0.5 sems to be magical, much better than 0.4 or 0.6
     struct norm_client *norm;
     for ( norm = rel->norm; norm; norm = norm->next )
     {
-        yaz_log(YLOG_LOG,"Normalizing client %d: scorefield=%d count=%d range=%f %f",
-                norm->num, norm->scorefield, norm->count, norm->min, norm->max);
+        yaz_log(YLOG_LOG,"Normalizing client %d: scorefield=%d count=%d range=%f %f = %f",
+                norm->num, norm->scorefield, norm->count, norm->min,
+                norm->max, norm->max-norm->min);
         norm->a = 1.0; // default normalizing factors, no change
         norm->b = 0.0;
         if ( norm->scorefield != scorefield_none &&
@@ -210,13 +211,26 @@ static void normalize_scores(struct relevance *rel)
             double chi;
             char *branch = "?";
             // initial guesses for the parameters
+            // Rmax = a * rmax + b    # want to be 1.0
+            // Rmin = a * rmin + b    # want to be 0.0
+            // Rmax - Rmin = a ( rmax - rmin )    # subtracting equations
+            // 1.0 - 0.0 = a ( rmax - rmin )
+            // a = 1 / range
+            // Rmin = a * rmin + b
+            // b = Rmin - a * rmin
+            //   = 0.0 - 1/range * rmin
+            //   = - rmin / range
+            
             if ( range < 1e-6 ) // practically zero
                 range = norm->max;
             a = 1.0 / range;
-            b = abs(norm->min);
+            b = -1.0 * norm->min / range;
+            // b = fabs(norm->min) / range;
             as = a / 10;
-            bs = b / 10;
+            bs = fabs(b) / 10;
             chi = squaresum( norm->records, a,b);
+            yaz_log(YLOG_LOG,"Initial done: it=%d: a=%f / %f  b=%f / %f  chi = %f",
+                        0, a, as, b, bs, chi );
             while (it++ < maxiterations)  // safeguard against things not converging
             {
                 double aplus = squaresum(norm->records, a+as, b);
@@ -269,7 +283,7 @@ static void normalize_scores(struct relevance *rel)
                         branch = "step b";
                     }
                 }
-                yaz_log(YLOG_LOG,"Fitting %s it=%d: a=%f %f  b=%f %f  chi=%f ap=%f am=%f, bp=%f bm=%f p=%f",
+                yaz_log(YLOG_LOG,"Fitting %s it=%d: a=%g %g  b=%g %g  chi=%g ap=%g am=%g, bp=%g bm=%g p=%g",
                     branch, it, a, as, b, bs, chi,
                     aplus, aminus, bplus, bminus, prevchi );
                 norm->a = a;
@@ -280,12 +294,8 @@ static void normalize_scores(struct relevance *rel)
 
                 }
             }
-            yaz_log(YLOG_LOG,"Fitting done: it=%d: a=%f / %f  b=%f / %f  chi = %f",
+            yaz_log(YLOG_LOG,"Fitting done: it=%d: a=%g / %g  b=%g / %g  chi = %g",
                         it-1, a, as, b, bs, chi );
-            yaz_log(YLOG_LOG,"  a: %f < %f %d",
-                    fabs(as)*enough, fabs(a), (fabs(as) * enough < fabs(a)) );
-            yaz_log(YLOG_LOG,"  b: %f < %f %d",
-                    fabs(bs)*enough, fabs(b), (fabs(bs) * enough < fabs(b)) );
         }
 
         if ( norm->scorefield != scorefield_none )
index f03b6fc..43c4128 100644 (file)
@@ -21,7 +21,7 @@
   <md-score>2.304635</md-score>
  </location>
  <count>1</count>
- <relevance>6645</relevance>
+ <relevance>8294</relevance>
  <recid>content: title water management problems and challenges in india author dinesh kumar m medium book</recid>
 </hit>
 <hit>
@@ -40,7 +40,7 @@
   <md-score>2.231453</md-score>
  </location>
  <count>1</count>
- <relevance>5215</relevance>
+ <relevance>6424</relevance>
  <recid>content: title the magic of water author hochschwender ted medium book</recid>
 </hit>
 <hit>
@@ -57,7 +57,7 @@
   <md-score>2.186368</md-score>
  </location>
  <count>1</count>
- <relevance>4334</relevance>
+ <relevance>5273</relevance>
  <recid>content: title water author de villiers marq medium book</recid>
 </hit>
 <hit>
@@ -74,7 +74,7 @@
   <md-score>2.186368</md-score>
  </location>
  <count>1</count>
- <relevance>4334</relevance>
+ <relevance>5273</relevance>
  <recid>content: title water use for public water supply in michigan medium book</recid>
 </hit>
 <hit>
@@ -91,7 +91,7 @@
   <md-score>2.114981</md-score>
  </location>
  <count>1</count>
- <relevance>2939</relevance>
+ <relevance>3449</relevance>
  <recid>content: title report to the iucn on water demand management country study medium book</recid>
 </hit>
 <hit>
   <md-score>2.061328</md-score>
  </location>
  <count>1</count>
- <relevance>1890</relevance>
+ <relevance>2078</relevance>
  <recid>content: title evaluation and control of water pollution in bhavani basin medium book</recid>
 </hit>
 <hit>
   <md-score>2.061328</md-score>
  </location>
  <count>1</count>
- <relevance>1890</relevance>
+ <relevance>2078</relevance>
  <recid>content: title unsia water cluster medium book</recid>
 </hit>
 <hit>
   <md-score>2.061328</md-score>
  </location>
  <count>1</count>
- <relevance>1890</relevance>
+ <relevance>2078</relevance>
  <recid>content: title water and water supplies author thresh john clough medium book</recid>
 </hit>
 <hit>
   <md-score>2.061328</md-score>
  </location>
  <count>1</count>
- <relevance>1890</relevance>
+ <relevance>2078</relevance>
  <recid>content: title water author majeed abdul medium book</recid>
 </hit>
 <hit>
   <md-score>2.061328</md-score>
  </location>
  <count>1</count>
- <relevance>1890</relevance>
+ <relevance>2078</relevance>
  <recid>content: title water law author fisher d e medium book</recid>
 </hit>
 <hit>
   <md-score>2.061328</md-score>
  </location>
  <count>1</count>
- <relevance>1890</relevance>
+ <relevance>2078</relevance>
  <recid>content: title water technology management medium book</recid>
 </hit>
 <hit>
   <md-score>2.037029</md-score>
  </location>
  <count>1</count>
- <relevance>1415</relevance>
+ <relevance>1457</relevance>
  <recid>content: title wonderful water author glover david medium book</recid>
 </hit>
 <hit>
   <md-score>2.016555</md-score>
  </location>
  <count>1</count>
- <relevance>1015</relevance>
+ <relevance>934</relevance>
  <recid>content: title a primer on fresh water medium book</recid>
 </hit>
 <hit>
   <md-score>2.016555</md-score>
  </location>
  <count>2</count>
- <relevance>1015</relevance>
+ <relevance>934</relevance>
  <recid>content: title water quality assessment of the state water project medium book</recid>
 </hit>
 <hit>
   <md-score>1.928196</md-score>
  </location>
  <count>1</count>
- <relevance>-710</relevance>
+ <relevance>-1322</relevance>
  <recid>content: title district water supply plan medium book</recid>
 </hit>
 <hit>
   <md-score>1.928196</md-score>
  </location>
  <count>1</count>
- <relevance>-710</relevance>
+ <relevance>-1322</relevance>
  <recid>content: title proposition medium book</recid>
 </hit>
 <hit>
   <md-score>1.928196</md-score>
  </location>
  <count>1</count>
- <relevance>-710</relevance>
+ <relevance>-1322</relevance>
  <recid>content: title wastewater and drinking water user charge survey medium book</recid>
 </hit>
 <hit>
   <md-score>1.928196</md-score>
  </location>
  <count>1</count>
- <relevance>-710</relevance>
+ <relevance>-1322</relevance>
  <recid>content: title water in press medium book</recid>
 </hit>
 <hit>
   <md-score>1.928196</md-score>
  </location>
  <count>1</count>
- <relevance>-710</relevance>
+ <relevance>-1322</relevance>
  <recid>content: title who governs water author frey hans medium book</recid>
 </hit>
 </show>
\ No newline at end of file