pivot-table: Define numeric formats of categories as well as their cells.
[pspp.git] / src / math / trimmed-mean.c
blobd75b539a6692cb1c1ab5e299ab6538cf6968811d
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2009, 2011 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/>. */
17 #include <config.h>
19 #include "math/trimmed-mean.h"
21 #include <math.h>
23 #include "data/val-type.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/cast.h"
26 #include "math/order-stats.h"
28 #include "gl/xalloc.h"
30 static void
31 acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc, double y)
33 struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent);
34 struct order_stats *os = &tm->parent;
36 if (cc > os->k[0].tc && cc <= os->k[1].tc)
37 tm->sum += c * y;
39 if (tm->cyk1p1 == SYSMIS && cc > os->k[0].tc)
40 tm->cyk1p1 = c * y;
43 static void
44 destroy (struct statistic *s)
46 struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent);
47 struct order_stats *os = &tm->parent;
48 free (os->k);
49 free (tm);
52 struct trimmed_mean *
53 trimmed_mean_create (double W, double tail)
55 struct trimmed_mean *tm = xzalloc (sizeof (*tm));
56 struct order_stats *os = &tm->parent;
57 struct statistic *stat = &os->parent;
59 os->n_k = 2;
60 os->k = xcalloc (2, sizeof (*os->k));
62 assert (tail >= 0);
63 assert (tail <= 1);
65 os->k[0].tc = tail * W;
66 os->k[1].tc = W * (1 - tail);
68 stat->accumulate = acc;
69 stat->destroy = destroy;
71 tm->cyk1p1 = SYSMIS;
72 tm->w = W;
73 tm->tail = tail;
75 return tm;
79 double
80 trimmed_mean_calculate (const struct trimmed_mean *tm)
82 const struct order_stats *os = (const struct order_stats *) tm;
84 return
86 (os->k[0].cc - os->k[0].tc) * os->k[0].y_p1
88 (tm->w - os->k[1].cc - os->k[0].tc) * os->k[1].y_p1
90 tm->sum
92 / ((1.0 - tm->tail * 2) * tm->w);