317e346e7b9182348fcd373cdc47591f8f9d327b
[pspp-builds.git] / src / math / factor-stats.c
1 /* PSPP - A program for statistical analysis . -*-c-*-
2
3 Copyright (C) 2004 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA. */
19
20 #include <config.h>
21 #include "factor-stats.h"
22 #include <data/value.h>
23 #include <libpspp/hash.h>
24 #include <libpspp/array.h>
25 #include <libpspp/alloc.h>
26 #include "moments.h"
27 #include "percentiles.h"
28
29 #include <stdlib.h>
30 #include <math.h>
31 #include <float.h>
32 #include <assert.h>
33 #include "histogram.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   m->histogram = 0;
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 *cn);
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 *cn);
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   /* Trimmed mean calculation */
145   if ( m->n_data <= 1 )
146     {
147       m->trimmed_mean = m->mean;
148       return;
149     }
150
151   m->histogram = histogram_create(10, m->min, m->max);
152
153   for ( i = 0 ; i < m->n_data ; ++i )
154     {
155       struct weighted_value **wv = (m->wvp) ;
156       gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
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, (const struct weighted_value **) m->wvp,
188           m->n_data, m->n, m->ptile_alg);
189
190   tukey_hinges ((const struct weighted_value **) m->wvp,
191                 m->n_data, m->n, m->hinge);
192
193   /* Special case here */
194   if ( k1 + 1 == k2 )
195     {
196       m->trimmed_mean = m->wvp[k2]->v.f;
197       return;
198     }
199
200   m->trimmed_mean = 0;
201   for ( i = k1 + 2 ; i <= k2 - 1 ; ++i )
202     {
203       m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
204     }
205
206
207   m->trimmed_mean += (m->n - m->wvp[k2 - 1]->cc - tc) * m->wvp[k2]->v.f ;
208   m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
209   m->trimmed_mean /= 0.9 * m->n ;
210
211
212 }
213
214
215 struct weighted_value *
216 weighted_value_create(void)
217 {
218   struct weighted_value *wv;
219   wv = xmalloc (sizeof *wv);
220
221   wv->cc = 0;
222   wv->case_nos = 0;
223
224   return wv;
225 }
226
227 void
228 weighted_value_free(struct weighted_value *wv)
229 {
230   struct case_node *cn ;
231
232   if ( !wv )
233     return ;
234
235   cn = wv->case_nos;
236
237   while(cn)
238     {
239       struct case_node *next = cn->next;
240
241       free(cn);
242       cn = next;
243     }
244
245   free(wv);
246
247 }
248
249
250
251
252
253 /* Create a factor statistics object with for N dependent vars
254    and ID0 and ID1 as the values of the independent variable */
255 struct factor_statistics *
256 create_factor_statistics (int n,
257                           union value *id0,
258                           union value *id1)
259 {
260   struct factor_statistics *f;
261
262   f = xmalloc (sizeof *f);
263
264   f->id[0] = id0;
265   f->id[1] = id1;
266   f->m = xnmalloc (n, sizeof *f->m);
267   memset (f->m, 0, sizeof(struct metrics) * n);
268   f->n_var = n;
269
270   return f;
271 }
272
273 void
274 metrics_destroy(struct metrics *m)
275 {
276   hsh_destroy(m->ordered_data);
277   hsh_destroy(m->ptile_hash);
278   if ( m-> histogram )
279     gsl_histogram_free(m->histogram);
280 }
281
282 void
283 factor_statistics_free(struct factor_statistics *f)
284 {
285
286   int i;
287   free (f->id[0]);
288   free (f->id[1]);
289   for ( i = 0 ; i < f->n_var; ++i )
290        metrics_destroy(&f->m[i]);
291   free(f->m) ;
292   free(f);
293 }
294
295
296 int
297 factor_statistics_compare(const struct factor_statistics *f0,
298                           const struct factor_statistics *f1, int width)
299 {
300
301   int cmp0;
302
303   assert(f0);
304   assert(f1);
305
306   cmp0 = compare_values(f0->id[0], f1->id[0], width);
307
308   if ( cmp0 != 0 )
309     return cmp0;
310
311
312   if ( ( f0->id[1]->f == SYSMIS ) && (f1->id[1]->f != SYSMIS) )
313     return 1;
314
315   if ( ( f0->id[1]->f != SYSMIS )  && (f1->id[1]->f == SYSMIS) )
316     return -1;
317
318   return compare_values (f0->id[1], f1->id[1], width);
319 }
320
321 unsigned int
322 factor_statistics_hash (const struct factor_statistics *f, int width)
323 {
324   unsigned int h;
325
326   h = hash_value (f->id[0], width);
327
328   if ( f->id[1]->f != SYSMIS )
329     h += hash_value(f->id[1], width);
330
331   return h;
332 }