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