Added the skewness and kurtosis calculations to 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 #include "moments.h"
28
29 #include <stdlib.h>
30 #include <math.h>
31 #include <float.h>
32 #include <assert.h>
33
34
35
36 void
37 metrics_precalc(struct metrics *fs)
38 {
39   assert (fs) ;
40
41   fs->n_missing = 0;
42
43   fs->min = DBL_MAX;
44   fs->max = -DBL_MAX;
45
46
47   fs->moments = moments1_create(MOMENT_KURTOSIS);
48
49   fs->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 /* 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
128   moments1_calculate (m->moments, &m->n, &m->mean, &m->var, 
129                       &m->skewness, &m->kurtosis);
130
131   moments1_destroy (m->moments);
132
133
134   m->stddev = sqrt(m->var);
135
136   /* FIXME: Check this is correct ???
137      Shouldn't we use the sample variance ??? */
138   m->stderr = sqrt (m->var / m->n) ;
139
140
141
142   m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
143   m->n_data = hsh_count(m->ordered_data);
144
145   if ( m->n_data == 0 ) 
146     {
147       m->trimmed_mean = m->mean;
148       return;
149     }
150
151
152   /* Trimmed mean calculation */
153
154   tc = m->n * 0.05 ;
155   k1 = -1;
156   k2 = -1;
157
158
159   for ( i = 0 ; i < m->n_data ; ++i ) 
160     {
161       cc += m->wvp[i]->w;
162       m->wvp[i]->cc = cc;
163
164       m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
165       
166       j += m->wvp[i]->w;
167       
168       if ( cc < tc ) 
169         k1 = i;
170
171     }
172
173   k2 = m->n_data;
174   for ( i = m->n_data -1  ; i >= 0; --i ) 
175     {
176       if ( tc > m->n - m->wvp[i]->cc) 
177         k2 = i;
178     }
179
180
181   m->trimmed_mean = 0;
182   for ( i = k1 + 2 ; i <= k2 - 1 ; ++i ) 
183     {
184       m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
185     }
186
187
188   m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
189   m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
190   m->trimmed_mean /= 0.9 * m->n ;
191
192 }
193
194
195 struct weighted_value *
196 weighted_value_create(void)
197 {
198   struct weighted_value *wv;
199   wv = xmalloc (sizeof (struct weighted_value ));
200
201   wv->cc = 0;
202   wv->case_nos = 0;
203
204   return wv;
205 }
206
207 void 
208 weighted_value_free(struct weighted_value *wv)
209 {
210   struct case_node *cn = wv->case_nos;
211
212   while(cn)
213     {
214       struct case_node *next = cn->next;
215       
216       free(cn);
217       cn = next;
218     }
219
220   free(wv);
221
222 }
223
224
225
226
227
228 /* Create a factor statistics object with for N dependent vars
229    and ID as the value of the independent variable */
230 struct factor_statistics * 
231 create_factor_statistics (int n, union value *id0, union value *id1)
232 {
233   struct factor_statistics *f;
234
235   f =  xmalloc( sizeof  ( struct factor_statistics ));
236
237   f->id[0] = *id0;
238   f->id[1] = *id1;
239   f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
240
241   return f;
242 }
243
244
245 void
246 factor_statistics_free(struct factor_statistics *f)
247 {
248   hsh_destroy(f->m->ordered_data);
249   free(f->m) ; 
250   free(f);
251 }
252
253
254
255
256 int 
257 factor_statistics_compare(const struct factor_statistics *f0,
258                           const struct factor_statistics *f1, int width)
259 {
260
261   int cmp0;
262
263   assert(f0);
264   assert(f1);
265
266   cmp0 = compare_values(&f0->id[0], &f1->id[0], width);
267
268   if ( cmp0 != 0 ) 
269     return cmp0;
270
271
272   if ( ( f0->id[1].f == SYSMIS )  && (f1->id[1].f != SYSMIS) ) 
273     return 1;
274
275   if ( ( f0->id[1].f != SYSMIS )  && (f1->id[1].f == SYSMIS) ) 
276     return -1;
277
278   return compare_values(&f0->id[1], &f1->id[1], width);
279   
280 }
281
282 unsigned int 
283 factor_statistics_hash(const struct factor_statistics *f, int width)
284 {
285   
286   unsigned int h;
287
288   h = hash_value(&f->id[0], width);
289   
290   if ( f->id[1].f != SYSMIS )
291     h += hash_value(&f->id[1], width);
292
293
294   return h;
295
296 }
297