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