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., 59 Temple Place - Suite 330, Boston, MA
21 #include "factor_stats.h"
22 #include "percentiles.h"
36 const char *ptile_alg_desc[] = {
39 N_("Weighted Average"),
42 N_("Empirical with averaging")
48 /* Individual Percentile algorithms */
50 /* Closest observation to tc1 */
51 double ptile_round(const struct weighted_value **wv,
52 const struct ptile_params *par);
55 /* Weighted average at y_tc2 */
56 double ptile_haverage(const struct weighted_value **wv,
57 const struct ptile_params *par);
60 /* Weighted average at y_tc1 */
61 double ptile_waverage(const struct weighted_value **wv,
62 const struct ptile_params *par);
65 /* Empirical distribution function */
66 double ptile_empirical(const struct weighted_value **wv,
67 const struct ptile_params *par);
70 /* Empirical distribution function with averaging*/
71 double ptile_aempirical(const struct weighted_value **wv,
72 const struct ptile_params *par);
77 /* Closest observation to tc1 */
79 ptile_round(const struct weighted_value **wv,
80 const struct ptile_params *par)
88 if ( wv[par->k1 + 1]->w >= 1 )
90 if ( par->g1_star < 0.5 )
93 x = wv[par->k1 + 1]->v.f;
100 x = wv[par->k1 + 1]->v.f;
107 /* Weighted average at y_tc2 */
109 ptile_haverage(const struct weighted_value **wv,
110 const struct ptile_params *par)
115 if ( par->g2_star >= 1.0 )
116 return wv[par->k2 + 1]->v.f ;
118 /* Special case for k2 + 1 >= n_data
119 (actually it's not a special case, but just avoids indexing errors )
121 if ( par->g2_star == 0 )
123 assert(par->g2 == 0 );
124 return wv[par->k2]->v.f;
127 /* Ditto for k2 < 0 */
130 a = wv[par->k2]->v.f;
133 if ( wv[par->k2 + 1]->w >= 1.0 )
134 return ( (1 - par->g2_star) * a +
135 par->g2_star * wv[par->k2 + 1]->v.f);
137 return ( (1 - par->g2) * a +
138 par->g2 * wv[par->k2 + 1]->v.f);
144 /* Weighted average at y_tc1 */
146 ptile_waverage(const struct weighted_value **wv,
147 const struct ptile_params *par)
151 if ( par->g1_star >= 1.0 )
152 return wv[par->k1 + 1]->v.f ;
156 a = wv[par->k1]->v.f;
159 if ( wv[par->k1 + 1]->w >= 1.0 )
160 return ( (1 - par->g1_star) * a +
161 par->g1_star * wv[par->k1 + 1]->v.f);
163 return ( (1 - par->g1) * a +
164 par->g1 * wv[par->k1 + 1]->v.f);
168 /* Empirical distribution function */
170 ptile_empirical(const struct weighted_value **wv,
171 const struct ptile_params *par)
173 if ( par->g1_star > 0 )
174 return wv[par->k1 + 1]->v.f;
176 return wv[par->k1]->v.f;
181 /* Empirical distribution function with averageing */
183 ptile_aempirical(const struct weighted_value **wv,
184 const struct ptile_params *par)
186 if ( par->g1_star > 0 )
187 return wv[par->k1 + 1]->v.f;
189 return (wv[par->k1]->v.f + wv[par->k1 + 1]->v.f ) / 2.0 ;
194 /* Compute the percentile p */
195 double ptile(double p,
196 const struct weighted_value **wv,
199 enum pc_alg algorithm);
205 const struct weighted_value **wv,
208 enum pc_alg algorithm)
214 struct ptile_params pp;
224 for ( i = 0 ; i < n_data ; ++i )
226 if ( wv[i]->cc <= tc1 )
229 if ( wv[i]->cc <= tc2 )
237 pp.g1 = ( tc1 - wv[pp.k1]->cc ) / wv[pp.k1 + 1]->w;
238 pp.g1_star = tc1 - wv[pp.k1]->cc ;
242 pp.g1 = tc1 / wv[pp.k1 + 1]->w;
247 if ( pp.k2 + 1 >= n_data )
256 pp.g2 = ( tc2 - wv[pp.k2]->cc ) / wv[pp.k2 + 1]->w;
257 pp.g2_star = tc2 - wv[pp.k2]->cc ;
261 pp.g2 = tc2 / wv[pp.k2 + 1]->w;
269 result = ptile_haverage(wv, &pp);
272 result = ptile_waverage(wv, &pp);
275 result = ptile_round(wv, &pp);
278 result = ptile_empirical(wv, &pp);
281 result = ptile_aempirical(wv, &pp);
292 Calculate the values of the percentiles in pc_hash.
293 wv is a sorted array of weighted values of the data set.
296 ptiles(struct hsh_table *pc_hash,
297 const struct weighted_value **wv,
300 enum pc_alg algorithm)
302 struct hsh_iterator hi;
303 struct percentile *p;
307 for ( p = hsh_first(pc_hash, &hi);
309 p = hsh_next(pc_hash, &hi))
311 p->v = ptile(p->p/100.0 , wv, n_data, w, algorithm);
317 /* Calculate Tukey's Hinges */
319 tukey_hinges(const struct weighted_value **wv,
326 double c_star = DBL_MAX;
332 for ( i = 0 ; i < n_data ; ++i )
334 c_star = min(c_star, wv[i]->w);
337 if ( c_star > 1 ) c_star = 1;
339 d = floor((w/c_star + 3 ) / 2.0)/ 2.0;
342 l[1] = w/2.0 + c_star/2.0;
343 l[2] = w + c_star - d*c_star;
349 for ( i = 0 ; i < n_data ; ++i )
351 if ( l[0] >= wv[i]->cc ) h[0] = i ;
352 if ( l[1] >= wv[i]->cc ) h[1] = i ;
353 if ( l[2] >= wv[i]->cc ) h[2] = i ;
356 for ( i = 0 ; i < 3 ; i++ )
360 a_star = l[i] - wv[h[i]]->cc ;
364 if ( h[i] + 1 >= n_data )
366 assert( a_star < 1 ) ;
367 hinge[i] = (1 - a_star) * wv[h[i]]->v.f;
372 a = a_star / ( wv[h[i] + 1]->cc ) ;
377 hinge[i] = wv[h[i] + 1]->v.f ;
381 if ( wv[h[i] + 1]->w >= 1)
383 hinge[i] = ( 1 - a_star) * wv[h[i]]->v.f
384 + a_star * wv[h[i] + 1]->v.f;
389 hinge[i] = (1 - a) * wv[h[i]]->v.f + a * wv[h[i] + 1]->v.f;
393 assert(hinge[0] <= hinge[1]);
394 assert(hinge[1] <= hinge[2]);
400 ptile_compare(const struct percentile *p1,
401 const struct percentile *p2,
409 else if (p1->p < p2->p)
418 ptile_hash(const struct percentile *p, void *aux UNUSED)
420 return hsh_hash_double(p->p);