Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# stats.h

    00001 /*
    00002  *             Copyright (C) 2005 Maarten Keijzer
    00003  *
    00004  *          This program is free software; you can redistribute it and/or modify
    00005  *          it under the terms of version 2 of the GNU General Public License as
    00006  *          published by the Free Software Foundation.
    00007  *
    00008  *          This program is distributed in the hope that it will be useful,
    00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00011  *          GNU General Public License for more details.
    00012  *
    00013  *          You should have received a copy of the GNU General Public License
    00014  *          along with this program; if not, write to the Free Software
    00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00016  */
    00017
    00018 #include <vector>
    00019
    00020 class Mean {
    00021
    00022     double n;
    00023     double mean;
    00024
    00025     public:
    00026     Mean() : n(0), mean(0) {}
    00027
    00028     void update(double v) {
    00029         n++;
    00030         double d = v - mean;
    00031         mean += 1/n * d;
    00032     }
    00033
    00034     double get_mean() const { return mean; }
    00035 };
    00036
    00037 class Var {
    00038     double n;
    00039     double mean;
    00040     double sumvar;
    00041
    00042     public:
    00043     Var() : n(0), mean(0), sumvar(0) {}
    00044
    00045     void update(double v) {
    00046         n++;
    00047         double d = v - mean;
    00048         mean += 1/n * d;
    00049         sumvar += (n-1)/n * d * d;
    00050     }
    00051
    00052     double get_mean() const { return mean;           }
    00053     double get_var()  const { return sumvar / (n-1); }
    00054     double get_std()  const { return sqrt(get_var());  }
    00055 };
    00056
    00058 class Cov {
    00059     double n;
    00060     double meana;
    00061     double meanb;
    00062     double sumcov;
    00063
    00064     public:
    00065     Cov() : n(0), meana(0), meanb(0), sumcov(0) {}
    00066
    00067     void update(double a, double b) {
    00068         ++n;
    00069         double da = a - meana;
    00070         double db = b - meanb;
    00071
    00072         meana += 1/n * da;
    00073         meanb += 1/n * db;
    00074
    00075         sumcov += (n-1)/n * da * db;
    00076     }
    00077
    00078     double get_meana() const { return meana; }
    00079     double get_meanb() const { return meanb; }
    00080     double get_cov()   const { return sumcov / (n-1); }
    00081 };
    00082
    00083 class CovMatrix {
    00084     double n;
    00085     std::vector<double> mean;
    00086     std::vector< std::vector<double> > sumcov;
    00087
    00088     public:
    00089     CovMatrix(unsigned dim) : n(0), mean(dim), sumcov(dim , std::vector<double>(dim)) {}
    00090
    00091     void update(const std::vector<double>& v) {
    00092         n++;
    00093
    00094         for (unsigned i = 0; i < v.size(); ++i) {
    00095             double d = v[i] - mean[i];
    00096             mean[i] += 1/n * d;
    00097
    00098             sumcov[i][i] += (n-1)/n * d * d;
    00099
    00100             for (unsigned j = i; j < v.size(); ++j) {
    00101                 double e = v[j] - mean[j]; // mean[j] is not updated yet
    00102
    00103                 double upd = (n-1)/n * d * e;
    00104
    00105                 sumcov[i][j] += upd;
    00106                 sumcov[j][i] += upd;
    00107
    00108             }
    00109         }
    00110
    00111     }
    00112
    00113     double get_mean(int i) const { return mean[i]; }
    00114     double get_var(int i ) const { return sumcov[i][i] / (n-1); }
    00115     double get_std(int i) const  { return sqrt(get_var(i)); }
    00116     double get_cov(int i, int j) const { return sumcov[i][j] / (n-1); }
    00117
    00118 };
    00119