Changeset 197
- Timestamp:
- Oct 27, 2004, 9:04:22 PM (18 years ago)
- Location:
- trunk/src
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Averager.cc
r137 r197 9 9 10 10 namespace theplu { 11 namespace cpptools{11 namespace statistics{ 12 12 13 13 … … 17 17 } 18 18 19 Averager::Averager(const double x)20 : n_(1), x_(x), xx_(x*x)21 {22 }23 24 19 Averager::Averager(const double x,const double xx,const long n) 25 20 : 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)31 21 { 32 22 } … … 45 35 } 46 36 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 8 8 9 9 namespace theplu{ 10 namespace cpptools{10 namespace statistics{ 11 11 class ostream; 12 12 … … 14 14 /// Class to calulate simple (first and second moments) averages. 15 15 /// 16 /// @see Weigh etdAverager16 /// @see WeightedAverager 17 17 /// 18 18 class Averager … … 26 26 27 27 /// 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. 29 30 /// 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); 37 32 38 ///39 /// Constructor taking gslapi::vector.40 ///41 Averager(const gslapi::vector&);42 43 33 /// 44 34 /// Copy constructor … … 47 37 48 38 /// 49 /// adding n(default=1) number of data point(s) with valued.39 /// Adding \a n (default=1) number of data point(s) with value \a d. 50 40 /// 51 41 inline void add(const double d,const long n=1) … … 53 43 54 44 /// 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$ 66 46 /// 67 47 inline double mean(void) const { return n_ ? x_/n_ : 0; } 68 48 69 49 /// 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$. 71 51 /// 72 52 inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; } 73 53 74 54 /// 75 /// @return number of data points55 /// @return Number of data points 76 56 /// 77 57 inline long n(void) const { return n_; } 78 58 79 59 /// 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 81 67 /// 82 68 inline void reset(void) { n_=0; x_=xx_=0.0;} … … 84 70 /// 85 71 /// The standard deviation is defined as the square root of the 86 /// variance. @return standard deviation72 /// variance. 87 73 /// 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()); } 89 77 90 78 /// 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$ 98 81 /// 99 82 inline double standard_error(void) const { return sqrt(variance()/n_); } 100 83 101 84 /// 102 /// @return the sum of x85 /// @return The sum of x 103 86 /// 104 87 inline double sum_x(void) const { return x_; } 105 88 106 89 /// 107 /// @return the sum of squares90 /// @return The sum of squares 108 91 /// 109 92 inline double sum_xsqr(void) const { return xx_; } 110 93 111 94 /// 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 116 101 /// 117 102 inline double variance(void) const … … 119 104 120 105 /// 121 /// equalityoperator106 /// The assignment operator 122 107 /// 123 inline Averageroperator=(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; } 125 110 126 111 /// 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 152 113 /// 153 114 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&);164 115 165 116 private: … … 168 119 }; 169 120 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 179 122 180 123 #endif -
trunk/src/AveragerPair.cc
r138 r197 10 10 11 11 namespace theplu { 12 namespace cpptools{12 namespace statistics{ 13 13 14 14 … … 18 18 } 19 19 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 30 20 AveragerPair::AveragerPair(const double x, const double xx, const double y, 31 21 const double yy, const double xy, const long n) 32 22 : 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)38 23 { 39 24 } … … 52 37 } 53 38 54 }} // of namespace cpptools and namespace theplu39 }} // of namespace statistics and namespace theplu -
trunk/src/AveragerPair.h
r191 r197 13 13 class gslapi::vector; 14 14 15 namespace cpptools{15 namespace statistics{ 16 16 /// 17 17 /// Class for taking care of mean and covariance of two variables. … … 27 27 28 28 /// 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 ///39 29 /// Constructor taking sum of \a x , \a xx , \a y , \a yy , xy and 40 30 /// number of pair of values \a n 41 31 /// 42 32 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); 44 34 45 ///46 /// Constructor taking two gslapi::vectors.47 ///48 AveragerPair(const gslapi::vector&, const gslapi::vector&);49 50 35 /// 51 36 /// Copy constructor … … 54 39 55 40 /// 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. 57 42 /// 58 43 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; } 67 45 68 46 /// 69 47 /// \f$\frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i 70 48 /// (x_i-m_x)^2\sum_i (y_i-m_y)^2}}\f$ 71 /// @return Pearson correlation 49 /// 50 /// @return Pearson correlation. 72 51 /// 73 52 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); } 76 55 77 56 /// 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. 82 63 /// 83 64 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; } 85 66 86 67 /// 87 /// @return number of pair of data points68 /// @return The number of pair of data points. 88 69 /// 89 70 inline long n(void) const { return x_.n(); } 90 71 91 72 /// 92 /// @return mean of xy73 /// @return The mean of xy. 93 74 /// 94 75 inline double mean_xy(void) const { return xy_/n(); } 95 76 96 77 /// 97 /// @return average squared difference between x and y \f$78 /// @return Average squared difference between x and y \f$ 98 79 /// \frac{1}{N} \sum (x-y)^2 \f$ 99 80 /// … … 101 82 102 83 /// 103 /// resets everything to zero84 /// Resets everything to zero 104 85 /// 105 inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0; }86 inline void reset(void) { x_.reset(); y_.reset(); xy_=0.0; } 106 87 107 88 /// 108 /// @return the sum of xy89 /// @return The sum of xy. 109 90 /// 110 91 inline double sum_xy(void) const { return xy_; } 111 92 112 93 /// 113 /// @return A verager for x94 /// @return A const refencer to the averager object for x. 114 95 /// 115 inline Averager x_averager(void) const 116 { return x_; } 96 inline const Averager& x_averager(void) const { return x_; } 117 97 118 98 /// 119 /// @return A veragerfor y99 /// @return A const reference to the averager object for y 120 100 /// 121 inline Averager y_averager(void) const 122 { return y_; } 123 101 inline const Averager& y_averager(void) const { return y_; } 124 102 125 103 /// 126 /// equalityoperator104 /// The assigment operator 127 105 /// 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; } 131 108 132 109 /// 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 140 111 /// 141 112 const AveragerPair& operator+=(const AveragerPair&); … … 148 119 }; 149 120 150 }} // of namespace cpptools and namespace theplu121 }} // of namespace statistics and namespace theplu 151 122 152 123 #endif -
trunk/src/Histogram.cc
r195 r197 28 28 : spacing_(spacing), xmax_(max), xmin_(min), sum_all_(), sum_histogram_() 29 29 { 30 histogram_=std::vector< WeightedAverager>(static_cast<u_int>31 30 histogram_=std::vector<statistics::WeightedAverager>(static_cast<u_int> 31 (floor((max-min) / spacing))); 32 32 } 33 33 … … 38 38 sum_histogram_() 39 39 { 40 histogram_=vector< WeightedAverager>(nof_bins);40 histogram_=vector<statistics::WeightedAverager>(nof_bins); 41 41 } 42 42 */ … … 67 67 { 68 68 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()); 70 70 } 71 71 … … 115 115 116 116 /* 117 const WeightedAverager& Histogram::operator[](double d) const117 const statistics::WeightedAverager& Histogram::operator[](double d) const 118 118 { 119 119 if ((d<xmin_) || (d>xmax_)) 120 return WeightedAverager();// requested bin not within boundaries120 return statistics::WeightedAverager();// requested bin not within boundaries 121 121 return histogram_[static_cast<u_int>(floor((d-xmin_) / 122 122 spacing_))]; -
trunk/src/Histogram.h
r195 r197 90 90 /// @return A const reference to an WeightedAverager object. 91 91 /// 92 inline const WeightedAverager& averager(void) const { return averager_histogram(); } 92 inline const statistics::WeightedAverager& averager(void) const 93 { return averager_histogram(); } 93 94 94 95 /// … … 100 101 /// @return A const reference to an WeightedAverager object. 101 102 /// 102 inline const WeightedAverager& averager_all(void) const { return sum_all_; } 103 inline const statistics::WeightedAverager& averager_all(void) const 104 { return sum_all_; } 103 105 104 106 /// … … 111 113 /// @return A const reference to an WeightedAverager object. 112 114 /// 113 inline const WeightedAverager& averager_histogram(void) const115 inline const statistics::WeightedAverager& averager_histogram(void) const 114 116 { return sum_histogram_; } 115 117 … … 137 139 inline double xmin(void) const { return xmin_; } 138 140 139 inline const WeightedAverager& operator[](int bin) const141 inline const statistics::WeightedAverager& operator[](int bin) const 140 142 { return histogram_[bin]; } 141 inline const WeightedAverager& operator[](u_int bin) const143 inline const statistics::WeightedAverager& operator[](u_int bin) const 142 144 { return histogram_[bin]; } 143 inline const WeightedAverager& operator[](u_long bin) const145 inline const statistics::WeightedAverager& operator[](u_long bin) const 144 146 { return histogram_[bin]; } 145 WeightedAverager operator[](double) const;147 statistics::WeightedAverager operator[](double) const; 146 148 const Histogram& operator=(const Histogram&); 147 149 … … 150 152 int remove_(const u_int bin,const double event,const double weight); 151 153 152 std::vector< WeightedAverager> histogram_;154 std::vector<statistics::WeightedAverager> histogram_; 153 155 double spacing_; 154 156 double xmax_; 155 157 double xmin_; 156 WeightedAverager sum_all_; // average of all presented events157 WeightedAverager sum_histogram_; // average of eventsin histogram158 statistics::WeightedAverager sum_all_; // average of all data 159 statistics::WeightedAverager sum_histogram_;// average of data in histogram 158 160 }; 159 161 -
trunk/src/Merge.cc
r106 r197 88 88 89 89 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; 92 92 // merge genes for each assay 93 93 for (u_int j=0; j<nof_assays_; j++){ … … 103 103 double U = -1; 104 104 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(); 106 106 } 107 107 // insert new values -
trunk/src/SVM.cc
r186 r197 194 194 195 195 // Calculating the bias from support vectors. E = output - bias - target 196 Averager bias;196 statistics::Averager bias; 197 197 for (size_t i=0; i<E.size(); i++) 198 198 if (alpha_train(i)) -
trunk/src/WeightedAverager.cc
r95 r197 9 9 10 10 namespace theplu { 11 namespace cpptools{11 namespace statistics{ 12 12 13 13 … … 29 29 30 30 31 }} // of namespace cpptools and namespace theplu31 }} // of namespace statistics and namespace theplu -
trunk/src/WeightedAverager.h
r194 r197 8 8 9 9 namespace theplu{ 10 namespace cpptools{10 namespace statistics{ 11 11 /// 12 12 /// Class to calulate simple (first and second moments) averages … … 54 54 sum_wx()/sum_w() : 0; } 55 55 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 56 62 /// 57 63 /// resets everything to zero … … 117 123 { wx_+=a.wx(); w_+=a.w(); wwx_+=a.sum_wwx(); return *this; } 118 124 119 ///120 /// operator to rescale object, i.e. each data point is rescaled121 /// \f$ x = d * x \f$122 ///123 inline WeightedAverager operator*=(double d)124 { wx_*=d; wwx_*=d; return *this; }125 126 127 125 private: 128 126 Averager w_; … … 137 135 138 136 139 }} // of namespace cpptools and namespace theplu137 }} // of namespace statistics and namespace theplu 140 138 141 139 #endif -
trunk/src/tScore.cc
r187 r197 31 31 data_ = data; 32 32 weight_ = gslapi::vector(target.size(),1); 33 Averager positive;34 Averager negative;33 statistics::Averager positive; 34 statistics::Averager negative; 35 35 for(size_t i=0; i<train_set_.size(); i++){ 36 36 if (target_[train_set_[i]]==1) … … 62 62 target_ = target; 63 63 weight_ = weight; 64 WeightedAverager positive;65 WeightedAverager negative;64 statistics::WeightedAverager positive; 65 statistics::WeightedAverager negative; 66 66 for(size_t i=0; i<train_set_.size(); i++){ 67 67 if (target_[train_set_[i]]==1)
Note: See TracChangeset
for help on using the changeset viewer.