#include "file-handle.h"
#include "lexer.h"
#include "misc.h"
+#include "moments.h"
#include "pool.h"
#include "settings.h"
#include "sfm.h"
#include "sort.h"
-#include "stats.h"
#include "str.h"
#include "var.h"
#include "vfm.h"
int int1, int2;
char *string;
int missing;
+ struct moments *moments;
};
/* Aggregation functions. */
agr->vars = v;
tail = v;
tail->next = NULL;
+ v->moments = NULL;
/* Create the target variable in the aggregate
dictionary. */
free (iter->arg[i].c);
free (iter->string);
}
+ else if (iter->function == SD)
+ moments_destroy (iter->moments);
free (iter);
}
free (agr->prev_break);
iter->dbl[0] += v->f * weight;
iter->dbl[1] += weight;
break;
- case SD:
- {
- double product = v->f * weight;
- iter->dbl[0] += product;
- iter->dbl[1] += product * v->f;
- iter->dbl[2] += weight;
- break;
- }
+ case SD:
+ moments_pass_two (iter->moments, v->f, weight);
+ break;
case MAX:
iter->dbl[0] = max (iter->dbl[0], v->f);
iter->int1 = 1;
v->f = i->dbl[1] != 0.0 ? i->dbl[0] / i->dbl[1] : SYSMIS;
break;
case SD:
- v->f = ((i->dbl[2] > 1.0)
- ? calc_stddev (calc_variance (i->dbl, i->dbl[2]))
- : SYSMIS);
+ {
+ double variance;
+
+ /* FIXME: we should use two passes. */
+ moments_calculate (i->moments, NULL, NULL, &variance,
+ NULL, NULL);
+ if (variance != SYSMIS)
+ v->f = sqrt (variance);
+ else
+ v->f = SYSMIS;
+ }
break;
case MAX:
case MIN:
case MAX | FSTRING:
memset (iter->string, 0, iter->src->width);
break;
+ case SD:
+ if (iter->moments == NULL)
+ iter->moments = moments_create (MOMENT_VARIANCE);
+ else
+ moments_clear (iter->moments);
+ break;
default:
iter->dbl[0] = iter->dbl[1] = iter->dbl[2] = 0.0;
iter->int1 = iter->int2 = 0;