1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "math/categoricals.h"
20 #include "math/interaction.h"
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"
32 #include "gl/xalloc.h"
36 struct hmap_node node; /* Node in hash map. */
38 double cc; /* The total of the weights of cases with this value */
40 void *user_data; /* A pointer to data which the caller can store stuff */
42 int subscript; /* A zero based integer, unique within the variable.
43 Can be used as an index into an array */
48 /* A map indexed by a union values */
51 const struct interaction *iact;
53 int base_subscript_short;
54 int base_subscript_long;
56 /* The number of distinct values of this variable */
59 /* A map of values indexed by subscript */
60 const struct value_node **reverse_value_map;
62 /* Total of the weights of this variable */
67 /* Comparison function to sort the reverse_value_map in ascending order */
69 compare_value_node (const void *vn1_, const void *vn2_, const void *aux)
71 const struct value_node * const *vn1 = vn1_;
72 const struct value_node * const *vn2 = vn2_;
73 const struct var_params *vp = aux;
75 return interaction_case_cmp_3way (vp->iact, (*vn1)->ccase, (*vn2)->ccase);
81 /* The weight variable */
82 const struct variable *wv;
84 /* An array of var_params */
85 struct var_params *vp;
87 /* The size of VP. (ie, the number of variables involved.) */
90 /* The number of categorical variables which contain entries.
91 In the absence of missing values, this will be equal to N_VP */
94 /* A map to enable the lookup of variables indexed by subscript.
95 This map considers only the N - 1 of the N variables.
97 int *reverse_variable_map_short;
99 /* Like the above, but uses all N variables */
100 int *reverse_variable_map_long;
106 /* Missing values to be excluded */
107 enum mv_class exclude;
109 /* Function to be called on each update */
112 /* Function specified by the caller to create user_data */
113 user_data_create_func *user_data_create;
115 /* Auxilliary data to be passed to update and user_data_create_func*/
122 categoricals_destroy ( struct categoricals *cat)
127 for (i = 0 ; i < cat->n_vp; ++i)
128 hmap_destroy (&cat->vp[i].map);
130 pool_destroy (cat->pool);
138 categoricals_dump (const struct categoricals *cat)
142 for (v = 0 ; v < cat->n_vp; ++v)
144 const struct var_params *vp = &cat->vp[v];
145 const struct hmap *m = &vp->map;
146 struct hmap_node *node ;
149 printf ("\n%s (%d) CC=%g n_cats=%d:\n",
150 var_get_name (vp->var), vp->base_subscript_long, vp->cc, vp->n_cats);
152 printf ("Reverse map\n");
153 for (x = 0 ; x < vp->n_cats; ++x)
156 const struct value_node *vn = vp->reverse_value_map[x];
158 var_append_value_name (vp->var, &vn->value, &s);
159 printf ("Value for %d is %s\n", x, ds_cstr(&s));
163 printf ("\nForward map\n");
164 for (node = hmap_first (m); node; node = hmap_next (m, node))
167 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
169 var_append_value_name (vp->var, &vn->value, &s);
170 printf ("Value: %s; Index %d; CC %g\n",
172 vn->subscript, vn->cc);
177 assert (cat->n_vars <= cat->n_vp);
180 printf ("Number of categorical variables: %d\n", cat->n_vp);
181 printf ("Number of non-empty categorical variables: %d\n", cat->n_vars);
182 printf ("Total number of categories: %d\n", cat->n_cats_total);
184 printf ("\nReverse variable map (short):\n");
185 for (v = 0 ; v < cat->n_cats_total - cat->n_vars; ++v)
186 printf ("%d ", cat->reverse_variable_map_short[v]);
188 printf ("\nReverse variable map (long):\n");
189 for (v = 0 ; v < cat->n_cats_total; ++v)
190 printf ("%d ", cat->reverse_variable_map_long[v]);
197 static struct value_node *
198 lookup_case (const struct hmap *map, const struct interaction *iact, const struct ccase *c)
200 struct value_node *nn;
201 size_t hash = interaction_case_hash (iact, c);
203 HMAP_FOR_EACH_WITH_HASH (nn, struct value_node, node, hash, map)
205 if (interaction_case_equal (iact, c, nn->ccase))
208 fprintf (stderr, "Warning: Hash table collision\n");
215 struct categoricals *
216 categoricals_create (const struct interaction **inter, size_t n_inter,
217 const struct variable *wv, enum mv_class exclude,
218 user_data_create_func *udf,
219 update_func *update, void *aux1, void *aux2
223 struct categoricals *cat = xmalloc (sizeof *cat);
227 cat->n_cats_total = 0;
229 cat->reverse_variable_map_short = NULL;
230 cat->reverse_variable_map_long = NULL;
231 cat->pool = pool_create ();
232 cat->exclude = exclude;
233 cat->update = update;
234 cat->user_data_create = udf;
240 cat->vp = pool_calloc (cat->pool, cat->n_vp, sizeof *cat->vp);
242 for (i = 0 ; i < cat->n_vp; ++i)
244 hmap_init (&cat->vp[i].map);
245 cat->vp[i].iact = inter[i];
254 categoricals_update (struct categoricals *cat, const struct ccase *c)
258 const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
260 assert (NULL == cat->reverse_variable_map_short);
261 assert (NULL == cat->reverse_variable_map_long);
263 for (i = 0 ; i < cat->n_vp; ++i)
265 const struct interaction *iact = cat->vp[i].iact;
267 struct value_node *node ;
269 if ( interaction_case_is_missing (iact, c, cat->exclude))
272 hash = interaction_case_hash (iact, c);
273 node = lookup_case (&cat->vp[i].map, iact, c);
277 node = pool_malloc (cat->pool, sizeof *node);
279 node->ccase = case_ref (c);
282 hmap_insert (&cat->vp[i].map, &node->node, hash);
285 if ( 0 == cat->vp[i].n_cats)
288 node->subscript = cat->vp[i].n_cats++ ;
290 if (cat->user_data_create)
291 node->user_data = cat->user_data_create (cat->aux1, cat->aux2);
295 cat->vp[i].cc += weight;
298 cat->update (node->user_data, cat->exclude, cat->wv, NULL, c, cat->aux1, cat->aux2);
302 /* Return the number of categories (distinct values) for variable N */
304 categoricals_n_count (const struct categoricals *cat, size_t n)
306 return hmap_count (&cat->vp[n].map);
310 /* Return the total number of categories */
312 categoricals_total (const struct categoricals *cat)
314 return cat->n_cats_total;
318 /* This function must be called *before* any call to categoricals_get_*_by subscript and
319 *after* all calls to categoricals_update */
321 categoricals_done (const struct categoricals *cat_)
323 /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
324 uses it will be more efficient that using a tree based structure, since it
325 is called only once, and means that subsequent lookups will be O(1).
327 1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
329 struct categoricals *cat = CONST_CAST (struct categoricals *, cat_);
333 cat->reverse_variable_map_short = pool_calloc (cat->pool,
334 cat->n_cats_total - cat->n_vars,
335 sizeof *cat->reverse_variable_map_short);
337 cat->reverse_variable_map_long = pool_calloc (cat->pool,
339 sizeof *cat->reverse_variable_map_long);
341 for (v = 0 ; v < cat->n_vp; ++v)
344 struct var_params *vp = &cat->vp[v];
345 int n_cats_total = categoricals_n_count (cat, v);
346 struct hmap_node *node ;
348 vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
350 vp->base_subscript_short = idx_short;
351 vp->base_subscript_long = idx_long;
353 for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
355 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
356 vp->reverse_value_map[vn->subscript] = vn;
359 /* For some purposes (eg CONTRASTS in ONEWAY) the values need to be sorted */
360 sort (vp->reverse_value_map, vp->n_cats, sizeof (const struct value_node *),
361 compare_value_node, vp);
363 /* Populate the reverse variable maps. */
364 for (i = 0; i < vp->n_cats - 1; ++i)
365 cat->reverse_variable_map_short[idx_short++] = v;
367 for (i = 0; i < vp->n_cats; ++i)
368 cat->reverse_variable_map_long[idx_long++] = v;
371 assert (cat->n_vars <= cat->n_vp);
376 reverse_variable_lookup_short (const struct categoricals *cat, int subscript)
378 assert (cat->reverse_variable_map_short);
379 assert (subscript >= 0);
380 assert (subscript < cat->n_cats_total - cat->n_vars);
382 return cat->reverse_variable_map_short[subscript];
386 reverse_variable_lookup_long (const struct categoricals *cat, int subscript)
388 assert (cat->reverse_variable_map_long);
389 assert (subscript >= 0);
390 assert (subscript < cat->n_cats_total);
392 return cat->reverse_variable_map_long[subscript];
397 /* Return the categorical variable corresponding to SUBSCRIPT */
398 const struct variable *
399 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
401 int index = reverse_variable_lookup_short (cat, subscript);
403 return cat->vp[index].iact->vars[0];
406 /* Return the interaction corresponding to SUBSCRIPT */
407 const struct interaction *
408 categoricals_get_interaction_by_subscript (const struct categoricals *cat, int subscript)
410 int index = reverse_variable_lookup_short (cat, subscript);
412 return cat->vp[index].iact;
416 /* Return the case corresponding to SUBSCRIPT */
417 static const struct ccase *
418 categoricals_get_case_by_subscript (const struct categoricals *cat, int subscript)
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];
429 categoricals_get_weight_by_subscript (const struct categoricals *cat, int subscript)
431 int vindex = reverse_variable_lookup_short (cat, subscript);
432 const struct var_params *vp = &cat->vp[vindex];
438 categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript)
440 int vindex = reverse_variable_lookup_short (cat, subscript);
441 const struct var_params *vp = &cat->vp[vindex];
443 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript_short];
448 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
449 for that subscript */
451 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
452 const struct ccase *c)
454 const struct interaction *iact = categoricals_get_interaction_by_subscript (cat, subscript);
456 const struct ccase *c2 = categoricals_get_case_by_subscript (cat, subscript);
458 return interaction_case_equal (iact, c, c2);
463 categoricals_get_n_variables (const struct categoricals *cat)
469 /* Return a case containing the set of values corresponding to SUBSCRIPT */
471 categoricals_get_case_by_category (const struct categoricals *cat, int subscript)
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];
482 categoricals_get_user_data_by_category (const struct categoricals *cat, int subscript)
484 int vindex = reverse_variable_lookup_long (cat, subscript);
485 const struct var_params *vp = &cat->vp[vindex];
487 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript_long];
488 return vn->user_data;