Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / math / factor-stats.c
index fde6abe1d99e01c3c6b5af19fdc9322256466260..a97d7f09349b7fabe25c49383357f87103205ac7 100644 (file)
@@ -1,29 +1,25 @@
-/* PSPP - A program for statistical analysis . -*-c-*-
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2004 Free Software Foundation, Inc.
 
-Copyright (C) 2004 Free Software Foundation, Inc.
-Author: John Darrington 2004
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-This program is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301, USA. */
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 #include "factor-stats.h"
+#include <data/val-type.h>
 #include <data/value.h>
 #include <libpspp/hash.h>
 #include <libpspp/array.h>
-#include <libpspp/alloc.h>
 #include "moments.h"
 #include "percentiles.h"
 
@@ -33,6 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 #include <assert.h>
 #include "histogram.h"
 
+#include "xalloc.h"
 
 void
 metrics_precalc(struct metrics *m)
@@ -60,13 +57,13 @@ metrics_precalc(struct metrics *m)
    If val is null, then treat it as MISSING
 */
 void
-metrics_calc(struct metrics *fs, const union value *val, 
-            double weight, int case_no)
+metrics_calc (struct metrics *fs, const union value *val,
+             double weight, int case_no)
 {
   struct weighted_value **wv;
   double x;
-  
-  if ( ! val ) 
+
+  if ( ! val )
     {
       fs->n_missing += weight;
       return ;
@@ -76,22 +73,20 @@ metrics_calc(struct metrics *fs, const union value *val,
 
   moments1_add(fs->moments, x, weight);
 
-
   if ( x < fs->min) fs->min = x;
   if ( x > fs->max) fs->max = x;
 
-
   wv = (struct weighted_value **) hsh_probe (fs->ordered_data,(void *) val );
 
-  if ( *wv  ) 
+  if ( *wv  )
     {
-      /* If this value has already been seen, then simply 
+      /* If this value has already been seen, then simply
         increase its weight  and push a new case number */
 
       struct case_node *cn;
 
       assert( (*wv)->v.f == val->f );
-      (*wv)->w += weight;      
+      (*wv)->w += weight;
 
       cn = xmalloc ( sizeof *cn);
       cn->next = (*wv)->case_nos ;
@@ -106,7 +101,7 @@ metrics_calc(struct metrics *fs, const union value *val,
       *wv = weighted_value_create();
       (*wv)->v = *val;
       (*wv)->w = weight;
-      
+
       cn = xmalloc (sizeof *cn);
       cn->next=0;
       cn->num = case_no;
@@ -123,9 +118,9 @@ metrics_postcalc(struct metrics *m)
   double tc ;
   int k1, k2 ;
   int i;
-  int j = 1;  
+  int j = 1;
 
-  moments1_calculate (m->moments, &m->n, &m->mean, &m->var, 
+  moments1_calculate (m->moments, &m->n, &m->mean, &m->var,
                      &m->skewness, &m->kurtosis);
 
   moments1_destroy (m->moments);
@@ -143,7 +138,7 @@ metrics_postcalc(struct metrics *m)
   m->n_data = hsh_count(m->ordered_data);
 
   /* Trimmed mean calculation */
-  if ( m->n_data <= 1 ) 
+  if ( m->n_data <= 1 )
     {
       m->trimmed_mean = m->mean;
       return;
@@ -151,7 +146,7 @@ metrics_postcalc(struct metrics *m)
 
   m->histogram = histogram_create(10, m->min, m->max);
 
-  for ( i = 0 ; i < m->n_data ; ++i ) 
+  for ( i = 0 ; i < m->n_data ; ++i )
     {
       struct weighted_value **wv = (m->wvp) ;
       gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
@@ -161,25 +156,25 @@ metrics_postcalc(struct metrics *m)
   k1 = -1;
   k2 = -1;
 
-  for ( i = 0 ; i < m->n_data ; ++i ) 
+  for ( i = 0 ; i < m->n_data ; ++i )
     {
       cc += m->wvp[i]->w;
       m->wvp[i]->cc = cc;
 
       m->wvp[i]->rank = j + (m->wvp[i]->w - 1) / 2.0 ;
-      
+
       j += m->wvp[i]->w;
-      
-      if ( cc < tc ) 
+
+      if ( cc < tc )
        k1 = i;
     }
 
-  
+
 
   k2 = m->n_data;
-  for ( i = m->n_data -1  ; i >= 0; --i ) 
+  for ( i = m->n_data -1  ; i >= 0; --i )
     {
-      if ( tc > m->n - m->wvp[i]->cc) 
+      if ( tc > m->n - m->wvp[i]->cc)
        k2 = i;
     }
 
@@ -192,14 +187,14 @@ metrics_postcalc(struct metrics *m)
                 m->n_data, m->n, m->hinge);
 
   /* Special case here */
-  if ( k1 + 1 == k2 ) 
+  if ( k1 + 1 == k2 )
     {
       m->trimmed_mean = m->wvp[k2]->v.f;
       return;
     }
 
   m->trimmed_mean = 0;
-  for ( i = k1 + 2 ; i <= k2 - 1 ; ++i ) 
+  for ( i = k1 + 2 ; i <= k2 - 1 ; ++i )
     {
       m->trimmed_mean += m->wvp[i]->v.f * m->wvp[i]->w;
     }
@@ -225,12 +220,12 @@ weighted_value_create(void)
   return wv;
 }
 
-void 
+void
 weighted_value_free(struct weighted_value *wv)
 {
   struct case_node *cn ;
 
-  if ( !wv ) 
+  if ( !wv )
     return ;
 
   cn = wv->case_nos;
@@ -238,7 +233,7 @@ weighted_value_free(struct weighted_value *wv)
   while(cn)
     {
       struct case_node *next = cn->next;
-      
+
       free(cn);
       cn = next;
     }
@@ -252,16 +247,18 @@ weighted_value_free(struct weighted_value *wv)
 
 
 /* Create a factor statistics object with for N dependent vars
-   and ID as the value of the independent variable */
-struct factor_statistics * 
-create_factor_statistics (int n, union value *id0, union value *id1)
+   and ID0 and ID1 as the values of the independent variable */
+struct factor_statistics *
+create_factor_statistics (int n,
+                         union value *id0,
+                         union value *id1)
 {
   struct factor_statistics *f;
 
   f = xmalloc (sizeof *f);
 
-  f->id[0] = *id0;
-  f->id[1] = *id1;
+  f->id[0] = id0;
+  f->id[1] = id1;
   f->m = xnmalloc (n, sizeof *f->m);
   memset (f->m, 0, sizeof(struct metrics) * n);
   f->n_var = n;
@@ -269,13 +266,12 @@ create_factor_statistics (int n, union value *id0, union value *id1)
   return f;
 }
 
-
-void 
+void
 metrics_destroy(struct metrics *m)
 {
   hsh_destroy(m->ordered_data);
   hsh_destroy(m->ptile_hash);
-  if ( m-> histogram ) 
+  if ( m-> histogram )
     gsl_histogram_free(m->histogram);
 }
 
@@ -283,17 +279,17 @@ void
 factor_statistics_free(struct factor_statistics *f)
 {
 
-  int i; 
-  for ( i = 0 ; i < f->n_var; ++i ) 
+  int i;
+  free (f->id[0]);
+  free (f->id[1]);
+  for ( i = 0 ; i < f->n_var; ++i )
        metrics_destroy(&f->m[i]);
-  free(f->m) ; 
+  free(f->m) ;
   free(f);
 }
 
 
-
-
-int 
+int
 factor_statistics_compare(const struct factor_statistics *f0,
                          const struct factor_statistics *f1, int width)
 {
@@ -303,33 +299,30 @@ factor_statistics_compare(const struct factor_statistics *f0,
   assert(f0);
   assert(f1);
 
-  cmp0 = compare_values(&f0->id[0], &f1->id[0], width);
+  cmp0 = compare_values(f0->id[0], f1->id[0], width);
 
-  if ( cmp0 != 0 ) 
+  if ( cmp0 != 0 )
     return cmp0;
 
 
-  if ( ( f0->id[1].f == SYSMIS )  && (f1->id[1].f != SYSMIS) ) 
+  if ( ( f0->id[1]->f == SYSMIS ) && (f1->id[1]->f != SYSMIS) )
     return 1;
 
-  if ( ( f0->id[1].f != SYSMIS )  && (f1->id[1].f == SYSMIS) ) 
+  if ( ( f0->id[1]->f != SYSMIS )  && (f1->id[1]->f == SYSMIS) )
     return -1;
 
-  return compare_values(&f0->id[1], &f1->id[1], width);
-  
+  return compare_values (f0->id[1], f1->id[1], width);
 }
 
-unsigned int 
-factor_statistics_hash(const struct factor_statistics *f, int width)
+unsigned int
+factor_statistics_hash (const struct factor_statistics *f, int width)
 {
-  
   unsigned int h;
 
-  h = hash_value(&f->id[0], width);
-  
-  if ( f->id[1].f != SYSMIS )
-    h += hash_value(&f->id[1], width);
+  h = hash_value (f->id[0], width);
+
+  if ( f->id[1]->f != SYSMIS )
+    h += hash_value(f->id[1], width);
 
   return h;
 }
-