1 /* PSPP - A program for statistical analysis . -*-c-*-
3 Copyright (C) 2004 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "factor-stats.h"
22 #include <data/value.h>
23 #include <libpspp/hash.h>
24 #include <libpspp/array.h>
25 #include <libpspp/alloc.h>
27 #include "percentiles.h"
33 #include "histogram.h"
37 metrics_precalc(struct metrics *m)
48 m->moments = moments1_create(MOMENT_KURTOSIS);
50 m->ordered_data = hsh_create(20,
51 (hsh_compare_func *) compare_values,
52 (hsh_hash_func *) hash_value,
53 (hsh_free_func *) weighted_value_free,
58 /* Include val in the calculation for the metrics.
59 If val is null, then treat it as MISSING
62 metrics_calc (struct metrics *fs, const union value *val,
63 double weight, int case_no)
65 struct weighted_value **wv;
70 fs->n_missing += weight;
76 moments1_add(fs->moments, x, weight);
79 if ( x < fs->min) fs->min = x;
80 if ( x > fs->max) fs->max = x;
83 wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
87 /* If this value has already been seen, then simply
88 increase its weight and push a new case number */
92 assert( (*wv)->v.f == val->f );
95 cn = xmalloc ( sizeof *cn);
96 cn->next = (*wv)->case_nos ;
103 struct case_node *cn;
105 *wv = weighted_value_create();
109 cn = xmalloc (sizeof *cn);
112 (*wv)->case_nos = cn;
119 metrics_postcalc(struct metrics *m)
127 moments1_calculate (m->moments, &m->n, &m->mean, &m->var,
128 &m->skewness, &m->kurtosis);
130 moments1_destroy (m->moments);
133 m->stddev = sqrt(m->var);
135 /* FIXME: Check this is correct ???
136 Shouldn't we use the sample variance ??? */
137 m->se_mean = sqrt (m->var / m->n) ;
141 m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
142 m->n_data = hsh_count(m->ordered_data);
144 /* Trimmed mean calculation */
145 if ( m->n_data <= 1 )
147 m->trimmed_mean = m->mean;
151 m->histogram = histogram_create(10, m->min, m->max);
153 for ( i = 0 ; i < m->n_data ; ++i )
155 struct weighted_value **wv = (m->wvp) ;
156 gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
163 for ( i = 0 ; i < m->n_data ; ++i )
168 m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
179 for ( i = m->n_data -1 ; i >= 0; --i )
181 if ( tc > m->n - m->wvp[i]->cc)
186 /* Calculate the percentiles */
187 ptiles (m->ptile_hash, (const struct weighted_value **) m->wvp,
188 m->n_data, m->n, m->ptile_alg);
190 tukey_hinges ((const struct weighted_value **) m->wvp,
191 m->n_data, m->n, m->hinge);
193 /* Special case here */
196 m->trimmed_mean = m->wvp[k2]->v.f;
201 for ( i = k1 + 2 ; i <= k2 - 1 ; ++i )
203 m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
207 m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
208 m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
209 m->trimmed_mean /= 0.9 * m->n ;
215 struct weighted_value *
216 weighted_value_create(void)
218 struct weighted_value *wv;
219 wv = xmalloc (sizeof *wv);
228 weighted_value_free(struct weighted_value *wv)
230 struct case_node *cn ;
239 struct case_node *next = cn->next;
253 /* Create a factor statistics object with for N dependent vars
254 and ID0 and ID1 as the values of the independent variable */
255 struct factor_statistics *
256 create_factor_statistics (int n,
260 struct factor_statistics *f;
262 f = xmalloc (sizeof *f);
266 f->m = xnmalloc (n, sizeof *f->m);
267 memset (f->m, 0, sizeof(struct metrics) * n);
274 metrics_destroy(struct metrics *m)
276 hsh_destroy(m->ordered_data);
277 hsh_destroy(m->ptile_hash);
279 gsl_histogram_free(m->histogram);
283 factor_statistics_free(struct factor_statistics *f)
289 for ( i = 0 ; i < f->n_var; ++i )
290 metrics_destroy(&f->m[i]);
297 factor_statistics_compare(const struct factor_statistics *f0,
298 const struct factor_statistics *f1, int width)
306 cmp0 = compare_values(f0->id[0], f1->id[0], width);
312 if ( ( f0->id[1]->f == SYSMIS ) && (f1->id[1]->f != SYSMIS) )
315 if ( ( f0->id[1]->f != SYSMIS ) && (f1->id[1]->f == SYSMIS) )
318 return compare_values (f0->id[1], f1->id[1], width);
322 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);