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
22 #include "factor_stats.h"
25 #include "algorithm.h"
28 #include "percentiles.h"
38 metrics_precalc(struct metrics *m)
49 m->moments = moments1_create(MOMENT_KURTOSIS);
51 m->ordered_data = hsh_create(20,
52 (hsh_compare_func *) compare_values,
53 (hsh_hash_func *) hash_value,
54 (hsh_free_func *) weighted_value_free,
59 /* Include val in the calculation for the metrics.
60 If val is null, then treat it as MISSING
63 metrics_calc(struct metrics *fs, const union value *val,
64 double weight, int case_no)
66 struct weighted_value **wv;
71 fs->n_missing += weight;
77 moments1_add(fs->moments, x, weight);
80 if ( x < fs->min) fs->min = x;
81 if ( x > fs->max) fs->max = x;
84 wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
88 /* If this value has already been seen, then simply
89 increase its weight and push a new case number */
93 assert( (*wv)->v.f == val->f );
96 cn = xmalloc( sizeof (struct case_node) ) ;
97 cn->next = (*wv)->case_nos ;
100 (*wv)->case_nos = cn;
104 struct case_node *cn;
106 *wv = weighted_value_create();
110 cn = xmalloc( sizeof (struct case_node) ) ;
113 (*wv)->case_nos = cn;
120 metrics_postcalc(struct metrics *m)
128 moments1_calculate (m->moments, &m->n, &m->mean, &m->var,
129 &m->skewness, &m->kurtosis);
131 moments1_destroy (m->moments);
134 m->stddev = sqrt(m->var);
136 /* FIXME: Check this is correct ???
137 Shouldn't we use the sample variance ??? */
138 m->se_mean = sqrt (m->var / m->n) ;
142 m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
143 m->n_data = hsh_count(m->ordered_data);
145 /* Trimmed mean calculation */
146 if ( m->n_data <= 1 )
148 m->trimmed_mean = m->mean;
152 m->histogram = histogram_create(10, m->min, m->max);
154 for ( i = 0 ; i < m->n_data ; ++i )
156 struct weighted_value **wv = (m->wvp) ;
157 gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
164 for ( i = 0 ; i < m->n_data ; ++i )
169 m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
180 for ( i = m->n_data -1 ; i >= 0; --i )
182 if ( tc > m->n - m->wvp[i]->cc)
187 /* Calculate the percentiles */
188 ptiles(m->ptile_hash, m->wvp, m->n_data, m->n, m->ptile_alg);
190 tukey_hinges(m->wvp, m->n_data, m->n, m->hinge);
192 /* Special case here */
195 m->trimmed_mean = m->wvp[k2]->v.f;
200 for ( i = k1 + 2 ; i <= k2 - 1 ; ++i )
202 m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
206 m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
207 m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
208 m->trimmed_mean /= 0.9 * m->n ;
214 struct weighted_value *
215 weighted_value_create(void)
217 struct weighted_value *wv;
218 wv = xmalloc (sizeof (struct weighted_value ));
227 weighted_value_free(struct weighted_value *wv)
229 struct case_node *cn ;
238 struct case_node *next = cn->next;
252 /* Create a factor statistics object with for N dependent vars
253 and ID as the value of the independent variable */
254 struct factor_statistics *
255 create_factor_statistics (int n, union value *id0, union value *id1)
257 struct factor_statistics *f;
259 f = xmalloc( sizeof ( struct factor_statistics ));
263 f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
264 memset (f->m, 0, sizeof(struct metrics) * n);
272 metrics_destroy(struct metrics *m)
274 hsh_destroy(m->ordered_data);
275 hsh_destroy(m->ptile_hash);
277 gsl_histogram_free(m->histogram);
281 factor_statistics_free(struct factor_statistics *f)
285 for ( i = 0 ; i < f->n_var; ++i )
286 metrics_destroy(&f->m[i]);
295 factor_statistics_compare(const struct factor_statistics *f0,
296 const struct factor_statistics *f1, int width)
304 cmp0 = compare_values(&f0->id[0], &f1->id[0], width);
310 if ( ( f0->id[1].f == SYSMIS ) && (f1->id[1].f != SYSMIS) )
313 if ( ( f0->id[1].f != SYSMIS ) && (f1->id[1].f == SYSMIS) )
316 return compare_values(&f0->id[1], &f1->id[1], width);
321 factor_statistics_hash(const struct factor_statistics *f, int width)
326 h = hash_value(&f->id[0], width);
328 if ( f->id[1].f != SYSMIS )
329 h += hash_value(&f->id[1], width);