TPCCLIB
Loading...
Searching...
No Matches
mean.c File Reference

Functions for calculating mean and stdev. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcstatist.h"

Go to the source code of this file.

Functions

int statMeanSD (double *data, unsigned int n, double *mean, double *sd, unsigned int *vn)
int fstatMeanSD (float *data, unsigned int n, float *mean, float *sd, unsigned int *vn)

Detailed Description

Functions for calculating mean and stdev.

Definition in file mean.c.

Function Documentation

◆ fstatMeanSD()

int fstatMeanSD ( float * data,
unsigned int n,
float * mean,
float * sd,
unsigned int * vn )

Calculate mean and sample standard deviation in an array of given float data. NaNs in the data are left out.

Author
Vesa Oikonen
See also
statMeanSD, floatMean, floatSum, floatMatch
Returns
0 if successful, otherwise <>0.
Parameters
dataPointer to data array of size n; data is not changed in any way.
nLength of data array.
meanPointer to variable where mean will be written; enter NULL if not needed.
sdPointer to variable where SD will be written; enter NULL if not needed.
vnPointer to variable where the number of valid (not NaN) samples will be written; enter NULL if not needed.

Definition at line 73 of file mean.c.

85 {
86 unsigned int i, m;
87 float sumsqr=0.0, sqrsum=0.0;
88
89 if(mean!=NULL) *mean=nanf("");
90 if(sd!=NULL) *sd=nanf("");
91 if(vn!=NULL) *vn=0;
92 if(n<1 || data==NULL) return(1);
93 for(i=m=0; i<n; i++) if(!isnan(data[i])) m++;
94 if(m<1) return(2);
95 if(vn!=NULL) *vn=m;
96
97 /* Calculate mean, and sum for possible SD calculation */
98 for(i=0; i<n; i++) if(!isnan(data[i])) sqrsum+=data[i];
99 if(mean!=NULL) *mean=sqrsum/(float)m;
100
101 /* Calculate SD, if required */
102 if(sd==NULL) return(0);
103 if(m==1) {*sd=0.0; return(0);}
104 sqrsum*=sqrsum;
105
106 for(i=0; i<n; i++) if(!isnan(data[i])) sumsqr+=data[i]*data[i];
107 float ff=sumsqr - sqrsum/(float)m; if(!(ff>0.0)) {*sd=0.0; return(0);}
108 *sd=sqrtf( ff/(float)(m-1) );
109
110 return(0);
111}

◆ statMeanSD()

int statMeanSD ( double * data,
unsigned int n,
double * mean,
double * sd,
unsigned int * vn )

Calculate mean and sample standard deviation in an array of given data. NaNs in the data are left out.

Author
Vesa Oikonen
See also
statMedian, doubleMean, doubleWMean, floatMean, fitLine, statSortDouble, doubleMatch, doubleRange, fstatMeanSD
Returns
0 if successful, otherwise <>0.
Parameters
dataPointer to data array of size n; data is not changed in any way.
nLength of data array.
meanPointer to variable where mean will be written; enter NULL if not needed.
sdPointer to variable where SD will be written; enter NULL if not needed.
vnPointer to variable where the number of valid (not NaN) samples will be written; enter NULL if not needed.

Definition at line 25 of file mean.c.

37 {
38 unsigned int i, m;
39 double sumsqr=0.0, sqrsum=0.0;
40
41 if(mean!=NULL) *mean=nan("");
42 if(sd!=NULL) *sd=nan("");
43 if(vn!=NULL) *vn=0;
44 if(n<1 || data==NULL) return(1);
45 for(i=m=0; i<n; i++) if(!isnan(data[i])) m++;
46 if(m<1) return(2);
47 if(vn!=NULL) *vn=m;
48
49 /* Calculate mean, and sum for possible SD calculation */
50 for(i=0; i<n; i++) if(!isnan(data[i])) sqrsum+=data[i];
51 if(mean!=NULL) *mean=sqrsum/(double)m;
52
53 /* Calculate SD, if required */
54 if(sd==NULL) return(0);
55 if(m==1) {*sd=0.0; return(0);}
56 sqrsum*=sqrsum;
57
58 for(i=0; i<n; i++) if(!isnan(data[i])) sumsqr+=data[i]*data[i];
59 double ff=sumsqr - sqrsum/(double)m; if(!(ff>0.0)) {*sd=0.0; return(0);}
60 *sd=sqrt( ff/(double)(m-1) );
61
62 return(0);
63}

Referenced by nloptIATGO(), nloptMPSOabsvMean(), and nloptMPSOpMean().