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

XY data: user can pass a sequence of minimisers to perform a fit

This commit is contained in:
Antonin Portelli 2016-04-01 21:38:49 +01:00
parent b3c2b17eef
commit c294553991
4 changed files with 91 additions and 14 deletions

View File

@ -250,7 +250,8 @@ const XYStatData & XYSampleData::getData(void)
} }
// fit ///////////////////////////////////////////////////////////////////////// // fit /////////////////////////////////////////////////////////////////////////
SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init, SampleFitResult XYSampleData::fit(std::vector<Minimizer *> &minimizer,
const DVec &init,
const std::vector<const DoubleModel *> &v) const std::vector<const DoubleModel *> &v)
{ {
computeVarMat(); computeVarMat();
@ -265,12 +266,17 @@ SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init,
FOR_STAT_ARRAY(result, s) FOR_STAT_ARRAY(result, s)
{ {
setDataToSample(s); setDataToSample(s);
sampleResult = data_.fit(minimizer, initCopy, v); if (s == central)
{
sampleResult = data_.fit(minimizer, initCopy, v);
}
else
{
sampleResult = data_.fit(*(minimizer.back()), initCopy, v);
}
initCopy = sampleResult.segment(0, initCopy.size()); initCopy = sampleResult.segment(0, initCopy.size());
result[s] = sampleResult; result[s] = sampleResult;
result.chi2_[s] = sampleResult.getChi2(); result.chi2_[s] = sampleResult.getChi2();
for (unsigned int j = 0; j < v.size(); ++j) for (unsigned int j = 0; j < v.size(); ++j)
{ {
result.model_[j].resize(nSample_); result.model_[j].resize(nSample_);
@ -284,6 +290,15 @@ SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init,
return result; return result;
} }
SampleFitResult XYSampleData::fit(Minimizer &minimizer,
const DVec &init,
const std::vector<const DoubleModel *> &v)
{
vector<Minimizer *> mv{&minimizer};
return fit(mv, init, v);
}
// schedule data initilization from samples //////////////////////////////////// // schedule data initilization from samples ////////////////////////////////////
void XYSampleData::scheduleDataInit(void) void XYSampleData::scheduleDataInit(void)
{ {

View File

@ -92,11 +92,16 @@ public:
// get internal XYStatData // get internal XYStatData
const XYStatData & getData(void); const XYStatData & getData(void);
// fit // fit
SampleFitResult fit(std::vector<Minimizer *> &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &v);
SampleFitResult fit(Minimizer &minimizer, const DVec &init, SampleFitResult fit(Minimizer &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &v); const std::vector<const DoubleModel *> &v);
template <typename... Mods> template <typename... Ts>
SampleFitResult fit(std::vector<Minimizer *> &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models);
template <typename... Ts>
SampleFitResult fit(Minimizer &minimizer, const DVec &init, SampleFitResult fit(Minimizer &minimizer, const DVec &init,
const DoubleModel &model, const Mods... models); const DoubleModel &model, const Ts... models);
private: private:
// schedule data initilization from samples // schedule data initilization from samples
void scheduleDataInit(void); void scheduleDataInit(void);
@ -118,17 +123,30 @@ private:
* XYSampleData template implementation * * XYSampleData template implementation *
******************************************************************************/ ******************************************************************************/
template <typename... Ts> template <typename... Ts>
SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init, SampleFitResult XYSampleData::fit(std::vector<Minimizer *> &minimizer,
const DVec &init,
const DoubleModel &model, const Ts... models) const DoubleModel &model, const Ts... models)
{ {
static_assert(static_or<std::is_assignable<DoubleModel &, Ts>::value...>::value, static_assert(static_or<std::is_assignable<DoubleModel &, Ts>::value...>::value,
"model arguments are not compatible with DoubleModel &"); "model arguments are not compatible with DoubleModel");
std::vector<const DoubleModel *> modelVector{&model, &models...}; std::vector<const DoubleModel *> modelVector{&model, &models...};
return fit(minimizer, init, modelVector); return fit(minimizer, init, modelVector);
} }
template <typename... Ts>
SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models)
{
static_assert(static_or<std::is_assignable<DoubleModel &, Ts>::value...>::value,
"model arguments are not compatible with DoubleModel");
std::vector<Minimizer *> mv{&minimizer};
return fit(mv, init, model, models...);
}
END_LATAN_NAMESPACE END_LATAN_NAMESPACE
#endif // Latan_XYSampleData_hpp_ #endif // Latan_XYSampleData_hpp_

View File

@ -24,6 +24,8 @@
using namespace std; using namespace std;
using namespace Latan; using namespace Latan;
static constexpr double maxXsiDev = 10.;
/****************************************************************************** /******************************************************************************
* FitResult implementation * * FitResult implementation *
******************************************************************************/ ******************************************************************************/
@ -220,7 +222,7 @@ const DMat & XYStatData::getFitVarMatPInv(void)
} }
// fit ///////////////////////////////////////////////////////////////////////// // fit /////////////////////////////////////////////////////////////////////////
FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init, FitResult XYStatData::fit(vector<Minimizer *> &minimizer, const DVec &init,
const vector<const DoubleModel *> &v) const vector<const DoubleModel *> &v)
{ {
// check model consistency // check model consistency
@ -257,14 +259,31 @@ FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
} }
// minimization // minimization
FitResult result; FitResult result;
DVec totalInit(totalNPar); DVec totalInit(totalNPar);
//// set total init vector
totalInit.segment(0, nPar) = init; totalInit.segment(0, nPar) = init;
totalInit.segment(nPar, layout.totalXSize) = totalInit.segment(nPar, layout.totalXSize) =
chi2DataVec_.segment(layout.totalYSize, layout.totalXSize); chi2DataVec_.segment(layout.totalYSize, layout.totalXSize);
minimizer.setInit(totalInit); for (auto &m: minimizer)
result = minimizer(chi2); {
m->setInit(totalInit);
//// do not allow more than maxXsiDev std. deviations on the x-axis
for (Index p = nPar; p < totalNPar; ++p)
{
double err;
err = sqrt(fitVar_.diagonal()(layout.totalYSize + p - nPar));
m->useLowLimit(p);
m->useHighLimit(p);
m->setLowLimit(p, totalInit(p) - maxXsiDev*err);
m->setHighLimit(p, totalInit(p) + maxXsiDev*err);
}
//// minimize and store results
result = (*m)(chi2);
totalInit = result;
}
result.chi2_ = chi2(result); result.chi2_ = chi2(result);
result.nPar_ = nPar; result.nPar_ = nPar;
result.nDof_ = layout.totalYSize - nPar; result.nDof_ = layout.totalYSize - nPar;
@ -281,6 +300,14 @@ FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
return result; return result;
} }
FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
const vector<const DoubleModel *> &v)
{
vector<Minimizer *> mv{&minimizer};
return fit(mv, init, v);
}
// create data ///////////////////////////////////////////////////////////////// // create data /////////////////////////////////////////////////////////////////
void XYStatData::createXData(const std::string name __dumb, const Index nData) void XYStatData::createXData(const std::string name __dumb, const Index nData)
{ {

View File

@ -87,9 +87,14 @@ public:
const DMat & getFitVarMat(void); const DMat & getFitVarMat(void);
const DMat & getFitVarMatPInv(void); const DMat & getFitVarMatPInv(void);
// fit // fit
FitResult fit(std::vector<Minimizer *> &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &v);
FitResult fit(Minimizer &minimizer, const DVec &init, FitResult fit(Minimizer &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &v); const std::vector<const DoubleModel *> &v);
template <typename... Ts> template <typename... Ts>
FitResult fit(std::vector<Minimizer *> &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models);
template <typename... Ts>
FitResult fit(Minimizer &minimizer, const DVec &init, FitResult fit(Minimizer &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models); const DoubleModel &model, const Ts... models);
protected: protected:
@ -125,7 +130,7 @@ private:
* XYStatData template implementation * * XYStatData template implementation *
******************************************************************************/ ******************************************************************************/
template <typename... Ts> template <typename... Ts>
FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init, FitResult XYStatData::fit(std::vector<Minimizer *> &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models) const DoubleModel &model, const Ts... models)
{ {
static_assert(static_or<std::is_assignable<DoubleModel &, Ts>::value...>::value, static_assert(static_or<std::is_assignable<DoubleModel &, Ts>::value...>::value,
@ -136,6 +141,18 @@ FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
return fit(minimizer, init, modelVector); return fit(minimizer, init, modelVector);
} }
template <typename... Ts>
FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models)
{
static_assert(static_or<std::is_assignable<DoubleModel &, Ts>::value...>::value,
"model arguments are not compatible with DoubleModel");
std::vector<Minimizer *> mv{&minimizer};
return fit(mv, init, model, models...);
}
/****************************************************************************** /******************************************************************************
* error check macros * * error check macros *
******************************************************************************/ ******************************************************************************/