1 /* PSPP - A program for statistical analysis . -*-c-*-
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Author: John Darrington 2004
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "factor_stats.h"
22 #include "percentiles.h"
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
40 const char *ptile_alg_desc[] = {
43 N_("Weighted Average"),
46 N_("Empirical with averaging")
52 /* Individual Percentile algorithms */
54 /* Closest observation to tc1 */
55 double ptile_round(const struct weighted_value **wv,
56 const struct ptile_params *par);
59 /* Weighted average at y_tc2 */
60 double ptile_haverage(const struct weighted_value **wv,
61 const struct ptile_params *par);
64 /* Weighted average at y_tc1 */
65 double ptile_waverage(const struct weighted_value **wv,
66 const struct ptile_params *par);
69 /* Empirical distribution function */
70 double ptile_empirical(const struct weighted_value **wv,
71 const struct ptile_params *par);
74 /* Empirical distribution function with averaging*/
75 double ptile_aempirical(const struct weighted_value **wv,
76 const struct ptile_params *par);
81 /* Closest observation to tc1 */
83 ptile_round(const struct weighted_value **wv,
84 const struct ptile_params *par)
92 if ( wv[par->k1 + 1]->w >= 1 )
94 if ( par->g1_star < 0.5 )
97 x = wv[par->k1 + 1]->v.f;
104 x = wv[par->k1 + 1]->v.f;
111 /* Weighted average at y_tc2 */
113 ptile_haverage(const struct weighted_value **wv,
114 const struct ptile_params *par)
119 if ( par->g2_star >= 1.0 )
120 return wv[par->k2 + 1]->v.f ;
122 /* Special case for k2 + 1 >= n_data
123 (actually it's not a special case, but just avoids indexing errors )
125 if ( par->g2_star == 0 )
127 assert(par->g2 == 0 );
128 return wv[par->k2]->v.f;
131 /* Ditto for k2 < 0 */
134 a = wv[par->k2]->v.f;
137 if ( wv[par->k2 + 1]->w >= 1.0 )
138 return ( (1 - par->g2_star) * a +
139 par->g2_star * wv[par->k2 + 1]->v.f);
141 return ( (1 - par->g2) * a +
142 par->g2 * wv[par->k2 + 1]->v.f);
148 /* Weighted average at y_tc1 */
150 ptile_waverage(const struct weighted_value **wv,
151 const struct ptile_params *par)
155 if ( par->g1_star >= 1.0 )
156 return wv[par->k1 + 1]->v.f ;
160 a = wv[par->k1]->v.f;
163 if ( wv[par->k1 + 1]->w >= 1.0 )
164 return ( (1 - par->g1_star) * a +
165 par->g1_star * wv[par->k1 + 1]->v.f);
167 return ( (1 - par->g1) * a +
168 par->g1 * wv[par->k1 + 1]->v.f);
172 /* Empirical distribution function */
174 ptile_empirical(const struct weighted_value **wv,
175 const struct ptile_params *par)
177 if ( par->g1_star > 0 )
178 return wv[par->k1 + 1]->v.f;
180 return wv[par->k1]->v.f;
185 /* Empirical distribution function with averageing */
187 ptile_aempirical(const struct weighted_value **wv,
188 const struct ptile_params *par)
190 if ( par->g1_star > 0 )
191 return wv[par->k1 + 1]->v.f;
193 return (wv[par->k1]->v.f + wv[par->k1 + 1]->v.f ) / 2.0 ;
198 /* Compute the percentile p */
199 double ptile(double p,
200 const struct weighted_value **wv,
203 enum pc_alg algorithm);
209 const struct weighted_value **wv,
212 enum pc_alg algorithm)
218 struct ptile_params pp;
228 for ( i = 0 ; i < n_data ; ++i )
230 if ( wv[i]->cc <= tc1 )
233 if ( wv[i]->cc <= tc2 )
241 pp.g1 = ( tc1 - wv[pp.k1]->cc ) / wv[pp.k1 + 1]->w;
242 pp.g1_star = tc1 - wv[pp.k1]->cc ;
246 pp.g1 = tc1 / wv[pp.k1 + 1]->w;
251 if ( pp.k2 + 1 >= n_data )
260 pp.g2 = ( tc2 - wv[pp.k2]->cc ) / wv[pp.k2 + 1]->w;
261 pp.g2_star = tc2 - wv[pp.k2]->cc ;
265 pp.g2 = tc2 / wv[pp.k2 + 1]->w;
273 result = ptile_haverage(wv, &pp);
276 result = ptile_waverage(wv, &pp);
279 result = ptile_round(wv, &pp);
282 result = ptile_empirical(wv, &pp);
285 result = ptile_aempirical(wv, &pp);
296 Calculate the values of the percentiles in pc_hash.
297 wv is a sorted array of weighted values of the data set.
300 ptiles(struct hsh_table *pc_hash,
301 const struct weighted_value **wv,
304 enum pc_alg algorithm)
306 struct hsh_iterator hi;
307 struct percentile *p;
311 for ( p = hsh_first(pc_hash, &hi);
313 p = hsh_next(pc_hash, &hi))
315 p->v = ptile(p->p/100.0 , wv, n_data, w, algorithm);
321 /* Calculate Tukey's Hinges */
323 tukey_hinges(const struct weighted_value **wv,
330 double c_star = DBL_MAX;
336 for ( i = 0 ; i < n_data ; ++i )
338 c_star = min(c_star, wv[i]->w);
341 if ( c_star > 1 ) c_star = 1;
343 d = floor((w/c_star + 3 ) / 2.0)/ 2.0;
346 l[1] = w/2.0 + c_star/2.0;
347 l[2] = w + c_star - d*c_star;
353 for ( i = 0 ; i < n_data ; ++i )
355 if ( l[0] >= wv[i]->cc ) h[0] = i ;
356 if ( l[1] >= wv[i]->cc ) h[1] = i ;
357 if ( l[2] >= wv[i]->cc ) h[2] = i ;
360 for ( i = 0 ; i < 3 ; i++ )
364 a_star = l[i] - wv[h[i]]->cc ;
368 if ( h[i] + 1 >= n_data )
370 assert( a_star < 1 ) ;
371 hinge[i] = (1 - a_star) * wv[h[i]]->v.f;
376 a = a_star / ( wv[h[i] + 1]->cc ) ;
381 hinge[i] = wv[h[i] + 1]->v.f ;
385 if ( wv[h[i] + 1]->w >= 1)
387 hinge[i] = ( 1 - a_star) * wv[h[i]]->v.f
388 + a_star * wv[h[i] + 1]->v.f;
393 hinge[i] = (1 - a) * wv[h[i]]->v.f + a * wv[h[i] + 1]->v.f;
397 assert(hinge[0] <= hinge[1]);
398 assert(hinge[1] <= hinge[2]);
404 ptile_compare(const struct percentile *p1,
405 const struct percentile *p2,
413 else if (p1->p < p2->p)
422 ptile_hash(const struct percentile *p, void *aux UNUSED)
424 return hsh_hash_double(p->p);