1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2012, 2013, 2019 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "data/case.h"
20 #include "data/format.h"
21 #include "data/variable.h"
23 #include "libpspp/bt.h"
24 #include "libpspp/hmap.h"
25 #include "libpspp/misc.h"
26 #include "libpspp/pool.h"
28 #include "math/moments.h"
29 #include "output/pivot-table.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) (msgid)
43 struct statistic_simple
45 struct statistic parent;
49 struct statistic_moment
51 struct statistic parent;
55 static struct statistic *
56 default_create (struct pool *pool)
58 struct statistic_moment *pvd = pool_alloc (pool, sizeof *pvd);
60 pvd->mom = moments1_create (MOMENT_KURTOSIS);
62 return (struct statistic *) pvd;
66 default_update (struct statistic *stat, double w, double x)
68 struct statistic_moment *pvd = (struct statistic_moment *) stat;
70 moments1_add (pvd->mom, x, w);
74 default_destroy (struct statistic *stat)
76 struct statistic_moment *pvd = (struct statistic_moment *) stat;
77 moments1_destroy (pvd->mom);
81 simple_destroy (struct statistic *stat UNUSED)
87 /* HARMONIC MEAN: The reciprocal of the sum of the reciprocals:
88 1 / ( 1/(x_0) + 1/(x_1) + ... + 1/(x_{n-1}) ) */
92 struct statistic parent;
97 static struct statistic *
98 harmonic_create (struct pool *pool)
100 struct harmonic_mean *hm = pool_alloc (pool, sizeof *hm);
105 return (struct statistic *) hm;
110 harmonic_update (struct statistic *stat, double w, double x)
112 struct harmonic_mean *hm = (struct harmonic_mean *) stat;
119 harmonic_get (const struct statistic *pvd)
121 const struct harmonic_mean *hm = (const struct harmonic_mean *) pvd;
123 return hm->n / hm->rsum;
128 /* GEOMETRIC MEAN: The nth root of the product of all n observations
129 pow ((x_0 * x_1 * ... x_{n - 1}), 1/n) */
130 struct geometric_mean
132 struct statistic parent;
137 static struct statistic *
138 geometric_create (struct pool *pool)
140 struct geometric_mean *gm = pool_alloc (pool, sizeof *gm);
145 return (struct statistic *) gm;
149 geometric_update (struct statistic *pvd, double w, double x)
151 struct geometric_mean *gm = (struct geometric_mean *)pvd;
152 gm->prod *= pow (x, w);
158 geometric_get (const struct statistic *pvd)
160 const struct geometric_mean *gm = (const struct geometric_mean *)pvd;
161 return pow (gm->prod, 1.0 / gm->n);
167 sum_get (const struct statistic *pvd)
171 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, &mean, 0, 0, 0);
178 n_get (const struct statistic *pvd)
182 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, 0, 0, 0, 0);
188 arithmean_get (const struct statistic *pvd)
192 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, &mean, 0, 0, 0);
198 variance_get (const struct statistic *pvd)
200 double n, mean, variance;
202 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, &mean, &variance, 0, 0);
209 stddev_get (const struct statistic *pvd)
211 return sqrt (variance_get (pvd));
218 skew_get (const struct statistic *pvd)
222 moments1_calculate (((struct statistic_moment *)pvd)->mom, NULL, NULL, NULL, &skew, 0);
228 sekurt_get (const struct statistic *pvd)
232 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, NULL, NULL, NULL, NULL);
234 return calc_sekurt (n);
238 seskew_get (const struct statistic *pvd)
242 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, NULL, NULL, NULL, NULL);
244 return calc_seskew (n);
248 kurt_get (const struct statistic *pvd)
252 moments1_calculate (((struct statistic_moment *)pvd)->mom, NULL, NULL, NULL, NULL, &kurt);
258 semean_get (const struct statistic *pvd)
262 moments1_calculate (((struct statistic_moment *)pvd)->mom, &n, NULL, &var, NULL, NULL);
264 return sqrt (var / n);
269 /* MIN: The smallest (closest to minus infinity) value. */
271 static struct statistic *
272 min_create (struct pool *pool)
274 struct statistic_simple *pvd = pool_alloc (pool, sizeof *pvd);
278 return (struct statistic *) pvd;
282 min_update (struct statistic *pvd, double w UNUSED, double x)
284 double *r = &((struct statistic_simple *)pvd)->acc;
291 min_get (const struct statistic *pvd)
293 double *r = &((struct statistic_simple *)pvd)->acc;
298 /* MAX: The largest (closest to plus infinity) value. */
300 static struct statistic *
301 max_create (struct pool *pool)
303 struct statistic_simple *pvd = pool_alloc (pool, sizeof *pvd);
307 return (struct statistic *) pvd;
311 max_update (struct statistic *pvd, double w UNUSED, double x)
313 double *r = &((struct statistic_simple *)pvd)->acc;
320 max_get (const struct statistic *pvd)
322 double *r = &((struct statistic_simple *)pvd)->acc;
331 struct statistic parent;
336 static struct statistic *
337 range_create (struct pool *pool)
339 struct range *r = pool_alloc (pool, sizeof *r);
344 return (struct statistic *) r;
348 range_update (struct statistic *pvd, double w UNUSED, double x)
350 struct range *r = (struct range *) pvd;
360 range_get (const struct statistic *pvd)
362 const struct range *r = (struct range *) pvd;
364 return r->max - r->min;
369 /* LAST: The last value (the one closest to the end of the file). */
371 static struct statistic *
372 last_create (struct pool *pool)
374 struct statistic_simple *pvd = pool_alloc (pool, sizeof *pvd);
376 return (struct statistic *) pvd;
380 last_update (struct statistic *pvd, double w UNUSED, double x)
382 struct statistic_simple *stat = (struct statistic_simple *) pvd;
388 last_get (const struct statistic *pvd)
390 const struct statistic_simple *stat = (struct statistic_simple *) pvd;
395 /* FIRST: The first value (the one closest to the start of the file). */
397 static struct statistic *
398 first_create (struct pool *pool)
400 struct statistic_simple *pvd = pool_alloc (pool, sizeof *pvd);
404 return (struct statistic *) pvd;
408 first_update (struct statistic *pvd, double w UNUSED, double x)
410 struct statistic_simple *stat = (struct statistic_simple *) pvd;
412 if (stat->acc == SYSMIS)
417 first_get (const struct statistic *pvd)
419 const struct statistic_simple *stat = (struct statistic_simple *) pvd;
424 /* Table of cell_specs */
425 const struct cell_spec cell_spec[n_MEANS_STATISTICS] = {
426 {N_("Mean"), "MEAN", NULL , default_create, default_update, arithmean_get, default_destroy},
427 {N_("N"), "COUNT", PIVOT_RC_COUNT, default_create, default_update, n_get, default_destroy},
428 {N_("Std. Deviation"), "STDDEV", NULL , default_create, default_update, stddev_get, default_destroy},
430 {N_("Median"), "MEDIAN", NULL , default_create, default_update, NULL, default_destroy},
431 {N_("Group Median"), "GMEDIAN", NULL , default_create, default_update, NULL, default_destroy},
433 {N_("S.E. Mean"), "SEMEAN", NULL , default_create, default_update, semean_get, default_destroy},
434 {N_("Sum"), "SUM", NULL , default_create, default_update, sum_get, default_destroy},
435 {N_("Minimum"), "MIN", NULL , min_create, min_update, min_get, simple_destroy},
436 {N_("Maximum"), "MAX", NULL , max_create, max_update, max_get, simple_destroy},
437 {N_("Range"), "RANGE", NULL , range_create, range_update, range_get, simple_destroy},
438 {N_("Variance"), "VARIANCE", PIVOT_RC_OTHER, default_create, default_update, variance_get, default_destroy},
439 {N_("Kurtosis"), "KURT", PIVOT_RC_OTHER, default_create, default_update, kurt_get, default_destroy},
440 {N_("S.E. Kurt"), "SEKURT", PIVOT_RC_OTHER, default_create, default_update, sekurt_get, default_destroy},
441 {N_("Skewness"), "SKEW", PIVOT_RC_OTHER, default_create, default_update, skew_get, default_destroy},
442 {N_("S.E. Skew"), "SESKEW", PIVOT_RC_OTHER, default_create, default_update, seskew_get, default_destroy},
443 {N_("First"), "FIRST", NULL , first_create, first_update, first_get, simple_destroy},
444 {N_("Last"), "LAST", NULL , last_create, last_update, last_get, simple_destroy},
446 {N_("Percent N"), "NPCT", PIVOT_RC_PERCENT, default_create, default_update, NULL, default_destroy},
447 {N_("Percent Sum"), "SPCT", PIVOT_RC_PERCENT, default_create, default_update, NULL, default_destroy},
449 {N_("Harmonic Mean"), "HARMONIC", NULL , harmonic_create, harmonic_update, harmonic_get, simple_destroy},
450 {N_("Geom. Mean"), "GEOMETRIC", NULL , geometric_create, geometric_update, geometric_get, simple_destroy}