Merge commit 'origin/stable'
[pspp-builds.git] / src / math / tukey-hinges.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008 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 #include <config.h>
18 #include "tukey-hinges.h"
19 #include <math/order-stats.h>
20
21 #include <gl/xalloc.h>
22 #include <libpspp/assertion.h>
23 #include <math.h>
24
25 void
26 tukey_hinges_calculate (const struct tukey_hinges *th, double hinge[3])
27 {
28   double a[3];
29   double a_star[3];
30   int i;
31   const struct order_stats *os = &th->parent;
32
33   for (i = 0 ; i < 3 ; ++i)
34     {
35       a_star[i] = os->k[i].tc - os->k[i].cc;
36       a[i] = a_star[i] / os->k[i].c_p1;
37
38       if (a_star[i] < 1)
39         {
40           if (os->k[i].c_p1 >= 1 )
41             {
42               hinge[i] = (1 - a_star[i]) * os->k[i].y
43                 + a_star[i] * os->k[i].y_p1;
44             }
45           else
46             {
47               hinge[i] = (1 - a[i]) * os->k[i].y
48                 + a[i] * os->k[i].y_p1;
49             }
50         }
51       else
52         {
53           hinge[i] = os->k[i].y_p1;
54         }
55
56     }
57 }
58
59 static void
60 destroy (struct statistic *s)
61 {
62   struct order_stats *os = (struct order_stats *) s;
63
64   free (os->k);
65   free (s);
66 };
67
68 struct statistic *
69 tukey_hinges_create (double W, double c_min)
70 {
71   double d;
72   struct tukey_hinges *th = xzalloc (sizeof (*th));
73   struct order_stats *os = (struct order_stats *) th;
74   struct statistic *stat = (struct statistic *) th;
75
76   assert (c_min >= 0);
77
78   os->n_k = 3;
79   os->k = xcalloc (sizeof (*os->k), 3);
80
81   if ( c_min >= 1.0)
82     {
83       d = floor ((W + 3) / 2.0) / 2.0;
84
85       os->k[0].tc = d;
86       os->k[1].tc = W/2.0 + 0.5;
87       os->k[2].tc = W + 1 - d;
88     }
89   else
90     {
91       d = floor ((W/c_min + 3.0)/ 2.0) / 2.0 ;
92       os->k[0].tc = d * c_min;
93       os->k[1].tc = (W + c_min) / 2.0;
94       os->k[2].tc = W + c_min * (1 - d);
95     }
96
97
98   stat->destroy = destroy;
99
100   return stat;
101 }