New function covariance_from_correlation
[pspp] / src / math / levene.c
index fe04d294e1acfbf214dadea6a53717ccc9a37646..0175ceb70dae6c33f1ea1d64d7f0252b3295d8e0 100644 (file)
@@ -22,6 +22,7 @@
 #include "libpspp/misc.h"
 #include "libpspp/hmap.h"
 #include "data/value.h"
+#include "data/val-type.h"
 
 #include <gl/xalloc.h>
 #include <assert.h>
@@ -44,7 +45,7 @@ struct levene
   /* Width of the categorical variable */
   int gvw ;
 
-  /* The value deviding the the groups. Valid only for dichotomous categorical variable.*/
+  /* The value dividing the groups. Valid only for dichotomous categorical variable.*/
   const union value *cutpoint;
 
 
@@ -133,12 +134,12 @@ levene_create (int indep_width, const union value *cutpoint)
 
 
 /* Data accumulation. First pass */
-void 
+void
 levene_pass_one (struct levene *nl, double value, double weight, const union value *gv)
 {
   struct lev *lev = find_group (nl, gv);
 
-  if ( nl->pass == 0 ) 
+  if ( nl->pass == 0 )
     {
       nl->pass = 1;
     }
@@ -159,7 +160,7 @@ levene_pass_one (struct levene *nl, double value, double weight, const union val
 }
 
 /* Data accumulation. Second pass */
-void 
+void
 levene_pass_two (struct levene *nl, double value, double weight, const union value *gv)
 {
   struct lev *lev = NULL;
@@ -185,7 +186,7 @@ levene_pass_two (struct levene *nl, double value, double weight, const union val
 }
 
 /* Data accumulation. Third pass */
-void 
+void
 levene_pass_three (struct levene *nl, double value, double weight, const union value *gv)
 {
   double z;
@@ -223,12 +224,16 @@ levene_calculate (struct levene *nl)
 
   double numerator = 0.0;
   double nn = 0.0;
-  if ( nl->pass == 3 )
-    {
-      nl->pass = 4;
-    }
 
-  assert (nl->pass == 4);
+  /* The Levene calculation requires three passes.
+     Normally this should have been done prior to calling this function.
+     However, in abnormal circumstances (eg. the dataset is empty) there
+     will have been no passes.
+   */
+  assert (nl->pass == 0 || nl->pass == 3);
+
+  if ( nl->pass == 0 )
+    return SYSMIS;
 
   nl->denominator *= hmap_count (&nl->hmap) - 1;
 
@@ -239,7 +244,7 @@ levene_calculate (struct levene *nl)
     }
 
   numerator *= nn - hmap_count (&nl->hmap);
-    
+
   return numerator / nl->denominator;
 }