259694e8fc5aafd2d982404b57f958a1f2e86d6e
[pspp-builds.git] / src / factor_stats.h
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 #ifndef FACTOR_STATS
22 #define FACTOR_STATS
23
24
25 /* FIXME: These things should probably be amalgamated with the 
26    group_statistics struct */
27
28 #include "hash.h"
29 #include "val.h"
30 #include <string.h>
31 #include <gsl/gsl_histogram.h>
32 #include "subclist.h"
33 #include "percentiles.h"
34
35 struct moments1;
36
37 struct metrics
38 {
39   double n;
40
41   double n_missing;
42   
43   double min;
44
45   double max;
46
47   double mean;
48   
49   double se_mean;
50
51   double var;
52
53   double stddev;
54
55   struct moments1 *moments;
56
57   gsl_histogram *histogram;
58
59   double skewness;
60   double kurtosis;
61
62   double trimmed_mean;
63
64   /* A hash of data for this factor. */
65   struct hsh_table *ordered_data;
66
67   /* A Pointer to this hash table AFTER it has been SORTED and crunched */
68   struct weighted_value **wvp;
69
70   /* The number of values in the above array
71      (if all the weights are 1, then this will
72      be the same as n) */
73   int n_data;
74
75   /* Percentile stuff */
76
77   /* A hash of struct percentiles */
78   struct hsh_table *ptile_hash;
79
80   /* Algorithm to be used for calculating percentiles */
81   enum pc_alg ptile_alg;
82
83   /* Tukey's Hinges */
84   double hinge[3];
85
86 };
87
88
89 struct metrics * metrics_create(void);
90
91 void metrics_precalc(struct metrics *m);
92
93 void metrics_calc(struct metrics *m, const union value *f, double weight, 
94                   int case_no);
95
96 void metrics_postcalc(struct metrics *m);
97
98 void  metrics_destroy(struct metrics *m);
99
100
101
102 /* Linked list of case nos */
103 struct case_node
104 {
105   int num;
106   struct case_node *next;
107 };
108
109 struct weighted_value 
110 {
111   union value v;
112
113   /* The weight */
114   double w;
115
116   /* The cumulative weight */
117   double cc; 
118
119   /* The rank */
120   double rank;
121
122   /* Linked list of cases nos which have this value */
123   struct case_node *case_nos;
124   
125 };
126
127
128 struct weighted_value *weighted_value_create(void);
129
130 void weighted_value_free(struct weighted_value *wv);
131
132
133
134 struct factor_statistics {
135
136   /* The values of the independent variables */
137   union value id[2];
138
139   /* The an array stats for this factor, one for each dependent var */
140   struct metrics *m;
141
142   /* The number of dependent variables */
143   int n_var;
144 };
145
146
147 /* Create a factor statistics object with for N dependent vars
148    and ID as the value of the independent variable */
149 struct factor_statistics * 
150 create_factor_statistics (int n, union value *id0, union value *id1);
151
152
153 void factor_statistics_free(struct factor_statistics *f);
154
155
156 /* Compare f0 and f1.
157    width is the width of the independent variable */
158 int 
159 factor_statistics_compare(const struct factor_statistics *f0,
160                           const struct factor_statistics *f1, int width);
161
162                               
163
164 unsigned int 
165 factor_statistics_hash(const struct factor_statistics *f, int width);
166
167 #endif