Added Boxplots to the EXAMINE subcommand repertoire
[pspp-builds.git] / src / factor_stats.c
index 2ba785a7d8e8c7044b2f42fa3ff2c94f96124d02..7c4cce000f928c63c7931469dab9396091d9d064 100644 (file)
@@ -25,6 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "algorithm.h"
 #include "alloc.h"
 #include "moments.h"
+#include "percentiles.h"
 
 #include <stdlib.h>
 #include <math.h>
@@ -51,9 +52,6 @@ metrics_precalc(struct metrics *m)
                                (hsh_hash_func *) hash_value,
                                (hsh_free_func *) weighted_value_free,
                                (void *) 0);
-
-
-
 }
 
 
@@ -144,20 +142,25 @@ metrics_postcalc(struct metrics *m)
   m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
   m->n_data = hsh_count(m->ordered_data);
 
-  if ( m->n_data == 0 ) 
+  m->histogram = histogram_create(10, m->min, m->max);
+
+  for ( i = 0 ; i < m->n_data ; ++i ) 
     {
-      m->trimmed_mean = m->mean;
-      return;
+      struct weighted_value **wv = (m->wvp) ;
+      gsl_histogram_accumulate(m->histogram, wv[i]->v.f, wv[i]->w);
     }
 
-
   /* Trimmed mean calculation */
+  if ( m->n_data <= 1 ) 
+    {
+      m->trimmed_mean = m->mean;
+      return;
+    }
 
   tc = m->n * 0.05 ;
   k1 = -1;
   k2 = -1;
 
-
   for ( i = 0 ; i < m->n_data ; ++i ) 
     {
       cc += m->wvp[i]->w;
@@ -169,9 +172,10 @@ metrics_postcalc(struct metrics *m)
       
       if ( cc < tc ) 
        k1 = i;
-
     }
 
+  
+
   k2 = m->n_data;
   for ( i = m->n_data -1  ; i >= 0; --i ) 
     {
@@ -180,6 +184,18 @@ metrics_postcalc(struct metrics *m)
     }
 
 
+  /* Calculate the percentiles */
+  ptiles(m->ptile_hash, m->wvp, m->n_data, m->n, m->ptile_alg);
+
+  tukey_hinges(m->wvp, m->n_data, m->n, m->hinge);
+
+  /* Special case here */
+  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 ) 
     {
@@ -191,14 +207,6 @@ metrics_postcalc(struct metrics *m)
   m->trimmed_mean += (m->wvp[k1 + 1]->cc - tc) * m->wvp[k1 + 1]->v.f ;
   m->trimmed_mean /= 0.9 * m->n ;
 
-  m->histogram = histogram_create(10, m->min, m->max);
-
-  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);
-    }
-
 }