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"
25 #include "algorithm.h"
37 metrics_precalc(struct metrics *m)
47 m->moments = moments1_create(MOMENT_KURTOSIS);
49 m->ordered_data = hsh_create(20,
50 (hsh_compare_func *) compare_values,
51 (hsh_hash_func *) hash_value,
52 (hsh_free_func *) weighted_value_free,
55 m->histogram = gsl_histogram_alloc(10);
60 /* Include val in the calculation for the metrics.
61 If val is null, then treat it as MISSING
64 metrics_calc(struct metrics *fs, const union value *val,
65 double weight, int case_no)
67 struct weighted_value **wv;
72 fs->n_missing += weight;
78 moments1_add(fs->moments, x, weight);
81 if ( x < fs->min) fs->min = x;
82 if ( x > fs->max) fs->max = x;
85 wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
89 /* If this value has already been seen, then simply
90 increase its weight and push a new case number */
94 assert( (*wv)->v.f == val->f );
97 cn = xmalloc( sizeof (struct case_node) ) ;
98 cn->next = (*wv)->case_nos ;
101 (*wv)->case_nos = cn;
105 struct case_node *cn;
107 *wv = weighted_value_create();
111 cn = xmalloc( sizeof (struct case_node) ) ;
114 (*wv)->case_nos = cn;
121 metrics_postcalc(struct metrics *m)
130 moments1_calculate (m->moments, &m->n, &m->mean, &m->var,
131 &m->skewness, &m->kurtosis);
133 moments1_destroy (m->moments);
136 m->stddev = sqrt(m->var);
138 /* FIXME: Check this is correct ???
139 Shouldn't we use the sample variance ??? */
140 m->se_mean = sqrt (m->var / m->n) ;
144 m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
145 m->n_data = hsh_count(m->ordered_data);
147 if ( m->n_data == 0 )
149 m->trimmed_mean = m->mean;
154 /* Trimmed mean calculation */
161 for ( i = 0 ; i < m->n_data ; ++i )
166 m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
176 for ( i = m->n_data -1 ; i >= 0; --i )
178 if ( tc > m->n - m->wvp[i]->cc)
184 for ( i = k1 + 2 ; i <= k2 - 1 ; ++i )
186 m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
190 m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
191 m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
192 m->trimmed_mean /= 0.9 * m->n ;
195 gsl_histogram_set_ranges_uniform(m->histogram, m->min, m->max);
197 for ( i = 0 ; i < m->n_data ; ++i )
199 struct weighted_value **wv = (m->wvp) ;
200 gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
206 struct weighted_value *
207 weighted_value_create(void)
209 struct weighted_value *wv;
210 wv = xmalloc (sizeof (struct weighted_value ));
219 weighted_value_free(struct weighted_value *wv)
221 struct case_node *cn = wv->case_nos;
225 struct case_node *next = cn->next;
239 /* Create a factor statistics object with for N dependent vars
240 and ID as the value of the independent variable */
241 struct factor_statistics *
242 create_factor_statistics (int n, union value *id0, union value *id1)
244 struct factor_statistics *f;
246 f = xmalloc( sizeof ( struct factor_statistics ));
250 f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
257 factor_statistics_free(struct factor_statistics *f)
259 hsh_destroy(f->m->ordered_data);
260 gsl_histogram_free(f->m->histogram);
269 factor_statistics_compare(const struct factor_statistics *f0,
270 const struct factor_statistics *f1, int width)
278 cmp0 = compare_values(&f0->id[0], &f1->id[0], width);
284 if ( ( f0->id[1].f == SYSMIS ) && (f1->id[1].f != SYSMIS) )
287 if ( ( f0->id[1].f != SYSMIS ) && (f1->id[1].f == SYSMIS) )
290 return compare_values(&f0->id[1], &f1->id[1], width);
295 factor_statistics_hash(const struct factor_statistics *f, int width)
300 h = hash_value(&f->id[0], width);
302 if ( f->id[1].f != SYSMIS )
303 h += hash_value(&f->id[1], width);