diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2011-02-19 01:46:03 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2011-02-19 01:46:03 +0000 |
commit | 784f745b792b45d1cce283fac3d5b0fcc6946adf (patch) | |
tree | bdaacca801c3b367b23be515dadaa2e0a0815616 /pre_estimaters | |
parent | 33e278850572786452388070cf7a3d745be25892 (diff) | |
download | opt-utilities-784f745b792b45d1cce283fac3d5b0fcc6946adf.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@177 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'pre_estimaters')
-rw-r--r-- | pre_estimaters/lin1d_estimater.hpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/pre_estimaters/lin1d_estimater.hpp b/pre_estimaters/lin1d_estimater.hpp new file mode 100644 index 0000000..0a76df3 --- /dev/null +++ b/pre_estimaters/lin1d_estimater.hpp @@ -0,0 +1,52 @@ +#ifndef LIN1D_ESTIMATER +#define LIN1D_ESTIMATER +#include "pre_estimater.hpp" +#include <misc/optvec.hpp> +#include <models/lin1d.hpp> +#include <vector> + +namespace opt_utilities +{ + template <typename T> + class lin1d_estimater + :public pre_estimater<T,T,std::vector<T>,std::string> + { + public: + lin1d_estimater() + { + this->set_model_id("1d linear model"); + } + + lin1d_estimater<T>* do_clone()const + { + return new lin1d_estimater<T>(*this); + } + + void do_estimate(const data_set<T,T>& d,model<T,T,std::vector<T>,std::string>& m)const + { + T n=d.size(); + T sy=0; + T sxx=0; + T sx=0; + T sxy=0; + + for(int i=0;i<d.size();++i) + { + sy+=d.get_data(i).get_y(); + sxx+=d.get_data(i).get_x()*d.get_data(i).get_x(); + sx+=d.get_data(i).get_x(); + sxy+=d.get_data(i).get_x()*d.get_data(i).get_y(); + } + T b=(sy*sxx-sx*sxy)/(n*sxx-sx*sx); + T k=(n*sxy-sx*sy)/(n*sxx-sx*sx); + + m.set_param_value("k",k); + m.set_param_value("b",b); + + } + }; +} + + + +#endif |