aboutsummaryrefslogtreecommitdiffstats
path: root/example/test_fitter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'example/test_fitter.cpp')
-rw-r--r--example/test_fitter.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/example/test_fitter.cpp b/example/test_fitter.cpp
new file mode 100644
index 0000000..48c4d8c
--- /dev/null
+++ b/example/test_fitter.cpp
@@ -0,0 +1,59 @@
+#include <core/optimizer.hpp>
+#include <methods/powell/powell_method.hpp>
+#include <methods/aga/aga.hpp>
+#include <core/fitter.hpp>
+#include <vector>
+#include <iostream>
+#include <data_sets/default_data_set.hpp>
+#include <statistics/chisq.hpp>
+
+using namespace opt_utilities;
+//declear a class derived from func_obj
+class lin1d
+ :public opt_utilities::model<double,double,std::vector<double>,std::string>
+ {
+ private:
+ model<double,double,std::vector<double> >* do_clone()const
+ {
+ return new lin1d(*this);
+ }
+ public:
+ lin1d()
+ {
+ this->push_param_info(param_info<std::vector<double> >("k",1,-10,10));
+ this->push_param_info(param_info<std::vector<double> >("b",0,-10,10));
+ }
+
+ public:
+ double do_eval(const double& x,const std::vector<double>& param)
+ {
+ return x*get_element(param,0)+get_element(param,1);
+ }
+
+ private:
+ std::string do_to_string()const
+ {
+ return "linear model\n"
+ "y=k*x+b\n";
+ }
+ };
+
+
+int main()
+{
+ fitter<double,double,std::vector<double>,double,std::string> f;
+ f.set_model(lin1d());
+ //f.set_method(powell_method<double,std::vector<double> >());
+ f.set_method(aga_method<double,std::vector<double> >());
+ default_data_set<double,double> ds;
+
+ for(int i=0;i<100;++i)
+ {
+ ds.add_data(data<double,double>(i,i*3+5,.1,.1,0,0));
+ }
+ f.load_data(ds);
+ f.set_statistic(chisq<double,double,std::vector<double>,double,std::string>());
+ f.fit();
+ cout<<f.get_param_value("k")<<endl;
+ cout<<f.get_param_value("b")<<endl;
+}