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