simpler alternative to the West algorithm to convince ourselves that we can still...
authorChristian Grothoff <christian@grothoff.org>
Wed, 23 Nov 2011 14:32:38 +0000 (14:32 +0000)
committerChristian Grothoff <christian@grothoff.org>
Wed, 23 Nov 2011 14:32:38 +0000 (14:32 +0000)
src/nse/gnunet-service-nse.c

index a0e7923ace61f758316e82d11d310b4515750a78..782f30514e6e61db6961b23c0df8b8b1467a9a37 100644 (file)
@@ -308,14 +308,16 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
   double std_dev;
   double variance;
   double val;
-  double weight;
+  double nsize;
+
+  /* Weighted incremental algorithm for stddev according to West (1979) */
+#if WEST
   double sumweight;
+  double weight;
   double q;
   double r;
   double temp;
-  double nsize;
 
-  /* Weighted incremental algorithm for stddev according to West (1979) */
   mean = 0.0;
   sum = 0.0;
   sumweight = 0.0;
@@ -335,6 +337,24 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
     sumweight = temp;
   }
   variance = sum / (sumweight - 1.0);
+#else
+  /* trivial version for debugging */
+  double vsq;
+
+  /* non-weighted trivial version */
+  sum = 0.0;
+  vsq = 0.0;
+  for (i = 0; i < estimate_count; i++)
+  {
+    val = htonl (size_estimate_messages
+                [(estimate_index - i +
+                  HISTORY_SIZE) % HISTORY_SIZE].matching_bits);
+    sum += val;
+    vsq += val * val;    
+  }  
+  mean = sum / estimate_count;
+  variance = vsq + mean * mean - 2 * mean * sum; // terrible for numerical stability...
+#endif
   GNUNET_assert (variance >= 0);
   std_dev = sqrt (variance);
   current_std_dev = std_dev;