b1f0ce84d9a397bdca0c71062dab79988d9eef0f
[pspp-builds.git] / src / math / categoricals.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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 <stdio.h>
20
21 #include "categoricals.h"
22
23 #include <gl/xalloc.h>
24 #include <data/variable.h>
25 #include <data/case.h>
26 #include <data/value.h>
27 #include <libpspp/hmap.h>
28 #include <libpspp/pool.h>
29
30 #include <libpspp/str.h>
31
32 struct value_node
33 {
34   struct hmap_node node;      /* Node in hash map. */
35   union value value;          /* The value being labeled. */
36   double cc;                  /* The total of the weights of cases with this value */
37   int subscript;              /* A zero based integer, unique within the variable.
38                                  Can be used as an index into an array */
39 };
40
41
42 struct var_params
43 {
44   /* A map indexed by a union values */
45   struct hmap map;
46
47   const struct variable *var;
48
49   int base_subscript;
50
51   /* The number of distinct values of this variable */
52   int n_cats;
53
54   /* A map of values indexed by subscript */
55   const struct value_node **reverse_value_map;
56
57   /* Total of the weights of this variable */
58   double cc; 
59 };
60
61
62 struct categoricals
63 {
64   const struct variable *wv;
65
66   size_t n_vars;
67
68   /* An array of var_params */
69   struct var_params *vp;
70
71   /* A map to enable the lookup of variables indexed by subscript */
72   int *reverse_variable_map;
73
74   size_t n_cats_total;
75
76   struct pool *pool;
77
78   /* Missing values to be excluded */
79   enum mv_class exclude;
80 };
81
82
83 void
84 categoricals_destroy ( struct categoricals *cat)
85 {
86   int i;
87   for (i = 0 ; i < cat->n_vars; ++i)
88     hmap_destroy (&cat->vp[i].map);
89
90   pool_destroy (cat->pool);
91   free (cat);
92 }
93
94
95 void
96 categoricals_dump (const struct categoricals *cat)
97 {
98   int v;
99
100   for (v = 0 ; v < cat->n_vars; ++v)
101     {
102       const struct var_params *vp = &cat->vp[v];
103       const struct hmap *m = &vp->map;
104       struct hmap_node *node ;
105       int x;
106      
107       printf ("\n%s (%d)  CC=%g n_cats=%d:\n", var_get_name (vp->var), vp->base_subscript, vp->cc, vp->n_cats);
108
109       printf ("Reverse map\n");
110       for (x = 0 ; x < vp->n_cats; ++x)
111         {
112           struct string s;
113           const struct value_node *vn = vp->reverse_value_map[x];
114           ds_init_empty (&s);
115           var_append_value_name (vp->var, &vn->value, &s);
116           printf ("Value for %d is %s\n", x, ds_cstr(&s));
117           ds_destroy (&s);
118         }
119
120       printf ("\nForward map\n");
121       for (node = hmap_first (m); node; node = hmap_next (m, node))
122         {
123           struct string s;
124           ds_init_empty (&s);
125           const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
126           var_append_value_name (vp->var, &vn->value, &s);
127           printf ("Value: %s; Index %d; CC %g\n",
128                   ds_cstr (&s),
129                   vn->subscript, vn->cc);
130           ds_destroy (&s);
131         }
132     }
133 }
134
135
136
137 static struct value_node *
138 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
139 {
140   struct value_node *foo;
141   unsigned int width = var_get_width (var);
142   size_t hash = value_hash (val, width, 0);
143
144   HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
145     {
146       if (value_equal (val, &foo->value, width))
147         break;
148
149       fprintf (stderr, "Warning: Hash table collision\n");
150     }
151
152   return foo;
153 }
154
155
156
157 struct categoricals *
158 categoricals_create (const struct variable **v, size_t n_vars,
159                      const struct variable *wv, enum mv_class exclude)
160 {
161   size_t i;
162   struct categoricals *cat = xmalloc (sizeof *cat);
163   
164   cat->n_vars = n_vars;
165   cat->wv = wv;
166   cat->n_cats_total = 0;
167   cat->reverse_variable_map = NULL;
168   cat->pool = pool_create ();
169   cat->exclude = exclude;
170
171   cat->vp = pool_calloc (cat->pool, n_vars, sizeof *cat->vp);
172
173   for (i = 0 ; i < cat->n_vars; ++i)
174     {
175       hmap_init (&cat->vp[i].map);
176       cat->vp[i].var = v[i];
177     }
178
179   return cat;
180 }
181
182
183
184 void
185 categoricals_update (struct categoricals *cat, const struct ccase *c)
186 {
187   size_t i;
188   
189   const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
190
191   assert (NULL == cat->reverse_variable_map);
192
193   for (i = 0 ; i < cat->n_vars; ++i)
194     {
195       const struct variable *var = cat->vp[i].var;
196       unsigned int width = var_get_width (var);
197       const union value *val = case_data (c, var);
198       size_t hash;
199       struct value_node *node ;
200
201       if ( var_is_value_missing (var, val, cat->exclude))
202         continue;
203
204       hash = value_hash (val, width, 0);
205       node = lookup_value (&cat->vp[i].map, var, val);
206
207       if ( NULL == node)
208         {
209           node = pool_malloc (cat->pool, sizeof *node);
210
211           value_init (&node->value, width);
212           value_copy (&node->value, val, width);
213           node->cc = 0.0;
214
215           hmap_insert (&cat->vp[i].map, &node->node,  hash);
216           cat->n_cats_total++;
217           node->subscript = cat->vp[i].n_cats++ ;
218         }
219
220       node->cc += weight;
221       cat->vp[i].cc += weight;
222     }
223 }
224
225 /* Return the number of categories (distinct values) for variable N */
226 size_t
227 categoricals_n_count (const struct categoricals *cat, size_t n)
228 {
229   return hmap_count (&cat->vp[n].map);
230 }
231
232
233 /* Return the index for value VAL in the Nth variable */
234 int
235 categoricals_index (const struct categoricals *cat, size_t n, const union value *val)
236 {
237   struct value_node *vn = lookup_value (&cat->vp[n].map, cat->vp[n].var, val);
238
239   if ( vn == NULL)
240     return -1;
241
242   return vn->subscript;
243 }
244
245
246 /* Return the total number of categories */
247 size_t
248 categoricals_total (const struct categoricals *cat)
249 {
250   return cat->n_cats_total;
251 }
252
253
254 /* This function must be called *before* any call to categoricals_get_*_by subscript an
255  *after* all calls to categoricals_update */
256 void
257 categoricals_done (struct categoricals *cat)
258 {
259   /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
260      uses it will be more efficient that using a tree based structure, since it
261      is called only once, and means that subsequent lookups will be O(1).
262
263      1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
264   */
265   int v;
266   int idx = 0;
267   cat->reverse_variable_map = pool_calloc (cat->pool, cat->n_cats_total, sizeof *cat->reverse_variable_map);
268   
269   for (v = 0 ; v < cat->n_vars; ++v)
270     {
271       int i;
272       struct var_params *vp = &cat->vp[v];
273       int n_cats_total = categoricals_n_count (cat, v);
274       struct hmap_node *node ;
275
276       vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
277
278       vp->base_subscript = idx;
279
280       for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
281         {
282           const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
283           vp->reverse_value_map[vn->subscript] = vn;
284         }
285
286       for (i = 0; i < vp->n_cats; ++i)
287         cat->reverse_variable_map[idx++] = v;
288     }
289 }
290
291
292
293 /* Return the categorical variable corresponding to SUBSCRIPT */
294 const struct variable *
295 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
296 {
297   int index;
298
299   assert (cat->reverse_variable_map);
300   
301   index = cat->reverse_variable_map[subscript];
302
303   return cat->vp[index].var;
304 }
305
306
307 /* Return the value corresponding to SUBSCRIPT */
308 const union value *
309 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
310 {
311   int vindex = cat->reverse_variable_map[subscript];
312   const struct var_params *vp = &cat->vp[vindex];
313   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
314
315   return &vn->value;
316 }
317
318
319 double
320 categoricals_get_weight_by_subscript (const struct categoricals *cat, int subscript)
321 {
322   int vindex = cat->reverse_variable_map[subscript];
323   const struct var_params *vp = &cat->vp[vindex];
324
325   return vp->cc;
326 }
327
328 double
329 categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript)
330 {
331   int vindex = cat->reverse_variable_map[subscript];
332   const struct var_params *vp = &cat->vp[vindex];
333
334   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
335   return vn->cc;
336 }
337
338
339 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
340    for that subscript */
341 double
342 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
343                                       const struct ccase *c)
344 {
345   const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
346   int width = var_get_width (var);
347
348   const union value *val = case_data (c, var);
349
350   return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);
351 }