* doStats * doEstimatorNormal: replaced use of Variance by CoMatrix

This commit is contained in:
Caner Candan 2010-07-13 13:20:07 +02:00
commit fc1adfcc78
4 changed files with 102 additions and 121 deletions

View file

@ -33,6 +33,7 @@
#include "doEstimator.h" #include "doEstimator.h"
#include "doModifierMass.h" #include "doModifierMass.h"
#include "doSampler.h" #include "doSampler.h"
#include "doStats.h"
using namespace boost::numeric::ublas; using namespace boost::numeric::ublas;
@ -106,6 +107,7 @@ public:
eoPop< EOT > selected_pop; eoPop< EOT > selected_pop;
//------------------------------------------------------------- //-------------------------------------------------------------
// Temporary solution used by plot to enumerate iterations in // Temporary solution used by plot to enumerate iterations in
// several files. // several files.
@ -158,19 +160,16 @@ public:
D distrib = _estimator(pop); D distrib = _estimator(pop);
double size = distrib.size(); double size = distrib.size();
assert(size > 0); assert(size > 0);
double vacc = 1; doHyperVolume hv;
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{ {
vacc *= sqrt(distrib.variance()[i]); hv.update( distrib.variance()[i] );
} }
assert(vacc <= std::numeric_limits<double>::max()); ofs_params_var << hv << std::endl;
ofs_params_var << vacc << std::endl;
} }
do do
@ -297,9 +296,8 @@ public:
// dicreasement // dicreasement
//------------------------------------------------------------- //-------------------------------------------------------------
{ // {
// double size = distrib.size(); // double size = distrib.size();
// assert(size > 0); // assert(size > 0);
// vector< double > vmin(size); // vector< double > vmin(size);
@ -310,17 +308,15 @@ public:
// vector< double > vrange = vmax - vmin; // vector< double > vrange = vmax - vmin;
// double vacc = 1; // doHyperVolume hv;
// for (int i = 0, size = vrange.size(); i < size; ++i) // for (int i = 0, size = vrange.size(); i < size; ++i)
// { // {
// vacc *= vrange(i); // hv.update( vrange(i) );
// } // }
// assert(vacc <= std::numeric_limits<double>::max()); // ofs_params << hv << std::endl;
// }
// ofs_params << vacc << std::endl;
}
//------------------------------------------------------------- //-------------------------------------------------------------
@ -332,19 +328,16 @@ public:
{ {
double size = distrib.size(); double size = distrib.size();
assert(size > 0); assert(size > 0);
double vacc = 1; doHyperVolume hv;
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{ {
vacc *= sqrt(distrib.variance()[i]); hv.update( distrib.variance()[i] );
} }
assert(vacc <= std::numeric_limits<double>::max()); ofs_params_var << hv << std::endl;
ofs_params_var << vacc << std::endl;
} }
//------------------------------------------------------------- //-------------------------------------------------------------

View file

@ -19,14 +19,16 @@ public:
unsigned int dimsize = pop[0].size(); unsigned int dimsize = pop[0].size();
assert(dimsize > 0); assert(dimsize > 0);
std::vector< Var > var(dimsize); //std::vector< doVar > var(dimsize);
doCovMatrix cov(dimsize);
for (unsigned int i = 0; i < popsize; ++i) for (unsigned int i = 0; i < popsize; ++i)
{ {
for (unsigned int d = 0; d < dimsize; ++d) cov.update(pop[i]);
{ // for (unsigned int d = 0; d < dimsize; ++d)
var[d].update(pop[i][d]); // {
} // var[d].update(pop[i][d]);
// }
} }
EOT mean(dimsize); EOT mean(dimsize);
@ -34,9 +36,12 @@ public:
for (unsigned int d = 0; d < dimsize; ++d) for (unsigned int d = 0; d < dimsize; ++d)
{ {
mean[d] = var[d].get_mean(); // mean[d] = var[d].get_mean();
variance[d] = var[d].get_var(); // variance[d] = var[d].get_var();
//variance[d] = var[d].get_std(); // perhaps I should use this !?! //variance[d] = var[d].get_std(); // perhaps I should use this !?!
mean[d] = cov.get_mean(d);
variance[d] = cov.get_var(d);
} }
return doNormal< EOT >(mean, variance); return doNormal< EOT >(mean, variance);

View file

@ -18,109 +18,96 @@
#ifndef _doStats_h #ifndef _doStats_h
#define _doStats_h #define _doStats_h
#include <cassert> #include <eoPrintable.h>
#include <vector>
class Mean { class doStats : public eoPrintable
{
public:
doStats();
double n; virtual void printOn(std::ostream&) const;
double mean;
public: protected:
Mean() : n(0), mean(0) {} double _n;
void update(double v) {
n++;
double d = v - mean;
mean += 1/n * d;
}
double get_mean() const { return mean; }
}; };
class Var { class doMean : public doStats
double n; {
double mean; public:
double sumvar; doMean();
public: virtual void update(double);
Var() : n(0), mean(0), sumvar(0) {} virtual void printOn(std::ostream&) const;
void update(double v) { double get_mean() const;
n++;
double d = v - mean;
mean += 1/n * d;
sumvar += (n-1)/n * d * d;
}
double get_mean() const { return mean; } protected:
double get_var() const { return sumvar / (n-1); } double _mean;
double get_std() const { return sqrt(get_var()); } };
class doVar : public doMean
{
public:
doVar();
virtual void update(double);
virtual void printOn(std::ostream&) const;
double get_var() const;
double get_std() const;
protected:
double _sumvar;
}; };
/** Single covariance between two variates */ /** Single covariance between two variates */
class Cov { class doCov : public doStats
double n; {
double meana; public:
double meanb; doCov();
double sumcov;
public: virtual void update(double, double);
Cov() : n(0), meana(0), meanb(0), sumcov(0) {} virtual void printOn(std::ostream&) const;
void update(double a, double b) { double get_meana() const;
++n; double get_meanb() const;
double da = a - meana; double get_cov() const;
double db = b - meanb;
meana += 1/n * da; protected:
meanb += 1/n * db; double _meana;
double _meanb;
sumcov += (n-1)/n * da * db; double _sumcov;
}
double get_meana() const { return meana; }
double get_meanb() const { return meanb; }
double get_cov() const { return sumcov / (n-1); }
}; };
class CovMatrix { class doCovMatrix : public doStats
double n; {
std::vector<double> mean; public:
std::vector< std::vector<double> > sumcov; doCovMatrix(unsigned dim);
public: virtual void update(const std::vector<double>&);
CovMatrix(unsigned dim) : n(0), mean(dim), sumcov(dim , std::vector<double>(dim)) {}
void update(const std::vector<double>& v) { double get_mean(int) const;
assert(v.size() == mean.size()); double get_var(int) const;
double get_std(int) const;
double get_cov(int, int) const;
n++; protected:
std::vector< double > _mean;
std::vector< std::vector< double > > _sumcov;
};
for (unsigned i = 0; i < v.size(); ++i) { class doHyperVolume : public doStats
double d = v[i] - mean[i]; {
mean[i] += 1/n * d; public:
doHyperVolume();
sumcov[i][i] += (n-1)/n * d * d; virtual void update(double);
virtual void printOn(std::ostream&) const;
for (unsigned j = i; j < v.size(); ++j) { double get_hypervolume() const;
double e = v[j] - mean[j]; // mean[j] is not updated yet
double upd = (n-1)/n * d * e;
sumcov[i][j] += upd;
sumcov[j][i] += upd;
}
}
}
double get_mean(int i) const { return mean[i]; }
double get_var(int i ) const { return sumcov[i][i] / (n-1); }
double get_std(int i) const { return sqrt(get_var(i)); }
double get_cov(int i, int j) const { return sumcov[i][j] / (n-1); }
protected:
double _hv;
}; };
#endif // !_doStats_h #endif // !_doStats_h

View file

@ -1,4 +0,0 @@
namespace DO
{
void test(){}
}