1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008 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/>. */
18 #include "percentiles.h"
19 #include <math/order-stats.h>
23 #define _(msgid) gettext (msgid)
24 #define N_(msgid) msgid
26 #include <libpspp/assertion.h>
27 #include <data/val-type.h>
28 #include <gl/xalloc.h>
29 #include <data/variable.h>
30 #include <data/casereader.h>
33 const char *const ptile_alg_desc[] = {
36 N_("Weighted Average"),
39 N_("Empirical with averaging")
45 percentile_calculate (const struct percentile *ptl, enum pc_alg alg)
47 struct percentile *mutable = (struct percentile *) ptl;
48 const struct order_stats *os = &ptl->parent;
50 assert (os->cc == ptl->w);
52 if ( ptl->g1 == SYSMIS)
53 mutable->g1 = (os->k[0].tc - os->k[0].cc) / os->k[0].c_p1;
55 if ( ptl->g1_star == SYSMIS)
56 mutable->g1_star = os->k[0].tc - os->k[0].cc;
58 if ( ptl->g2 == SYSMIS)
60 if ( os->k[1].c == 0 )
61 mutable->g2 = os->k[1].tc / os->k[1].c_p1;
62 else if ( os->k[1].c_p1 == 0 )
65 mutable->g2 = (os->k[1].tc - os->k[1].cc) / os->k[1].c_p1;
68 if ( ptl->g2_star == SYSMIS)
70 if ( os->k[1].c == 0 )
71 mutable->g2_star = os->k[1].tc;
72 else if ( os->k[1].c_p1 == 0 )
75 mutable->g2_star = os->k[1].tc - os->k[1].cc;
81 if ( ptl->g1_star >= 1.0)
85 double a = ( os->k[0].y == SYSMIS ) ? 0 : os->k[0].y;
87 if (os->k[0].c_p1 >= 1.0)
88 return (1 - ptl->g1_star) * a + ptl->g1_star * os->k[0].y_p1;
90 return (1 - ptl->g1) * a + ptl->g1 * os->k[0].y_p1;
96 double a = ( os->k[0].y == SYSMIS ) ? 0 : os->k[0].y;
98 if (os->k[0].c_p1 >= 1.0)
99 return (ptl->g1_star < 0.5) ? a : os->k[0].y_p1;
101 return (ptl->g1 < 0.5) ? a : os->k[0].y_p1;
106 if ( ptl->g1_star == 0 )
109 return os->k[0].y_p1;
113 if ( ptl->g2_star >= 1.0)
115 return os->k[1].y_p1;
119 double a = ( os->k[1].y == SYSMIS ) ? 0 : os->k[1].y;
121 if ( os->k[1].c_p1 >= 1.0)
123 if ( ptl->g2_star == 0)
126 return (1 - ptl->g2_star) * a + ptl->g2_star * os->k[1].y_p1;
130 return (1 - ptl->g2) * a + ptl->g2 * os->k[1].y_p1;
137 if ( ptl->g1_star == 0 )
138 return (os->k[0].y + os->k[0].y_p1)/ 2.0;
140 return os->k[0].y_p1;
155 destroy (struct statistic *stat)
157 struct order_stats *os = (struct order_stats *) stat;
164 percentile_create (double p, double W)
166 struct percentile *ptl = xzalloc (sizeof (*ptl));
167 struct order_stats *os = (struct order_stats *) ptl;
168 struct statistic *stat = (struct statistic *) ptl;
177 os->k = xcalloc (sizeof (*os->k), 2);
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;