From: John Darrington Date: Thu, 22 Mar 2012 21:00:01 +0000 (+0100) Subject: Improve the way in which histogram bin ranges are chosen X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d491c340db92242050d2e1002f68e0bee11eb8a;hp=d6f63e70f4ec5f70e25f8c0bb9f33f65f8dc2f34;p=pspp Improve the way in which histogram bin ranges are chosen --- diff --git a/src/math/histogram.c b/src/math/histogram.c index 89619bcca5..0b2369f63c 100644 --- a/src/math/histogram.c +++ b/src/math/histogram.c @@ -60,22 +60,31 @@ histogram_create (double bin_width, double min, double max) int bins; struct histogram *h = xmalloc (sizeof *h); struct statistic *stat = &h->parent; - const short max_sign = max >= 0; - const short min_sign = min >= 0; double upper_limit, lower_limit; assert (max >= min); - lower_limit = trunc (2 * abs (min) / bin_width) - 1; - lower_limit *= bin_width / 2; - lower_limit *= min_sign; + if (max == min) + bin_width = 1; - upper_limit = trunc (2 * abs(max) / bin_width) + 1; - upper_limit *= bin_width / 2; - upper_limit *= max_sign; + lower_limit = floor (2 * min / bin_width) - 1; + upper_limit = floor (2 * max / bin_width) + 1; - bins = (upper_limit - lower_limit) / bin_width; + /* The range must be an even number of half bin_widths */ + if ( (int)(upper_limit - lower_limit) % 2) + { + /* Extend the range at the end which gives the least unused space */ + if (remainder (min, bin_width / 2.0) > remainder (max, bin_width / 2.0)) + lower_limit --; + else + upper_limit ++; + } + + bins = (upper_limit - lower_limit) / 2.0; + + upper_limit *= bin_width / 2; + lower_limit *= bin_width / 2; h->gsl_hist = gsl_histogram_alloc (bins); diff --git a/tests/language/stats/examine.at b/tests/language/stats/examine.at index 64f6d7c573..24005a0675 100644 --- a/tests/language/stats/examine.at +++ b/tests/language/stats/examine.at @@ -519,6 +519,7 @@ END DATA EXAMINE quality /STATISTICS descriptives + /PLOT = histogram . ]) AT_CHECK([pspp -o pspp.csv examine.sps]) @@ -639,4 +640,4 @@ x,Highest,1,threehundred,300.00 ,,5,five ,5.00 ]) -AT_CLEANUP \ No newline at end of file +AT_CLEANUP