1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 00:45:36 +00:00
LatAnalyze/examples/exFit.cpp

55 lines
1.4 KiB
C++
Raw Normal View History

2014-03-03 12:41:48 +00:00
#include <iostream>
#include <cmath>
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/CompiledModel.hpp>
#include <LatAnalyze/MinuitMinimizer.hpp>
#include <LatAnalyze/Plot.hpp>
#include <LatAnalyze/RandGen.hpp>
#include <LatAnalyze/XYStatData.hpp>
2014-03-03 12:41:48 +00:00
using namespace std;
using namespace Latan;
const Index nPoint = 20;
const double exactPar[2] = {0.5,5.0}, dx = 10.0/static_cast<double>(nPoint);
int main(void)
{
// generate fake data
2015-02-24 17:00:19 +00:00
XYStatData data(nPoint, 1, 1);
RandGen rg;
double x_k, y_k;
DoubleModel f = compile("return p_1*exp(-x_0*p_0);", 1, 2);
2014-03-13 18:20:40 +00:00
2014-03-03 12:41:48 +00:00
for (Index k = 0; k < nPoint; ++k)
{
2014-03-03 18:57:53 +00:00
x_k = k*dx;
2014-10-14 16:36:20 +01:00
y_k = f(&x_k, exactPar) + rg.gaussian(0.0, 0.1);
cout << x_k << " " << y_k << " " << 0.1 << endl;
2014-03-03 18:57:53 +00:00
data.x(0, k) = x_k;
2014-10-14 16:36:20 +01:00
data.y(0, k) = y_k;
2014-03-03 12:41:48 +00:00
}
data.yyVar(0, 0).diagonal() = DMat::Constant(nPoint, 1, 0.1*0.1);
data.assumeXExact(0);
2014-10-14 16:36:20 +01:00
2014-03-03 12:41:48 +00:00
// fit
DVec init = DVec::Constant(2, 0.5);
FitResult p;
MinuitMinimizer minimizer;
2014-03-03 12:41:48 +00:00
data.fitAllPoints();
2014-04-07 15:56:53 +01:00
p = data.fit(minimizer, init, f);
2014-03-03 18:33:53 +00:00
cout << "a= " << p(0) << " b= " << p(1);
cout << " chi^2/ndof= " << p.getChi2PerDof();
cout << " p-value= " << p.getPValue() <<endl;
2014-03-03 18:33:53 +00:00
2014-03-03 12:41:48 +00:00
// plot result
Plot plot;
plot << LogScale(Axis::y) << PlotData(data);
2014-03-03 18:33:53 +00:00
plot << Color("rgb 'blue'") << PlotFunction(p.getModel(), 0.0, 10.0);
2014-03-03 12:41:48 +00:00
plot.display();
return EXIT_SUCCESS;
}