00001
00002 #include <math.h>
00003
00004 #include "stddev.h"
00005
00006
00010 double stddev::calc_mean_( const double *v, const size_t N )
00011 {
00012 double mean = 0.0;
00013 double sum = 0.0;
00014
00015 for (size_t i = 0; i < N; i++) {
00016 sum = sum + v[i];
00017 }
00018 mean = sum / static_cast<double>(N);
00019 return mean;
00020 }
00021
00022
00026 double stddev::sd( const double *v,
00027 const size_t N,
00028 const double mean )
00029 {
00030 double stdDev = 0.0;
00031 mean_ = mean;
00032
00033
00034 double stdDevSum = 0;
00035 double x;
00036 for (size_t i = 0; i < N; i++) {
00037 x = v[i] - mean_;
00038 stdDevSum = stdDevSum + (x * x);
00039 }
00040 double variance = stdDevSum / static_cast<double>(N-1);
00041 stdDev = sqrt( variance );
00042 return stdDev;
00043 }
00044
00045
00050 double stddev::sd( const double *v, const size_t N )
00051 {
00052 double stdDev = 0.0;
00053 if (v != 0) {
00054 double mean = calc_mean_(v, N);
00055 stdDev = sd( v, N, mean );
00056 }
00057 return stdDev;
00058 }
00059