math: Improve comments.
[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_values (const struct ccase *cx, double y_i, double c_i, double cc_i,
63                  struct order_stats **os, size_t n_os)
64 {
65   for (size_t j = 0; j < n_os; ++j)
66     {
67       struct order_stats *tos = os[j];
68       struct statistic  *stat = &tos->parent;
69
70       for (struct k *k = tos->k; k < &tos->k[tos->n_k]; ++k)
71         {
72           /* Update 'k' lower values. */
73           if (cc_i <= k->tc)
74             {
75               k->cc = cc_i;
76               k->c = c_i;
77               k->y = y_i;
78             }
79
80           /* Update 'k' upper values. */
81           if (cc_i > k->tc && k->c_p1 == 0)
82             {
83               k->cc_p1 = cc_i;
84               k->c_p1 = c_i;
85               k->y_p1 = y_i;
86             }
87         }
88
89       if (stat->accumulate)
90         stat->accumulate (stat, cx, c_i, cc_i, y_i);
91
92       tos->cc = cc_i;
93     }
94 }
95
96 /* Reads all the cases from READER and accumulates their data into the N_OS
97    order statistics in OS, taking data from case index DATA_IDX and weights
98    from case index WEIGHT_IDX.  WEIGHT_IDX may be -1 to assume weight 1.
99
100    Takes ownership of READER.
101
102    Data values must be numeric and sorted in ascending order.  Use
103    sort_execute_1var() or related functions to sort unsorted data before
104    passing it to this function. */
105 void
106 order_stats_accumulate_idx (struct order_stats **os, size_t n_os,
107                             struct casereader *reader,
108                             int weight_idx, int data_idx)
109 {
110   struct ccase *cx;
111   struct ccase *prev_cx = NULL;
112   double prev_value = -DBL_MAX;
113
114   double cc_i = 0;
115   double c_i = 0;
116
117   for (; (cx = casereader_read (reader)) != NULL; case_unref (cx))
118     {
119       const double weight = weight_idx == -1 ? 1.0 : case_num_idx (cx, weight_idx);
120       if (weight == SYSMIS)
121         continue;
122
123       const double this_value = case_num_idx (cx, data_idx);
124       assert (this_value >= prev_value);
125
126       if (prev_value == -DBL_MAX || prev_value == this_value)
127         c_i += weight;
128
129       if (prev_value > -DBL_MAX && this_value > prev_value)
130         {
131           update_k_values (prev_cx, prev_value, c_i, cc_i, os, n_os);
132           c_i = weight;
133         }
134
135       case_unref (prev_cx);
136       cc_i += weight;
137       prev_value = this_value;
138       prev_cx = case_ref (cx);
139     }
140
141   update_k_values (prev_cx, prev_value, c_i, cc_i, os, n_os);
142   case_unref (prev_cx);
143
144   casereader_destroy (reader);
145 }
146
147 /* Reads all the cases from READER and accumulates their data into the N_OS
148    order statistics in OS, taking data from DATA_VAR and weights from
149    WEIGHT_VAR.  Drops cases for which the value of DATA_VAR is missing
150    according to EXCLUDE.  WEIGHT_VAR may be NULL to assume weight 1.
151
152    Takes ownership of READER.
153
154    DATA_VAR must be numeric and sorted in ascending order.  Use
155    sort_execute_1var() or related functions to sort unsorted data before
156    passing it to this function. */
157 void
158 order_stats_accumulate (struct order_stats **os, size_t n_os,
159                         struct casereader *reader,
160                         const struct variable *weight_var,
161                         const struct variable *data_var,
162                         enum mv_class exclude)
163 {
164   reader = casereader_create_filter_missing (reader, &data_var, 1,
165                                              exclude, NULL, NULL);
166
167   order_stats_accumulate_idx (os, n_os, reader,
168                               weight_var ? var_get_case_index (weight_var) : -1,
169                               var_get_case_index (data_var));
170 }