* 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 {
double n;
double mean;
class doStats : public eoPrintable
{
public: public:
Mean() : n(0), mean(0) {} doStats();
void update(double v) { virtual void printOn(std::ostream&) const;
n++;
double d = v - mean;
mean += 1/n * d;
}
double get_mean() const { return mean; } protected:
double _n;
}; };
class Var { class doMean : public doStats
double n; {
double mean;
double sumvar;
public: public:
Var() : n(0), mean(0), sumvar(0) {} doMean();
void update(double v) { virtual void update(double);
n++; virtual void printOn(std::ostream&) const;
double d = v - mean;
mean += 1/n * d;
sumvar += (n-1)/n * d * d;
}
double get_mean() const { return mean; } double get_mean() const;
double get_var() const { return sumvar / (n-1); }
double get_std() const { return sqrt(get_var()); } protected:
double _mean;
};
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;
double meanb;
double sumcov;
public: public:
Cov() : n(0), meana(0), meanb(0), sumcov(0) {} doCov();
void update(double a, double b) { virtual void update(double, double);
++n; virtual void printOn(std::ostream&) const;
double da = a - meana;
double db = b - meanb;
meana += 1/n * da; double get_meana() const;
meanb += 1/n * db; double get_meanb() const;
double get_cov() const;
sumcov += (n-1)/n * da * db; protected:
} double _meana;
double _meanb;
double get_meana() const { return meana; } double _sumcov;
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;
std::vector< std::vector<double> > sumcov;
public: public:
CovMatrix(unsigned dim) : n(0), mean(dim), sumcov(dim , std::vector<double>(dim)) {} doCovMatrix(unsigned dim);
void update(const std::vector<double>& v) { virtual void update(const std::vector<double>&);
assert(v.size() == mean.size());
n++; double get_mean(int) const;
double get_var(int) const;
double get_std(int) const;
double get_cov(int, int) const;
for (unsigned i = 0; i < v.size(); ++i) { protected:
double d = v[i] - mean[i]; std::vector< double > _mean;
mean[i] += 1/n * d; std::vector< std::vector< double > > _sumcov;
};
sumcov[i][i] += (n-1)/n * d * d; class doHyperVolume : public doStats
{
public:
doHyperVolume();
for (unsigned j = i; j < v.size(); ++j) { virtual void update(double);
double e = v[j] - mean[j]; // mean[j] is not updated yet virtual void printOn(std::ostream&) const;
double upd = (n-1)/n * d * e; double get_hypervolume() const;
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(){}
}