contrib/mul/pdf1d/pdf1d_calc_mean_var.cxx
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_calc_mean_var.cxx
00002 #include "pdf1d_calc_mean_var.h"
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Calculate mean and variance of 1D data.
00007 
00008 void pdf1d_calc_mean_var(double& mean, double& var,
00009                          const vnl_vector<double>& d)
00010 {
00011   pdf1d_calc_mean_var(mean,var,d.data_block(),d.size());
00012 }
00013 void pdf1d_calc_mean_var(double& mean, double& var,
00014                          const double* d, int n)
00015 {
00016   double sum=0;
00017   double sum2 = 0;
00018   for (int i=0;i<n;++i)
00019   {
00020     sum+=d[i];
00021     sum2+=d[i]*d[i];
00022   }
00023 
00024   mean = sum/n;
00025   var  = (sum2 - n*mean*mean)/(n-1);
00026 }