aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorastrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675>2011-02-16 17:40:00 +0000
committerastrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675>2011-02-16 17:40:00 +0000
commit33e278850572786452388070cf7a3d745be25892 (patch)
tree1bfb34ce72b49aac2007a2d5a2ba88ee1091615d
parent9212101cfe6acca9b6bbce8f329c170adb782c2f (diff)
downloadopt-utilities-33e278850572786452388070cf7a3d745be25892.tar.bz2
git-svn-id: file:///home/svn/opt_utilities@176 ed2142bd-67ad-457f-ba7c-d818d4011675
-rw-r--r--pre_estimater/lin1d_estimater.hpp26
-rw-r--r--pre_estimater/pre_estimater.hpp23
2 files changed, 45 insertions, 4 deletions
diff --git a/pre_estimater/lin1d_estimater.hpp b/pre_estimater/lin1d_estimater.hpp
index e36f2a7..0a76df3 100644
--- a/pre_estimater/lin1d_estimater.hpp
+++ b/pre_estimater/lin1d_estimater.hpp
@@ -11,18 +11,38 @@ namespace opt_utilities
class lin1d_estimater
:public pre_estimater<T,T,std::vector<T>,std::string>
{
- private:
+ public:
lin1d_estimater()
{
+ this->set_model_id("1d linear model");
}
- lin1d_estimater<T> do_clone()const
+ lin1d_estimater<T>* do_clone()const
{
return new lin1d_estimater<T>(*this);
}
- void do_estimate(const data<T,T>& d,model<T,T,std::vector<T>,std::string>& m)const
+ 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);
+
}
};
}
diff --git a/pre_estimater/pre_estimater.hpp b/pre_estimater/pre_estimater.hpp
index 9810683..859c5ca 100644
--- a/pre_estimater/pre_estimater.hpp
+++ b/pre_estimater/pre_estimater.hpp
@@ -56,6 +56,27 @@ namespace opt_utilities
pre_estimatable()
:ppe(0)
{}
+
+ pre_estimatable(const pre_estimatable<Ty,Tx,Tp,Tstr>& rhs)
+ {
+ if(rhs.ppe)
+ {
+ ppe=rhs.ppe->clone();
+ }
+ }
+
+ pre_estimatable& operator=(const pre_estimatable<Ty,Tx,Tp,Tstr>& rhs)
+ {
+ if(this==&rhs)
+ {
+ return *this;
+ }
+ if(ppe)
+ {
+ ppe->destroy();
+ }
+ ppe=rhs.ppe->clone();
+ }
void set_pre_estimater(const pre_estimater<Ty,Tx,Tp,Tstr>& pe)
{
@@ -74,7 +95,7 @@ namespace opt_utilities
{
if(ppe)
{
- delete ppe;
+ ppe->destroy();
}
}