Changeset 204
- Timestamp:
- Nov 1, 2004, 7:13:55 PM (19 years ago)
- Location:
- trunk/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/RegressionLinear.h
r202 r204 43 43 /// This function computes the best-fit linear regression 44 44 /// coefficients (k,m) of the model \f$ y = kx + m \f$ from 45 /// vectors \a x and \a y, by minimizing \f$ \sum{(kx_i+m- x_i)^2}45 /// vectors \a x and \a y, by minimizing \f$ \sum{(kx_i+m-y_i)^2} 46 46 /// \f$. 47 47 /// 48 48 inline int fit(const gslapi::vector& x, const gslapi::vector& y) 49 { return gsl_fit_linear_vector(x.gsl_vector_pointer(),50 51 &cov00_, &cov01_, &cov11_, &sumsq_); }49 { return gsl_fit_linear_vector(x.gsl_vector_pointer(), 50 y.gsl_vector_pointer(), &m_, &k_, 51 &cov00_, &cov01_, &cov11_, &sumsq_); } 52 52 53 53 /// 54 54 /// This function computes the best-fit linear regression 55 /// coefficients (k,m) of the model \f$ y = kx + m \f$ from from 56 /// vectors \a x and \a y, by minimizing \f$ \sum 57 /// w_i(kx_i+m-x_i)^2 \f$. 55 /// coefficients (k,m) of the model \f$ y = kx + m \f$ from 56 /// vectors \a x and \a y, by minimizing \f$ \sum w_i(kx_i+m-y_i)^2 57 /// \f$. The weight \f$ w_i \f$ is the inverse of the variance for 58 /// \f$ y_i \f$ 58 59 /// 59 inline int fit_weighted(const gslapi::vector x, const gslapi::vector y, 60 const gslapi::vector w) 61 { return gsl_fit_wlinear_vector(x.gsl_vector_pointer(), 62 w.gsl_vector_pointer(), 63 y.gsl_vector_pointer(), &m_, &k_, 64 &cov00_, &cov01_, &cov11_, &sumsq_); } 60 inline int fit_weighted(gslapi::vector& x,gslapi::vector& y, 61 gslapi::vector& w) 62 { return gsl_fit_wlinear_vector(x.gsl_vector_pointer(), 63 w.gsl_vector_pointer(), 64 y.gsl_vector_pointer(), 65 &m_, &k_, 66 &cov00_, &cov01_, &cov11_, &sumsq_); } 65 67 66 68 private: -
trunk/src/RegressionNaive.cc
r202 r204 21 21 } 22 22 23 int RegressionNaive::fit(const gslapi::vector& x, const gslapi::vector& y)24 {25 Averager a;26 for (size_t i=0; i<y.size(); i++)27 a.add(y(i));28 m_=a.mean();29 var_=a.variance();30 return 0;31 }32 23 33 int RegressionNaive::fit_weighted(const gslapi::vector& x,34 const gslapi::vector& y,35 const gslapi::vector& w)36 {37 WeightedAverager a;38 for (size_t i=0; i<y.size(); i++)39 a.add(y(i),w(i));40 m_=a.mean();41 var_=a.standard_error()*a.standard_error();42 return 0;43 }44 24 45 25 }} // of namespace cpptools and namespace theplu -
trunk/src/RegressionNaive.h
r202 r204 6 6 // C++ tools include 7 7 ///////////////////// 8 #include "Averager.h" 8 9 #include "Regression.h" 9 10 #include "vector.h" 10 11 #include "WeightedAverager.h" 11 12 // Standard C++ includes 12 13 //////////////////////// … … 45 46 /// \sum{(y_i-m)^2} \f$. 46 47 /// 47 int fit(const gslapi::vector& x, const gslapi::vector& y); 48 48 inline int fit(const gslapi::vector& x, const gslapi::vector& y) 49 { 50 Averager a; 51 for (size_t i=0; i<y.size(); i++) 52 a.add(y(i)); 53 m_=a.mean(); 54 var_=a.standard_error()*a.standard_error(); 55 return 0; 56 } 49 57 50 58 /// 51 59 /// This function computes the best-fit for the naive model \f$ y 52 60 /// = m \f$ from vectors \a x and \a y, by minimizing \f$ \sum 53 /// w_i(y_i-m)^2 \f$. 61 /// w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$ is the inverse of the 62 /// variance for \f$ y_i \f$ 54 63 /// 55 int fit_weighted(const gslapi::vector& x, const gslapi::vector& y, 56 const gslapi::vector& w); 57 64 inline int fit_weighted(gslapi::vector x,gslapi::vector y,gslapi::vector w) 65 { 66 WeightedAverager a; 67 for (size_t i=0; i<y.size(); i++) 68 a.add(y(i), w(i)); 69 m_=a.mean(); 70 var_=a.standard_error()*a.standard_error(); 71 return 0; 72 } 73 58 74 59 75 private: 60 double m_; // only parameter in naive model 61 double var_; // variance of the parameter m_ 76 double var_; 77 double m_; 78 double sumsq_; 62 79 63 80 };
Note: See TracChangeset
for help on using the changeset viewer.