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