Changeset 729 for trunk/yat/regression
- Timestamp:
- Jan 5, 2007, 5:00:15 PM (17 years ago)
- Location:
- trunk/yat/regression
- Files:
-
- 1 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/regression/Linear.cc
r727 r729 31 31 32 32 Linear::Linear(void) 33 : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0), 34 chisq_(0) 33 : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0) 35 34 { 36 35 } … … 60 59 } 61 60 62 double Linear::chisq(void) const63 {64 return chisq_;65 }66 67 61 void Linear::fit(const utility::vector& x, const utility::vector& y) 68 62 { … … 77 71 chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()* 78 72 ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() ); 79 r2_= 1-chisq_/ap_.x_averager().sum_xx_centered();80 73 alpha_var_ = s2() / x.size(); 81 74 beta_var_ = s2() / ap_.x_averager().sum_xx_centered(); … … 85 78 { 86 79 return alpha_ + beta_ * (x - ap_.x_averager().mean()); 87 }88 89 double Linear::r2(void) const90 {91 return r2_;92 80 } 93 81 -
trunk/yat/regression/Linear.h
r728 r729 88 88 double beta_var(void) const; 89 89 90 /**91 Chi-squared is calculated as \f$ \sum92 (y_i-\alpha-\beta(x_i-m_x))^2 \f$93 */94 double chisq(void) const;95 96 90 /** 97 91 Model is fitted by minimizing \f$ \sum{(y_i - \alpha - \beta … … 138 132 double beta_; 139 133 double beta_var_; 140 double chisq_;141 double r2_; // coefficient of determination142 134 }; 143 135 -
trunk/yat/regression/LinearWeighted.cc
r724 r729 45 45 } 46 46 47 double LinearWeighted::alpha_ err(void) const47 double LinearWeighted::alpha_var(void) const 48 48 { 49 49 return sqrt(alpha_var_); … … 55 55 } 56 56 57 double LinearWeighted::beta_ err(void) const57 double LinearWeighted::beta_var(void) const 58 58 { 59 59 return sqrt(beta_var_); … … 92 92 } 93 93 94 double LinearWeighted::mse(void) const 95 { 96 return mse_; 97 } 98 99 double LinearWeighted::prediction_error(const double x, const double w) const 94 double LinearWeighted::prediction_error2(const double x, const double w) const 100 95 { 101 96 return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2(w)); … … 107 102 } 108 103 109 double LinearWeighted::standard_error (const double x) const104 double LinearWeighted::standard_error2(const double x) const 110 105 { 111 106 return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_) ); -
trunk/yat/regression/LinearWeighted.h
r718 r729 63 63 /// @return standard deviation of parameter \f$ \alpha \f$ 64 64 /// 65 double alpha_ err(void) const;65 double alpha_var(void) const; 66 66 67 67 /// … … 73 73 /// @return standard deviation of parameter \f$ \beta \f$ 74 74 /// 75 double beta_ err(void) const;75 double beta_var(void) const; 76 76 77 77 /** … … 88 88 89 89 /// 90 /// @brief Mean Squared Error91 ///92 double mse(void) const;93 94 ///95 90 /// Function predicting value using the linear model: 96 91 /// \f$ y =\alpha + \beta (x - m) \f$ … … 99 94 100 95 /// 101 /// estimated deviation from predicted value for a new data point102 /// in @a x with weight @a w96 /// estimated squared deviation from predicted value for a new 97 /// data point in @a x with weight @a w 103 98 /// 104 double prediction_error (const double x, const double w=1) const;99 double prediction_error2(const double x, const double w=1) const; 105 100 106 101 /** … … 113 108 Var(\beta)*(x-m)} \f$. 114 109 */ 115 double standard_error (const double x) const;110 double standard_error2(const double x) const; 116 111 117 112 private: -
trunk/yat/regression/Local.cc
r718 r729 115 115 assert(i<y_err_.size()); 116 116 y_predicted_(i) = regressor_->predict(x(i*step_size)); 117 y_err_(i) = regressor_->standard_error(x(i*step_size));117 y_err_(i) = sqrt(regressor_->standard_error2(x(i*step_size))); 118 118 } 119 119 } -
trunk/yat/regression/Makefile.am
r682 r729 26 26 LinearWeighted.cc Local.cc MultiDimensional.cc \ 27 27 MultiDimensionalWeighted.cc Naive.cc NaiveWeighted.cc \ 28 OneDimensional.cc Polynomial.cc PolynomialWeighted.cc 28 OneDimensional.cc OneDimensionalWeighted.cc Polynomial.cc \ 29 PolynomialWeighted.cc 29 30 30 31 include_regressiondir = $(includedir)/yat/regression -
trunk/yat/regression/Naive.cc
r728 r729 45 45 46 46 47 double Naive::chisq(void) const48 {49 return ap_.y_averager().sum_xx_centered();50 }51 52 53 47 void Naive::fit(const utility::vector& x, const utility::vector& y) 54 48 { … … 56 50 for (size_t i=0; i<y.size(); i++) 57 51 ap_.add(x(i),y(i)); 58 52 chisq_ = ap_.y_averager().sum_xx_centered(); 59 53 } 60 54 -
trunk/yat/regression/Naive.h
r728 r729 57 57 virtual ~Naive(void); 58 58 59 /**60 Chi-squared \f$ \sum (x_i-m)^2 \f$61 */62 double chisq(void) const;63 64 59 /// 65 60 /// This function computes the best-fit for the naive model \f$ y … … 77 72 \f$ \frac{\sum \epsilon_i^2}{N-1} \f$ 78 73 79 @return variance of residuals74 @return Conditional variance 80 75 */ 81 76 double s2(void) const; 82 77 83 78 /// 84 /// @return standard error 79 /// \f$ \frac{s^2}{N} \f$ 80 /// 81 /// @return squared standard error 85 82 /// 86 83 /// @see statistics::Averager -
trunk/yat/regression/NaiveWeighted.cc
r718 r729 50 50 ap_.reset(); 51 51 ap_.add_values(x,y,utility::vector(x.size(),1.0), w); 52 chisq_ = ap_.y_averager().sum_xx_centered(); 52 53 } 54 53 55 54 56 double NaiveWeighted::predict(const double x) const … … 57 59 } 58 60 59 double NaiveWeighted::mse(void) const 61 62 double NaiveWeighted::s2(double w) const 60 63 { 61 return ap_.y_averager().std();64 return chisq_/(w*(ap_.y_averager().n()-1)); 62 65 } 63 66 64 double NaiveWeighted::standard_error(const double x) const 65 { 66 return ap_.y_averager().standard_error(); 67 68 double NaiveWeighted::standard_error2(const double x) const 69 { 70 return chisq_/((ap_.y_averager().n()-1)*ap_.sum_w()); 67 71 } 68 72 -
trunk/yat/regression/NaiveWeighted.h
r718 r729 73 73 double predict(const double x) const; 74 74 75 /// 76 /// @brief For a naive model mse is identical to standard deviation 77 /// 78 /// @see AveragerWeighted 79 /// 80 double mse(void) const; 75 /** 76 \f$ \frac{\sum w_i\epsilon_i^2}{ w \left(\frac{\left(\sum 77 w_i\right)^2}{\sum w_i^2}-1\right)} \f$ 81 78 82 /// 83 /// @return estimation of error of model value in @a x 84 /// 85 double standard_error(const double x) const; 79 Rescaling all weights, both in fit and the passed @a w, results 80 in the same returned value. 81 82 @return Conditional variance of Y with weight @a w. 83 */ 84 double s2(const double w=1) const; 85 86 /** 87 \f$ \frac{\sum w_i\epsilon_i^2}{ \left(\frac{\left(\sum 88 w_i\right)^2}{\sum w_i^2}-1\right)\sum w_i} \f$ 89 90 @return estimated squared error of model value in @a x 91 */ 92 double standard_error2(const double x) const; 86 93 87 94 private: -
trunk/yat/regression/OneDimensional.cc
r727 r729 29 29 30 30 OneDimensional::OneDimensional(void) 31 : chisq_(0) 31 32 { 32 33 } … … 37 38 38 39 40 double OneDimensional::chisq(void) const 41 { 42 return chisq_; 43 } 44 45 39 46 double OneDimensional::prediction_error2(const double x) const 40 47 { 41 return chisq()+standard_error2(x);48 return s2()+standard_error2(x); 42 49 } 43 50 … … 63 70 64 71 65 double OneDimensional::r _squared(void) const72 double OneDimensional::r2(void) const 66 73 { 67 return chisq()/variance();74 return 1 - chisq()/ap_.y_averager().sum_xx_centered(); 68 75 } 69 76 -
trunk/yat/regression/OneDimensional.h
r728 r729 58 58 @brief Chi-squared 59 59 60 Chi-squared is defined as the \f$ \frac61 {\sum{(\hat{y_i}-y_i)^2}}{1} \f$60 Chi-squared is defined as the \f$ 61 \sum{(\hat{y_i}-y_i)^2} \f$ 62 62 */ 63 virtual double chisq(void) const=0;63 double chisq(void) const; 64 64 65 65 /** … … 79 79 deviation a new data point will have from value the model 80 80 provides: \f$ E(Y|x - \hat{y}(x))^2 \f$ and is typically 81 divided into two terms \f$ E(Y|x - E(Y|x))^2 \f$ and \f$ 82 E(E(Y|x) - \hat{y}(x))^2 \f$, which is the conditional variance 83 given \f$ x \f$ and the squared standard error (see 84 standard_error2()) of the model estimation in \f$ x \f$, 85 respectively. 81 divided into the conditional variance ( see s2() ) 82 given \f$ x \f$ and the squared standard error ( see 83 standard_error2() ) of the model estimation in \f$ x \f$. 86 84 87 85 @return expected squared prediction error for a new data point … … 108 106 109 107 /** 110 r -squared is defined as \f$\frac{Var(Y|x)}{Var(Y)} \f$ or the108 r2 is defined as \f$ 1 - \frac{Var(Y|x)}{Var(Y)} \f$ or the 111 109 fraction of the variance explained by the regression model. 110 111 @see s2() 112 112 */ 113 double r _squared(void) const;113 double r2(void) const; 114 114 115 115 /** 116 @return variance of residuals 116 \f$ E(Y|x - E(Y|x))^2 \f$ 117 118 @return Conditional variance of Y 117 119 */ 118 120 virtual double s2(void) const=0; … … 136 138 statistics::AveragerPair ap_; 137 139 140 /// 141 /// @see chisq() 142 /// 143 double chisq_; 138 144 }; 139 145 -
trunk/yat/regression/OneDimensionalWeighted.h
r702 r729 47 47 /// Default Constructor. 48 48 /// 49 inline OneDimensionalWeighted(void){}49 OneDimensionalWeighted(void); 50 50 51 51 /// 52 52 /// Destructor 53 53 /// 54 virtual ~OneDimensionalWeighted(void) {};54 virtual ~OneDimensionalWeighted(void); 55 55 56 56 /** … … 64 64 const utility::vector& w)=0; 65 65 66 /**67 @brief Mean Squared Error68 69 Mean Squared Error is defined as the weighted mean of the70 squared residiuals \f$ \frac{\sum w_i(y_i-\hat{y}_i)^2}{\sum71 w_i} \f$, which is minimized when fitting the regression model.72 */73 virtual double mse(void) const=0;74 75 66 /// 76 67 /// @return expected value in @a x according to the fitted model … … 79 70 80 71 /** 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$ 72 The prediction error is defined as expected squared deviation a 73 new data point (with weight @a w) will be from the model 74 value \f$ E((Y|x - \hat{y}(x))^2|w) \f$ and is typically 75 divided into the conditional variance ( see s2() ) 76 given \f$ x \f$ and the squared standard error ( see 77 standard_error2() ) of the model estimation in \f$ x \f$. 78 79 \f$ E((Y|x - E(Y|x))^2|w) + E((E(Y|x) - \hat{y}(x))^2) \f$ 94 80 95 81 @return expected prediction error for a new data point in @a x 82 with weight @a w. 96 83 */ 97 double prediction_error(const double x, const double w=1.0) const 98 { return sqrt(mse()+pow(standard_error(x),2)); } 84 double prediction_error2(const double x, const double w=1.0) const; 99 85 100 86 /** 101 r -squaredis defined as \f$ \frac{\sum87 r2 is defined as \f$ \frac{\sum 102 88 w_i(y_i-\hat{y}_i)^2}{\sum w_i(y_i-m_y)^2} \f$ or the fraction 103 89 of the variance explained by the regression model. 104 90 */ 105 inline double r_squared(void) const 106 { return mse()/variance(); } 91 double r2(void) const; 107 92 108 93 /** 109 The standard error is defined as \f$ \sqrt{E((Y|x - 110 \hat{y}(x))^2) }\f$ 94 \f$ s^2 \f$ is the estimation of variance of residuals or 95 equivalently the conditional variance of Y. 96 97 @return Conditional variance of Y 98 */ 99 virtual double s2(double w=1) const=0; 100 101 /** 102 The standard error is defined as \f$ E((Y|x,w - 103 \hat{y}(x))^2) \f$ 111 104 112 105 @return error of model value in @a x 113 106 */ 114 virtual double standard_error (const double x) const=0;107 virtual double standard_error2(const double x) const=0; 115 108 116 109 protected: … … 120 113 statistics::AveragerPairWeighted ap_; 121 114 115 /** 116 @brief Chi-squared 117 118 Chi-squared is defined as the \f$ 119 \sum{w_i(\hat{y_i}-y_i)^2} \f$ 120 */ 121 double chisq_; 122 122 123 private: 123 inline double variance(double w=1) const124 { return ap_.y_averager().variance(); }125 126 127 124 }; 128 125 -
trunk/yat/regression/Polynomial.cc
r728 r729 41 41 42 42 43 double Polynomial::chisq(void) const44 {45 return md_.chisq();46 }47 48 49 43 const utility::matrix& Polynomial::covariance(void) const 50 44 { … … 60 54 X(i,j)=X(i,j-1)*x(i); 61 55 md_.fit(X,y); 56 chisq_ = md_.chisq(); 62 57 } 63 58 -
trunk/yat/regression/Polynomial.h
r728 r729 28 28 #include "MultiDimensional.h" 29 29 #include "yat/utility/vector.h" 30 31 #include <gsl/gsl_multifit.h>32 33 #include <cassert>34 30 35 31 namespace theplu { … … 76 72 77 73 /// 78 /// @brief Sum of squared residuals79 ///80 double chisq(void) const;81 82 ///83 74 /// @return value in @a x of model 84 75 /// -
trunk/yat/regression/PolynomialWeighted.cc
r718 r729 54 54 } 55 55 56 double PolynomialWeighted:: mse(void) const56 double PolynomialWeighted::s2(const double) const 57 57 { 58 58 return mse_; … … 76 76 } 77 77 78 double PolynomialWeighted::standard_error (const double x) const78 double PolynomialWeighted::standard_error2(const double x) const 79 79 { 80 80 utility::vector vec(power_+1,1); -
trunk/yat/regression/PolynomialWeighted.h
r718 r729 70 70 /// @brief Mean Squared Error 71 71 /// 72 double mse(void) const;72 double s2(const double) const; 73 73 74 74 /// … … 86 86 /// @return error of model value in @a x 87 87 /// 88 double standard_error (const double x) const;88 double standard_error2(const double x) const; 89 89 90 90 private:
Note: See TracChangeset
for help on using the changeset viewer.