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