Delete trailing whitespace at end of lines.
[pspp-builds.git] / src / language / stats / binomial.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include <libpspp/compiler.h>
21 #include <output/table.h>
22 #include <libpspp/alloc.h>
23
24 #include <data/case.h>
25 #include <data/casereader.h>
26 #include <data/dictionary.h>
27 #include <data/procedure.h>
28 #include <data/variable.h>
29 #include <data/value.h>
30 #include <data/value-labels.h>
31
32 #include <libpspp/message.h>
33 #include <libpspp/assertion.h>
34
35 #include "binomial.h"
36 #include "freq.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 #include <libpspp/misc.h>
42
43 #include <gsl/gsl_cdf.h>
44 #include <gsl/gsl_randist.h>
45 #include <gsl-extras/gsl-extras.h>
46
47 #include <minmax.h>
48
49 #include <libpspp/hash.h>
50
51 static double calculate_binomial_internal (double n1, double n2,
52                                            double p);
53
54
55 static void
56 swap (double *i1, double *i2)
57 {
58   double temp = *i1;
59   *i1 = *i2;
60   *i2 = temp;
61 }
62
63 static double
64 calculate_binomial (double n1, double n2, double p)
65 {
66   const double n = n1 + n2;
67   const bool test_reversed = (n1 / n > p ) ;
68   if ( test_reversed )
69     {
70       p = 1 - p ;
71       swap (&n1, &n2);
72     }
73
74   return calculate_binomial_internal (n1, n2, p);
75 }
76
77 static double
78 calculate_binomial_internal (double n1, double n2, double p)
79 {
80   /* SPSS Statistical Algorithms has completely different and WRONG
81      advice here. */
82
83   double sig1tailed = gslextras_cdf_binomial_P (n1, n1 + n2, p);
84
85   if ( p == 0.5 )
86     return sig1tailed > 0.5 ? 1.0 :sig1tailed * 2.0;
87
88   return sig1tailed ;
89 }
90
91 static bool
92 do_binomial (const struct dictionary *dict,
93              struct casereader *input,
94              const struct binomial_test *bst,
95              struct freq_mutable *cat1,
96              struct freq_mutable *cat2,
97              enum mv_class exclude
98              )
99 {
100   bool warn = true;
101
102   const struct one_sample_test *ost = (const struct one_sample_test *) bst;
103   struct ccase c;
104
105   while (casereader_read(input, &c))
106     {
107       int v;
108       double w = dict_get_case_weight (dict, &c, &warn);
109
110       for (v = 0 ; v < ost->n_vars ; ++v )
111         {
112           const struct variable *var = ost->vars[v];
113           const union value *value = case_data (&c, var);
114           int width = var_get_width (var);
115
116           if (var_is_value_missing (var, value, exclude))
117             break;
118
119           if ( NULL == cat1[v].value )
120             {
121               cat1[v].value = value_dup (value, width);
122               cat1[v].count = w;
123             }
124           else if ( 0 == compare_values (cat1[v].value, value, width))
125             cat1[v].count += w;
126           else if ( NULL == cat2[v].value )
127             {
128               cat2[v].value = value_dup (value, width);
129               cat2[v].count = w;
130             }
131           else if ( 0 == compare_values (cat2[v].value, value, width))
132             cat2[v].count += w;
133           else if ( bst->category1 == SYSMIS)
134             msg (ME, _("Variable %s is not dichotomous"), var_get_name (var));
135         }
136
137       case_destroy (&c);
138     }
139   return casereader_destroy (input);
140 }
141
142
143
144 void
145 binomial_execute (const struct dataset *ds,
146                   struct casereader *input,
147                   enum mv_class exclude,
148                   const struct npar_test *test)
149 {
150   int v;
151   const struct binomial_test *bst = (const struct binomial_test *) test;
152   const struct one_sample_test *ost = (const struct one_sample_test*) test;
153
154   struct freq_mutable *cat1 = xzalloc (sizeof (*cat1) * ost->n_vars);
155   struct freq_mutable *cat2 = xzalloc (sizeof (*cat1) * ost->n_vars);
156
157   assert ((bst->category1 == SYSMIS) == (bst->category2 == SYSMIS) );
158
159   if ( bst->category1 != SYSMIS )
160     {
161       union value v;
162       v.f = bst->category1;
163       cat1->value = value_dup (&v, 0);
164     }
165
166   if ( bst->category2 != SYSMIS )
167     {
168       union value v;
169       v.f = bst->category2;
170       cat2->value = value_dup (&v, 0);
171     }
172
173   if (do_binomial (dataset_dict(ds), input, bst, cat1, cat2, exclude))
174     {
175       struct tab_table *table = tab_create (7, ost->n_vars * 3 + 1, 0);
176
177       tab_dim (table, tab_natural_dimensions);
178
179       tab_title (table, _("Binomial Test"));
180
181       tab_headers (table, 2, 0, 1, 0);
182
183       tab_box (table, TAL_1, TAL_1, -1, TAL_1,
184                0, 0, table->nc - 1, tab_nr(table) - 1 );
185
186       for (v = 0 ; v < ost->n_vars; ++v)
187         {
188           double n_total, sig;
189           const struct variable *var = ost->vars[v];
190           tab_hline (table, TAL_1, 0, tab_nc (table) -1, 1 + v * 3);
191
192           /* Titles */
193           tab_text (table, 0, 1 + v * 3, TAB_LEFT, var_to_string (var));
194           tab_text (table, 1, 1 + v * 3, TAB_LEFT, _("Group1"));
195           tab_text (table, 1, 2 + v * 3, TAB_LEFT, _("Group2"));
196           tab_text (table, 1, 3 + v * 3, TAB_LEFT, _("Total"));
197
198           /* Test Prop */
199           tab_float (table, 5, 1 + v * 3, TAB_NONE, bst->p, 8, 3);
200
201           /* Category labels */
202           tab_text (table, 2, 1 + v * 3, TAB_NONE,
203                     var_get_value_name (var, cat1[v].value));
204           tab_text (table, 2, 2 + v * 3, TAB_NONE,
205                     var_get_value_name (var, cat2[v].value));
206
207           /* Observed N */
208           tab_float (table, 3, 1 + v * 3, TAB_NONE, cat1[v].count, 8, 0);
209           tab_float (table, 3, 2 + v * 3, TAB_NONE, cat2[v].count, 8, 0);
210
211           n_total = cat1[v].count + cat2[v].count;
212           tab_float (table, 3, 3 + v * 3, TAB_NONE, n_total, 8, 0);
213
214           /* Observed Proportions */
215           tab_float (table, 4, 1 + v * 3, TAB_NONE,
216                      cat1[v].count / n_total, 8, 3);
217           tab_float (table, 4, 2 + v * 3, TAB_NONE,
218                      cat2[v].count / n_total, 8, 3);
219           tab_float (table, 4, 3 + v * 3, TAB_NONE,
220                      (cat1[v].count + cat2[v].count) / n_total, 8, 2);
221
222           /* Significance */
223           sig = calculate_binomial (cat1[v].count, cat2[v].count, bst->p);
224           tab_float (table, 6, 1 + v * 3, TAB_NONE, sig, 8, 3);
225         }
226
227       tab_text (table,  2, 0,  TAB_CENTER, _("Category"));
228       tab_text (table,  3, 0,  TAB_CENTER, _("N"));
229       tab_text (table,  4, 0,  TAB_CENTER, _("Observed Prop."));
230       tab_text (table,  5, 0,  TAB_CENTER, _("Test Prop."));
231
232       tab_text (table,  6, 0,  TAB_CENTER | TAT_PRINTF,
233                 _("Exact Sig. (%d-tailed)"),
234                 bst->p == 0.5 ? 2: 1);
235
236       tab_vline (table, TAL_2, 2, 0, tab_nr (table) -1);
237       tab_submit (table);
238     }
239
240   for (v = 0; v < ost->n_vars; v++)
241     {
242       free (cat1[v].value);
243       free (cat2[v].value);
244     }
245   free (cat1);
246   free (cat2);
247 }