92bb3d1d3270bfa4878ed2498d72fce799c6c2d6
[pspp] / src / math / categoricals.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2012, 2014 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 <float.h>
23 #include <stdio.h>
24
25 #include "data/case.h"
26 #include "data/value.h"
27 #include "data/variable.h"
28 #include "libpspp/array.h"
29 #include "libpspp/hmap.h"
30 #include "libpspp/pool.h"
31 #include "libpspp/str.h"
32 #include "libpspp/hash-functions.h"
33
34 #include "gl/xalloc.h"
35
36 #define CATEGORICALS_DEBUG 0
37
38 struct value_node
39 {
40   struct hmap_node node;      /* Node in hash map. */
41   union value val;            /* The value */
42   int index;                  /* A zero based unique index for this value */
43 };
44
45 static struct value_node *
46 lookup_value (const struct hmap *map, const union value *val,
47               unsigned int hash, int width)
48 {
49   struct value_node *vn;
50   HMAP_FOR_EACH_WITH_HASH (vn, struct value_node, node, hash, map)
51     if (value_equal (&vn->val, val, width))
52       return vn;
53   return NULL;
54 }
55
56 /* A variable used in a categoricals object.  */
57 struct variable_node
58   {
59     struct hmap_node node;      /* In struct categorical's 'varmap'. */
60     const struct variable *var; /* The variable. */
61     struct hmap valmap;         /* Contains "struct value_node"s. */
62   };
63
64 static int
65 compare_value_node_3way (const void *vn1_, const void *vn2_, const void *aux)
66 {
67   const struct value_node *const *vn1p = vn1_;
68   const struct value_node *const *vn2p = vn2_;
69
70   const struct variable_node *vn = aux;
71
72   return value_compare_3way (&(*vn1p)->val, &(*vn2p)->val,
73                              var_get_width (vn->var));
74 }
75
76 static struct variable_node *
77 lookup_variable (const struct hmap *map, const struct variable *var, unsigned int hash)
78 {
79   struct variable_node *vn;
80   HMAP_FOR_EACH_WITH_HASH (vn, struct variable_node, node, hash, map)
81     if (vn->var == var)
82       return vn;
83   return NULL;
84 }
85
86 struct interact_params
87 {
88   /* The interaction, and an array with iact->n_vars elements such that
89      varnodes[x] points to the variable_node for iact->vars[x]. */
90   const struct interaction *iact;
91   struct variable_node **varnodes;
92
93   /* An example of each interaction that appears in the data, like a frequency
94      table for 'iact'.  By construction, the number of elements must be less
95      than or equal to 'n_cats'.
96
97      categoricals_update() updates 'ivmap' case-by-case, then
98      categoricals_done() dumps 'ivmap' into 'ivs' and sorts it. */
99   struct hmap ivmap;            /* Contains "struct interaction_value"s. */
100   struct interaction_value **ivs;
101
102   int base_df;
103   int base_cats;
104
105   /* Product of hmap_count(&varnodes[*]->valmap), that is, the maximum number
106      of distinct values of this interaction. */
107   int n_cats;
108
109   /* Product of degrees of freedom of all the variables. */
110   int df_prod;
111
112   double *enc_sum;
113
114   /* Sum of ivs[*]->cc. */
115   double cc;
116 };
117
118 struct interaction_value
119   {
120     struct hmap_node node;    /* In struct interact_params's ivmap. */
121     struct ccase *ccase;      /* A case representative of the interaction. */
122     double cc;                /* Total weight of cases for this interaction. */
123     void *user_data;
124   };
125
126 static int
127 compare_interaction_value_3way (const void *vn1_, const void *vn2_,
128                                 const void *aux)
129 {
130   const struct interaction_value *const *vn1p = vn1_;
131   const struct interaction_value *const *vn2p = vn2_;
132
133   const struct interact_params *iap = aux;
134
135   return interaction_case_cmp_3way (iap->iact, (*vn1p)->ccase, (*vn2p)->ccase);
136 }
137
138 struct categoricals
139 {
140   /* The weight variable */
141   const struct variable *wv;
142
143   struct interact_params *iap;  /* Interaction parameters. */
144   size_t n_iap;
145
146   /* Contains a "struct variable_node" for each variable in 'iap'. */
147   struct hmap varmap;
148
149   /* A map to enable the lookup of variables indexed by subscript.
150      This map considers only the N - 1 of the N variables.
151   */
152   int *df_to_iact; /* 'df_sum' elements. */
153   size_t df_sum;
154
155   /* Like the above, but uses all N variables. */
156   int *cat_to_iact; /* 'n_cats_total' elements. */
157   size_t n_cats_total;
158
159   struct pool *pool;
160
161   /* Missing values in the factor variables to be excluded */
162   enum mv_class fctr_excl;
163
164   const void *aux1;
165   void *aux2;
166
167   bool sane;
168
169   const struct payload *payload;
170 };
171
172
173 bool
174 categoricals_isbalanced (const struct categoricals *cat)
175 {
176   for (int i = 0 ; i < cat->n_iap; ++i)
177     {
178       int v;
179       const struct interact_params *iap = &cat->iap[i];
180
181       double oval = -1.0;
182       for (v = 0; v < hmap_count (&iap->ivmap); ++v)
183         {
184           const struct interaction_value *iv = iap->ivs[v];
185           if (oval == -1.0)
186             oval = iv->cc;
187           if (oval != iv->cc)
188             return false;
189         }
190     }
191   return true;
192 }
193
194
195 static void
196 categoricals_dump (const struct categoricals *cat)
197 {
198   if (CATEGORICALS_DEBUG)
199     {
200       int i;
201
202       printf ("df to interaction map:\n");
203       for (i = 0; i < cat->df_sum; ++i)
204         printf (" %d", cat->df_to_iact[i]);
205       printf ("\n");
206
207       printf ("Category to interaction map:\n");
208       for (i = 0; i < cat->n_cats_total; ++i)
209         printf (" %d", cat->cat_to_iact[i]);
210       printf ("\n");
211
212       printf ("Number of interactions %zu\n", cat->n_iap);
213       for (i = 0 ; i < cat->n_iap; ++i)
214         {
215           int v;
216           struct string str;
217           const struct interact_params *iap = &cat->iap[i];
218           const struct interaction *iact = iap->iact;
219
220           ds_init_empty (&str);
221           interaction_to_string (iact, &str);
222
223           printf ("\nInteraction: \"%s\" (number of categories: %d); ", ds_cstr (&str), iap->n_cats);
224           ds_destroy (&str);
225           printf ("Base index (df/categories): %d/%d\n", iap->base_df, iap->base_cats);
226
227           printf ("\t(");
228           for (v = 0; v < hmap_count (&iap->ivmap); ++v)
229             {
230               int vv;
231               const struct interaction_value *iv = iap->ivs[v];
232
233               if (v > 0)  printf ("   ");
234               printf ("{");
235               for (vv = 0; vv < iact->n_vars; ++vv)
236                 {
237                   const struct variable *var = iact->vars[vv];
238                   const union value *val = case_data (iv->ccase, var);
239                   struct variable_node *vn = iap->varnodes[vv];
240                   const int width = var_get_width (var);
241                   unsigned int valhash = value_hash (val, width, 0);
242                   struct value_node *valn = lookup_value (&vn->valmap, val, valhash, width);
243
244                   assert (vn->var == var);
245
246                   printf ("%.*g(%d)", DBL_DIG + 1, val->f, valn->index);
247                   if (vv < iact->n_vars - 1)
248                     printf (", ");
249                 }
250               printf ("}");
251             }
252           printf (")\n");
253         }
254     }
255 }
256
257 void
258 categoricals_destroy (struct categoricals *cat)
259 {
260   if (!cat)
261     return;
262
263   for (int i = 0; i < cat->n_iap; ++i)
264     {
265       /* Unref any cases that we reffed. */
266       struct interaction_value *iv;
267       HMAP_FOR_EACH (iv, struct interaction_value, node, &cat->iap[i].ivmap)
268         {
269           if (cat->payload && cat->payload->destroy)
270             cat->payload->destroy (cat->aux1, cat->aux2, iv->user_data);
271           case_unref (iv->ccase);
272         }
273
274       free (cat->iap[i].enc_sum);
275       hmap_destroy (&cat->iap[i].ivmap);
276     }
277
278   /* Interate over each variable and delete its value map.
279
280      The values themselves are part of the pool. */
281   struct variable_node *vn;
282   HMAP_FOR_EACH (vn, struct variable_node, node, &cat->varmap)
283     hmap_destroy (&vn->valmap);
284
285   hmap_destroy (&cat->varmap);
286
287   pool_destroy (cat->pool);
288
289   free (cat);
290 }
291
292 static struct interaction_value *
293 lookup_case (const struct hmap *map, const struct interaction *iact,
294              const struct ccase *c)
295 {
296   size_t hash = interaction_case_hash (iact, c, 0);
297   struct interaction_value *iv;
298   HMAP_FOR_EACH_WITH_HASH (iv, struct interaction_value, node, hash, map)
299     if (interaction_case_equal (iact, c, iv->ccase))
300       return iv;
301   return NULL;
302 }
303
304 /* Returns true iff CAT is sane, that is, if it is complete and has at least
305    one value. */
306 bool
307 categoricals_sane (const struct categoricals *cat)
308 {
309   return cat->sane;
310 }
311
312 /* Creates and returns a new categoricals object whose variables come from the
313    N_INTER interactions objects in the array starting at INTER.  (The INTER
314    objects must outlive the categoricals object because it uses them
315    internally.)
316
317    FCTR_EXCL determines which cases are listwise ignored by
318    categoricals_update(). */
319 struct categoricals *
320 categoricals_create (struct interaction *const *inter, size_t n_inter,
321                      const struct variable *wv, enum mv_class fctr_excl)
322 {
323   struct categoricals *cat = xzalloc (sizeof *cat);
324   cat->iap = pool_calloc (cat->pool, n_inter, sizeof *cat->iap);
325   cat->n_iap = n_inter;
326   cat->wv = wv;
327   cat->pool = pool_create ();
328   cat->fctr_excl = fctr_excl;
329
330   hmap_init (&cat->varmap);
331   for (size_t i = 0; i < cat->n_iap; ++i)
332     {
333       struct interact_params *iap = &cat->iap[i];
334       hmap_init (&iap->ivmap);
335       iap->iact = inter[i];
336       iap->cc = 0.0;
337       iap->varnodes = pool_nmalloc (cat->pool, iap->iact->n_vars,
338                                     sizeof *iap->varnodes);
339       for (size_t v = 0; v < inter[i]->n_vars; ++v)
340         {
341           const struct variable *var = inter[i]->vars[v];
342           unsigned int hash = hash_pointer (var, 0);
343           struct variable_node *vn = lookup_variable (&cat->varmap, var, hash);
344           if (!vn)
345             {
346               vn = pool_malloc (cat->pool, sizeof *vn);
347               vn->var = var;
348               hmap_init (&vn->valmap);
349               hmap_insert (&cat->varmap, &vn->node,  hash);
350             }
351           iap->varnodes[v] = vn;
352         }
353     }
354
355   return cat;
356 }
357
358 void
359 categoricals_update (struct categoricals *cat, const struct ccase *c)
360 {
361   if (!cat)
362     return;
363   assert (!cat->df_to_iact);
364   assert (!cat->cat_to_iact);
365
366   double weight;
367   weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
368   weight = var_force_valid_weight (cat->wv, weight, NULL);
369
370   /* Update the frequency table for each variable. */
371   struct variable_node *vn;
372   HMAP_FOR_EACH (vn, struct variable_node, node, &cat->varmap)
373     {
374       const int width = var_get_width (vn->var);
375       const union value *val = case_data (c, vn->var);
376       unsigned int hash = value_hash (val, width, 0);
377
378       struct value_node *valn = lookup_value (&vn->valmap, val, hash, width);
379       if (valn == NULL)
380         {
381           valn = pool_malloc (cat->pool, sizeof *valn);
382           valn->index = -1;
383           value_init (&valn->val, width);
384           value_copy (&valn->val, val, width);
385           hmap_insert (&vn->valmap, &valn->node, hash);
386         }
387     }
388
389   /* Update the frequency table for full interactions. */
390   for (int i = 0; i < cat->n_iap; ++i)
391     {
392       struct interact_params *iap = &cat->iap[i];
393       const struct interaction *iact = iap->iact;
394       if (interaction_case_is_missing (iact, c, cat->fctr_excl))
395         continue;
396
397       unsigned int hash = interaction_case_hash (iact, c, 0);
398       struct interaction_value *node = lookup_case (&iap->ivmap, iact, c);
399       if (!node)
400         {
401           node = pool_malloc (cat->pool, sizeof *node);
402           node->ccase = case_ref (c);
403           node->cc = weight;
404
405           hmap_insert (&iap->ivmap, &node->node, hash);
406
407           if (cat->payload)
408             node->user_data = cat->payload->create (cat->aux1, cat->aux2);
409         }
410       else
411         node->cc += weight;
412       iap->cc += weight;
413
414       if (cat->payload)
415         cat->payload->update (cat->aux1, cat->aux2, node->user_data, c,
416                               weight);
417     }
418 }
419
420 /* Return the number of categories (distinct values) for interaction IDX in
421    CAT. */
422 size_t
423 categoricals_n_count (const struct categoricals *cat, size_t idx)
424 {
425   return hmap_count (&cat->iap[idx].ivmap);
426 }
427
428 /* Return the total number of categories across all interactions in CAT. */
429 size_t
430 categoricals_n_total (const struct categoricals *cat)
431 {
432   return categoricals_is_complete (cat) ? cat->n_cats_total : 0;
433 }
434
435 /* Returns the number of degrees of freedom for interaction IDX within CAT. */
436 size_t
437 categoricals_df (const struct categoricals *cat, size_t idx)
438 {
439   const struct interact_params *iap = &cat->iap[idx];
440   return iap->df_prod;
441 }
442
443 /* Returns the total degrees of freedom for CAT. */
444 size_t
445 categoricals_df_total (const struct categoricals *cat)
446 {
447   return cat ? cat->df_sum : 0;
448 }
449
450 /* Returns true iff categoricals_done() has been called for CAT. */
451 bool
452 categoricals_is_complete (const struct categoricals *cat)
453 {
454   return cat->df_to_iact != NULL;
455 }
456
457 /* This function must be called (once) before any call to the *_by_subscript or
458   *_by_category functions, but AFTER any calls to categoricals_update.  If this
459   function returns false, then no calls to _by_subscript or *_by_category are
460   allowed. */
461 void
462 categoricals_done (const struct categoricals *cat_)
463 {
464   struct categoricals *cat = CONST_CAST (struct categoricals *, cat_);
465   if (!cat || categoricals_is_complete (cat))
466     return;
467
468   /* Assign 'index' to each variables' value_nodes, counting up from 0 in
469      ascending order by value. */
470   struct variable_node *vn;
471   HMAP_FOR_EACH (vn, struct variable_node, node, &cat->varmap)
472     {
473       size_t n_vals = hmap_count (&vn->valmap);
474       if (!n_vals)
475         {
476           cat->sane = false;
477           return;
478         }
479
480       struct value_node **nodes = xcalloc (sizeof *nodes, n_vals);
481       int x = 0;
482       struct value_node *valnd;
483       HMAP_FOR_EACH (valnd, struct value_node, node, &vn->valmap)
484         nodes[x++] = valnd;
485       sort (nodes, n_vals, sizeof *nodes, compare_value_node_3way, vn);
486       for (x = 0; x < n_vals; ++x)
487         nodes[x]->index = x;
488       free (nodes);
489     }
490
491   /* Calculate the degrees of freedom, and the number of categories. */
492   cat->df_sum = 0;
493   cat->n_cats_total = 0;
494   for (int i = 0 ; i < cat->n_iap; ++i)
495     {
496       struct interact_params *iap = &cat->iap[i];
497       const struct interaction *iact = iap->iact;
498
499       iap->df_prod = 1;
500       iap->n_cats = 1;
501       for (int v = 0 ; v < iact->n_vars; ++v)
502         {
503           size_t n_vals = hmap_count (&iap->varnodes[v]->valmap);
504
505           iap->df_prod *= n_vals - 1;
506           iap->n_cats *= n_vals;
507         }
508
509       if (iact->n_vars > 0)
510         cat->df_sum += iap->df_prod;
511       cat->n_cats_total += iap->n_cats;
512     }
513
514
515   cat->df_to_iact = pool_calloc (cat->pool, cat->df_sum,
516                                  sizeof *cat->df_to_iact);
517
518   cat->cat_to_iact = pool_calloc (cat->pool, cat->n_cats_total,
519                                   sizeof *cat->cat_to_iact);
520
521   int idx_df = 0;
522   int idx_cat = 0;
523   for (int i = 0; i < cat->n_iap; ++i)
524     {
525       struct interact_params *iap = &cat->iap[i];
526
527       iap->base_df = idx_df;
528       iap->base_cats = idx_cat;
529
530       /* For some purposes (eg CONTRASTS in ONEWAY) the values need to be
531          sorted. */
532       iap->ivs = pool_nmalloc (cat->pool, hmap_count (&iap->ivmap),
533                                sizeof *iap->ivs);
534       int x = 0;
535       struct interaction_value *ivn;
536       HMAP_FOR_EACH (ivn, struct interaction_value, node, &iap->ivmap)
537         iap->ivs[x++] = ivn;
538       sort (iap->ivs, x, sizeof *iap->ivs,
539             compare_interaction_value_3way, iap);
540
541       /* Populate the variable maps. */
542       if (iap->iact->n_vars)
543         for (int j = 0; j < iap->df_prod; ++j)
544           cat->df_to_iact[idx_df++] = i;
545
546       for (int j = 0; j < iap->n_cats; ++j)
547         cat->cat_to_iact[idx_cat++] = i;
548     }
549
550   categoricals_dump (cat);
551
552   /* Tally up the sums for all the encodings */
553   for (int i = 0; i < cat->n_iap; ++i)
554     {
555       struct interact_params *iap = &cat->iap[i];
556       const struct interaction *iact = iap->iact;
557
558       const int df = iact->n_vars ? iap->df_prod : 0;
559
560       iap->enc_sum = xcalloc (df, sizeof *iap->enc_sum);
561
562       for (int y = 0; y < hmap_count (&iap->ivmap); ++y)
563         {
564           struct interaction_value *iv = iap->ivs[y];
565           for (int x = iap->base_df;
566                x < iap->base_df + df; ++x)
567             {
568               const double bin = categoricals_get_effects_code_for_case (
569                 cat, x, iv->ccase);
570               iap->enc_sum[x - iap->base_df] += bin * iv->cc;
571             }
572           if (cat->payload && cat->payload->calculate)
573             cat->payload->calculate (cat->aux1, cat->aux2, iv->user_data);
574         }
575     }
576
577   cat->sane = true;
578 }
579
580 static struct interact_params *
581 df_to_iap (const struct categoricals *cat, int subscript)
582 {
583   assert (subscript >= 0);
584   assert (subscript < cat->df_sum);
585
586   return &cat->iap[cat->df_to_iact[subscript]];
587 }
588
589 static struct interact_params *
590 cat_index_to_iap (const struct categoricals *cat, int cat_index)
591 {
592   assert (cat_index >= 0);
593   assert (cat_index < cat->n_cats_total);
594
595   return &cat->iap[cat->cat_to_iact[cat_index]];
596 }
597
598 /* Return the interaction corresponding to SUBSCRIPT. */
599 const struct interaction *
600 categoricals_get_interaction_by_subscript (const struct categoricals *cat,
601                                            int subscript)
602 {
603   return df_to_iap (cat, subscript)->iact;
604 }
605
606 double
607 categoricals_get_weight_by_subscript (const struct categoricals *cat,
608                                       int subscript)
609 {
610   return df_to_iap (cat, subscript)->cc;
611 }
612
613 double
614 categoricals_get_sum_by_subscript (const struct categoricals *cat,
615                                    int subscript)
616 {
617   const struct interact_params *iap = df_to_iap (cat, subscript);
618   return iap->enc_sum[subscript - iap->base_df];
619 }
620
621
622 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
623    for that subscript */
624 static double
625 categoricals_get_code_for_case (const struct categoricals *cat, int subscript,
626                                 const struct ccase *c,
627                                 bool effects_coding)
628 {
629   const struct interaction *iact
630     = categoricals_get_interaction_by_subscript (cat, subscript);
631
632   const struct interact_params *iap = df_to_iap (cat, subscript);
633
634   double result = 1.0;
635   int dfp = 1;
636   for (int v = 0; v < iact->n_vars; ++v)
637     {
638       const struct variable *var = iact->vars[v];
639
640       const union value *val = case_data (c, var);
641       const int width = var_get_width (var);
642
643       const unsigned int hash = value_hash (val, width, 0);
644       const struct value_node *valn
645         = lookup_value (&iap->varnodes[v]->valmap, val, hash, width);
646
647       const int df = hmap_count (&iap->varnodes[v]->valmap) - 1;
648       const int dfpn = dfp * df;
649
650       if (effects_coding && valn->index == df)
651         result = -result;
652       else
653         {
654           /* Translate subscript into an index for the individual variable. */
655           const int index = ((subscript - iap->base_df) % dfpn) / dfp;
656           if (valn->index != index)
657             return 0.0;
658         }
659       dfp = dfpn;
660     }
661
662   return result;
663 }
664
665
666 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
667    for that subscript. */
668 double
669 categoricals_get_dummy_code_for_case (const struct categoricals *cat,
670                                       int subscript, const struct ccase *c)
671 {
672   return categoricals_get_code_for_case (cat, subscript, c, false);
673 }
674
675 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
676    for that subscript.
677    Else if it is the last category, return -1.
678    Otherwise return 0.
679  */
680 double
681 categoricals_get_effects_code_for_case (const struct categoricals *cat,
682                                         int subscript, const struct ccase *c)
683 {
684   return categoricals_get_code_for_case (cat, subscript, c, true);
685 }
686
687 /* Return a case containing the set of values corresponding to
688    the Nth Category of the IACTth interaction */
689 const struct ccase *
690 categoricals_get_case_by_category_real (const struct categoricals *cat,
691                                         int iact, int n)
692 {
693   const struct interact_params *iap = &cat->iap[iact];
694   return n < hmap_count (&iap->ivmap) ? iap->ivs[n]->ccase : NULL;
695 }
696
697 /* Return a the user data corresponding to the Nth Category of the IACTth
698    interaction. */
699 void *
700 categoricals_get_user_data_by_category_real (const struct categoricals *cat,
701                                              int iact, int n)
702 {
703   const struct interact_params *iap = &cat->iap[iact];
704   return n < hmap_count (&iap->ivmap) ? iap->ivs[n]->user_data : NULL;
705 }
706
707 /* Return a case containing the set of values corresponding to CAT_INDEX. */
708 const struct ccase *
709 categoricals_get_case_by_category (const struct categoricals *cat,
710                                    int cat_index)
711 {
712   const struct interact_params *iap = cat_index_to_iap (cat, cat_index);
713   const struct interaction_value *vn = iap->ivs[cat_index - iap->base_cats];
714   return vn->ccase;
715 }
716
717 void *
718 categoricals_get_user_data_by_category (const struct categoricals *cat,
719                                         int cat_index)
720 {
721   const struct interact_params *iap = cat_index_to_iap (cat, cat_index);
722   const struct interaction_value *iv = iap->ivs[cat_index - iap->base_cats];
723   return iv->user_data;
724 }
725 \f
726 void
727 categoricals_set_payload (struct categoricals *cat, const struct payload *p,
728                           const void *aux1, void *aux2)
729 {
730   cat->payload = p;
731   cat->aux1 = aux1;
732   cat->aux2 = aux2;
733 }