2016-03-16 20:03:32 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <LatAnalyze/CompiledModel.hpp>
|
|
|
|
#include <LatAnalyze/MinuitMinimizer.hpp>
|
2016-04-01 21:40:22 +01:00
|
|
|
#include <LatAnalyze/NloptMinimizer.hpp>
|
2016-04-04 19:09:18 +01:00
|
|
|
#include <LatAnalyze/Plot.hpp>
|
2016-03-16 20:03:32 +00:00
|
|
|
#include <LatAnalyze/RandGen.hpp>
|
|
|
|
#include <LatAnalyze/XYSampleData.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
2016-03-23 17:08:25 +00:00
|
|
|
const Index nPoint1 = 10, nPoint2 = 10;
|
2016-03-16 20:03:32 +00:00
|
|
|
const Index nSample = 1000;
|
2016-04-04 19:09:18 +01:00
|
|
|
const double xErr = .1, yErr = .3;
|
2016-03-23 17:08:25 +00:00
|
|
|
const double exactPar[2] = {0.5,5.};
|
|
|
|
const double dx1 = 10.0/static_cast<double>(nPoint1);
|
|
|
|
const double dx2 = 5.0/static_cast<double>(nPoint2);
|
2016-03-16 20:03:32 +00:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
// generate fake data
|
|
|
|
XYSampleData data(nSample);
|
2016-03-23 17:08:25 +00:00
|
|
|
RandGen rg;
|
|
|
|
double xBuf[2];
|
|
|
|
DoubleModel f([](const double *x, const double *p)
|
|
|
|
{return p[1]*exp(-x[0]*p[0])+x[1];}, 2, 2);
|
2016-03-16 20:03:32 +00:00
|
|
|
|
2016-04-04 19:09:18 +01:00
|
|
|
cout << "-- generating fake data..." << endl;
|
2016-03-31 12:12:30 +01:00
|
|
|
data.addXDim(nPoint1);
|
|
|
|
data.addXDim(nPoint2);
|
|
|
|
data.addYDim();
|
2016-03-23 17:08:25 +00:00
|
|
|
for (Index s = central; s < nSample; ++s)
|
2016-03-16 20:03:32 +00:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
for (Index i1 = 0; i1 < nPoint1; ++i1)
|
2016-03-16 20:03:32 +00:00
|
|
|
{
|
2016-03-23 17:08:25 +00:00
|
|
|
xBuf[0] = i1*dx1;
|
|
|
|
data.x(i1, 0)[s] = rg.gaussian(xBuf[0], xErr);
|
|
|
|
for (Index i2 = 0; i2 < nPoint2; ++i2)
|
|
|
|
{
|
2016-04-04 19:09:18 +01:00
|
|
|
xBuf[1] = i2*dx2;
|
|
|
|
data.x(i2, 1)[s] = xBuf[1];
|
|
|
|
data.y(data.dataIndex(i1, i2), 0)[s] =
|
2016-03-23 17:08:25 +00:00
|
|
|
rg.gaussian(f(xBuf, exactPar), yErr);
|
|
|
|
}
|
2016-03-16 20:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-23 17:08:25 +00:00
|
|
|
data.assumeXExact(true, 1);
|
2016-04-01 21:40:22 +01:00
|
|
|
|
|
|
|
// set minimizers
|
|
|
|
DVec init = DVec::Constant(2, 0.1);
|
|
|
|
SampleFitResult p;
|
|
|
|
NloptMinimizer globalMin(NloptMinimizer::Algorithm::GN_CRS2_LM);
|
|
|
|
MinuitMinimizer localMin;
|
|
|
|
vector<Minimizer *> min{&globalMin, &localMin};
|
|
|
|
|
2016-04-04 19:09:18 +01:00
|
|
|
globalMin.setPrecision(0.1);
|
2016-04-01 21:40:22 +01:00
|
|
|
globalMin.setMaxIteration(10000);
|
|
|
|
globalMin.useLowLimit(0);
|
|
|
|
globalMin.setLowLimit(0, 0.);
|
|
|
|
globalMin.useHighLimit(0);
|
|
|
|
globalMin.setHighLimit(0, 20.);
|
|
|
|
globalMin.useLowLimit(1);
|
|
|
|
globalMin.setLowLimit(1, 0.);
|
|
|
|
globalMin.useHighLimit(1);
|
|
|
|
globalMin.setHighLimit(1, 20.);
|
2016-03-16 20:03:32 +00:00
|
|
|
|
|
|
|
// fit
|
2016-04-04 19:09:18 +01:00
|
|
|
cout << "-- fit..." << endl;
|
2016-03-31 12:12:30 +01:00
|
|
|
f.parName().setName(0, "m");
|
|
|
|
f.parName().setName(1, "A");
|
2016-04-01 21:40:22 +01:00
|
|
|
p = data.fit(min, init, f);
|
2016-03-31 12:12:30 +01:00
|
|
|
p.print();
|
2016-03-16 20:03:32 +00:00
|
|
|
|
2016-04-04 19:09:18 +01:00
|
|
|
// plot
|
|
|
|
Plot plot;
|
|
|
|
DVec ref(2);
|
|
|
|
XYStatData res;
|
|
|
|
|
|
|
|
cout << "-- generating plots..." << endl;
|
|
|
|
ref(1) = 0.;
|
|
|
|
res = data.getPartialResiduals(p, ref, 0).getData();
|
|
|
|
plot << PlotRange(Axis::x, 0., 10.);
|
|
|
|
plot << Color("rgb 'blue'");
|
|
|
|
plot << PlotPredBand(p.getModel(_).bind(0, ref), 0., 10.);
|
|
|
|
plot << Color("rgb 'blue'");
|
|
|
|
plot << PlotFunction(p.getModel().bind(0, ref), 0., 10.);
|
|
|
|
plot << Color("rgb 'red'");
|
|
|
|
plot << PlotData(res);
|
|
|
|
plot.display();
|
|
|
|
|
2016-03-16 20:03:32 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|