1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2009 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/>. */
18 #include "trimmed-mean.h"
19 #include <math/order-stats.h>
21 #include <gl/xalloc.h>
22 #include <libpspp/assertion.h>
23 #include <libpspp/cast.h>
25 #include <data/val-type.h>
29 acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc, double y)
31 struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent);
32 struct order_stats *os = &tm->parent;
34 if ( cc > os->k[0].tc && cc < os->k[1].tc)
37 if ( tm->cyk1p1 == SYSMIS && cc >os->k[0].tc)
42 destroy (struct statistic *s)
44 struct trimmed_mean *tm = UP_CAST (s, struct trimmed_mean, parent.parent);
45 struct order_stats *os = &tm->parent;
51 trimmed_mean_create (double W, double tail)
53 struct trimmed_mean *tm = xzalloc (sizeof (*tm));
54 struct order_stats *os = &tm->parent;
55 struct statistic *stat = &os->parent;
58 os->k = xcalloc (sizeof (*os->k), 2);
63 os->k[0].tc = tail * W;
64 os->k[1].tc = W * (1 - tail);
66 stat->accumulate = acc;
67 stat->destroy = destroy;
78 trimmed_mean_calculate (const struct trimmed_mean *tm)
80 const struct order_stats *os = (const struct order_stats *) tm;
82 assert (os->cc == tm->w);
86 (os->k[0].cc_p1 - os->k[0].tc) * os->k[0].y_p1
88 (os->k[1].cc - os->k[1].tc) * os->k[1].y_p1
95 ( (1.0 - 2 * tm->tail) * tm->w);