a387d3d4c270b1685984b6758e899f98f239a5f6
[pspp] / src / math / percentiles.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 "math/percentiles.h"
20
21 #include "data/casereader.h"
22 #include "data/val-type.h"
23 #include "data/variable.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/cast.h"
26 #include "math/order-stats.h"
27
28 #include "gl/xalloc.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34 double
35 percentile_calculate (const struct percentile *ptl, enum pc_alg alg)
36 {
37   struct percentile *mutable = CONST_CAST (struct percentile *, ptl);
38   const struct order_stats *os = &ptl->parent;
39
40   if (ptl->g1 == SYSMIS)
41     mutable->g1 = (os->k[0].tc - os->k[0].cc) / os->k[0].c_p1;
42
43   if (ptl->g1_star == SYSMIS)
44     mutable->g1_star = os->k[0].tc - os->k[0].cc;
45
46   if (ptl->g2 == SYSMIS)
47     {
48       if (os->k[1].c == 0)
49         mutable->g2 = os->k[1].tc / os->k[1].c_p1;
50       else if (os->k[1].c_p1 == 0)
51         mutable->g2 = 0;
52       else
53         mutable->g2 = (os->k[1].tc - os->k[1].cc) / os->k[1].c_p1;
54     }
55
56   if (ptl->g2_star == SYSMIS)
57     {
58       if (os->k[1].c == 0)
59         mutable->g2_star = os->k[1].tc;
60       else if (os->k[1].c_p1 == 0)
61         mutable->g2_star = 0;
62       else
63         mutable->g2_star = os->k[1].tc - os->k[1].cc;
64     }
65
66   switch (alg)
67     {
68     case PC_WAVERAGE:
69       if (ptl->g1_star >= 1.0)
70         return os->k[0].y_p1;
71       else
72         {
73           double a = (os->k[0].y == SYSMIS) ? 0 : os->k[0].y;
74
75           if (os->k[0].c_p1 >= 1.0)
76             return (1 - ptl->g1_star) * a + ptl->g1_star * os->k[0].y_p1;
77           else
78             return (1 - ptl->g1) * a + ptl->g1 * os->k[0].y_p1;
79         }
80       break;
81
82     case PC_ROUND:
83       {
84         double a = (os->k[0].y == SYSMIS) ? 0 : os->k[0].y;
85
86         if (os->k[0].c_p1 >= 1.0)
87           return (ptl->g1_star < 0.5) ? a : os->k[0].y_p1;
88         else
89           return (ptl->g1 < 0.5) ? a : os->k[0].y_p1;
90       }
91       break;
92
93     case PC_EMPIRICAL:
94       if (ptl->g1_star == 0)
95         return os->k[0].y;
96       else
97         return os->k[0].y_p1;
98       break;
99
100     case PC_HAVERAGE:
101       if (ptl->g2_star >= 1.0)
102         {
103           return os->k[1].y_p1;
104         }
105       else
106         {
107           double a = (os->k[1].y == SYSMIS) ? 0 : os->k[1].y;
108
109           if (os->k[1].c_p1 >= 1.0)
110             {
111               if (ptl->g2_star == 0)
112                 return os->k[1].y;
113
114               return (1 - ptl->g2_star) * a + ptl->g2_star * os->k[1].y_p1;
115             }
116           else
117             {
118               return (1 - ptl->g2) * a + ptl->g2 * os->k[1].y_p1;
119             }
120         }
121
122       break;
123
124     case PC_AEMPIRICAL:
125       if (ptl->g1_star == 0)
126         return (os->k[0].y + os->k[0].y_p1)/ 2.0;
127       else
128         return os->k[0].y_p1;
129       break;
130
131     default:
132       NOT_REACHED ();
133       break;
134     }
135
136   NOT_REACHED ();
137
138   return SYSMIS;
139 }
140
141
142 static void
143 destroy (struct statistic *stat)
144 {
145   struct percentile *ptl = UP_CAST (stat, struct percentile, parent.parent);
146   struct order_stats *os = &ptl->parent;
147   free (os->k);
148   free (ptl);
149 }
150
151
152 struct percentile *
153 percentile_create (double p, double W)
154 {
155   struct percentile *ptl = xzalloc (sizeof (*ptl));
156   struct order_stats *os = &ptl->parent;
157   struct statistic *stat = &os->parent;
158
159   assert (p >= 0);
160   assert (p <= 1.0);
161
162   ptl->ptile = p;
163   ptl->w = W;
164
165   os->n_k = 2;
166   os->k = xcalloc (2, sizeof (*os->k));
167   os->k[0].tc = W * p;
168   os->k[1].tc = (W + 1.0) * p;
169
170   ptl->g1 = ptl->g1_star = SYSMIS;
171   ptl->g2 = ptl->g2_star = SYSMIS;
172
173   os->k[1].y_p1 = os->k[1].y = SYSMIS;
174   os->k[0].y_p1 = os->k[0].y = SYSMIS;
175
176   stat->destroy = destroy;
177
178   return ptl;
179 }
180