fe259ff4e790d31f91b4ce7c4a7f3906836e93a5
[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
25 #include <stdlib.h>
26 #include <math.h>
27 #include <float.h>
28
29 void
30 metrics_precalc(struct metrics *fs)
31 {
32   fs->n = 0;
33   fs->ssq = 0;
34   fs->sum = 0;
35   fs->min = DBL_MAX;
36   fs->max = -DBL_MAX;
37 }
38
39 void
40 metrics_calc(struct metrics *fs, double x, double weight)
41 {
42   fs->n    += weight;
43   fs->ssq  += x * x * weight;
44   fs->sum  += x * weight;
45
46   if ( x < fs->min) fs->min = x;
47   if ( x > fs->max) fs->max = x;
48
49 }
50
51 void
52 metrics_postcalc(struct metrics *fs)
53 {
54   double sample_var; 
55   fs->mean = fs->sum / fs->n;
56
57   sample_var = ( fs->ssq / fs->n  - fs->mean * fs->mean );
58
59   fs->var  = fs->n * sample_var / ( fs->n - 1) ;
60   fs->stddev = sqrt(fs->var);
61
62
63   /* FIXME: Check this is correct ???
64      Shouldn't we use the sample variance ??? */
65   fs->stderr = sqrt (fs->var / fs->n) ;
66
67 }
68
69
70 /* Functions for hashes */
71
72 void 
73 free_factor_stats(struct factor_statistics *f, int width UNUSED)
74 {
75   free (f);
76 }
77
78 int
79 compare_indep_values(const struct factor_statistics *f1, 
80                      const struct factor_statistics *f2, 
81                      int width)
82 {
83   return compare_values(f1->id, f2->id, width);
84 }
85
86
87 unsigned 
88 hash_indep_value(const struct factor_statistics *f, int width)
89 {
90   return hash_value(f->id, width);
91 }