Rewrote most of the examine command.
[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
28 #include <stdlib.h>
29 #include <math.h>
30 #include <float.h>
31 #include <assert.h>
32
33
34
35 void
36 metrics_precalc(struct metrics *fs)
37 {
38   assert (fs) ;
39
40   fs->n = 0;
41   fs->n_missing = 0;
42   fs->ssq = 0;
43   fs->sum = 0;
44   fs->min = DBL_MAX;
45   fs->max = -DBL_MAX;
46
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,
51                                 (void *) 0);
52
53 }
54
55
56 /* Include val in the calculation for the metrics.
57    If val is null, then treat it as MISSING
58 */
59 void
60 metrics_calc(struct metrics *fs, const union value *val, 
61              double weight, int case_no)
62 {
63   struct weighted_value **wv;
64   double x;
65   
66   if ( ! val ) 
67     {
68       fs->n_missing += weight;
69       return ;
70     }
71
72   x = val->f;
73   fs->n    += weight;
74   fs->ssq  += x * x * weight;
75   fs->sum  += x * weight;
76
77   if ( x < fs->min) fs->min = x;
78   if ( x > fs->max) fs->max = x;
79
80
81   wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
82
83   if ( *wv  ) 
84     {
85       /* If this value has already been seen, then simply 
86          increase its weight  and push a new case number */
87
88       struct case_node *cn;
89
90       assert( (*wv)->v.f == val->f );
91       (*wv)->w += weight;      
92
93       cn = xmalloc( sizeof (struct case_node) ) ;
94       cn->next = (*wv)->case_nos ;
95       cn->num = case_no;
96
97       (*wv)->case_nos = cn;
98     }
99   else
100     {
101       struct case_node *cn;
102
103       *wv = weighted_value_create();
104       (*wv)->v = *val;
105       (*wv)->w = weight;
106       
107       cn = xmalloc( sizeof (struct case_node) ) ;
108       cn->next=0;
109       cn->num = case_no;
110       (*wv)->case_nos  = cn;
111
112     }
113
114 }
115
116 void
117 metrics_postcalc(struct metrics *m)
118 {
119   double sample_var; 
120   double cc = 0.0;
121   double tc ;
122   int k1, k2 ;
123   int i;
124   int j = 1;  
125
126   struct weighted_value **data;
127
128
129   int n_data;
130   
131   m->mean = m->sum / m->n;
132
133   sample_var = ( m->ssq / m->n  - m->mean * m->mean );
134
135   m->var  = m->n * sample_var / ( m->n - 1) ;
136   m->stddev = sqrt(m->var);
137
138
139   /* FIXME: Check this is correct ???
140      Shouldn't we use the sample variance ??? */
141   m->stderr = sqrt (m->var / m->n) ;
142
143   data = (struct weighted_value **) hsh_data(m->ordered_data);
144   n_data = hsh_count(m->ordered_data);
145
146   if ( n_data == 0 ) 
147     {
148       m->trimmed_mean = m->mean;
149       return;
150     }
151
152
153   m->wv = xmalloc(sizeof(struct weighted_value ) * n_data);
154
155   for ( i = 0 ; i < n_data ; ++i )
156       m->wv[i] = *(data[i]);
157
158   sort (m->wv, n_data, sizeof (struct weighted_value) , 
159         (algo_compare_func *) compare_values, 0);
160
161   
162   /* Trimmed mean calculation */
163
164   tc = m->n * 0.05 ;
165   k1 = -1;
166   k2 = -1;
167
168
169   for ( i = 0 ; i < n_data ; ++i ) 
170     {
171       cc += m->wv[i].w;
172       m->wv[i].cc = cc;
173
174       m->wv[i].rank = j + (m->wv[i].w - 1) / 2.0 ;
175       
176       j += m->wv[i].w;
177       
178       if ( cc < tc ) 
179         k1 = i;
180
181     }
182
183   k2 = n_data;
184   for ( i = n_data -1  ; i >= 0; --i ) 
185     {
186       if ( tc > m->n - m->wv[i].cc) 
187         k2 = i;
188     }
189
190
191   m->trimmed_mean = 0;
192   for ( i = k1 + 2 ; i <= k2 - 1 ; ++i ) 
193     {
194       m->trimmed_mean += m->wv[i].v.f * m->wv[i].w;
195     }
196
197
198   m->trimmed_mean += (m->n - m->wv[k2 - 1].cc - tc) * m->wv[k2].v.f ;
199   m->trimmed_mean += (m->wv[k1 + 1].cc - tc) * m->wv[k1 + 1].v.f ;
200   m->trimmed_mean /= 0.9 * m->n ;
201
202 }
203
204
205 struct weighted_value *
206 weighted_value_create(void)
207 {
208   struct weighted_value *wv;
209   wv = xmalloc (sizeof (struct weighted_value ));
210
211   wv->cc = 0;
212   wv->case_nos = 0;
213
214   return wv;
215 }
216
217 void 
218 weighted_value_free(struct weighted_value *wv)
219 {
220   struct case_node *cn = wv->case_nos;
221
222   while(cn)
223     {
224       struct case_node *next = cn->next;
225       
226       free(cn);
227       cn = next;
228     }
229
230   free(wv);
231
232 }
233
234
235
236
237
238 /* Create a factor statistics object with for N dependent vars
239    and ID as the value of the independent variable */
240 struct factor_statistics * 
241 create_factor_statistics (int n, union value *id0, union value *id1)
242 {
243   struct factor_statistics *f;
244
245   f =  xmalloc( sizeof  ( struct factor_statistics ));
246
247   f->id[0] = *id0;
248   f->id[1] = *id1;
249   f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
250
251   return f;
252 }
253
254
255 void
256 factor_statistics_free(struct factor_statistics *f)
257 {
258   free(f->m) ; 
259
260   free(f);
261 }
262
263
264
265
266
267
268 int 
269 factor_statistics_compare(const struct factor_statistics *f0,
270                           const struct factor_statistics *f1, void *aux)
271 {
272
273   int cmp0;
274
275   assert(f0);
276   assert(f1);
277
278   cmp0 = compare_values(&f0->id[0], &f1->id[0], aux);
279
280   if ( cmp0 != 0 ) 
281     return cmp0;
282
283
284   if ( ( f0->id[1].f == SYSMIS )  && (f1->id[1].f != SYSMIS) ) 
285     return 1;
286
287   if ( ( f0->id[1].f != SYSMIS )  && (f1->id[1].f == SYSMIS) ) 
288     return -1;
289
290   return compare_values(&f0->id[1], &f1->id[1], aux);
291   
292 }
293
294 unsigned int 
295 factor_statistics_hash(const struct factor_statistics *f, void *aux)
296 {
297   
298   unsigned int h;
299
300   h = hash_value(&f->id[0], aux);
301   
302   if ( f->id[1].f != SYSMIS )
303     h += hash_value(&f->id[1], aux);
304
305
306   return h;
307
308 }
309