# Changeset 221

Ignore:
Timestamp:
Dec 30, 2004, 11:36:25 PM (18 years ago)
Message:

interface to regression modified

Location:
trunk/src
Files:
8 edited

Unmodified
Removed
• ## trunk/src/Regression.cc

 r213 Regression::Regression() : sumsq_(0) { }

• ## trunk/src/RegressionLocal.cc

 r216 Regression& r, RegressionKernel& k, const gslapi::vector& estimated_x) : kernel_(&k), regression_(&r), estimated_x_(estimated_x), estimated_y_(estimated_x), estimated_y_err_(estimated_x) const size_t nof_predictions) : kernel_(&k), regression_(&r) { estimated_x_ = gslapi::vector(nof_predictions); estimated_y_ = estimated_x_; estimated_y_err_ = estimated_x_; //sorting data with respect to x // Peter, could this be done without all the copying } sort(data_.begin(), data_.end()); for (size_t i=0; i((i+0.5)/nof_predictions*data_.size())].first; } // copying data // Peter, this is a stupid solution which could be // improved when the vector class is updated. // improved by using a moving view (in the vector class). gslapi::vector x(index_max-index_min+1); gslapi::vector y(index_max-index_min+1); for (size_t j=0; j10) std::cout << x(j) << "\t" << y(j) << std::endl; } // calculating weights for (size_t j=0; jfit_weighted(x,y,w); regression_->estimate(estimated_x_(i), std::cout << "x: " << estimated_x_(i) << std::endl; std::cout << "x.begin: " << x(0) << std::endl; std::cout << "x.end: " << x(x.size()-1) << std::endl; regression_->fit(x,y,w); std::cout << "estimating value\n"; regression_->predict(estimated_x_(i), estimated_y_(i), estimated_y_err_(i)); } } std::ostream& operator<< (std::ostream& s, const RegressionLocal& r) {
• ## trunk/src/RegressionLocal.h

 r216 /// /// Constructor loading the object with data, type of regressor /// and type kernel. /// Constructor loading the object with data, type of regressor, /// type of kernel and in how many points to predict. /// RegressionLocal(const gslapi::vector& x, const gslapi::vector& y, Regression& r, RegressionKernel& k, const gslapi::vector&); Regression& r, RegressionKernel& k, const size_t); /// virtual ~RegressionLocal(void) {}; /// /// Function returning the points where to predict /// inline gslapi::vector estimated_x(void) const { return estimated_x_; } /// /// Function returning predicted values /// inline gslapi::vector estimated_y(void) const { return estimated_y_; } /// /// Function returning error of predictions /// inline gslapi::vector estimated_y_err(void) const {return estimated_y_err_;} /// /// Function /// Function performing the fit, using a \a fraction of the data /// point and regression method defined in the constructor. The /// algorithm uses equally many points around the point to /// predict. If this is not possible (because the point is too far /// left/right) the points to the extreme left/right is used. /// void fit(const double fraction); private:
• ## trunk/src/RegressionNaive.h

 r216 // $Id$ #ifndef _theplu_statistics_regression_linear_ #define _theplu_statistics_regression_linear_ #ifndef _theplu_statistics_regression_naive_ #define _theplu_statistics_regression_naive_ // C++ tools include // Standard C++ includes //////////////////////// #include //#include #include /// Class for Regression. /// class RegressionNaive : public Regression /// virtual ~RegressionNaive(void) {}; inline void estimate(const double x, double& y, double& y_err) { y=m_; y_err=sqrt(var_); } /// /// \sum{(y_i-m)^2} \f$. /// inline int fit(const gslapi::vector& x, const gslapi::vector& y) inline void fit(const gslapi::vector& x, const gslapi::vector& y) { Averager a; a.add(y(i)); m_=a.mean(); var_=a.standard_error()*a.standard_error(); return 0; s2_=a.variance(); m_err_=a.standard_error(); } /// This function computes the best-fit for the naive model \f$ y /// = m \f$from vectors \a x and \a y, by minimizing \f$ \sum /// w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$is the inverse of the /// variance for \f$ y_i \f$/// w_i(y_i-m)^2 \f$. The weight \f$w_i \f$ is proportional to /// the inverse of the variance for \f$y_i \f$ /// inline int fit_weighted(const gslapi::vector& x, const gslapi::vector& y, const gslapi::vector& w) inline void fit(const gslapi::vector& x, const gslapi::vector& y, const gslapi::vector& w) { WeightedAverager a; a.add(y(i), w(i)); m_=a.mean(); var_=a.standard_error()*a.standard_error(); return 0; m_err_=a.standard_error(); s2_=m_err_*m_err_*w.sum(); } /// /// @return the parameter m /// inline double m(void) const { return m_; } /// /// @return standard deviation of parameter m /// inline double alpha_var(void) const { return m_err_; } /// /// Function predicting value using the naive model. \a y_err is /// the expected deviation from the line for a new data point. If /// weights are used a weight can be specified for the new point. /// inline void predict(const double x, double& y, double& y_err, const double w=1.0) const { y = m_; y_err = sqrt(m_err_*m_err_ + s2_/w); } private: double var_; double s2_; // noise level ( var = s2/w in weighted case) double m_; double m_err_; };
Note: See TracChangeset for help on using the changeset viewer.