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