Histogram calculations: Use integer arithmetic
[pspp] / src / math / histogram.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2008, 2009, 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "math/histogram.h"
20
21 #include <gsl/gsl_histogram.h>
22 #include <math.h>
23
24 #include "libpspp/message.h"
25 #include "libpspp/assertion.h"
26 #include "libpspp/cast.h"
27 #include "math/chart-geometry.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33
34 #include "gl/xalloc.h"
35
36 void
37 histogram_add (struct histogram *h, double y, double c)
38 {
39   struct statistic *stat = &h->parent;
40   stat->accumulate (stat, NULL, c, 0, y);
41 }
42
43 static void
44 acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
45 {
46   struct histogram *hist = UP_CAST (s, struct histogram, parent);
47
48   gsl_histogram_accumulate (hist->gsl_hist, y, c);
49 }
50
51 static void
52 destroy (struct statistic *s)
53 {
54   struct histogram *h = UP_CAST (s, struct histogram, parent);
55   gsl_histogram_free (h->gsl_hist);
56   free (s);
57 }
58
59
60 struct histogram *
61 histogram_create (double bin_width, double min, double max)
62 {
63   int bins;
64   struct histogram *h = xmalloc (sizeof *h);
65   struct statistic *stat = &h->parent;
66
67   const double half_bin_width = bin_width / 2.0;
68
69   /* The lower and upper limits of the histogram, in units of half
70      bin widths */
71   int lower_limit, upper_limit;
72
73   /* -1 if the lower end of the range contains more unused space
74      than the upper end.
75      +1 otherwise.  */
76   short sparse_end = 0;
77
78   if (max == min)
79     {
80       msg (MW, _("Not creating histogram because the data contains less than 2 distinct values"));
81       free (h);
82       return NULL;
83     }
84
85   assert (max > min);
86
87   {
88     double ul, ll;
89     double lower_tail = modf (min / half_bin_width, &ll);
90     double upper_tail = modf (max / half_bin_width, &ul);
91     lower_limit = ll - 1;
92     upper_limit = ul + 1;
93     
94     sparse_end = lower_tail < upper_tail ? -1 : +1;
95   }
96
97   /* The range must be an EVEN number of half bin_widths */
98   if ( (upper_limit - lower_limit) % 2)
99     {
100       /* Extend the range at the end which gives the least unused space */
101       if (sparse_end == +1)
102         lower_limit--;
103       else
104         upper_limit++;
105       
106       /* Now the other end has more space */
107       sparse_end *= -1;
108     }
109
110   /* But the range should be aligned to an ODD number of
111      half bin widths, so that the labels are aesthetically pleasing ones. */
112   if ( lower_limit % 2 == 0)
113     {
114       lower_limit += -sparse_end ;
115       upper_limit += -sparse_end ;
116     }
117
118   bins = (upper_limit - lower_limit) / 2.0;
119
120   /* Force the number of bins to lie in a sensible range */
121   if (bins > 25) 
122     bins = 25;
123
124   if (bins < 1)
125     bins = 1;
126
127   h->gsl_hist = gsl_histogram_alloc (bins);
128
129   gsl_histogram_set_ranges_uniform (h->gsl_hist,
130                                     lower_limit * half_bin_width,
131                                     upper_limit * half_bin_width);
132
133   stat->accumulate = acc;
134   stat->destroy = destroy;
135
136   return h;
137 }
138