| 1 |
/* @(#)moment.c 19.1 (ES0-DMD) 02/25/03 13:43:13 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/* @(#)moment.c 19.1 (ESO) 02/25/03 13:43:13 */ |
| 30 |
|
| 31 |
#include <math.h> |
| 32 |
|
| 33 |
void moment(data,n,ave,adev,sdev,svar,skew,curt) |
| 34 |
int n; |
| 35 |
double *ave,*adev,*sdev,*svar,*skew,*curt; |
| 36 |
float data[]; |
| 37 |
{ |
| 38 |
int j; |
| 39 |
double s,p; |
| 40 |
void nrerror(); |
| 41 |
|
| 42 |
if (n <= 1) nrerror("n must be at least 2 in MOMENT"); |
| 43 |
s=0.0; |
| 44 |
for (j=1;j<=n;j++) s += (double)data[j]; |
| 45 |
*ave=s/n; |
| 46 |
*adev=(*svar)=(*skew)=(*curt)=0.0; |
| 47 |
for (j=1;j<=n;j++) { |
| 48 |
*adev += fabs(s=(double)data[j]-(*ave)); |
| 49 |
*svar += (p=s*s); |
| 50 |
*skew += (p *= s); |
| 51 |
*curt += (p *= s); |
| 52 |
} |
| 53 |
*adev /= n; |
| 54 |
*svar /= (n-1); |
| 55 |
*sdev=sqrt(*svar); |
| 56 |
if (*svar) { |
| 57 |
*skew /= (n*(*svar)*(*sdev)); |
| 58 |
*curt=(*curt)/(n*(*svar)*(*svar))-3.0; |
| 59 |
} else nrerror("No skew/kurtosis when variance = 0 (in MOMENT)"); |
| 60 |
} |