e8e078f4b6f066913d1081aadab2c7692886d19a
[pspp] / src / math / order-stats.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008, 2009, 2011 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
19 #include "math/order-stats.h"
20
21 #include <string.h>
22
23 #include "data/casereader.h"
24 #include "data/val-type.h"
25 #include "data/variable.h"
26 #include "libpspp/assertion.h"
27
28 #include "gl/xalloc.h"
29
30 #if 0
31
32 #include <stdio.h>
33
34 static void
35 order_stats_dump_k1 (const struct order_stats *os)
36 {
37   struct k *k = &os->k[0];
38   printf ("K1: tc %g; c %g cc %g ccp %g\n",
39           k->tc, k->c, k->cc, k->cc_p1);
40
41 }
42
43 static void
44 order_stats_dump_k2 (const struct order_stats *os)
45 {
46   struct k *k = &os->k[1];
47   printf ("K2: tc %g; c %g cc %g ccp %g\n",
48           k->tc, k->c, k->cc, k->cc_p1);
49 }
50
51
52 void
53 order_stats_dump (const struct order_stats *os)
54 {
55   order_stats_dump_k1 (os);
56   order_stats_dump_k2 (os);
57 }
58
59 #endif
60
61 static void
62 update_k_lower (struct k *kk,
63                 double y_i, double c_i, double cc_i)
64 {
65   if (cc_i <= kk->tc)
66     {
67       kk->cc = cc_i;
68       kk->c = c_i;
69       kk->y = y_i;
70     }
71 }
72
73
74 static void
75 update_k_upper (struct k *kk,
76                 double y_i, double c_i, double cc_i)
77 {
78   if (cc_i > kk->tc && kk->c_p1 == 0)
79     {
80       kk->cc_p1 = cc_i;
81       kk->c_p1 = c_i;
82       kk->y_p1 = y_i;
83     }
84 }
85
86
87 static void
88 update_k_values (const struct ccase *cx, double y_i, double c_i, double cc_i,
89                  struct order_stats **os, size_t n_os)
90 {
91   int j;
92
93   for (j = 0 ; j < n_os ; ++j)
94     {
95       int k;
96       struct order_stats *tos = os[j];
97       struct statistic  *stat = &tos->parent;
98       for (k = 0 ; k < tos->n_k; ++k)
99         {
100           struct k *myk = &tos->k[k];
101           update_k_lower (myk, y_i, c_i, cc_i);
102           update_k_upper (myk, y_i, c_i, cc_i);
103         }
104
105       if (stat->accumulate)
106         stat->accumulate (stat, cx, c_i, cc_i, y_i);
107
108       tos->cc = cc_i;
109     }
110 }
111
112
113 void
114 order_stats_accumulate_idx (struct order_stats **os, size_t nos,
115                             struct casereader *reader,
116                             int wt_idx,
117                             int val_idx)
118 {
119   struct ccase *cx;
120   struct ccase *prev_cx = NULL;
121   double prev_value = -DBL_MAX;
122
123   double cc_i = 0;
124   double c_i = 0;
125
126   for (; (cx = casereader_read (reader)) != NULL; case_unref (cx))
127     {
128       const double weight = wt_idx == -1 ? 1.0 : case_num_idx (cx, wt_idx);
129       const double this_value = case_num_idx (cx, val_idx);
130
131       if (weight == SYSMIS)
132         continue;
133
134       /* The casereader MUST be sorted */
135       assert (this_value >= prev_value);
136
137       if (prev_value == -DBL_MAX || prev_value == this_value)
138         c_i += weight;
139
140       if (prev_value > -DBL_MAX && this_value > prev_value)
141         {
142           update_k_values (prev_cx, prev_value, c_i, cc_i, os, nos);
143           c_i = weight;
144         }
145
146       case_unref (prev_cx);
147       cc_i += weight;
148       prev_value = this_value;
149       prev_cx = case_ref (cx);
150     }
151
152   update_k_values (prev_cx, prev_value, c_i, cc_i, os, nos);
153   case_unref (prev_cx);
154
155   casereader_destroy (reader);
156 }
157
158
159 void
160 order_stats_accumulate (struct order_stats **os, size_t nos,
161                         struct casereader *reader,
162                         const struct variable *wv,
163                         const struct variable *var,
164                         enum mv_class exclude)
165 {
166   /* Filter out missing cases */
167   reader = casereader_create_filter_missing (reader, &var, 1,
168                                              exclude, NULL, NULL);
169
170   order_stats_accumulate_idx (os, nos,
171                               reader,
172                               wv ? var_get_case_index (wv) : -1,
173                               var_get_case_index (var));
174 }