Changeset 702 for trunk/yat/regression
- Timestamp:
- Oct 26, 2006, 4:04:35 PM (17 years ago)
- Location:
- trunk/yat/regression
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/regression/Linear.h
r697 r702 50 50 inline Linear(void) 51 51 : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0), 52 m _x_(0){}52 mse_(0), m_x_(0){} 53 53 54 54 /// … … 87 87 88 88 /// 89 /// @brief Mean Squared Error 90 /// 91 inline double mse(void) const { return mse_; } 92 93 /// 89 94 /// @return value in @a x of model 90 95 /// … … 112 117 double beta_; 113 118 double beta_var_; 119 double mse_; 114 120 double m_x_; // average of x values 115 121 double r2_; // coefficient of determination -
trunk/yat/regression/LinearWeighted.cc
r682 r702 39 39 // product wx*wy, so we can send in w and a dummie to get what we 40 40 // want. 41 utility::vector dummie(w.size(),1); 42 statistics::AveragerPairWeighted ap; 43 ap.add_values(x,y,w,dummie); 44 45 double m_x = ap.x_averager().mean(); 46 double m_y = ap.y_averager().mean(); 47 48 double sxy = ap.sum_xy_centered(); 49 50 double sxx = ap.x_averager().sum_xx_centered(); 51 double syy = ap.y_averager().sum_xx_centered(); 41 ap_.reset(); 42 ap_.add_values(x,y,utility::vector(x.size(),1),w); 52 43 53 44 // estimating the noise level. see attached document for motivation 54 45 // of the expression. 55 s2_= (syy -sxy*sxy/sxx)/(w.sum()-2*(w*w)/w.sum()) ;46 s2_= (syy()-sxy()*sxy()/sxx())/(w.sum()-2*(w*w)/w.sum()) ; 56 47 57 alpha_ = m_y ;58 beta_ = sxy /sxx;59 alpha_var_ = ap .y_averager().standard_error() *60 ap .y_averager().standard_error();61 beta_var_ = s2_/sxx ;62 m_x_=m_x ;48 alpha_ = m_y(); 49 beta_ = sxy()/sxx(); 50 alpha_var_ = ap_.y_averager().standard_error() * 51 ap_.y_averager().standard_error(); 52 beta_var_ = s2_/sxx(); 53 m_x_=m_x(); 63 54 } 64 55 -
trunk/yat/regression/LinearWeighted.h
r682 r702 51 51 : OneDimensionalWeighted(), alpha_(0), alpha_var_(0), beta_(0), 52 52 beta_var_(0), 53 m_x_(0) {}53 m_x_(0), s2_(0) {} 54 54 55 55 /// … … 86 86 \alpha \f$ and \f$ \beta \f$ are independent. 87 87 **/ 88 /// @todo calculate mse 88 89 void fit(const utility::vector& x, const utility::vector& y, 89 90 const utility::vector& w); 90 91 92 /// 93 /// @brief Mean Squared Error 94 /// 95 inline double mse(void) const { return mse_; } 96 91 97 /// 92 98 /// Function predicting value using the linear model: … … 100 106 /// 101 107 inline double prediction_error(const double x, const double w=1) const 102 { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2 _/w); }108 { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2(w)); } 103 109 104 110 /** 105 111 estimated error \f$ y_{err} = \sqrt{ Var(\alpha) + 106 112 Var(\beta)*(x-m)} \f$. 107 * */113 */ 108 114 inline double standard_error(const double x) const 109 115 { return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_) ); } 110 116 111 /// 112 /// Function returning the coefficient of determination, 113 /// i.e. fraction of variance explained by the linear model. 114 /// 115 inline double r2(void) const { return r2_; } 117 /** 118 Noise level for points with weight @a w. 119 */ 120 inline double s2(double w=1) const { return s2_/w; } 116 121 117 122 private: … … 121 126 LinearWeighted(const LinearWeighted&); 122 127 128 inline double m_x(void) const {return ap_.x_averager().mean(); } 129 inline double m_y(void) const {return ap_.y_averager().mean(); } 130 inline double sxx(void) const {return ap_.x_averager().sum_xx_centered(); } 131 inline double syy(void) const {return ap_.y_averager().sum_xx_centered(); } 132 inline double sxy(void) const {return ap_.sum_xy_centered(); } 133 123 134 double alpha_; 124 135 double alpha_var_; … … 127 138 double m_x_; // average of x values 128 139 double r2_; // coefficient of determination 140 double s2_; 141 double mse_; 129 142 }; 130 143 -
trunk/yat/regression/Naive.cc
r698 r702 50 50 double Naive::standard_error(const double x) const 51 51 { 52 return sqrt(mse_/ap_.n());52 return ap_.y_averager().standard_error(); 53 53 } 54 54 -
trunk/yat/regression/Naive.h
r697 r702 49 49 /// Default Constructor. 50 50 /// 51 inline Naive(void) : OneDimensional(), m _(0.0), m_err_(0.0) {}51 inline Naive(void) : OneDimensional(), mse_(0.0) {} 52 52 53 53 /// … … 70 70 71 71 /// 72 /// @brief Mean Squared Error 73 /// 74 inline double mse(void) const { return mse_; } 75 76 /// 72 77 /// Function predicting value using the naive model. 73 78 /// … … 80 85 81 86 private: 82 double m_; 83 double m_err_; // error of estimation of mean m_ 84 87 double mse_; 85 88 }; 86 89 -
trunk/yat/regression/NaiveWeighted.cc
r682 r702 27 27 #include "yat/utility/vector.h" 28 28 29 #include <cassert> 30 29 31 namespace theplu { 30 32 namespace yat { … … 35 37 const utility::vector& w) 36 38 { 37 statistics::AveragerWeighted a; 38 for (size_t i=0; i<y.size(); i++) 39 a.add(y(i), w(i)); 40 m_=a.mean(); 41 m_err_=a.standard_error(); 42 s2_=m_err_*m_err_*w.sum(); 39 assert(x.size()==y.size()); 40 assert(y.size()==w.size()); 41 ap_.reset(); 42 ap_.add_values(x,y,utility::vector(x.size(),1.0), w); 43 43 } 44 44 -
trunk/yat/regression/NaiveWeighted.h
r682 r702 51 51 /// 52 52 inline NaiveWeighted(void) 53 : OneDimensionalWeighted() , m_(0.0), m_err_(0.0){}53 : OneDimensionalWeighted() {} 54 54 55 55 /// … … 58 58 virtual ~NaiveWeighted(void) {}; 59 59 60 / //61 ///This function computes the best-fit for the naive model \f$ y62 ///= m \f$ from vectors \a x and \a y, by minimizing \f$ \sum63 ///w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$ is proportional to64 ///the inverse of the variance for \f$ y_i \f$65 ///60 /** 61 This function computes the best-fit for the naive model \f$ y 62 = m \f$ from vectors \a x and \a y, by minimizing \f$ \sum 63 w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$ is proportional to 64 the inverse of the variance for \f$ y_i \f$ 65 */ 66 66 void fit(const utility::vector& x, 67 67 const utility::vector& y, … … 72 72 /// weighted average. 73 73 /// 74 inline double predict(const double x) const { return m_; } 74 inline double predict(const double x) const 75 { return ap_.y_averager().mean(); } 75 76 76 77 /// 77 /// @return expected prediction error for a new data point in @a x 78 /// with weight @a w 78 /// @brief For a naive model mse is identical to standard deviation 79 /// 80 /// @see AveragerWeighted 79 81 /// 80 inline double prediction_error(const double x, const double w=1) const 81 { return sqrt(m_err_*m_err_ + s2_/w); } 82 inline double mse(void) const { return ap_.y_averager().std(); } 82 83 83 84 /// … … 85 86 /// 86 87 inline double standard_error(const double x) const 87 { return m_err_; }88 { return ap_.y_averager().standard_error(); } 88 89 89 90 private: … … 93 94 NaiveWeighted(const NaiveWeighted&); 94 95 95 double m_;96 double m_err_; // error of estimation of mean m_97 98 96 }; 99 97 -
trunk/yat/regression/OneDimensional.h
r698 r702 65 65 @brief Mean Squared Error 66 66 67 msd is defined as the mean of the squared residiuals \f$ 68 \frac{1}{N}\sum{(y_i-\hat{y}_i)^2} \f$, which is minimized when 69 fitting the regression model. 70 71 @return data values mean squared deviation from the model 67 Mean Squared Error is defined as the \f$ \frac 68 \sum{(\hat{y_i}-y_i)^2{df} \f$ where \f$ df \f$ number of 69 degree of freedom typically is number data points minus number 70 of paramers in model. 72 71 */ 73 inline double mse(void) const { return mse_; }72 virtual double mse(void) const=0; 74 73 75 74 /// … … 103 102 /// given the x-value (see prediction_error()). 104 103 /// 104 /// @param os Ostream printout is sent to 105 105 /// @param n number of points printed 106 106 /// @param min smallest x-value for which the model is printed … … 114 114 fraction of the variance explained by the regression model. 115 115 */ 116 inline double r_squared(void) const { return mse()/variance _; }116 inline double r_squared(void) const { return mse()/variance(); } 117 117 118 118 /** … … 126 126 protected: 127 127 /// 128 /// Variance of y 129 /// 130 inline double variance(void) const { return ap_.y_averager().variance(); } 131 132 /// 128 133 /// Averager for pair of x and y 129 134 /// 130 135 statistics::AveragerPair ap_; 131 136 132 ///133 /// mean squared error (see mse())134 ///135 double mse_;136 137 ///138 /// variance of y139 ///140 double variance_;141 137 }; 142 138 -
trunk/yat/regression/OneDimensionalWeighted.h
r696 r702 25 25 */ 26 26 27 #include "yat/statistics/AveragerPairWeighted.h" 28 27 29 #include <ostream> 28 30 … … 35 37 36 38 /// 37 /// Abstract Base Class for One Dimensional fitting in a weighted 38 /// fashion. 39 /// 40 /// @todo document 39 /// Abstract Base Class for One Dimensional fitting in a weighted 40 /// fashion. 41 41 /// 42 42 class OneDimensionalWeighted … … 44 44 45 45 public: 46 47 48 49 inline OneDimensionalWeighted(void) :s2_(0){}46 /// 47 /// Default Constructor. 48 /// 49 inline OneDimensionalWeighted(void){} 50 50 51 51 /// 52 52 /// Destructor 53 53 /// 54 54 virtual ~OneDimensionalWeighted(void) {}; 55 55 56 56 /** … … 64 64 const utility::vector& w)=0; 65 65 66 /** 67 @brief Mean Squared Error 68 69 Mean Squared Error is defined as the weighted mean of the 70 squared residiuals \f$ \frac{\sum w_i(y_i-\hat{y}_i)^2}{\sum 71 w_i} \f$, which is minimized when fitting the regression model. 72 */ 73 virtual double mse(void) const=0; 74 66 75 /// 67 /// function predicting in one point.76 /// @return expected value in @a x according to the fitted model 68 77 /// 69 78 virtual double predict(const double x) const=0; 70 79 71 /// 72 /// @return expected prediction error for a new data point in @a x 73 /// with weight @a w 74 /// 75 virtual double prediction_error(const double x, const double w=1) const=0; 80 /** 81 The prediction error is defined as the square root of the 82 expected squared deviation a new data point will have from 83 value the model provides. The expected squared deviation is 84 defined as \f$ E((Y|x,w - \hat{y}(x))^2) \f$ which is equal to 85 \f$ E((Y|x,w - E(Y|x))^2) + E((E(Y|x) - \hat{y}(x))^2) \f$, 86 which is the conditional variance given \f$ x \f$ and the 87 squared standard error (see standard_error()) of the model 88 estimation in \f$ x \f$, respectively. 89 90 The conditional variance is inversely proportional to the 91 weight \f$ w \f$ and is calculated as \f$ Var(Y|x,w) = 92 \frac{1}{w}\frac{\sum w_i(y_i-\hat{y}_i)^2\sum w_i^2} 93 {\left(\sum w_i\right)^2} =\frac{\sum w_i^2}{w\sum w_i}mse\f$ 94 95 @return expected prediction error for a new data point in @a x 96 */ 97 double prediction_error(const double x, const double w=1.0) const 98 { return sqrt(mse()+pow(standard_error(x),2)); } 76 99 77 100 /** 78 The standard error is defined as \f$ \sqrt{E(Y|x - 79 \hat{y}(x))^2 }\f$ 101 r-squared is defined as \f$ \frac{\sum 102 w_i(y_i-\hat{y}_i)^2}{\sum w_i(y_i-m_y)^2} \f$ or the fraction 103 of the variance explained by the regression model. 104 */ 105 inline double r_squared(void) const 106 { return mse()/variance(); } 107 108 /** 109 The standard error is defined as \f$ \sqrt{E((Y|x - 110 \hat{y}(x))^2) }\f$ 80 111 81 112 @return error of model value in @a x … … 83 114 virtual double standard_error(const double x) const=0; 84 115 85 116 protected: 86 117 /// 87 /// noise level - the typical variance for a point with weight w 88 /// is s2/w 118 /// Averager for pair of x and y 89 119 /// 90 double s2_; 120 statistics::AveragerPairWeighted ap_; 121 122 private: 123 inline double variance(double w=1) const 124 { return ap_.y_averager().variance(); } 125 126 91 127 }; 92 128 -
trunk/yat/regression/Polynomial.h
r697 r702 48 48 /// 49 49 inline Polynomial(size_t power) 50 : OneDimensional(), power_(power) {}50 : OneDimensional(), mse_(0), power_(power) {} 51 51 52 52 /// … … 67 67 68 68 /// 69 /// @todo 70 /// @brief Mean Squared Error 71 /// 72 inline double mse(void) const { return mse_; } 73 74 /// 69 75 /// @return value in @a x of model 70 76 /// … … 78 84 private: 79 85 MultiDimensional md_; 86 double mse_; 80 87 size_t power_; 81 88 -
trunk/yat/regression/PolynomialWeighted.h
r682 r702 46 46 /// 47 47 inline PolynomialWeighted(size_t power) 48 : OneDimensionalWeighted(), power_(power) {}48 : OneDimensionalWeighted(), mse_(0), power_(power) {} 49 49 50 50 /// … … 69 69 70 70 /// 71 /// @todo 72 /// @brief Mean Squared Error 73 /// 74 inline double mse(void) const { return mse_; } 75 76 /// 71 77 /// function predicting in one point. 72 78 /// … … 86 92 private: 87 93 MultiDimensionalWeighted md_; 94 double mse_; 88 95 size_t power_; 89 96
Note: See TracChangeset
for help on using the changeset viewer.