23457ce67f3771c0654099b3adea9dfe16f3a586
[pspp-builds.git] / src / math / moments.h
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 #ifndef HEADER_MOMENTS
18 #define HEADER_MOMENTS
19
20 #include <stddef.h>
21 #include <data/value.h>
22
23 /* Moments of the mean.
24    Higher-order moments have higher values. */
25 enum moment
26   {
27     MOMENT_NONE,                /* No moments. */
28     MOMENT_MEAN,                /* First-order moment. */
29     MOMENT_VARIANCE,            /* Second-order moment. */
30     MOMENT_SKEWNESS,            /* Third-order moment. */
31     MOMENT_KURTOSIS             /* Fourth-order moment. */
32   };
33
34 struct moments;
35
36 /* Two-pass moments. */
37 struct moments *moments_create (enum moment max_moment);
38 void moments_clear (struct moments *);
39 void moments_pass_one (struct moments *, double value, double weight);
40 void moments_pass_two (struct moments *, double value, double weight);
41 void moments_calculate (const struct moments *,
42                         double *weight,
43                         double *mean, double *variance,
44                         double *skewness, double *kurtosis);
45 void moments_destroy (struct moments *);
46
47 /* Convenience functions for two-pass moments. */
48 void moments_of_doubles (const double *array, size_t cnt,
49                          double *weight,
50                          double *mean, double *variance,
51                          double *skewness, double *kurtosis);
52 void moments_of_values (const union value *array, size_t cnt,
53                         double *weight,
54                         double *mean, double *variance,
55                         double *skewness, double *kurtosis);
56
57 /* One-pass moments.  Use only if two passes are impractical. */
58 struct moments1 *moments1_create (enum moment max_moment);
59 void moments1_clear (struct moments1 *);
60 void moments1_add (struct moments1 *, double value, double weight);
61 void moments1_calculate (const struct moments1 *,
62                          double *weight,
63                          double *mean, double *variance,
64                          double *skewness, double *kurtosis);
65 void moments1_destroy (struct moments1 *);
66
67 /* Standard errors. */
68 double calc_semean (double stddev, double weight);
69 double calc_seskew (double weight);
70 double calc_sekurt (double weight);
71
72 #endif /* moments.h */