Changeset 197


Ignore:
Timestamp:
Oct 27, 2004, 9:04:22 PM (18 years ago)
Author:
Jari Häkkinen
Message:

Cleanup interfaces for Averager classes and move stuff into statsitics
namespace.

Location:
trunk/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Averager.cc

    r137 r197  
    99
    1010namespace theplu {
    11 namespace cpptools{
     11namespace statistics{
    1212 
    1313
     
    1717  }
    1818
    19   Averager::Averager(const double x)
    20     : n_(1), x_(x), xx_(x*x)
    21   {
    22   }
    23 
    2419  Averager::Averager(const double x,const double xx,const long n)
    2520    : n_(n), x_(x), xx_(xx)
    26   {
    27   }
    28 
    29   Averager::Averager(const gslapi::vector& vec)
    30     :n_(vec.size()), x_(vec.sum()), xx_(vec*vec)
    3121  {
    3222  }
     
    4535  }
    4636
    47   const Averager& Averager::operator*=(const Averager& N)
    48   {
    49     long    Rn=10;
    50     double  NM=N.mean();
    51     double  M=mean();
    52     double  x=M*NM;
    53     double  var=(variance()*NM*NM+N.variance()*M*M);
    54 
    55     *this=Averager(Rn*x ,var*Rn*(Rn-1)+x*x*Rn, Rn);
    56     return *this;
    57   }
    58 
    59   const Averager& Averager::operator/=(const Averager& N)
    60   {
    61     long    Rn=10;
    62     double  NM=N.mean();
    63    
    64     if(NM) {
    65       double x=mean()/NM;
    66       double var=(variance()+N.variance()*x*x)/(NM*NM);
    67       *this=Averager(Rn*x ,var*Rn*(Rn-1)+x*x*Rn, Rn);
    68     }
    69     else
    70       reset();
    71    
    72     return *this;
    73   }
    74  
    75   Averager operator*(double d,const Averager& a) {
    76     Averager tmp=a;
    77     return tmp*=d;
    78   }
    79 
    80   std::ostream& theplu::cpptools::operator<<(std::ostream& o, const Averager& a)
    81   {
    82     return o << a.mean() << ' ' << a.standard_error();
    83   }
    84 
    85 }} // of namespace cpptools and namespace theplu
     37}} // of namespace statistics and namespace theplu
  • trunk/src/Averager.h

    r194 r197  
    88
    99namespace theplu{
    10 namespace cpptools{
     10namespace statistics{
    1111  class ostream;
    1212
     
    1414  /// Class to calulate simple (first and second moments) averages.
    1515  ///
    16   /// @see WeighetdAverager
     16  /// @see WeightedAverager
    1717  ///
    1818  class Averager
     
    2626   
    2727    ///
    28     /// Constructor taking the value of first data point.
     28    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
     29    /// number of samples \a n.
    2930    ///
    30     Averager(const double);
    31    
    32     ///
    33     /// Constructor taking sum of x, sum of squared x and number of
    34     /// samples.
    35     ///
    36     Averager(const double, const double, const long);
     31    Averager(const double x, const double xx, const long n);
    3732
    38     ///
    39     /// Constructor taking gslapi::vector.
    40     ///
    41     Averager(const gslapi::vector&);
    42    
    4333    ///
    4434    /// Copy constructor
     
    4737   
    4838    ///
    49     /// adding n(default=1) number of data point(s) with value d.
     39    /// Adding \a n (default=1) number of data point(s) with value \a d.
    5040    ///
    5141    inline void add(const double d,const long n=1)
     
    5343
    5444    ///
    55     /// @return average
    56     ///
    57     inline double average(void) const {return mean();}
    58 
    59     ///
    60     /// @return sum of squared values \f$ \sum x_i^2 \f$
    61     ///
    62     inline double average_sqr(void) const {return mean_sqr();}
    63  
    64     ///
    65     /// @return average \f$ \frac{\sum x_i}{n} \f$
     45    /// @return Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
    6646    ///
    6747    inline double mean(void) const { return n_ ? x_/n_ : 0; }
    6848 
    6949    ///
    70     /// @return @return sum of squared values \f$ \sum x_i^2 \f$.
     50    /// @return Mean of squared values \f$ \frac{1}{n}\sum x_i^2 \f$.
    7151    ///
    7252    inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; }
    7353
    7454    ///
    75     /// @return number of data points
     55    /// @return Number of data points
    7656    ///
    7757    inline long n(void) const { return n_; }
    7858
    7959    ///
    80     /// resets everything to zero
     60    /// Rescales the object, \f$ \forall x_i \rightarrow a*x_i\f$, \f$
     61    /// \forall x_i^2 \rightarrow a^2*x_i^2 \f$
     62    ///
     63    inline void rescale(double a) { x_*=a; xx_*=a*a; }
     64
     65    ///
     66    /// Resets everything to zero
    8167    ///
    8268    inline void reset(void) { n_=0; x_=xx_=0.0;}
     
    8470    ///
    8571    /// The standard deviation is defined as the square root of the
    86     /// variance. @return standard deviation
     72    /// variance.
    8773    ///
    88     inline double standard_deviation(void) const {  return sqrt(variance()); }
     74    /// @return The standard deviation, root of the variance().
     75    ///
     76    inline double std(void) const { return sqrt(variance()); }
    8977
    9078    ///
    91     /// The standard deviation is defined as the square root of the
    92     /// variance. @return standard deviation
    93     ///
    94     inline double std(void) const {  return sqrt(variance()); }
    95 
    96     ///
    97     /// @return standard error, i.e. standard deviation of the mean
     79    /// @return Standard error, i.e. standard deviation of the mean
     80    /// \f$ \sqrt{variance()/n} \f$
    9881    ///
    9982    inline double standard_error(void) const { return sqrt(variance()/n_); }
    10083
    10184    ///
    102     /// @return the sum of x
     85    /// @return The sum of x
    10386    ///
    10487    inline double sum_x(void) const { return x_; }
    10588
    10689    ///
    107     /// @return the sum of squares
     90    /// @return The sum of squares
    10891    ///
    10992    inline double sum_xsqr(void) const { return xx_; }
    11093
    11194    ///
    112     ///The variance is calculated using the (n-1) correction, which
    113     ///means it is the best unbiased estimator of the variance \f$
    114     ///\frac{1}{N-1}\sum_i (x_i-m)^2\f$, where \f$m\f$ is the
    115     ///mean@return variance
     95    /// The variance is calculated using the \f$ (n-1) \f$ correction,
     96    /// which means it is the best unbiased estimator of the variance
     97    /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2\f$, where \f$m\f$ is the
     98    /// mean.
     99    ///
     100    /// @return The variance
    116101    ///
    117102    inline double variance(void) const
     
    119104
    120105    ///
    121     /// equality operator
     106    /// The assignment operator
    122107    ///
    123     inline Averager operator=(const Averager& a)
    124     { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; }
     108    inline const Averager& operator=(const Averager& a)
     109      { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; }
    125110
    126111    ///
    127     /// operator to add a double to the object
    128     ///
    129     inline Averager operator+=(double d)
    130     { add(d); return *this; }
    131 
    132     ///
    133     /// operator to add a "one"
    134     ///
    135     inline Averager operator++(void)
    136     { add(1); return *this; }
    137 
    138     ///
    139     /// Operator to rescale the object
    140     ///
    141     inline Averager operator*=(double d)
    142     { x_*=d; xx_*=d*d; return *this; }
    143 
    144     ///
    145     /// Operator to rescale the object
    146     ///
    147     inline Averager operator/=(double d)
    148     { return (*this)*=(1/d); }
    149 
    150     ///
    151     /// operator to add another Averager
     112    /// Operator to add another Averager
    152113    ///
    153114    const Averager& operator+=(const Averager&);
    154 
    155     ///
    156     /// operator to multiply with an Averager. See code for details.
    157     ///
    158     const Averager& operator*=(const Averager&);
    159 
    160     ///
    161     /// operator to multiply with an Averager. See code for details.
    162     ///
    163     const Averager& operator/=(const Averager&);
    164115
    165116  private:
     
    168119  };
    169120
    170   Averager operator*(double,const Averager&);
    171  
    172   ///
    173   /// The output operator
    174   ///
    175   std::ostream& operator<<( std::ostream&, const Averager&);
    176 
    177 
    178 }} // of namespace cpptools and namespace theplu
     121}} // of namespace statistics and namespace theplu
    179122
    180123#endif
  • trunk/src/AveragerPair.cc

    r138 r197  
    1010
    1111namespace theplu {
    12 namespace cpptools{
     12namespace statistics{
    1313 
    1414
     
    1818  }
    1919
    20   AveragerPair::AveragerPair(const std::pair<double,double> z)
    21     : x_(Averager(z.first)), y_(Averager(z.second)), xy_(z.first*z.second)
    22   {
    23   }
    24 
    25   AveragerPair::AveragerPair(const double x, const double y)
    26     : x_(Averager(x)), y_(Averager(y)), xy_(x*y)
    27   {
    28   }
    29 
    3020  AveragerPair::AveragerPair(const double x, const double xx, const double y,
    3121                     const double yy, const double xy, const long n)
    3222    : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy)
    33   {
    34   }
    35 
    36   AveragerPair::AveragerPair(const gslapi::vector& x, const gslapi::vector& y )
    37     :x_(Averager(x)), y_(Averager(y)), xy_(x*y)
    3823  {
    3924  }
     
    5237  }
    5338
    54 }} // of namespace cpptools and namespace theplu
     39}} // of namespace statistics and namespace theplu
  • trunk/src/AveragerPair.h

    r191 r197  
    1313  class gslapi::vector;
    1414
    15 namespace cpptools{
     15namespace statistics{
    1616  ///
    1717  /// Class for taking care of mean and covariance of two variables.
     
    2727   
    2828    ///
    29     /// Constructor taking first pair of values.
    30     ///
    31     AveragerPair(const std::pair<double, double>);
    32    
    33     ///
    34     /// Constructor taking first pair of values.
    35     ///
    36     AveragerPair(const double, const double);
    37    
    38     ///
    3929    /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and
    4030    /// number of pair of values \a n
    4131    ///
    4232    AveragerPair(const double x, const double xx, const double y,
    43             const double yy, const double xy, const long);
     33                const double yy, const double xy, const long);
    4434
    45     ///
    46     /// Constructor taking two gslapi::vectors.
    47     ///
    48     AveragerPair(const gslapi::vector&, const gslapi::vector&);
    49    
    5035    ///
    5136    /// Copy constructor
     
    5439   
    5540    ///
    56     /// adding \a n pair(s) of data points with value \a x and \a y.
     41    /// Adding \a n pairs of data points with value \a x and \a y.
    5742    ///
    5843    inline void add(const double x, const double y, const long n=1)
    59     {x_.add(x,n); y_.add(y,n), xy_ += n*x*y;}
    60 
    61     ///
    62     /// adding \a n pairs of data points with values z.first(=x) and
    63     /// z.second(=y)
    64     ///
    65     inline void add(const std::pair<double, double> z, const long n=1)
    66     {x_.add(z.first,n); y_.add(z.second,n), xy_+=n*z.first*z.second;}
     44      { x_.add(x,n); y_.add(y,n), xy_ += n*x*y; }
    6745
    6846    ///
    6947    /// \f$\frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
    7048    /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}}\f$
    71     /// @return Pearson correlation
     49    ///
     50    /// @return Pearson correlation.
    7251    ///
    7352    inline double correlation(void) const
    74     { return (x_.std()>0 && y_.std()>0) ?
    75         (covariance() / (x_.std()*y_.std()) ) : 0; }
     53      { return ((x_.std()>0 && y_.std()>0) ?
     54                (covariance() / (x_.std()*y_.std()) ) : 0); }
    7655 
    7756    ///
    78     /// The covariance is calculated using the (n-1) correction, which
    79     /// means it is the best unbiased estimator of the covariance \f$
    80     /// \frac{1}{N-1}\sum_i (x_i-m_x)(y_i-m_y)\f$, where \f$m\f$ is the
    81     /// mean. @return covariance
     57    /// The covariance is calculated using the \f$ (n-1) \f$
     58    /// correction, which means it is the best unbiased estimator of
     59    /// the covariance \f$ \frac{1}{N-1}\sum_i (x_i-m_x)(y_i-m_y)\f$,
     60    /// where \f$m\f$ is the mean.
     61    ///
     62    /// @return The covariance.
    8263    ///
    8364    inline double covariance(void) const
    84     { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / (n()-1): 0; }
     65      { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / (n()-1): 0; }
    8566 
    8667    ///
    87     /// @return number of pair of data points
     68    /// @return The number of pair of data points.
    8869    ///
    8970    inline long n(void) const { return x_.n(); }
    9071
    9172    ///
    92     /// @return mean of xy
     73    /// @return The mean of xy.
    9374    ///
    9475    inline double mean_xy(void) const { return xy_/n(); }
    9576
    9677    ///
    97     /// @return average squared difference between x and y \f$
     78    /// @return Average squared difference between x and y \f$
    9879    /// \frac{1}{N} \sum (x-y)^2 \f$
    9980    ///
     
    10182
    10283    ///
    103     /// resets everything to zero
     84    /// Resets everything to zero
    10485    ///
    105     inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0;}
     86    inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0; }
    10687
    10788    ///
    108     /// @return the sum of xy
     89    /// @return The sum of xy.
    10990    ///
    11091    inline double sum_xy(void) const { return xy_; }
    11192
    11293    ///
    113     /// @return Averager for x
     94    /// @return A const refencer to the averager object for x.
    11495    ///
    115     inline Averager x_averager(void) const
    116     { return x_; }
     96    inline const Averager& x_averager(void) const { return x_; }
    11797
    11898    ///
    119     /// @return Averager for y
     99    /// @return A const reference to the averager object for y
    120100    ///
    121     inline Averager y_averager(void) const
    122     { return y_; }
    123 
     101    inline const Averager& y_averager(void) const { return y_; }
    124102
    125103    ///
    126     /// equality operator
     104    /// The assigment operator
    127105    ///
    128     inline AveragerPair operator=(const AveragerPair& a)
    129     { x_=a.x_averager(); y_=a.y_averager();
    130     xy_=a.sum_xy(); return *this; }
     106    inline const AveragerPair& operator=(const AveragerPair& a)
     107      { x_=a.x_; y_=a.y_; xy_=a.xy_; return *this; }
    131108
    132109    ///
    133     /// operator to add a double to the object
    134     ///
    135     //    inline Averager operator+=(std::pair<double, double> z)
    136     //{ add(z); return *this; }
    137 
    138     ///
    139     /// operator to add another Averager
     110    /// Operator to add another Averager
    140111    ///
    141112    const AveragerPair& operator+=(const AveragerPair&);
     
    148119  };
    149120
    150 }} // of namespace cpptools and namespace theplu
     121}} // of namespace statistics and namespace theplu
    151122
    152123#endif
  • trunk/src/Histogram.cc

    r195 r197  
    2828  : spacing_(spacing), xmax_(max), xmin_(min), sum_all_(), sum_histogram_()
    2929{
    30   histogram_=std::vector<WeightedAverager>(static_cast<u_int>
    31                                    (floor((max-min) / spacing)));
     30  histogram_=std::vector<statistics::WeightedAverager>(static_cast<u_int>
     31                                                  (floor((max-min) / spacing)));
    3232}
    3333
     
    3838    sum_histogram_()
    3939{
    40   histogram_=vector<WeightedAverager>(nof_bins);
     40  histogram_=vector<statistics::WeightedAverager>(nof_bins);
    4141}
    4242*/
     
    6767{
    6868  for (u_int i=0; i<histogram_.size(); i++)
    69     histogram_[i]*= 1.0/sum_all_.mean();
     69    histogram_[i].rescale(1.0/sum_all_.mean());
    7070}
    7171
     
    115115
    116116/*
    117 const WeightedAverager& Histogram::operator[](double d) const
     117const statistics::WeightedAverager& Histogram::operator[](double d) const
    118118{
    119119  if ((d<xmin_) || (d>xmax_))
    120     return WeightedAverager();    // requested bin not within boundaries
     120    return statistics::WeightedAverager();// requested bin not within boundaries
    121121  return histogram_[static_cast<u_int>(floor((d-xmin_) /
    122122               spacing_))];
  • trunk/src/Histogram.h

    r195 r197  
    9090    /// @return A const reference to an WeightedAverager object.
    9191    ///
    92     inline const WeightedAverager& averager(void) const { return averager_histogram(); }
     92    inline const statistics::WeightedAverager& averager(void) const
     93      { return averager_histogram(); }
    9394
    9495    ///
     
    100101    /// @return A const reference to an WeightedAverager object.
    101102    ///
    102     inline const WeightedAverager& averager_all(void) const { return sum_all_; }
     103    inline const statistics::WeightedAverager& averager_all(void) const
     104      { return sum_all_; }
    103105
    104106    ///
     
    111113    /// @return A const reference to an WeightedAverager object.
    112114    ///
    113     inline const WeightedAverager& averager_histogram(void) const
     115    inline const statistics::WeightedAverager& averager_histogram(void) const
    114116      { return sum_histogram_; }
    115117
     
    137139    inline double xmin(void) const { return xmin_; }
    138140
    139     inline const WeightedAverager& operator[](int bin) const
     141    inline const statistics::WeightedAverager& operator[](int bin) const
    140142      { return histogram_[bin]; }
    141     inline const WeightedAverager& operator[](u_int bin) const
     143    inline const statistics::WeightedAverager& operator[](u_int bin) const
    142144      { return histogram_[bin]; }
    143     inline const WeightedAverager& operator[](u_long bin) const
     145    inline const statistics::WeightedAverager& operator[](u_long bin) const
    144146      { return histogram_[bin]; }
    145     WeightedAverager operator[](double) const;
     147    statistics::WeightedAverager operator[](double) const;
    146148    const Histogram& operator=(const Histogram&);
    147149
     
    150152    int remove_(const u_int bin,const double event,const double weight);
    151153
    152     std::vector<WeightedAverager> histogram_;
     154    std::vector<statistics::WeightedAverager> histogram_;
    153155    double spacing_;
    154156    double xmax_;
    155157    double xmin_;
    156     WeightedAverager sum_all_;        // average of all presented events
    157     WeightedAverager sum_histogram_;  // average of events in histogram
     158    statistics::WeightedAverager sum_all_;      // average of all data
     159    statistics::WeightedAverager sum_histogram_;// average of data in histogram
    158160  };
    159161
  • trunk/src/Merge.cc

    r106 r197  
    8888
    8989  void Merge::merging(vector<u_int> index, gslapi::matrix& weights){
    90     WeightedAverager w_av;
    91     Averager av;
     90    statistics::WeightedAverager w_av;
     91    statistics::Averager av;
    9292    // merge genes for each assay
    9393    for (u_int j=0; j<nof_assays_; j++){
     
    103103      double U = -1;
    104104      if(av.n()>0){ // if av not empty, else missing
    105         U = 1/av.average()+w_av.squared_error();
     105        U = 1/av.mean()+w_av.squared_error();
    106106      }
    107107      // insert new values
  • trunk/src/SVM.cc

    r186 r197  
    194194
    195195    // Calculating the bias from support vectors. E = output - bias - target
    196     Averager bias;
     196    statistics::Averager bias;
    197197    for (size_t i=0; i<E.size(); i++)
    198198      if (alpha_train(i))
  • trunk/src/WeightedAverager.cc

    r95 r197  
    99
    1010namespace theplu {
    11 namespace cpptools{
     11namespace statistics{
    1212 
    1313
     
    2929
    3030
    31 }} // of namespace cpptools and namespace theplu
     31}} // of namespace statistics and namespace theplu
  • trunk/src/WeightedAverager.h

    r194 r197  
    88
    99namespace theplu{
    10 namespace cpptools{
     10namespace statistics{
    1111  ///
    1212  /// Class to calulate simple (first and second moments) averages
     
    5454                                       sum_wx()/sum_w() : 0; }
    5555 
     56    ///
     57    /// rescale object, i.e. each data point is rescaled
     58    /// \f$ x = a * x \f$
     59    ///
     60    inline void rescale(double a) { wx_.rescale(a); wwx_*=a; }
     61
    5662    ///
    5763    /// resets everything to zero
     
    117123    { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; }
    118124
    119     ///
    120     /// operator to rescale object, i.e. each data point is rescaled
    121     /// \f$ x = d * x \f$
    122     ///
    123     inline WeightedAverager operator*=(double d)
    124     { wx_*=d; wwx_*=d; return *this; }
    125 
    126 
    127125  private:
    128126    Averager w_;
     
    137135
    138136
    139 }} // of namespace cpptools and namespace theplu
     137}} // of namespace statistics and namespace theplu
    140138
    141139#endif
  • trunk/src/tScore.cc

    r187 r197  
    3131    data_ = data;
    3232    weight_ = gslapi::vector(target.size(),1);
    33     Averager positive;
    34     Averager negative;
     33    statistics::Averager positive;
     34    statistics::Averager negative;
    3535    for(size_t i=0; i<train_set_.size(); i++){
    3636      if (target_[train_set_[i]]==1)
     
    6262    target_ = target;
    6363    weight_ = weight;
    64     WeightedAverager positive;
    65     WeightedAverager negative;
     64    statistics::WeightedAverager positive;
     65    statistics::WeightedAverager negative;
    6666    for(size_t i=0; i<train_set_.size(); i++){
    6767      if (target_[train_set_[i]]==1)
Note: See TracChangeset for help on using the changeset viewer.