Changed all the licence notices in all the files.
[pspp-builds.git] / src / factor_stats.c
index 1b1de91adb4c4dab8e6ffa8d2cb7e9957af0d9a9..4508caf889c3c4eceed0756a048089ddb688043c 100644 (file)
@@ -15,8 +15,8 @@ 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., 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA. */
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA. */
 
 #include "factor_stats.h"
 #include "config.h"
@@ -25,33 +25,34 @@ 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>
 #include <float.h>
 #include <assert.h>
-
+#include <chart.h>
 
 
 void
-metrics_precalc(struct metrics *fs)
+metrics_precalc(struct metrics *m)
 {
-  assert (fs) ;
+  assert (m) ;
 
-  fs->n_missing = 0;
+  m->n_missing = 0;
 
-  fs->min = DBL_MAX;
-  fs->max = -DBL_MAX;
+  m->min = DBL_MAX;
+  m->max = -DBL_MAX;
 
+  m->histogram = 0;
 
-  fs->moments = moments1_create(MOMENT_KURTOSIS);
+  m->moments = moments1_create(MOMENT_KURTOSIS);
 
-  fs->ordered_data = hsh_create(20,
+  m->ordered_data = hsh_create(20,
                                (hsh_compare_func *) compare_values,
                                (hsh_hash_func *) hash_value,
                                (hsh_free_func *) weighted_value_free,
                                (void *) 0);
-
 }
 
 
@@ -124,7 +125,6 @@ metrics_postcalc(struct metrics *m)
   int i;
   int j = 1;  
 
-
   moments1_calculate (m->moments, &m->n, &m->mean, &m->var, 
                      &m->skewness, &m->kurtosis);
 
@@ -135,27 +135,32 @@ metrics_postcalc(struct metrics *m)
 
   /* FIXME: Check this is correct ???
      Shouldn't we use the sample variance ??? */
-  m->stderr = sqrt (m->var / m->n) ;
+  m->se_mean = sqrt (m->var / m->n) ;
 
 
 
   m->wvp = (struct weighted_value **) hsh_sort(m->ordered_data);
   m->n_data = hsh_count(m->ordered_data);
 
-  if ( m->n_data == 0 ) 
+  /* Trimmed mean calculation */
+  if ( m->n_data <= 1 ) 
     {
       m->trimmed_mean = m->mean;
       return;
     }
 
+  m->histogram = histogram_create(10, m->min, m->max);
 
-  /* Trimmed mean calculation */
+  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);
+    }
 
   tc = m->n * 0.05 ;
   k1 = -1;
   k2 = -1;
 
-
   for ( i = 0 ; i < m->n_data ; ++i ) 
     {
       cc += m->wvp[i]->w;
@@ -167,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 ) 
     {
@@ -178,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 ) 
     {
@@ -189,6 +207,7 @@ 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 ;
 
+
 }
 
 
@@ -207,7 +226,12 @@ weighted_value_create(void)
 void 
 weighted_value_free(struct weighted_value *wv)
 {
-  struct case_node *cn = wv->case_nos;
+  struct case_node *cn ;
+
+  if ( !wv ) 
+    return ;
+
+  cn = wv->case_nos;
 
   while(cn)
     {
@@ -237,15 +261,29 @@ create_factor_statistics (int n, union value *id0, union value *id1)
   f->id[0] = *id0;
   f->id[1] = *id1;
   f->m = xmalloc( sizeof ( struct metrics ) * n ) ;
+  memset (f->m, 0, sizeof(struct metrics) * n);
+  f->n_var = n;
 
   return f;
 }
 
 
+void 
+metrics_destroy(struct metrics *m)
+{
+  hsh_destroy(m->ordered_data);
+  hsh_destroy(m->ptile_hash);
+  if ( m-> histogram ) 
+    gsl_histogram_free(m->histogram);
+}
+
 void
 factor_statistics_free(struct factor_statistics *f)
 {
-  hsh_destroy(f->m->ordered_data);
+
+  int i; 
+  for ( i = 0 ; i < f->n_var; ++i ) 
+       metrics_destroy(&f->m[i]);
   free(f->m) ; 
   free(f);
 }