From ac9640a64222cfcf36c401715cad0a2009a07fb6 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Wed, 9 May 2012 21:21:01 +0200 Subject: [PATCH] HISTOGRAMS: Fix bin width problems on large numbers of bins --- src/language/stats/examine.c | 2 -- src/language/stats/frequencies.q | 1 - src/math/histogram.c | 4 +++- src/math/histogram.h | 8 +++++++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/language/stats/examine.c b/src/language/stats/examine.c index 9bc287e510..3288f5bd11 100644 --- a/src/language/stats/examine.c +++ b/src/language/stats/examine.c @@ -1605,8 +1605,6 @@ calculate_n (const void *aux1, void *aux2 UNUSED, void *user_data) / (1 + log2 (es[v].cc)) ; - bin_width = chart_rounded_tick (bin_width); - es[v].histogram = histogram_create (bin_width, es[v].minimum, es[v].maximum); } diff --git a/src/language/stats/frequencies.q b/src/language/stats/frequencies.q index b9da4677d1..b5ad8c4779 100644 --- a/src/language/stats/frequencies.q +++ b/src/language/stats/frequencies.q @@ -1142,7 +1142,6 @@ freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft, /* Freedman-Diaconis' choice of bin width. */ iqr = calculate_iqr (frq); bin_width = 2 * iqr / pow (valid_freq, 1.0 / 3.0); - bin_width = chart_rounded_tick (bin_width); histogram = histogram_create (bin_width, x_min, x_max); diff --git a/src/math/histogram.c b/src/math/histogram.c index 7fc2d08e3d..afc40013e0 100644 --- a/src/math/histogram.c +++ b/src/math/histogram.c @@ -222,12 +222,14 @@ histogram_create (double bin_width, double min, double max) assert (bin_width > 0); + bin_width = chart_rounded_tick (bin_width); bins = adjust_bin_ranges (bin_width, min, max, &adjusted_min, &adjusted_max); /* Force the number of bins to lie in a sensible range. */ if (bins > MAX_BINS) { - bins = adjust_bin_ranges ((max - min) / (double) (MAX_BINS - 1), + bin_width = chart_rounded_tick ((max - min) / (double) (MAX_BINS - 1)); + bins = adjust_bin_ranges (bin_width, min, max, &adjusted_min, &adjusted_max); } diff --git a/src/math/histogram.h b/src/math/histogram.h index f03e767c5e..3f8fc3e7e7 100644 --- a/src/math/histogram.h +++ b/src/math/histogram.h @@ -30,7 +30,13 @@ struct histogram gsl_histogram *gsl_hist; }; -struct histogram * histogram_create (double bin_width, double max, double min); +/* + Prepare a histogram for data which lies in the range [min, max) + bin_width is a nominal figure only. It is a hint about what might be + an good approximate bin width, but the implementation will adjust it + as it thinks fit. + */ +struct histogram * histogram_create (double bin_width, double min, double max); void histogram_add (struct histogram *h, double y, double c); -- 2.30.2