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"
36 metrics_precalc(struct metrics *fs)
47 fs->ordered_data = hsh_create(20,
48 (hsh_compare_func *) compare_values,
49 (hsh_hash_func *) hash_value,
50 (hsh_free_func *) weighted_value_free,
56 /* Include val in the calculation for the metrics.
57 If val is null, then treat it as MISSING
60 metrics_calc(struct metrics *fs, const union value *val,
61 double weight, int case_no)
63 struct weighted_value **wv;
68 fs->n_missing += weight;
74 fs->ssq += x * x * weight;
75 fs->sum += x * weight;
77 if ( x < fs->min) fs->min = x;
78 if ( x > fs->max) fs->max = x;
81 wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
85 /* If this value has already been seen, then simply
86 increase its weight and push a new case number */
90 assert( (*wv)->v.f == val->f );
93 cn = xmalloc( sizeof (struct case_node) ) ;
94 cn->next = (*wv)->case_nos ;
101 struct case_node *cn;
103 *wv = weighted_value_create();
107 cn = xmalloc( sizeof (struct case_node) ) ;
110 (*wv)->case_nos = cn;
117 metrics_postcalc(struct metrics *m)
126 m->mean = m->sum / m->n;
128 sample_var = ( m->ssq / m->n - m->mean * m->mean );
130 m->var = m->n * sample_var / ( m->n - 1) ;
131 m->stddev = sqrt(m->var);
134 /* FIXME: Check this is correct ???
135 Shouldn't we use the sample variance ??? */
136 m->stderr = sqrt (m->var / m->n) ;
138 m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
139 m->n_data = hsh_count(m->ordered_data);
141 if ( m->n_data == 0 )
143 m->trimmed_mean = m->mean;
148 /* Trimmed mean calculation */
155 for ( i = 0 ; i < m->n_data ; ++i )
160 m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
170 for ( i = m->n_data -1 ; i >= 0; --i )
172 if ( tc > m->n - m->wvp[i]->cc)
178 for ( i = k1 + 2 ; i <= k2 - 1 ; ++i )
180 m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
184 m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
185 m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
186 m->trimmed_mean /= 0.9 * m->n ;
191 struct weighted_value *
192 weighted_value_create(void)
194 struct weighted_value *wv;
195 wv = xmalloc (sizeof (struct weighted_value ));
204 weighted_value_free(struct weighted_value *wv)
206 struct case_node *cn = wv->case_nos;
210 struct case_node *next = cn->next;
224 /* Create a factor statistics object with for N dependent vars
225 and ID as the value of the independent variable */
226 struct factor_statistics *
227 create_factor_statistics (int n, union value *id0, union value *id1)
229 struct factor_statistics *f;
231 f = xmalloc( sizeof ( struct factor_statistics ));
235 f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
242 factor_statistics_free(struct factor_statistics *f)
244 hsh_destroy(f->m->ordered_data);
253 factor_statistics_compare(const struct factor_statistics *f0,
254 const struct factor_statistics *f1, void *aux)
262 cmp0 = compare_values(&f0->id[0], &f1->id[0], aux);
268 if ( ( f0->id[1].f == SYSMIS ) && (f1->id[1].f != SYSMIS) )
271 if ( ( f0->id[1].f != SYSMIS ) && (f1->id[1].f == SYSMIS) )
274 return compare_values(&f0->id[1], &f1->id[1], aux);
279 factor_statistics_hash(const struct factor_statistics *f, void *aux)
284 h = hash_value(&f->id[0], aux);
286 if ( f->id[1].f != SYSMIS )
287 h += hash_value(&f->id[1], aux);