mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2025-06-18 15:27:05 +01:00
first commit for the new fit interface
This commit is contained in:
@ -26,7 +26,7 @@ noinst_PROGRAMS = \
|
||||
exRootFinder
|
||||
|
||||
if HAVE_MINUIT
|
||||
noinst_PROGRAMS += exMin
|
||||
noinst_PROGRAMS += exFit exMin
|
||||
endif
|
||||
|
||||
exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
|
||||
@ -37,6 +37,12 @@ exDerivative_SOURCES = exDerivative.cpp
|
||||
exDerivative_CFLAGS = -g -O2
|
||||
exDerivative_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
||||
|
||||
if HAVE_MINUIT
|
||||
exFit_SOURCES = exFit.cpp
|
||||
exFit_CFLAGS = -g -O2
|
||||
exFit_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
||||
endif
|
||||
|
||||
exInterp_SOURCES = exInterp.cpp
|
||||
exInterp_CFLAGS = -g -O2
|
||||
exInterp_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
||||
|
@ -1,54 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <LatAnalyze/CompiledModel.hpp>
|
||||
#include <LatAnalyze/MinuitMinimizer.hpp>
|
||||
#include <LatAnalyze/Plot.hpp>
|
||||
#include <LatAnalyze/RandGen.hpp>
|
||||
#include <LatAnalyze/XYStatData.hpp>
|
||||
#include <LatAnalyze/FitInterface.hpp>
|
||||
|
||||
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
|
||||
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);
|
||||
|
||||
for (Index k = 0; k < nPoint; ++k)
|
||||
{
|
||||
x_k = k*dx;
|
||||
y_k = f(&x_k, exactPar) + rg.gaussian(0.0, 0.1);
|
||||
cout << x_k << " " << y_k << " " << 0.1 << endl;
|
||||
data.x(0, k) = x_k;
|
||||
data.y(0, k) = y_k;
|
||||
}
|
||||
data.yyVar(0, 0).diagonal() = DMat::Constant(nPoint, 1, 0.1*0.1);
|
||||
data.assumeXExact(0);
|
||||
|
||||
// fit
|
||||
DVec init = DVec::Constant(2, 0.5);
|
||||
FitResult p;
|
||||
MinuitMinimizer minimizer;
|
||||
FitInterface f;
|
||||
|
||||
data.fitAllPoints();
|
||||
p = data.fit(minimizer, init, f);
|
||||
|
||||
cout << "a= " << p(0) << " b= " << p(1);
|
||||
cout << " chi^2/ndof= " << p.getChi2PerDof();
|
||||
cout << " p-value= " << p.getPValue() <<endl;
|
||||
|
||||
// plot result
|
||||
Plot plot;
|
||||
|
||||
plot << LogScale(Axis::y) << PlotData(data);
|
||||
plot << Color("rgb 'blue'") << PlotFunction(p.getModel(), 0.0, 10.0);
|
||||
plot.display();
|
||||
f.addXDim("a", 3);
|
||||
f.addXDim("b", 4);
|
||||
f.addXDim("c", 3);
|
||||
f.addYDim("y");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
54
examples/exFit.old.cpp
Normal file
54
examples/exFit.old.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <LatAnalyze/CompiledModel.hpp>
|
||||
#include <LatAnalyze/MinuitMinimizer.hpp>
|
||||
#include <LatAnalyze/Plot.hpp>
|
||||
#include <LatAnalyze/RandGen.hpp>
|
||||
#include <LatAnalyze/XYStatData.hpp>
|
||||
|
||||
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
|
||||
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);
|
||||
|
||||
for (Index k = 0; k < nPoint; ++k)
|
||||
{
|
||||
x_k = k*dx;
|
||||
y_k = f(&x_k, exactPar) + rg.gaussian(0.0, 0.1);
|
||||
cout << x_k << " " << y_k << " " << 0.1 << endl;
|
||||
data.x(0, k) = x_k;
|
||||
data.y(0, k) = y_k;
|
||||
}
|
||||
data.yyVar(0, 0).diagonal() = DMat::Constant(nPoint, 1, 0.1*0.1);
|
||||
data.assumeXExact(0);
|
||||
|
||||
// fit
|
||||
DVec init = DVec::Constant(2, 0.5);
|
||||
FitResult p;
|
||||
MinuitMinimizer minimizer;
|
||||
|
||||
data.fitAllPoints();
|
||||
p = data.fit(minimizer, init, f);
|
||||
|
||||
cout << "a= " << p(0) << " b= " << p(1);
|
||||
cout << " chi^2/ndof= " << p.getChi2PerDof();
|
||||
cout << " p-value= " << p.getPValue() <<endl;
|
||||
|
||||
// plot result
|
||||
Plot plot;
|
||||
|
||||
plot << LogScale(Axis::y) << PlotData(data);
|
||||
plot << Color("rgb 'blue'") << PlotFunction(p.getModel(), 0.0, 10.0);
|
||||
plot.display();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user