math: Make 'accumulate' a feature of order statistics, not all stats.
[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 <math.h>
22 #include <string.h>
23
24 #include "data/casereader.h"
25 #include "data/val-type.h"
26 #include "data/variable.h"
27 #include "libpspp/assertion.h"
28
29 #include "gl/xalloc.h"
30
31 #if 0
32
33 #include <stdio.h>
34
35 static void
36 order_stats_dump_k1 (const struct order_stats *os)
37 {
38   struct k *k = &os->k[0];
39   printf ("K1: tc %g; c %g cc %g ccp %g\n",
40           k->tc, k->c, k->cc, k->cc_p1);
41
42 }
43
44 static void
45 order_stats_dump_k2 (const struct order_stats *os)
46 {
47   struct k *k = &os->k[1];
48   printf ("K2: tc %g; c %g cc %g ccp %g\n",
49           k->tc, k->c, k->cc, k->cc_p1);
50 }
51
52
53 void
54 order_stats_dump (const struct order_stats *os)
55 {
56   order_stats_dump_k1 (os);
57   order_stats_dump_k2 (os);
58 }
59
60 #endif
61
62 static void
63 update_k_values (const struct ccase *cx, double y_i, double c_i, double cc_i,
64                  struct order_stats **os, size_t n_os)
65 {
66   for (size_t j = 0; j < n_os; ++j)
67     {
68       struct order_stats *tos = os[j];
69       struct statistic  *stat = &tos->parent;
70
71       for (struct k *k = tos->k; k < &tos->k[tos->n_k]; ++k)
72         {
73           /* Update 'k' lower values. */
74           if (cc_i <= k->tc)
75             {
76               k->cc = cc_i;
77               k->c = c_i;
78               k->y = y_i;
79             }
80
81           /* Update 'k' upper values. */
82           if (cc_i > k->tc && k->c_p1 == 0)
83             {
84               k->cc_p1 = cc_i;
85               k->c_p1 = c_i;
86               k->y_p1 = y_i;
87             }
88         }
89
90       if (tos->accumulate)
91         tos->accumulate (stat, cx, c_i, cc_i, y_i);
92     }
93 }
94
95 /* Reads all the cases from READER and accumulates their data into the N_OS
96    order statistics in OS, taking data from case index DATA_IDX and weights
97    from case index WEIGHT_IDX.  WEIGHT_IDX may be -1 to assume weight 1.
98
99    This function must be used only once per order_stats.
100
101    Takes ownership of READER.
102
103    Data values must be numeric and sorted in ascending order.  Use
104    sort_execute_1var() or related functions to sort unsorted data before
105    passing it to this function. */
106 void
107 order_stats_accumulate_idx (struct order_stats **os, size_t n_os,
108                             struct casereader *reader,
109                             int weight_idx, int data_idx)
110 {
111   struct ccase *cx;
112   struct ccase *prev_cx = NULL;
113   double prev_value = -DBL_MAX;
114
115   double cc_i = 0;
116   double c_i = 0;
117
118   for (; (cx = casereader_read (reader)) != NULL; case_unref (cx))
119     {
120       const double weight = weight_idx == -1 ? 1.0 : case_num_idx (cx, weight_idx);
121       if (weight == SYSMIS || weight <= 0)
122         continue;
123
124       const double this_value = case_num_idx (cx, data_idx);
125       if (!isfinite (this_value) || this_value == SYSMIS)
126         continue;
127
128       if (!prev_cx || this_value > prev_value)
129         {
130           if (prev_cx)
131             update_k_values (prev_cx, prev_value, c_i, cc_i, os, n_os);
132           prev_value = this_value;
133           c_i = weight;
134         }
135       else
136         {
137           /* Data values must be sorted. */
138           assert (this_value == prev_value);
139
140           c_i += weight;
141         }
142
143       cc_i += weight;
144       case_unref (prev_cx);
145       prev_cx = case_ref (cx);
146     }
147   if (prev_cx)
148     {
149       update_k_values (prev_cx, prev_value, c_i, cc_i, os, n_os);
150       case_unref (prev_cx);
151     }
152
153   casereader_destroy (reader);
154 }
155
156 /* Reads all the cases from READER and accumulates their data into the N_OS
157    order statistics in OS, taking data from DATA_VAR and weights from
158    WEIGHT_VAR.  Drops cases for which the value of DATA_VAR is missing
159    according to EXCLUDE.  WEIGHT_VAR may be NULL to assume weight 1.
160
161    This function must be used only once per order_stats.
162
163    Takes ownership of READER.
164
165    DATA_VAR must be numeric and sorted in ascending order.  Use
166    sort_execute_1var() or related functions to sort unsorted data before
167    passing it to this function. */
168 void
169 order_stats_accumulate (struct order_stats **os, size_t n_os,
170                         struct casereader *reader,
171                         const struct variable *weight_var,
172                         const struct variable *data_var,
173                         enum mv_class exclude)
174 {
175   reader = casereader_create_filter_missing (reader, &data_var, 1,
176                                              exclude, NULL, NULL);
177
178   order_stats_accumulate_idx (os, n_os, reader,
179                               weight_var ? var_get_case_index (weight_var) : -1,
180                               var_get_case_index (data_var));
181 }