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