histogram.c: histogram_create now takes bin width instead of the number of bins
authorJohn Darrington <john@darrington.wattle.id.au>
Tue, 20 Mar 2012 18:03:09 +0000 (19:03 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Tue, 20 Mar 2012 17:40:09 +0000 (18:40 +0100)
src/language/stats/examine.c
src/language/stats/frequencies.q
src/math/histogram.c
src/math/histogram.h

index 62c99afce14311b33f0a7f78d337747b504e9ae6..5d9f642641e038ec6dd343659bf6dbca52db1ba6 100644 (file)
@@ -40,6 +40,7 @@
 #include "math/interaction.h"
 #include "math/box-whisker.h"
 #include "math/categoricals.h"
+#include "math/chart-geometry.h"
 #include "math/histogram.h"
 #include "math/moments.h"
 #include "math/np.h"
@@ -1516,8 +1517,15 @@ calculate_n (const void *aux1, void *aux2 UNUSED, void *user_data)
 
       if (examine->histogram)
         {
+          /* Sturges Rule */
+          double bin_width = abs (es[v].minimum - es[v].maximum)
+            / (1 + log2 (es[v].cc))
+            ;
+
+          bin_width = chart_rounded_tick (bin_width);
+
           es[v].histogram =
-            histogram_create (10, es[v].minimum, es[v].maximum);
+            histogram_create (bin_width, es[v].minimum, es[v].maximum);
         }
 
       es[v].sorted_reader = casewriter_make_reader (es[v].sorted_writer);
index a2ffe91c1c68145795daebbf104dd217a27a9541..d7754220fa4efe7fc2a4861ef4a806fb9deb29b8 100644 (file)
@@ -43,6 +43,8 @@
 #include "libpspp/str.h"
 #include "math/histogram.h"
 #include "math/moments.h"
+#include "math/chart-geometry.h"
+
 #include "output/chart-item.h"
 #include "output/charts/piechart.h"
 #include "output/charts/plot-hist.h"
@@ -1114,10 +1116,9 @@ freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
 {
   double x_min, x_max, valid_freq;
   int i;
-
+  double bin_width;
   struct histogram *histogram;
   double iqr;
-  int bins;
 
   /* Find out the extremes of the x value, within the range to be included in
      the histogram, and sum the total frequency of those values. */
@@ -1137,19 +1138,10 @@ freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
 
   /* Freedman-Diaconis' choice of bin width. */
   iqr = calculate_iqr (frq);
-  if (iqr != SYSMIS)
-    {
-      double bin_width = 2 * iqr / pow (valid_freq, 1.0 / 3.0);
-      bins = (x_max - x_min) / bin_width;
-      if (bins < 5)
-        bins = 5;
-      else if (bins > 400)
-        bins = 400;
-    }
-  else
-    bins = 5;
+  bin_width = 2 * iqr / pow (valid_freq, 1.0 / 3.0);
+  bin_width = chart_rounded_tick (bin_width);
 
-  histogram = histogram_create (bins, x_min, x_max);
+  histogram = histogram_create (bin_width, x_min, x_max);
   for (i = 0; i < ft->n_valid; i++)
     {
       const struct freq *f = &ft->valid[i];
index 1b55d204b866fa2e15dc4ff3f84d3b3a509a9915..89619bcca54555bf2c7358bb677d7fdf30f9d179 100644 (file)
@@ -55,27 +55,30 @@ destroy (struct statistic *s)
 
 
 struct histogram *
-histogram_create (int bins, double min, double max)
+histogram_create (double bin_width, double min, double max)
 {
+  int bins;
   struct histogram *h = xmalloc (sizeof *h);
   struct statistic *stat = &h->parent;
-  double upper_limit, lower_limit;
-
-  double bin_width = chart_rounded_tick ((max - min) / (double) bins);
-  double bin_width_2 = bin_width / 2.0;
+  const short max_sign = max >= 0;
+  const short min_sign = min >= 0;
 
-  int n =  ceil (max / (bin_width_2) ) ;
+  double upper_limit, lower_limit;
 
   assert (max >= min);
 
-  if ( ! (n % 2 ) ) n++;
-  upper_limit = n * bin_width_2;
+  lower_limit = trunc (2 * abs (min) / bin_width) - 1;
+  lower_limit *= bin_width / 2;
+  lower_limit *= min_sign;
 
-  n =  floor (min / (bin_width_2) ) ;
-  if ( ! (n % 2 ) ) n--;
-  lower_limit = n * bin_width_2;
+  upper_limit = trunc (2 * abs(max) / bin_width) + 1;
+  upper_limit *= bin_width / 2;
+  upper_limit *= max_sign;
+  
+  bins = (upper_limit - lower_limit) / bin_width;
 
   h->gsl_hist = gsl_histogram_alloc (bins);
+
   gsl_histogram_set_ranges_uniform (h->gsl_hist, lower_limit, upper_limit);
 
   stat->accumulate = acc;
index 4c0e54fb0488ae38cd931684f52e472476463303..f03e767c5eec3cdc72e0d284b805689e4885eb5d 100644 (file)
@@ -30,7 +30,7 @@ struct histogram
   gsl_histogram *gsl_hist;
 };
 
-struct histogram * histogram_create (int bins, double max, double min);
+struct histogram * histogram_create (double bin_width, double max, double min);
 
 void histogram_add (struct histogram *h, double y, double c);