# Changeset 430

Ignore:
Timestamp:
Dec 8, 2005, 11:53:08 PM (17 years ago)
Message:

changed interface of Regression::Local

Location:
trunk
Files:
8 edited

Unmodified
Removed
• ## trunk/lib/statistics/Linear.cc

 r429 } void Linear::predict(const double x, double& y, double& y_err) void Linear::predict(const double x, double& y, double& y_err) const { y = alpha_ + beta_ * (x-m_x_);
• ## trunk/lib/statistics/Linear.h

 r429 /// the expected deviation from the line for a new data point. /// void predict(const double x, double& y, double& y_err); void predict(const double x, double& y, double& y_err) const; ///
• ## trunk/lib/statistics/LinearWeighted.h

 r429 { y = alpha_ + beta_ * (x-m_x_); y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); y_err_ = y_err = sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_)+s2_/w ); x_=x; y_=y; y_err_=y_err; }
• ## trunk/lib/statistics/Local.cc

 r429 #include //#include #include #include namespace theplu { void Local::fit(std::ostream& s, const double f, const u_int step_size) void Local::fit(const double width, const gslapi::vector& points) { // Peter, avoid this copying x_=points; y_predicted_ = gslapi::vector(points.size()); y_err_ = gslapi::vector(points.size()); sort(data_.begin(), data_.end()); // coying data to 2 gslapi vectors ONCE to use views from gslapi::vector x_total(data_.size()); gslapi::vector y_total(data_.size()); for (size_t j=0; jprint_header(s); s << std::endl; size_t min_index = 0; size_t max_index = 1; // number of points on each side size_t nof_points = static_cast(f*data_.size()); double width = 0; int min_index = 0; for (size_t i=0; i (data_[data_.size()-1].first+ data_[data_.size()-nof_points].first)/2 ){ min_index = data_.size() - nof_points; width = data_[i].first - data_[data_.size()-nof_points].first; } else { // stepping forward until x is in the middle while(data_[min_index+nof_points-1].first-data_[i].first < data_[i].first-data_[min_index].first) min_index++; width = data_[min_index+nof_points-1].first-data_[min_index].first; // Peter, should check if x gets closer to centrum if we step // back one step } while (points(min_index) < x(i)-width) min_index++; if (min_index) min_index--; while (points(max_index) < x(i)+width && max_indexweight( (x(j)- (data_[min_index].first + data_[min_index+nof_points-1].first) /2) /width ); gslapi::vector w(max_index-min_index+1); for (size_t j=0; jweight( (x_local(j)- x(i))/width ); // fitting the regressor locally regressor_->fit(x,y,w); x_.push_back(data_[i].first); y_.push_back(0.0); y_err_.push_back(0.0); regressor_->predict(x_[x_.size()-1], y_[y_.size()-1], y_err_[y_err_.size()-1]); regressor_->print(s); s << std::endl; regressor_->fit(x_local,y_local,w); regressor_->predict(x(i), y_predicted_(i),y_err_(i)); } } std::ostream& operator<<(std::ostream& os, const Local& r) { os << "# column 1: x\n" << "# column 2: y\n" << "# column 3: y_err\n"; for (size_t i=0; i
• ## trunk/lib/statistics/Local.h

 r429 #include #include namespace theplu { /// /// Function returning the points where to predict /// inline const std::vector& x(void) const { return x_; } /// /// Function returning predicted values /// inline const std::vector& y(void) const { return y_; } inline const gslapi::vector& y_predicted(void) const { return y_predicted_; } /// /// Function returning error of predictions /// inline const std::vector& y_err(void) const { return y_err_; } inline const gslapi::vector& y_err(void) const { return y_err_; } /// /// Performs the fit in data defined by add using a /// RegressionKernel and a Regression method defined in the /// constructor. The function starts by regressing over the lowest /// x value, followed by stepping forward \a step_size number of /// points, et cetera. The kernel is centralized over the x-value /// in question and the width is set so \a fraction of all points /// are used. The result is sent to ostream \a s, using print /// function in used Regression. /// constructor. For each element in vector \a x a fit is /// performed. The kernel used has width \a width and is /// centralized over the data point \f\$ x_i \f\$, which means data /// in \f\$ [x-width, x+width] is used in the fit. /// void fit(std::ostream& s, const double fraction, const u_int step_size=1); void fit(const double width, const gslapi::vector& x); /// /// @return x-values where fitting was performed. /// inline const gslapi::vector& x(void) const { return x_; } private: Kernel* kernel_; OneDimensionalWeighted* regressor_; std::vector x_; std::vector y_; std::vector y_err_; gslapi::vector x_; gslapi::vector y_predicted_; gslapi::vector y_err_; }; }}} // of namespaces regression, statisitcs and thep /// /// The output operator for the RegressionLocal class. /// std::ostream& operator<<(std::ostream&, const Local& ); }}} // of namespaces regression, statistics and thep #endif
• ## trunk/lib/statistics/Naive.cc

 r429 } void Naive::predict(const double x, double& y, double& y_err) void Naive::predict(const double x, double& y, double& y_err) const { y = ap_.x_averager().mean();
• ## trunk/lib/statistics/Naive.h

 r429 /// estimation of the mean. /// void predict(const double x, double& y, double& y_err) ; void predict(const double x, double& y, double& y_err) const; ///
• ## trunk/test/regression_test.cc

 r429 gslapi::vector w(4); w(0)=0.1;  w(1)=0.2;  w(2)=0.3;  w(3)=0.4; // testing regression::LinearWeighted *error << "testing regression::LinearWeighted" << std::endl; statistics::regression::LinearWeighted linear_w; linear_w.fit(x,y,w); linear_w.predict(1990,y_predicted,y_predicted_err); if (y_predicted!=12.8){ *error << "regression_Linear: cannot reproduce fit." << std::endl; *error << "regression_LinearWeighted: cannot reproduce fit." << std::endl; ok=false; } // testing regression::NaiveWeighted *error << "testing regression::NaiveWeighted" << std::endl; statistics::regression::NaiveWeighted naive_w; naive_w.fit(x,y,w); // testing regression::Local *error << "testing regression::Local" << std::endl; statistics::regression::KernelBox kb; statistics::regression::LinearWeighted rl; // testing regression::Polynomial *error << "testing regression::Polynomial" << std::endl; { std::ifstream s("data/regression_gauss.data"); } *error << "testing regression::Linear" << std::endl; statistics::regression::Linear lin; *error << "testing regression::Naive" << std::endl; statistics::regression::Naive naive; if (error!=&std::cerr) delete error; { statistics::regression::Local rl(r,k); gslapi::vector points(1000); for (size_t i=0; i<1000; i++){ double x = i; double y = 10.0; rl.add(x, y); rl.add(i, 10); points(i)=i; } std::ofstream myout("/dev/null"); rl.fit(myout,0.1,1); rl.fit(100, points); std::vector y = rl.y(); gslapi::vector y = rl.y_predicted(); for (size_t i=0; i
Note: See TracChangeset for help on using the changeset viewer.