math: Make 'accumulate' a feature of order statistics, not all stats.
[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 /* Return the value of the percentile. */
31 double
32 percentile_calculate (const struct percentile *ptl, enum pc_alg alg)
33 {
34   struct percentile *mutable = CONST_CAST (struct percentile *, ptl);
35   const struct order_stats *os = &ptl->parent;
36
37   if (ptl->g1 == SYSMIS)
38     mutable->g1 = (os->k[0].tc - os->k[0].cc) / os->k[0].c_p1;
39
40   if (ptl->g1_star == SYSMIS)
41     mutable->g1_star = os->k[0].tc - os->k[0].cc;
42
43   if (ptl->g2 == SYSMIS)
44     {
45       if (os->k[1].c == 0)
46         mutable->g2 = os->k[1].tc / os->k[1].c_p1;
47       else if (os->k[1].c_p1 == 0)
48         mutable->g2 = 0;
49       else
50         mutable->g2 = (os->k[1].tc - os->k[1].cc) / os->k[1].c_p1;
51     }
52
53   if (ptl->g2_star == SYSMIS)
54     {
55       if (os->k[1].c == 0)
56         mutable->g2_star = os->k[1].tc;
57       else if (os->k[1].c_p1 == 0)
58         mutable->g2_star = 0;
59       else
60         mutable->g2_star = os->k[1].tc - os->k[1].cc;
61     }
62
63   switch (alg)
64     {
65     case PC_WAVERAGE:
66       if (ptl->g1_star >= 1.0)
67         return os->k[0].y_p1;
68       else
69         {
70           double a = (os->k[0].y == SYSMIS) ? 0 : os->k[0].y;
71
72           if (os->k[0].c_p1 >= 1.0)
73             return (1 - ptl->g1_star) * a + ptl->g1_star * os->k[0].y_p1;
74           else
75             return (1 - ptl->g1) * a + ptl->g1 * os->k[0].y_p1;
76         }
77       break;
78
79     case PC_ROUND:
80       {
81         double a = (os->k[0].y == SYSMIS) ? 0 : os->k[0].y;
82
83         if (os->k[0].c_p1 >= 1.0)
84           return (ptl->g1_star < 0.5) ? a : os->k[0].y_p1;
85         else
86           return (ptl->g1 < 0.5) ? a : os->k[0].y_p1;
87       }
88       break;
89
90     case PC_EMPIRICAL:
91       if (ptl->g1_star == 0)
92         return os->k[0].y;
93       else
94         return os->k[0].y_p1;
95       break;
96
97     case PC_HAVERAGE:
98       if (ptl->g2_star >= 1.0)
99         {
100           return os->k[1].y_p1;
101         }
102       else
103         {
104           double a = (os->k[1].y == SYSMIS) ? 0 : os->k[1].y;
105
106           if (os->k[1].c_p1 >= 1.0)
107             {
108               if (ptl->g2_star == 0)
109                 return os->k[1].y;
110
111               return (1 - ptl->g2_star) * a + ptl->g2_star * os->k[1].y_p1;
112             }
113           else
114             {
115               return (1 - ptl->g2) * a + ptl->g2 * os->k[1].y_p1;
116             }
117         }
118
119       break;
120
121     case PC_AEMPIRICAL:
122       if (ptl->g1_star == 0)
123         return (os->k[0].y + os->k[0].y_p1)/ 2.0;
124       else
125         return os->k[0].y_p1;
126       break;
127
128     default:
129       NOT_REACHED ();
130       break;
131     }
132
133   NOT_REACHED ();
134
135   return SYSMIS;
136 }
137
138
139 static void
140 destroy (struct statistic *stat)
141 {
142   struct percentile *ptl = UP_CAST (stat, struct percentile, parent.parent);
143   free (ptl);
144 }
145
146 /* Create the Pth percentile.
147    W is the total sum of weights in the data set.
148 */
149 struct percentile *
150 percentile_create (double p, double W)
151 {
152   assert (p >= 0);
153   assert (p <= 1.0);
154
155   struct percentile *ptl = xmalloc (sizeof *ptl);
156   *ptl = (struct percentile) {
157     .parent = {
158       .parent = {
159         .destroy = destroy
160       },
161       .k = ptl->k,
162       .n_k = 2,
163     },
164     .ptile = p,
165     .w = W,
166     .g1 = SYSMIS,
167     .g1_star = SYSMIS,
168     .g2 = SYSMIS,
169     .g2_star = SYSMIS,
170     .k[0] = { .tc = W * p, .y = SYSMIS, .y_p1 = SYSMIS },
171     .k[1] = { .tc = (W + 1.0) * p, .y = SYSMIS, .y_p1 = SYSMIS },
172   };
173   return ptl;
174 }