Rewrote the EXAMINE command.
[pspp-builds.git] / src / math / order-stats.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 "order-stats.h"
19 #include <libpspp/assertion.h>
20 #include <data/val-type.h>
21 #include <gl/xalloc.h>
22 #include <data/variable.h>
23 #include <data/casereader.h>
24 #include <string.h>
25
26
27 static void
28 update_k_lower (struct k *kk,
29                 double y_i, double c_i, double cc_i)
30 {
31   if ( cc_i <= kk->tc )
32     {
33       kk->cc = cc_i;
34       kk->c = c_i;
35       kk->y = y_i;
36     }
37 }
38
39
40 static void
41 update_k_upper (struct k *kk,
42                 double y_i, double c_i, double cc_i)
43 {
44   if ( cc_i > kk->tc && kk->c_p1 == 0)
45     {
46       kk->cc_p1 = cc_i;
47       kk->c_p1 = c_i;
48       kk->y_p1 = y_i;
49     }
50 }
51
52
53 static void
54 update_k_values (const struct ccase *cx, double y_i, double c_i, double cc_i,
55                  struct order_stats **os, size_t n_os)
56 {
57   int j;
58   for (j = 0 ; j < n_os ; ++j)
59     {
60       int k;
61       struct order_stats *tos = os[j];
62       struct statistic  *stat = (struct statistic *) tos;
63       for (k = 0 ; k < tos->n_k; ++k)
64         {
65           struct k *myk = &tos->k[k];
66           update_k_lower (myk, y_i, c_i, cc_i);
67           update_k_upper (myk, y_i, c_i, cc_i);
68         }
69
70       if ( stat->accumulate )
71         stat->accumulate (stat, cx, c_i, cc_i, y_i);
72     }
73 }
74
75
76 void
77 order_stats_accumulate (struct order_stats **os, size_t nos,
78                     struct casereader *reader,
79                     const struct variable *wv,
80                     const struct variable *var)
81 {
82   struct ccase cx;
83   struct ccase prev_cx;
84   double prev_value = -DBL_MAX;
85
86   double cc_i = 0;
87   double c_i = 0;
88
89   case_nullify (&prev_cx);
90
91   for (; casereader_read (reader, &cx); case_destroy (&cx))
92     {
93       const double weight = wv ? case_data (&cx, wv)->f : 1.0;
94       const double this_value = case_data (&cx, var)->f;
95
96       /* The casereader MUST be sorted */
97       assert (this_value >= prev_value);
98
99       case_destroy (&prev_cx);
100
101       if ( prev_value == -DBL_MAX || prev_value == this_value)
102         c_i += weight;
103
104       if ( prev_value > -DBL_MAX && this_value > prev_value)
105         {
106           update_k_values (&prev_cx, prev_value, c_i, cc_i, os, nos);
107           c_i = weight;
108         }
109
110       cc_i += weight;
111       prev_value = this_value;
112       case_clone (&prev_cx, &cx);
113     }
114
115   update_k_values (&prev_cx, prev_value, c_i, cc_i, os, nos);
116   case_destroy (&prev_cx);
117
118   casereader_destroy (reader);
119 }