#include <moment.h> void ini_moment(m, mom, ndat) void accum_moment(m, x, w) void decr_moment(m, x, w) void reset_moment(m) real show_moment(m, mom) int n_moment(m) real sum_moment(m) real mean_moment(m) real median_moment(m) real sigma_moment(m) real skewness_moment(m) real kurtosis_moment(m) real min_moment(m); real max_moment(m); Moment *m; int mom, ndat; real x, w;
If only a datamin/max is needed, setting mom<0 can be used to prevent the more expensive moment calculations.
Moving or running averages (or moments) can be done by supplying ndat>0 to ini_moment. It will keep a memory of the last ndat data values and the moments now become running moments.
Note that the median_moment can only be used in x (the weights are ignored) and moving moment where ndat>0.
mean_moment returns the mean value, where sigma_moment returns the variance
real x[100], w[100];
int i,n=100;
Moment m;
...
ini_moment(&m,2,0); /* up to 2nd order moment, and using no circular
buffer */
for (i=0; i<n; i)
accum_moment(&m,x[i],w[i]);
printf("Mean: %g Dispersion: %g\n",
mean_moment(&m), sigma_moment(&m));
typedef struct {
int mom;
int n;
real *sum;
real datamin, datamax;
int ndat;
int idat;
real *dat;
real *sum;
} Moment;
from the standard include file moment.h.
http://apophenia.sourceforge.net/
~/src/kernel/misc moment.c
30-oct-93 Created PJT 8-nov-93 fixed init bug PJT 13-jun-95 added decr_moment PJT 2-feb-05 added moving moments PJT