86277333ff4ec67611833860d2402611dfd133a3
[pspp] / src / math / box-whisker.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008, 2009, 2011 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 "box-whisker.h"
20
21 #include <math.h>
22 #include <float.h>
23
24 #include "data/case.h"
25 #include "data/data-out.h"
26 #include "data/val-type.h"
27 #include "data/variable.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/str.h"
31 #include "math/order-stats.h"
32 #include "math/tukey-hinges.h"
33
34 #include "gl/xalloc.h"
35
36 static void
37 destroy (struct statistic *s)
38 {
39   struct box_whisker *bw = UP_CAST (s, struct box_whisker, parent.parent);
40   struct order_stats *os = &bw->parent;
41   struct ll *ll;
42
43   for (ll = ll_head (&bw->outliers); ll != ll_null (&bw->outliers);)
44     {
45       struct outlier *e = ll_data (ll, struct outlier, ll);
46
47       ll = ll_next (ll);
48
49       ds_destroy (&e->label);
50       free (e);
51     }
52
53   free (os->k);
54   free (s);
55 };
56
57
58 static void
59 acc (struct statistic *s, const struct ccase *cx,
60      double c UNUSED, double cc UNUSED, double y)
61 {
62   struct box_whisker *bw = UP_CAST (s, struct box_whisker, parent.parent);
63   bool extreme;
64
65   if (y > bw->hinges[2] + bw->step) /* Upper outlier */
66     {
67       extreme = (y > bw->hinges[2] + 2 * bw->step) ;
68     }
69
70   else if (y < bw->hinges[0] - bw->step) /* Lower outlier */
71     {
72       extreme = (y < bw->hinges[0] - 2 * bw->step) ;
73     }
74
75   else /* Not an outlier */
76     {
77       if (bw->whiskers[0] == SYSMIS)
78         bw->whiskers[0] = y;
79
80       if (y > bw->whiskers[1])
81         bw->whiskers[1] = y;
82
83       return;
84     }
85
86   /* y is an outlier */
87
88   struct outlier *o = XZALLOC (struct outlier);
89   o->value = y;
90   o->extreme = extreme;
91   ds_init_empty (&o->label);
92
93   if (bw->id_var)
94     {
95       char *s = data_out (case_data_idx (cx, bw->id_idx),
96                           var_get_encoding (bw->id_var),
97                           var_get_print_format (bw->id_var),
98                           settings_get_fmt_settings ());
99
100       ds_put_cstr (&o->label, s);
101       free (s);
102     }
103   else
104     {
105       ds_put_format (&o->label,
106                      "%ld",
107                      (casenumber) case_num_idx (cx, bw->id_idx));
108     }
109
110   ll_push_head (&bw->outliers, &o->ll);
111 }
112
113 void
114 box_whisker_whiskers (const struct box_whisker *bw, double whiskers[2])
115 {
116   whiskers[0] = bw->whiskers[0];
117   whiskers[1] = bw->whiskers[1];
118 }
119
120 void
121 box_whisker_hinges (const struct box_whisker *bw, double hinges[3])
122 {
123   hinges[0] = bw->hinges[0];
124   hinges[1] = bw->hinges[1];
125   hinges[2] = bw->hinges[2];
126 }
127
128 const struct ll_list *
129 box_whisker_outliers (const struct box_whisker *bw)
130 {
131   return &bw->outliers;
132 }
133
134 /*
135   Create a box_whisker struct, suitable for generating a boxplot.
136
137   TH are the tukey hinges of the dataset.
138
139   id_idx is the index into the casereader which will be used to label
140   outliers.
141   id_var is the variable from which that label came, or NULL
142 */
143 struct box_whisker *
144 box_whisker_create (const struct tukey_hinges *th,
145                     size_t id_idx, const struct variable *id_var)
146 {
147   double hinges[3];
148   tukey_hinges_calculate (th, hinges);
149
150   struct box_whisker *w = xmalloc (sizeof *w);
151   *w = (struct box_whisker) {
152     .parent = {
153       .parent = {
154         .accumulate = acc,
155         .destroy = destroy,
156       },
157     },
158     .hinges = { hinges[0], hinges[1], hinges[2] },
159     .whiskers = { SYSMIS, hinges[2] },
160     .outliers = LL_INITIALIZER (w->outliers),
161     .step = (hinges[2] - hinges[0]) * 1.5,
162     .id_idx = id_idx,
163     .id_var = id_var,
164   };
165
166   return w;
167 }