Merge remote-tracking branch 'origin/master' into sheet
[pspp] / src / language / stats / ks-one-sample.c
index aba2cbdef4cb5d0db99ccdc1bf5516e76000c3a4..ea7ac71b7e036aa5d39456f67cae4ffa26f921c4 100644 (file)
@@ -93,7 +93,7 @@ theoretical_exponential (const struct ks *ks, double x)
 }
 
 
-static const  theoreticalfp theoreticalf[4] = 
+static const  theoreticalfp theoreticalf[4] =
 {
   theoretical_normal,
   theoretical_uniform,
@@ -101,7 +101,7 @@ static const  theoreticalfp theoreticalf[4] =
   theoretical_exponential
 };
 
-/* 
+/*
    Return the assymptotic approximation to the significance of Z
  */
 static double
@@ -109,7 +109,7 @@ ks_asymp_sig (double z)
 {
   if (z < 0.27)
     return 1;
-  
+
   if (z >= 3.1)
     return 0;
 
@@ -130,9 +130,10 @@ static void show_results (const struct ks *, const struct ks_one_sample_test *,
 
 void
 ks_one_sample_execute (const struct dataset *ds,
-                  struct casereader *input,
-                   enum mv_class exclude,
-                  const struct npar_test *test)
+                      struct casereader *input,
+                      enum mv_class exclude,
+                      const struct npar_test *test,
+                      bool x UNUSED, double y UNUSED)
 {
   const struct dictionary *dict = dataset_dict (ds);
   const struct ks_one_sample_test *kst = UP_CAST (test, const struct ks_one_sample_test, parent.parent);
@@ -142,8 +143,9 @@ ks_one_sample_execute (const struct dataset *ds,
   const struct fmt_spec *wfmt = wvar ? var_get_print_format (wvar) : & F_8_0;
   bool warn = true;
   int v;
+  struct casereader *r = casereader_clone (input);
 
-  struct ks *ks = xcalloc (sizeof *ks, ost->n_vars);
+  struct ks *ks = xcalloc (ost->n_vars, sizeof *ks);
 
   for (v = 0; v < ost->n_vars; ++v)
     {
@@ -156,8 +158,6 @@ ks_one_sample_execute (const struct dataset *ds,
       ks[v].ssq = 0;
     }
 
-  struct casereader *r = casereader_clone (input);
-
   for (; (c = casereader_read (r)) != NULL; case_unref (c))
     {
       const double weight = dict_get_case_weight (dict, c, &warn);
@@ -166,7 +166,7 @@ ks_one_sample_execute (const struct dataset *ds,
        {
          const struct variable *var = ost->vars[v];
          const union value *val = case_data (c, var);
-      
+
          if (var_is_value_missing (var, val, exclude))
            continue;
 
@@ -182,6 +182,10 @@ ks_one_sample_execute (const struct dataset *ds,
 
   for (v = 0; v < ost->n_vars; ++v)
     {
+      const struct variable *var = ost->vars[v];
+      double cc = 0;
+      double prev_empirical = 0;
+
       switch (kst->dist)
        {
        case KS_UNIFORM:
@@ -211,16 +215,13 @@ ks_one_sample_execute (const struct dataset *ds,
        case KS_EXPONENTIAL:
          if (kst->p[0] != SYSMIS)
            ks[v].mu = ks[v].sigma = kst->p[0];
-         else 
+         else
            ks[v].mu = ks[v].sigma = ks[v].sum / ks[v].obs_cc;
          break;
        default:
          NOT_REACHED ();
        }
 
-      const struct variable *var = ost->vars[v];
-      double cc = 0;
-      double prev_empirical = 0;
       r = sort_execute_1var (casereader_clone (input), var);
       for (; (c = casereader_read (r)) != NULL; case_unref (c))
        {
@@ -235,19 +236,19 @@ ks_one_sample_execute (const struct dataset *ds,
          cc += weight;
 
          empirical = cc / ks[v].obs_cc;
-      
+
          theoretical = theoreticalf[kst->dist] (&ks[v], val->f);
-      
+
          d = empirical - theoretical;
          dp = prev_empirical - theoretical;
 
          if (d > 0)
-           maximize (&ks[v].diff_pos, d); 
+           maximize (&ks[v].diff_pos, d);
          else
            minimize (&ks[v].diff_neg, d);
 
          if (dp > 0)
-           maximize (&ks[v].diff_pos, dp); 
+           maximize (&ks[v].diff_pos, dp);
          else
            minimize (&ks[v].diff_neg, dp);
 
@@ -275,7 +276,7 @@ show_results (const struct ks *ks,
   const int nc = kst->parent.n_vars + column_headers;
   const int nr = 8 + row_headers;
   struct tab_table *table = tab_create (nc, nr);
-
+  tab_set_format (table, RC_WEIGHT, wfmt);
   tab_headers (table, row_headers, 0, column_headers, 0);
 
   tab_title (table, _("One-Sample Kolmogorov-Smirnov Test"));
@@ -296,7 +297,7 @@ show_results (const struct ks *ks,
     case KS_NORMAL:
       tab_text (table,  0, 2,
                TAT_TITLE | TAB_LEFT , _("Normal Parameters"));
-      
+
       tab_text (table,  1, 2,
                TAT_TITLE | TAB_LEFT , _("Mean"));
       tab_text (table,  1, 3,
@@ -305,7 +306,7 @@ show_results (const struct ks *ks,
     case KS_UNIFORM:
       tab_text (table,  0, 2,
                TAT_TITLE | TAB_LEFT , _("Uniform Parameters"));
-      
+
       tab_text (table,  1, 2,
                TAT_TITLE | TAB_LEFT , _("Minimum"));
       tab_text (table,  1, 3,
@@ -314,14 +315,14 @@ show_results (const struct ks *ks,
     case KS_POISSON:
       tab_text (table,  0, 2,
                TAT_TITLE | TAB_LEFT , _("Poisson Parameters"));
-      
+
       tab_text (table,  1, 2,
                TAT_TITLE | TAB_LEFT , _("Lambda"));
       break;
     case KS_EXPONENTIAL:
       tab_text (table,  0, 2,
                TAT_TITLE | TAB_LEFT , _("Exponential Parameters"));
-      
+
       tab_text (table,  1, 2,
                TAT_TITLE | TAB_LEFT , _("Scale"));
       break;
@@ -337,27 +338,27 @@ show_results (const struct ks *ks,
       double z = 0;
       const int col = 2 + i;
       tab_text (table, col, 0,
-               TAT_TITLE | TAB_CENTER , 
+               TAT_TITLE | TAB_CENTER ,
                var_to_string (kst->parent.vars[i]));
 
       switch (kst->dist)
        {
        case KS_UNIFORM:
-         tab_double (table, col, 1, 0, ks[i].obs_cc, wfmt);
-         tab_double (table, col, 2, 0, ks[i].test_min, NULL);
-         tab_double (table, col, 3, 0, ks[i].test_max, NULL);
+         tab_double (table, col, 1, 0, ks[i].obs_cc, NULL, RC_WEIGHT);
+         tab_double (table, col, 2, 0, ks[i].test_min, NULL, RC_OTHER);
+         tab_double (table, col, 3, 0, ks[i].test_max, NULL, RC_OTHER);
          break;
 
        case KS_NORMAL:
-         tab_double (table, col, 1, 0, ks[i].obs_cc, wfmt);
-         tab_double (table, col, 2, 0, ks[i].mu, NULL);
-         tab_double (table, col, 3, 0, ks[i].sigma, NULL);
+         tab_double (table, col, 1, 0, ks[i].obs_cc, NULL, RC_WEIGHT);
+         tab_double (table, col, 2, 0, ks[i].mu, NULL, RC_OTHER);
+         tab_double (table, col, 3, 0, ks[i].sigma, NULL, RC_OTHER);
          break;
 
        case KS_POISSON:
        case KS_EXPONENTIAL:
-         tab_double (table, col, 1, 0, ks[i].obs_cc, wfmt);
-         tab_double (table, col, 2, 0, ks[i].mu, NULL);
+         tab_double (table, col, 1, 0, ks[i].obs_cc, NULL, RC_WEIGHT);
+         tab_double (table, col, 2, 0, ks[i].mu, NULL, RC_OTHER);
          break;
 
        default:
@@ -369,13 +370,13 @@ show_results (const struct ks *ks,
 
       z = sqrt (ks[i].obs_cc) * abs;
 
-      tab_double (table, col, 5, 0, ks[i].diff_pos, NULL);
-      tab_double (table, col, 6, 0, ks[i].diff_neg, NULL);
+      tab_double (table, col, 5, 0, ks[i].diff_pos, NULL, RC_OTHER);
+      tab_double (table, col, 6, 0, ks[i].diff_neg, NULL, RC_OTHER);
 
-      tab_double (table, col, 4, 0, abs, NULL);
+      tab_double (table, col, 4, 0, abs, NULL, RC_OTHER);
 
-      tab_double (table, col, 7, 0, z, NULL);
-      tab_double (table, col, 8, 0, ks_asymp_sig (z), NULL);
+      tab_double (table, col, 7, 0, z, NULL, RC_OTHER);
+      tab_double (table, col, 8, 0, ks_asymp_sig (z), NULL, RC_PVALUE);
     }