Merge commit 'origin/stable'
[pspp-builds.git] / src / math / box-whisker.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008 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 #include "box-whisker.h"
19 #include "order-stats.h"
20 #include "tukey-hinges.h"
21 #include <gl/xalloc.h>
22 #include <libpspp/assertion.h>
23 #include <math.h>
24 #include <float.h>
25 #include <data/val-type.h>
26 #include <libpspp/str.h>
27 #include <data/case.h>
28 #include <data/variable.h>
29
30 static void
31 destroy (struct statistic *s)
32 {
33   struct order_stats *os = (struct order_stats *) s;
34   struct box_whisker *bw = (struct box_whisker *) s;
35   struct ll *ll;
36
37   for (ll = ll_head (&bw->outliers); ll != ll_null (&bw->outliers); )
38     {
39       struct outlier *e = ll_data (ll, struct outlier, ll);
40
41       ll = ll_next (ll);
42
43       ds_destroy (&e->label);
44       free (e);
45     }
46
47   free (os->k);
48   free (s);
49 };
50
51
52 static void
53 acc (struct statistic *s, const struct ccase *cx,
54      double c UNUSED, double cc UNUSED, double y)
55 {
56   struct box_whisker *bw = (struct box_whisker *) s;
57   bool extreme;
58   struct outlier *o;
59
60   if ( y < bw->hinges[2] + bw->step)
61       bw->whiskers[1] = y;
62
63   if (bw->whiskers[0] == SYSMIS ||  bw->hinges[0] - bw->step > y)
64       bw->whiskers[0] = y;
65
66   if ( y > bw->hinges[2] + bw->step)
67     extreme = (y > bw->hinges[2] + 2 * bw->step) ;
68
69   else if (y < bw->hinges[0] - bw->step)
70     extreme = (y < bw->hinges[0] - 2 * bw->step) ;
71
72   else
73     return;
74
75   o = xzalloc (sizeof *o) ;
76   o->value = y;
77   o->extreme = extreme;
78   ds_init_empty (&o->label);
79
80   if (bw->id_var)
81     var_append_value_name (bw->id_var,
82                            case_data (cx, bw->id_var),
83                            &o->label);
84   else
85     ds_put_format (&o->label,
86                    "%ld",
87                    (casenumber) case_data_idx (cx, bw->casenumber_idx)->f);
88
89   ll_push_head (&bw->outliers, &o->ll);
90 }
91
92 void
93 box_whisker_whiskers (const struct box_whisker *bw, double whiskers[2])
94 {
95   whiskers[0] = bw->whiskers[0];
96   whiskers[1] = bw->whiskers[1];
97 }
98
99 void
100 box_whisker_hinges (const struct box_whisker *bw, double hinges[3])
101 {
102   hinges[0] = bw->hinges[0];
103   hinges[1] = bw->hinges[1];
104   hinges[2] = bw->hinges[2];
105 }
106
107 const struct ll_list *
108 box_whisker_outliers (const struct box_whisker *bw)
109 {
110   return &bw->outliers;
111 }
112
113 struct statistic *
114 box_whisker_create (const struct tukey_hinges *th,
115                     const struct variable *id_var,  size_t casenumber_idx)
116 {
117   struct box_whisker *w = xzalloc (sizeof (*w));
118   struct order_stats *os = (struct order_stats *) w;
119   struct statistic *stat = (struct statistic *) w;
120
121   os->n_k = 0;
122
123   stat->destroy = destroy;
124   stat->accumulate = acc;
125
126   tukey_hinges_calculate (th, w->hinges);
127
128   w->casenumber_idx = casenumber_idx;
129   w->id_var = id_var;
130
131   w->step = (w->hinges[2] - w->hinges[0]) * 1.5;
132
133   w->whiskers[1] = w->hinges[2];
134   w->whiskers[0] = SYSMIS;
135
136   ll_init (&w->outliers);
137
138   return stat;
139 }