Fixed numerous memory leaks. Changed many of the test cases to use a canonical
[pspp-builds.git] / src / factor_stats.c
1 /* PSPP - A program for statistical analysis . -*-c-*-
2
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Author: John Darrington 2004
5
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.
10
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.
15
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
19 02111-1307, USA. */
20
21 #include "factor_stats.h"
22 #include "config.h"
23 #include "val.h"
24 #include "hash.h"
25 #include "algorithm.h"
26 #include "alloc.h"
27 #include "moments.h"
28 #include "percentiles.h"
29
30 #include <stdlib.h>
31 #include <math.h>
32 #include <float.h>
33 #include <assert.h>
34 #include <chart.h>
35
36
37 void
38 metrics_precalc(struct metrics *m)
39 {
40   assert (m) ;
41
42   m->n_missing = 0;
43
44   m->min = DBL_MAX;
45   m->max = -DBL_MAX;
46
47
48   m->moments = moments1_create(MOMENT_KURTOSIS);
49
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,
54                                 (void *) 0);
55 }
56
57
58 /* Include val in the calculation for the metrics.
59    If val is null, then treat it as MISSING
60 */
61 void
62 metrics_calc(struct metrics *fs, const union value *val, 
63              double weight, int case_no)
64 {
65   struct weighted_value **wv;
66   double x;
67   
68   if ( ! val ) 
69     {
70       fs->n_missing += weight;
71       return ;
72     }
73
74   x = val->f;
75
76   moments1_add(fs->moments, x, weight);
77
78
79   if ( x < fs->min) fs->min = x;
80   if ( x > fs->max) fs->max = x;
81
82
83   wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
84
85   if ( *wv  ) 
86     {
87       /* If this value has already been seen, then simply 
88          increase its weight  and push a new case number */
89
90       struct case_node *cn;
91
92       assert( (*wv)->v.f == val->f );
93       (*wv)->w += weight;      
94
95       cn = xmalloc( sizeof (struct case_node) ) ;
96       cn->next = (*wv)->case_nos ;
97       cn->num = case_no;
98
99       (*wv)->case_nos = cn;
100     }
101   else
102     {
103       struct case_node *cn;
104
105       *wv = weighted_value_create();
106       (*wv)->v = *val;
107       (*wv)->w = weight;
108       
109       cn = xmalloc( sizeof (struct case_node) ) ;
110       cn->next=0;
111       cn->num = case_no;
112       (*wv)->case_nos  = cn;
113
114     }
115
116 }
117
118 void
119 metrics_postcalc(struct metrics *m)
120 {
121   double cc = 0.0;
122   double tc ;
123   int k1, k2 ;
124   int i;
125   int j = 1;  
126
127   moments1_calculate (m->moments, &m->n, &m->mean, &m->var, 
128                       &m->skewness, &m->kurtosis);
129
130   moments1_destroy (m->moments);
131
132
133   m->stddev = sqrt(m->var);
134
135   /* FIXME: Check this is correct ???
136      Shouldn't we use the sample variance ??? */
137   m->se_mean = sqrt (m->var / m->n) ;
138
139
140
141   m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
142   m->n_data = hsh_count(m->ordered_data);
143
144   m->histogram = histogram_create(10, m->min, m->max);
145
146   for ( i = 0 ; i < m->n_data ; ++i ) 
147     {
148       struct weighted_value **wv = (m->wvp) ;
149       gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
150     }
151
152   /* Trimmed mean calculation */
153   if ( m->n_data <= 1 ) 
154     {
155       m->trimmed_mean = m->mean;
156       return;
157     }
158
159   tc = m->n * 0.05 ;
160   k1 = -1;
161   k2 = -1;
162
163   for ( i = 0 ; i < m->n_data ; ++i ) 
164     {
165       cc += m->wvp[i]->w;
166       m->wvp[i]->cc = cc;
167
168       m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
169       
170       j += m->wvp[i]->w;
171       
172       if ( cc < tc ) 
173         k1 = i;
174     }
175
176   
177
178   k2 = m->n_data;
179   for ( i = m->n_data -1  ; i >= 0; --i ) 
180     {
181       if ( tc > m->n - m->wvp[i]->cc) 
182         k2 = i;
183     }
184
185
186   /* Calculate the percentiles */
187   ptiles(m->ptile_hash, m->wvp, m->n_data, m->n, m->ptile_alg);
188
189   tukey_hinges(m->wvp, m->n_data, m->n, m->hinge);
190
191   /* Special case here */
192   if ( k1 + 1 == k2 ) 
193     {
194       m->trimmed_mean = m->wvp[k2]->v.f;
195       return;
196     }
197
198   m->trimmed_mean = 0;
199   for ( i = k1 + 2 ; i <= k2 - 1 ; ++i ) 
200     {
201       m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
202     }
203
204
205   m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
206   m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
207   m->trimmed_mean /= 0.9 * m->n ;
208
209
210 }
211
212
213 struct weighted_value *
214 weighted_value_create(void)
215 {
216   struct weighted_value *wv;
217   wv = xmalloc (sizeof (struct weighted_value ));
218
219   wv->cc = 0;
220   wv->case_nos = 0;
221
222   return wv;
223 }
224
225 void 
226 weighted_value_free(struct weighted_value *wv)
227 {
228   struct case_node *cn ;
229
230   if ( !wv ) 
231     return ;
232
233   cn = wv->case_nos;
234
235   while(cn)
236     {
237       struct case_node *next = cn->next;
238       
239       free(cn);
240       cn = next;
241     }
242
243   free(wv);
244
245 }
246
247
248
249
250
251 /* Create a factor statistics object with for N dependent vars
252    and ID as the value of the independent variable */
253 struct factor_statistics * 
254 create_factor_statistics (int n, union value *id0, union value *id1)
255 {
256   struct factor_statistics *f;
257
258   f =  xmalloc( sizeof  ( struct factor_statistics ));
259
260   f->id[0] = *id0;
261   f->id[1] = *id1;
262   f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
263   memset (f->m, 0, sizeof(struct metrics) * n);
264   f->n_var = n;
265
266   return f;
267 }
268
269
270 void 
271 metrics_destroy(struct metrics *m)
272 {
273   hsh_destroy(m->ordered_data);
274   hsh_destroy(m->ptile_hash);
275   gsl_histogram_free(m->histogram);
276 }
277
278 void
279 factor_statistics_free(struct factor_statistics *f)
280 {
281
282   int i; 
283   for ( i = 0 ; i < f->n_var; ++i ) 
284        metrics_destroy(&f->m[i]);
285   free(f->m) ; 
286   free(f);
287 }
288
289
290
291
292 int 
293 factor_statistics_compare(const struct factor_statistics *f0,
294                           const struct factor_statistics *f1, int width)
295 {
296
297   int cmp0;
298
299   assert(f0);
300   assert(f1);
301
302   cmp0 = compare_values(&f0->id[0], &f1->id[0], width);
303
304   if ( cmp0 != 0 ) 
305     return cmp0;
306
307
308   if ( ( f0->id[1].f == SYSMIS )  && (f1->id[1].f != SYSMIS) ) 
309     return 1;
310
311   if ( ( f0->id[1].f != SYSMIS )  && (f1->id[1].f == SYSMIS) ) 
312     return -1;
313
314   return compare_values(&f0->id[1], &f1->id[1], width);
315   
316 }
317
318 unsigned int 
319 factor_statistics_hash(const struct factor_statistics *f, int width)
320 {
321   
322   unsigned int h;
323
324   h = hash_value(&f->id[0], width);
325   
326   if ( f->id[1].f != SYSMIS )
327     h += hash_value(&f->id[1], width);
328
329
330   return h;
331
332 }
333