Changeset 430
- Timestamp:
- Dec 8, 2005, 11:53:08 PM (18 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/statistics/Linear.cc
r429 r430 34 34 } 35 35 36 void Linear::predict(const double x, double& y, double& y_err) 36 void Linear::predict(const double x, double& y, double& y_err) const 37 37 { 38 38 y = alpha_ + beta_ * (x-m_x_); -
trunk/lib/statistics/Linear.h
r429 r430 67 67 /// the expected deviation from the line for a new data point. 68 68 /// 69 void predict(const double x, double& y, double& y_err) ;69 void predict(const double x, double& y, double& y_err) const; 70 70 71 71 /// -
trunk/lib/statistics/LinearWeighted.h
r429 r430 75 75 { 76 76 y = alpha_ + beta_ * (x-m_x_); 77 y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w );77 y_err_ = y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); 78 78 x_=x; 79 79 y_=y; 80 y_err_=y_err;81 80 } 82 81 -
trunk/lib/statistics/Local.cc
r429 r430 7 7 #include <c++_tools/statistics/OneDimensionalWeighted.h> 8 8 9 //#include <algorithm> 9 #include <algorithm> 10 #include <iostream> 10 11 11 12 namespace theplu { … … 14 15 15 16 16 void Local::fit(std::ostream& s, const double f, 17 const u_int step_size) 17 void Local::fit(const double width, const gslapi::vector& points) 18 18 { 19 // Peter, avoid this copying 20 x_=points; 21 y_predicted_ = gslapi::vector(points.size()); 22 y_err_ = gslapi::vector(points.size()); 23 19 24 sort(data_.begin(), data_.end()); 20 25 21 26 // coying data to 2 gslapi vectors ONCE to use views from 22 gslapi::vector x _total(data_.size());23 gslapi::vector y _total(data_.size());24 for (size_t j=0; j<x _total.size(); j++)25 x _total(j)=data_[j].first;26 for (size_t j=0; j<y _total.size(); j++)27 y _total(j)=data_[j].second;27 gslapi::vector x(data_.size()); 28 gslapi::vector y(data_.size()); 29 for (size_t j=0; j<x.size(); j++) 30 x(j)=data_[j].first; 31 for (size_t j=0; j<y.size(); j++) 32 y(j)=data_[j].second; 28 33 29 regressor_->print_header(s);30 s << std::endl;34 size_t min_index = 0; 35 size_t max_index = 1; 31 36 32 // number of points on each side 33 size_t nof_points = static_cast<size_t>(f*data_.size()); 34 double width = 0; 35 int min_index = 0; 36 for (size_t i=0; i<data_.size(); i+=step_size) { 37 // determining boundaries for window/kernel 37 for (size_t i=0; i<points.size(); i++) { 38 38 39 // extreme left case 40 if (data_[i].first < 41 (data_[nof_points-1].first+data_[nof_points-1].first)/2 ){ 42 min_index=0; 43 width = data_[nof_points-1].first-data_[0].first; 44 } 45 // extreme right case 46 else if (data_[i].first > (data_[data_.size()-1].first+ 47 data_[data_.size()-nof_points].first)/2 ){ 48 min_index = data_.size() - nof_points; 49 width = data_[i].first - data_[data_.size()-nof_points].first; 50 } 51 else { 52 // stepping forward until x is in the middle 53 while(data_[min_index+nof_points-1].first-data_[i].first 54 < data_[i].first-data_[min_index].first) 55 min_index++; 56 width = data_[min_index+nof_points-1].first-data_[min_index].first; 57 // Peter, should check if x gets closer to centrum if we step 58 // back one step 59 } 39 while (points(min_index) < x(i)-width) 40 min_index++; 41 if (min_index) 42 min_index--; 43 while (points(max_index) < x(i)+width && max_index<points.size()-1) 44 max_index++; 60 45 61 62 gslapi::vector x(x_total, min_index, nof_points); 63 gslapi::vector y(y_total, min_index, nof_points); 46 gslapi::vector x_local(x, min_index, max_index-min_index+1); 47 gslapi::vector y_local(y, min_index, max_index-min_index+1); 64 48 65 49 // calculating weights 66 gslapi::vector w(nof_points); 67 for (size_t j=0; j<y.size(); j++) 68 w(j) = kernel_->weight( (x(j)- (data_[min_index].first + 69 data_[min_index+nof_points-1].first) /2) 70 /width ); 50 gslapi::vector w(max_index-min_index+1); 51 for (size_t j=0; j<y_local.size(); j++) 52 w(j) = kernel_->weight( (x_local(j)- x(i))/width ); 71 53 72 54 // fitting the regressor locally 73 regressor_->fit(x,y,w); 74 x_.push_back(data_[i].first); 75 y_.push_back(0.0); 76 y_err_.push_back(0.0); 77 regressor_->predict(x_[x_.size()-1], y_[y_.size()-1], 78 y_err_[y_err_.size()-1]); 79 regressor_->print(s); 80 s << std::endl; 55 regressor_->fit(x_local,y_local,w); 56 regressor_->predict(x(i), y_predicted_(i),y_err_(i)); 81 57 } 82 58 } 83 59 60 std::ostream& operator<<(std::ostream& os, const Local& r) 61 { 62 os << "# column 1: x\n" 63 << "# column 2: y\n" 64 << "# column 3: y_err\n"; 65 for (size_t i=0; i<r.x().size(); i++) { 66 os << r.x()(i) << "\t" 67 << r.y_predicted()(i) << "\t" 68 << r.y_err()(i) << "\n"; 69 } 84 70 71 return os; 72 } 85 73 }}} // of namespaces regression, statisitcs and thep -
trunk/lib/statistics/Local.h
r429 r430 8 8 #include <c++_tools/gslapi/vector.h> 9 9 10 #include <iostream> 10 11 11 12 namespace theplu { … … 48 49 49 50 /// 50 /// Function returning the points where to predict51 ///52 inline const std::vector<double>& x(void) const { return x_; }53 54 ///55 51 /// Function returning predicted values 56 52 /// 57 inline const std::vector<double>& y(void) const { return y_; } 53 inline const gslapi::vector& y_predicted(void) const 54 { return y_predicted_; } 58 55 59 56 /// 60 57 /// Function returning error of predictions 61 58 /// 62 inline const std::vector<double>& y_err(void) const { return y_err_; }59 inline const gslapi::vector& y_err(void) const { return y_err_; } 63 60 64 61 /// 65 62 /// Performs the fit in data defined by add using a 66 63 /// RegressionKernel and a Regression method defined in the 67 /// constructor. The function starts by regressing over the lowest 68 /// x value, followed by stepping forward \a step_size number of 69 /// points, et cetera. The kernel is centralized over the x-value 70 /// in question and the width is set so \a fraction of all points 71 /// are used. The result is sent to ostream \a s, using print 72 /// function in used Regression. 64 /// constructor. For each element in vector \a x a fit is 65 /// performed. The kernel used has width \a width and is 66 /// centralized over the data point \f$ x_i \f$, which means data 67 /// in \f$ [x-width, x+width] is used in the fit. 73 68 /// 74 void fit( std::ostream& s, const double fraction, const u_int step_size=1);69 void fit(const double width, const gslapi::vector& x); 75 70 71 /// 72 /// @return x-values where fitting was performed. 73 /// 74 inline const gslapi::vector& x(void) const { return x_; } 76 75 77 76 private: … … 84 83 Kernel* kernel_; 85 84 OneDimensionalWeighted* regressor_; 86 std::vector<double> x_;87 std::vector<double> y_;88 std::vector<double> y_err_;85 gslapi::vector x_; 86 gslapi::vector y_predicted_; 87 gslapi::vector y_err_; 89 88 90 89 }; 91 90 92 }}} // of namespaces regression, statisitcs and thep 91 /// 92 /// The output operator for the RegressionLocal class. 93 /// 94 std::ostream& operator<<(std::ostream&, const Local& ); 95 96 97 }}} // of namespaces regression, statistics and thep 93 98 94 99 #endif -
trunk/lib/statistics/Naive.cc
r429 r430 23 23 } 24 24 25 void Naive::predict(const double x, double& y, double& y_err) 25 void Naive::predict(const double x, double& y, double& y_err) const 26 26 { 27 27 y = ap_.x_averager().mean(); -
trunk/lib/statistics/Naive.h
r429 r430 54 54 /// estimation of the mean. 55 55 /// 56 void predict(const double x, double& y, double& y_err) ;56 void predict(const double x, double& y, double& y_err) const; 57 57 58 58 /// -
trunk/test/regression_test.cc
r429 r430 42 42 gslapi::vector w(4); w(0)=0.1; w(1)=0.2; w(2)=0.3; w(3)=0.4; 43 43 44 // testing regression::LinearWeighted44 *error << "testing regression::LinearWeighted" << std::endl; 45 45 statistics::regression::LinearWeighted linear_w; 46 46 linear_w.fit(x,y,w); … … 49 49 linear_w.predict(1990,y_predicted,y_predicted_err); 50 50 if (y_predicted!=12.8){ 51 *error << "regression_Linear : cannot reproduce fit." << std::endl;51 *error << "regression_LinearWeighted: cannot reproduce fit." << std::endl; 52 52 ok=false; 53 53 } 54 54 55 55 // testing regression::NaiveWeighted 56 *error << "testing regression::NaiveWeighted" << std::endl; 56 57 statistics::regression::NaiveWeighted naive_w; 57 58 naive_w.fit(x,y,w); … … 65 66 66 67 // testing regression::Local 68 *error << "testing regression::Local" << std::endl; 67 69 statistics::regression::KernelBox kb; 68 70 statistics::regression::LinearWeighted rl; … … 78 80 79 81 // testing regression::Polynomial 82 *error << "testing regression::Polynomial" << std::endl; 80 83 { 81 84 std::ifstream s("data/regression_gauss.data"); … … 98 101 } 99 102 103 *error << "testing regression::Linear" << std::endl; 104 statistics::regression::Linear lin; 105 106 *error << "testing regression::Naive" << std::endl; 107 statistics::regression::Naive naive; 108 100 109 if (error!=&std::cerr) 101 110 delete error; … … 110 119 { 111 120 statistics::regression::Local rl(r,k); 121 gslapi::vector points(1000); 112 122 for (size_t i=0; i<1000; i++){ 113 double x = i; 114 double y = 10.0; 115 rl.add(x, y); 123 rl.add(i, 10); 124 points(i)=i; 116 125 } 117 126 118 std::ofstream myout("/dev/null"); 119 rl.fit(myout,0.1,1); 127 rl.fit(100, points); 120 128 121 std::vector<double> y = rl.y();129 gslapi::vector y = rl.y_predicted(); 122 130 for (size_t i=0; i<y.size(); i++) 123 if (y [i]!=10.0){131 if (y(i)!=10.0){ 124 132 return false; 125 133 }
Note: See TracChangeset
for help on using the changeset viewer.