1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "math/percentiles.h"
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"
28 #include "gl/xalloc.h"
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
34 const char *const ptile_alg_desc[] = {
37 N_("Weighted Average"),
40 N_("Empirical with averaging")
46 percentile_calculate (const struct percentile *ptl, enum pc_alg alg)
48 struct percentile *mutable = CONST_CAST (struct percentile *, ptl);
49 const struct order_stats *os = &ptl->parent;
51 if ( ptl->g1 == SYSMIS)
52 mutable->g1 = (os->k[0].tc - os->k[0].cc) / os->k[0].c_p1;
54 if ( ptl->g1_star == SYSMIS)
55 mutable->g1_star = os->k[0].tc - os->k[0].cc;
57 if ( ptl->g2 == SYSMIS)
59 if ( os->k[1].c == 0 )
60 mutable->g2 = os->k[1].tc / os->k[1].c_p1;
61 else if ( os->k[1].c_p1 == 0 )
64 mutable->g2 = (os->k[1].tc - os->k[1].cc) / os->k[1].c_p1;
67 if ( ptl->g2_star == SYSMIS)
69 if ( os->k[1].c == 0 )
70 mutable->g2_star = os->k[1].tc;
71 else if ( os->k[1].c_p1 == 0 )
74 mutable->g2_star = os->k[1].tc - os->k[1].cc;
80 if ( ptl->g1_star >= 1.0)
84 double a = ( os->k[0].y == SYSMIS ) ? 0 : os->k[0].y;
86 if (os->k[0].c_p1 >= 1.0)
87 return (1 - ptl->g1_star) * a + ptl->g1_star * os->k[0].y_p1;
89 return (1 - ptl->g1) * a + ptl->g1 * os->k[0].y_p1;
95 double a = ( os->k[0].y == SYSMIS ) ? 0 : os->k[0].y;
97 if (os->k[0].c_p1 >= 1.0)
98 return (ptl->g1_star < 0.5) ? a : os->k[0].y_p1;
100 return (ptl->g1 < 0.5) ? a : os->k[0].y_p1;
105 if ( ptl->g1_star == 0 )
108 return os->k[0].y_p1;
112 if ( ptl->g2_star >= 1.0)
114 return os->k[1].y_p1;
118 double a = ( os->k[1].y == SYSMIS ) ? 0 : os->k[1].y;
120 if ( os->k[1].c_p1 >= 1.0)
122 if ( ptl->g2_star == 0)
125 return (1 - ptl->g2_star) * a + ptl->g2_star * os->k[1].y_p1;
129 return (1 - ptl->g2) * a + ptl->g2 * os->k[1].y_p1;
136 if ( ptl->g1_star == 0 )
137 return (os->k[0].y + os->k[0].y_p1)/ 2.0;
139 return os->k[0].y_p1;
154 destroy (struct statistic *stat)
156 struct percentile *ptl = UP_CAST (stat, struct percentile, parent.parent);
157 struct order_stats *os = &ptl->parent;
164 percentile_create (double p, double W)
166 struct percentile *ptl = xzalloc (sizeof (*ptl));
167 struct order_stats *os = &ptl->parent;
168 struct statistic *stat = &os->parent;
177 os->k = xcalloc (2, sizeof (*os->k));
179 os->k[1].tc = (W + 1.0) * p;
181 ptl->g1 = ptl->g1_star = SYSMIS;
182 ptl->g2 = ptl->g2_star = SYSMIS;
184 os->k[1].y_p1 = os->k[1].y = SYSMIS;
185 os->k[0].y_p1 = os->k[0].y = SYSMIS;
187 stat->destroy = destroy;