Changeset 216
- Timestamp:
- Dec 29, 2004, 10:29:54 AM (18 years ago)
- Location:
- trunk/src
- Files:
-
- 2 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Makefile.am
r207 r216 14 14 Kernel.cc kNNI.cc matrix.cc Merge.cc NNI.cc PCA.cc \ 15 15 PolynomialKernelFunction.cc random_singleton.cc \ 16 RegressionKernelBox.cc \ 16 17 RegressionLinear.cc RegressionLocal.cc RegressionNaive.cc ROC.cc \ 17 18 Score.cc Statistics.cc \ … … 32 33 KernelFunction.h kNNI.h matrix.h Merge.h NNI.h PCA.h Pearson.h \ 33 34 PolynomialKernelFunction.h random_singleton.h \ 34 Regression.h Regression Linear.h RegressionLocal.h RegressionNaive.h \35 R OC.h Score.h \35 Regression.h RegressionKernel.h RegressionKernelBox.h \ 36 RegressionLinear.h RegressionLocal.h RegressionNaive.h ROC.h Score.h \ 36 37 PolynomialKernelFunction.h random_singleton.h Regression.h \ 37 RegressionKernel.h RegressionLinear.h ROC.h Score.h \ 38 RegressionKernel.h RegressionKernelBox.h \ 39 RegressionLinear.h ROC.h Score.h \ 38 40 Statistics.h stl_utility.h SVD.h SVM.h tScore.h vector.h WeNNI.h \ 39 41 WeightedAverager.h -
trunk/src/Regression.h
r213 r216 34 34 virtual ~Regression(void) {}; 35 35 36 virtual void estimate(const double x, double& y, double& y_err) const=0; 37 36 38 /// 37 39 /// This function computes the best-fit given a model (see … … 44 46 /// This function computes the best-fit given a model (see 45 47 /// specific class for details) by minimizing \f$ 46 /// \sum{ (w_i\hat{y_i}-y_i)^2} \f$, where \f$ \hat{y} \f$ is the48 /// \sum{w_i(\hat{y_i}-y_i)^2} \f$, where \f$ \hat{y} \f$ is the 47 49 /// fitted value. The weight \f$ w_i \f$ is should be proportional 48 50 /// to the inverse of the variance for \f$ y_i \f$ -
trunk/src/RegressionKernel.h
r205 r216 8 8 9 9 /// 10 /// 10 /// Virtual class for calculating the weights in Local Regression 11 11 /// 12 12 13 13 class RegressionKernel 14 14 { 15 RegressionKernel(void); 15 16 public: 17 /// 18 /// Constructor 19 /// 20 RegressionKernel(); 21 22 /// 23 /// Function calculating the weight 24 /// 25 virtual double weight(const double) const=0; 16 26 }; 17 27 -
trunk/src/RegressionLinear.cc
r213 r216 20 20 } 21 21 22 23 22 }} // of namespace statistics and namespace theplu -
trunk/src/RegressionLinear.h
r213 r216 40 40 virtual ~RegressionLinear(void) {}; 41 41 42 inline void estimate(const double x, double& y, double& y_err) const 43 { 44 gsl_fit_linear_est(x, m_, k_, cov00_, cov01_, cov11_, &y, &y_err); 45 } 46 42 47 /// 43 48 /// This function computes the best-fit linear regression … … 47 52 /// 48 53 inline int fit(const gslapi::vector& x, const gslapi::vector& y) 49 { return gsl_fit_linear(x.gsl_vector_pointer()->data, 54 { 55 return gsl_fit_linear(x.gsl_vector_pointer()->data, 50 56 x.gsl_vector_pointer()->stride, 51 57 y.gsl_vector_pointer()->data, … … 67 73 inline int fit_weighted(const gslapi::vector& x, const gslapi::vector& y, 68 74 const gslapi::vector& w) 69 { return gsl_fit_wlinear(x.gsl_vector_pointer()->data, 75 { 76 return gsl_fit_wlinear(x.gsl_vector_pointer()->data, 70 77 x.gsl_vector_pointer()->stride, 71 78 w.gsl_vector_pointer()->data, -
trunk/src/RegressionLocal.cc
r206 r216 8 8 #include "vector.h" 9 9 10 #include "algorithm" 10 11 11 12 namespace theplu { … … 14 15 RegressionLocal::RegressionLocal(const gslapi::vector& x, 15 16 const gslapi::vector& y, 16 const Regression& r, 17 const RegressionKernel& k) 18 : kernel_(&k), regression_(&r) 17 Regression& r, 18 RegressionKernel& k, 19 const gslapi::vector& estimated_x) 20 : kernel_(&k), regression_(&r), estimated_x_(estimated_x), 21 estimated_y_(estimated_x), estimated_y_err_(estimated_x) 19 22 { 23 //sorting data with respect to x 24 // Peter, could this be done without all the copying 25 std::vector<std::pair<double, double> > data; 26 for (size_t i=0; i<x.size(); i++){ 27 std::pair<double, double> tmp(x(i),y(i)); 28 data_.push_back(tmp); 29 } 30 sort(data_.begin(), data_.end()); 31 } 32 33 void RegressionLocal::fit(const double f) 34 { 35 // number of points on each side 36 size_t nof_data_points = static_cast<size_t>(f/2*data_.size()); 37 size_t index_min = 0; 38 size_t index_max = 0; 39 for (size_t i=0; i<estimated_x_.size(); i++) { 40 // extreme left case 41 if (data_[nof_data_points-1].first>estimated_x_(i)){ 42 index_min = 0; 43 index_max = 2*nof_data_points-1; 44 } 45 46 // extreme right case 47 else if (data_[data_.size()-estimated_x_.size()-1].first< 48 estimated_x_(i)){ 49 index_min = data_.size()-2*nof_data_points; 50 index_max = data_.size()-1; 51 } 52 53 // normal case - window in the middle 54 else { 55 for (size_t j=nof_data_points; j<data_.size(); j++) 56 if (data_[j].first>estimated_x_(i)) { 57 index_min = j-nof_data_points; 58 index_max = j+nof_data_points-1; 59 break; 60 } 61 } 62 63 // copying data 64 // Peter, this is a stupid solution which could be 65 // improved when the vector class is updated. 66 gslapi::vector x(index_max-index_min+1); 67 gslapi::vector y(index_max-index_min+1); 68 gslapi::vector w(index_max-index_min+1); 69 for (size_t j=0; j<x.size(); j++) 70 x(j)=data_[index_min+j].first; 71 for (size_t j=0; j<y.size(); j++) 72 y(j)=data_[index_min+j].second; 73 // calculating weights 74 for (size_t j=0; j<y.size(); j++) 75 w(j) = kernel_->weight( 2*(x(j)-data_[index_min].first)/ 76 (data_[index_max].first- 77 data_[index_min].first)-1); 78 79 80 // fitting the regressor locally 81 regression_->fit_weighted(x,y,w); 82 regression_->estimate(estimated_x_(i), 83 estimated_y_(i), 84 estimated_y_err_(i)); 85 } 20 86 } 21 87 22 88 89 std::ostream& operator<< (std::ostream& s, const RegressionLocal& r) 90 { 91 gslapi::vector x=r.estimated_x(); 92 gslapi::vector y(r.estimated_y()); 93 gslapi::vector y_err(r.estimated_y_err()); 94 for (size_t i=0; i<x.size(); i++){ 95 s << x(i) << "\t"; 96 s << y(i) << "\t"; 97 s << y_err(i) << "\n"; 98 } 99 return s; 100 } 101 23 102 }} // of namespace cpptools and namespace theplu -
trunk/src/RegressionLocal.h
r206 r216 32 32 33 33 /// 34 /// Constructor doing the thing. 34 /// Constructor loading the object with data, type of regressor 35 /// and type kernel. 35 36 /// 36 RegressionLocal(const gslapi::vector& x, const gslapi::vector& y, 37 const Regression&, const RegressionKernel&); 37 RegressionLocal(const gslapi::vector& x, const gslapi::vector& y, 38 Regression& r, RegressionKernel& k, 39 const gslapi::vector&); 38 40 39 41 /// … … 47 49 virtual ~RegressionLocal(void) {}; 48 50 51 inline gslapi::vector estimated_x(void) const { return estimated_x_; } 52 inline gslapi::vector estimated_y(void) const { return estimated_y_; } 53 inline gslapi::vector estimated_y_err(void) const {return estimated_y_err_;} 54 55 /// 56 /// Function 57 /// 58 void fit(const double fraction); 49 59 50 51 gslapi::vector data_x_;60 private: 61 std::vector<std::pair<double, double> > data_; 52 62 gslapi::vector data_y_; 53 constRegressionKernel* kernel_;54 constRegression* regression_;63 RegressionKernel* kernel_; 64 Regression* regression_; 55 65 gslapi::vector estimated_x_; 56 66 gslapi::vector estimated_y_; 67 gslapi::vector estimated_y_err_; 57 68 69 58 70 }; 71 72 /// 73 /// The output operator for Local Regression Class. 74 /// 75 std::ostream& operator<< (std::ostream& s, const RegressionLocal&); 59 76 60 77 }} // of namespace statistics and namespace theplu -
trunk/src/RegressionNaive.h
r213 r216 13 13 //////////////////////// 14 14 #include <gsl/gsl_fit.h> 15 #include <utility> 15 16 16 17 namespace theplu { … … 40 41 /// 41 42 virtual ~RegressionNaive(void) {}; 43 44 inline void estimate(const double x, double& y, double& y_err) 45 { y=m_; y_err=sqrt(var_); } 42 46 43 47 ///
Note: See TracChangeset
for help on using the changeset viewer.