Remove unused function
[pspp] / src / math / categoricals.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 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/categoricals.h"
20 #include "math/interaction.h"
21
22 #include <stdio.h>
23
24 #include "data/case.h"
25 #include "data/value.h"
26 #include "data/variable.h"
27 #include "libpspp/array.h"
28 #include "libpspp/hmap.h"
29 #include "libpspp/pool.h"
30 #include "libpspp/str.h"
31
32 #include "gl/xalloc.h"
33
34 struct value_node
35 {
36   struct hmap_node node;      /* Node in hash map. */
37   struct ccase *ccase;
38   double cc;                  /* The total of the weights of cases with this value */
39
40   void *user_data;            /* A pointer to data which the caller can store stuff */
41
42   int subscript;              /* A zero based integer, unique within the variable.
43                                  Can be used as an index into an array */
44 };
45
46 struct var_params
47 {
48   /* A map indexed by a union values */
49   struct hmap map;
50
51   const struct interaction *iact;
52
53   int base_subscript_short;
54   int base_subscript_long;
55
56   /* The number of distinct values of this variable */
57   int n_cats;
58
59   /* A map of values indexed by subscript */
60   const struct value_node **reverse_value_map;
61
62   /* Total of the weights of this variable */
63   double cc; 
64 };
65
66
67 /* Comparison function to sort the reverse_value_map in ascending order */
68 static int
69 compare_value_node (const void *vn1_, const void *vn2_, const void *aux)
70 {
71   const struct value_node * const *vn1 = vn1_;
72   const struct value_node * const *vn2 = vn2_;
73   const struct var_params *vp = aux;
74
75   return interaction_case_cmp_3way (vp->iact, (*vn1)->ccase, (*vn2)->ccase);
76 }
77
78
79 struct categoricals
80 {
81   /* The weight variable */
82   const struct variable *wv;
83
84   /* An array of var_params */
85   struct var_params *vp;
86
87   /* The size of VP. (ie, the number of variables involved.) */
88   size_t n_vp;
89
90   /* The number of categorical variables which contain entries.
91      In the absence of missing values, this will be equal to N_VP */
92   size_t n_vars;
93
94   /* A map to enable the lookup of variables indexed by subscript.
95      This map considers only the N - 1 of the N variables.
96    */
97   int *reverse_variable_map_short;
98
99   /* Like the above, but uses all N variables */
100   int *reverse_variable_map_long;
101
102   size_t n_cats_total;
103
104   struct pool *pool;
105
106   /* Missing values to be excluded */
107   enum mv_class exclude;
108
109   /* Function to be called on each update */
110   update_func *update;
111
112   /* Function specified by the caller to create user_data */
113   user_data_create_func *user_data_create;
114
115   /* Auxilliary data to be passed to update and user_data_create_func*/
116   void *aux1;
117   void *aux2;
118 };
119
120
121 void
122 categoricals_destroy ( struct categoricals *cat)
123 {
124   int i;
125   if (cat != NULL)
126     {
127       for (i = 0 ; i < cat->n_vp; ++i)
128         {
129           struct hmap *map = &cat->vp[i].map;
130           struct value_node *nn;
131
132           HMAP_FOR_EACH (nn, struct value_node, node, map)
133             {
134               case_unref (nn->ccase);
135             }     
136           
137           hmap_destroy (map);
138         }
139       
140       pool_destroy (cat->pool);
141       free (cat);
142     }
143 }
144
145
146 #if 0
147 void
148 categoricals_dump (const struct categoricals *cat)
149 {
150   int v;
151
152   for (v = 0 ; v < cat->n_vp; ++v)
153     {
154       const struct var_params *vp = &cat->vp[v];
155       const struct hmap *m = &vp->map;
156       struct hmap_node *node ;
157       int x;
158      
159       printf ("\n%s (%d)  CC=%g n_cats=%d:\n", 
160               var_get_name (vp->var), vp->base_subscript_long, vp->cc, vp->n_cats);
161
162       printf ("Reverse map\n");
163       for (x = 0 ; x < vp->n_cats; ++x)
164         {
165           struct string s;
166           const struct value_node *vn = vp->reverse_value_map[x];
167           ds_init_empty (&s);
168           var_append_value_name (vp->var, &vn->value, &s);
169           printf ("Value for %d is %s\n", x, ds_cstr(&s));
170           ds_destroy (&s);
171         }
172
173       printf ("\nForward map\n");
174       for (node = hmap_first (m); node; node = hmap_next (m, node))
175         {
176           struct string s;
177           const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
178           ds_init_empty (&s);
179           var_append_value_name (vp->var, &vn->value, &s);
180           printf ("Value: %s; Index %d; CC %g\n",
181                   ds_cstr (&s),
182                   vn->subscript, vn->cc);
183           ds_destroy (&s);
184         }
185     }
186
187   assert (cat->n_vars <= cat->n_vp);
188
189   printf ("\n");
190   printf ("Number of categorical variables: %d\n", cat->n_vp);
191   printf ("Number of non-empty categorical variables: %d\n", cat->n_vars);
192   printf ("Total number of categories: %d\n", cat->n_cats_total);
193
194   printf ("\nReverse variable map (short):\n");
195   for (v = 0 ; v < cat->n_cats_total - cat->n_vars; ++v)
196     printf ("%d ", cat->reverse_variable_map_short[v]);
197
198   printf ("\nReverse variable map (long):\n");
199   for (v = 0 ; v < cat->n_cats_total; ++v)
200     printf ("%d ", cat->reverse_variable_map_long[v]);
201
202   printf ("\n");
203 }
204 #endif
205
206
207 static struct value_node *
208 lookup_case (const struct hmap *map, const struct interaction *iact, const struct ccase *c)
209 {
210   struct value_node *nn;
211   size_t hash = interaction_case_hash (iact, c);
212
213   HMAP_FOR_EACH_WITH_HASH (nn, struct value_node, node, hash, map)
214     {
215       if (interaction_case_equal (iact, c, nn->ccase))
216         break;
217
218       fprintf (stderr, "Warning: Hash table collision\n");
219     }
220
221   return nn;
222 }
223
224
225 struct categoricals *
226 categoricals_create (const struct interaction **inter, size_t n_inter,
227                      const struct variable *wv, enum mv_class exclude,
228                      user_data_create_func *udf,
229                      update_func *update, void *aux1, void *aux2
230                      )
231 {
232   size_t i;
233   struct categoricals *cat = xmalloc (sizeof *cat);
234   
235   cat->n_vp = n_inter;
236   cat->wv = wv;
237   cat->n_cats_total = 0;
238   cat->n_vars = 0;
239   cat->reverse_variable_map_short = NULL;
240   cat->reverse_variable_map_long = NULL;
241   cat->pool = pool_create ();
242   cat->exclude = exclude;
243   cat->update = update;
244   cat->user_data_create = udf;
245
246   cat->aux1 = aux1;
247   cat->aux2 = aux2;
248
249
250   cat->vp = pool_calloc (cat->pool, cat->n_vp, sizeof *cat->vp);
251
252   for (i = 0 ; i < cat->n_vp; ++i)
253     {
254       hmap_init (&cat->vp[i].map);
255       cat->vp[i].iact = inter[i];
256     }
257
258   return cat;
259 }
260
261
262
263 void
264 categoricals_update (struct categoricals *cat, const struct ccase *c)
265 {
266   size_t i;
267   
268   const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
269
270   assert (NULL == cat->reverse_variable_map_short);
271   assert (NULL == cat->reverse_variable_map_long);
272
273   for (i = 0 ; i < cat->n_vp; ++i)
274     {
275       const struct interaction *iact = cat->vp[i].iact;
276       size_t hash;
277       struct value_node *node ;
278
279       if ( interaction_case_is_missing (iact, c, cat->exclude))
280         continue;
281
282       hash = interaction_case_hash (iact, c);
283       node = lookup_case (&cat->vp[i].map, iact, c);
284
285       if ( NULL == node)
286         {
287           node = pool_malloc (cat->pool, sizeof *node);
288
289           node->ccase = case_ref (c);
290           node->cc = 0.0;
291
292           hmap_insert (&cat->vp[i].map, &node->node,  hash);
293           cat->n_cats_total++;
294           
295           if ( 0 == cat->vp[i].n_cats)
296             cat->n_vars++;
297
298           node->subscript = cat->vp[i].n_cats++ ;
299
300           if (cat->user_data_create)
301             node->user_data = cat->user_data_create (cat->aux1, cat->aux2);
302         }
303
304       node->cc += weight;
305       cat->vp[i].cc += weight;
306
307       if (cat->update)
308         cat->update (node->user_data, cat->exclude, cat->wv, NULL, c, cat->aux1, cat->aux2);
309     }
310 }
311
312 /* Return the number of categories (distinct values) for variable N */
313 size_t
314 categoricals_n_count (const struct categoricals *cat, size_t n)
315 {
316   return hmap_count (&cat->vp[n].map);
317 }
318
319
320 /* Return the total number of categories */
321 size_t
322 categoricals_total (const struct categoricals *cat)
323 {
324   return cat->n_cats_total;
325 }
326
327
328 /* This function must be called *before* any call to categoricals_get_*_by subscript and
329  *after* all calls to categoricals_update */
330 void
331 categoricals_done (const struct categoricals *cat_)
332 {
333   /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
334      uses it will be more efficient that using a tree based structure, since it
335      is called only once, and means that subsequent lookups will be O(1).
336
337      1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
338   */
339   struct categoricals *cat = CONST_CAST (struct categoricals *, cat_);
340   int v;
341   int idx_short = 0;
342   int idx_long = 0;
343   cat->reverse_variable_map_short = pool_calloc (cat->pool,
344                                                  cat->n_cats_total - cat->n_vars,
345                                                  sizeof *cat->reverse_variable_map_short);
346
347   cat->reverse_variable_map_long = pool_calloc (cat->pool,
348                                                 cat->n_cats_total,
349                                                 sizeof *cat->reverse_variable_map_long);
350   
351   for (v = 0 ; v < cat->n_vp; ++v)
352     {
353       int i;
354       struct var_params *vp = &cat->vp[v];
355       int n_cats_total = categoricals_n_count (cat, v);
356       struct hmap_node *node ;
357
358       vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
359
360       vp->base_subscript_short = idx_short;
361       vp->base_subscript_long = idx_long;
362
363       for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
364         {
365           const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
366           vp->reverse_value_map[vn->subscript] = vn;
367         }
368
369       /* For some purposes (eg CONTRASTS in ONEWAY) the values need to be sorted */
370       sort (vp->reverse_value_map, vp->n_cats, sizeof (const struct value_node *),
371             compare_value_node, vp);
372
373       /* Populate the reverse variable maps. */
374       for (i = 0; i < vp->n_cats - 1; ++i)
375         cat->reverse_variable_map_short[idx_short++] = v;
376
377       for (i = 0; i < vp->n_cats; ++i)
378         cat->reverse_variable_map_long[idx_long++] = v;
379     }
380
381   assert (cat->n_vars <= cat->n_vp);
382 }
383
384
385 static int
386 reverse_variable_lookup_short (const struct categoricals *cat, int subscript)
387 {
388   assert (cat->reverse_variable_map_short);
389   assert (subscript >= 0);
390   assert (subscript < cat->n_cats_total - cat->n_vars);
391
392   return cat->reverse_variable_map_short[subscript];
393 }
394
395 static int
396 reverse_variable_lookup_long (const struct categoricals *cat, int subscript)
397 {
398   assert (cat->reverse_variable_map_long);
399   assert (subscript >= 0);
400   assert (subscript < cat->n_cats_total);
401
402   return cat->reverse_variable_map_long[subscript];
403 }
404
405
406 /* Return the interaction corresponding to SUBSCRIPT */
407 const struct interaction *
408 categoricals_get_interaction_by_subscript (const struct categoricals *cat, int subscript)
409 {
410   int index = reverse_variable_lookup_short (cat, subscript);
411
412   return cat->vp[index].iact;
413 }
414
415
416 /* Return the case corresponding to SUBSCRIPT */
417 static const struct ccase *
418 categoricals_get_case_by_subscript (const struct categoricals *cat, int subscript)
419 {
420   int vindex = reverse_variable_lookup_short (cat, subscript);
421   const struct var_params *vp = &cat->vp[vindex];
422   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript_short];
423
424   return vn->ccase;
425 }
426
427
428 double
429 categoricals_get_weight_by_subscript (const struct categoricals *cat, int subscript)
430 {
431   int vindex = reverse_variable_lookup_short (cat, subscript);
432   const struct var_params *vp = &cat->vp[vindex];
433
434   return vp->cc;
435 }
436
437 double
438 categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript)
439 {
440   int vindex = reverse_variable_lookup_short (cat, subscript);
441   const struct var_params *vp = &cat->vp[vindex];
442
443   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript_short];
444   return vn->cc;
445 }
446
447
448 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
449    for that subscript */
450 double
451 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
452                                       const struct ccase *c)
453 {
454   const struct interaction *iact = categoricals_get_interaction_by_subscript (cat, subscript);
455
456   const struct ccase *c2 =  categoricals_get_case_by_subscript (cat, subscript);
457
458   return interaction_case_equal (iact, c, c2);
459 }
460
461
462 size_t
463 categoricals_get_n_variables (const struct categoricals *cat)
464 {
465   return cat->n_vars;
466 }
467
468
469 /* Return a case containing the set of values corresponding to SUBSCRIPT */
470 const struct ccase *
471 categoricals_get_case_by_category (const struct categoricals *cat, int subscript)
472 {
473   int vindex = reverse_variable_lookup_long (cat, subscript);
474   const struct var_params *vp = &cat->vp[vindex];
475   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript_long];
476
477   return vn->ccase;
478 }
479
480
481 void *
482 categoricals_get_user_data_by_category (const struct categoricals *cat, int subscript)
483 {
484   int vindex = reverse_variable_lookup_long (cat, subscript);
485   const struct var_params *vp = &cat->vp[vindex];
486
487   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript_long];
488   return vn->user_data;
489 }