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

XY*Data: variadic functions for fit

This commit is contained in:
Antonin Portelli 2014-04-03 10:03:46 +02:00
parent 166ed550ae
commit 9a415017c8
4 changed files with 64 additions and 49 deletions

View File

@ -203,21 +203,28 @@ XYSampleData::ConstSampleBlock XYSampleData::y(const Index j, const Index k)
}
// fit /////////////////////////////////////////////////////////////////////////
SampleFitResult XYSampleData::fit(
const std::vector<const DoubleModel *> &modelVector,
Minimizer &minimizer, const DVec &init,
const FitVerbosity verbosity)
SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &modelVector)
{
const Index nSample = x_.size();
FitResult sampleResult;
SampleFitResult result;
bool initChi2;
// sample loop
result.resize(nSample);
result.chi2_.resize(nSample);
FOR_STAT_ARRAY(x_, s)
{
// reinit chi^2 for central value only
if (s == central)
{
data_.reinitChi2(true);
}
else
{
data_.reinitChi2(false);
}
// set data
setDataToSample(s);
@ -225,8 +232,7 @@ SampleFitResult XYSampleData::fit(
initChi2 = (s == central);
// fit
sampleResult = data_.fit(modelVector, minimizer, init, initChi2,
verbosity);
sampleResult = data_.fit(minimizer, init, modelVector);
// store result
result[s] = sampleResult;
@ -243,18 +249,6 @@ SampleFitResult XYSampleData::fit(
return result;
}
SampleFitResult XYSampleData::fit(const DoubleModel &model,
Minimizer &minimizer,
const DVec &init,
const FitVerbosity verbosity)
{
vector<const DoubleModel *> modelVector(1);
modelVector[0] = &model;
return fit(modelVector, minimizer, init, verbosity);
}
void XYSampleData::setDataToSample(const Index s)
{
// compute covariance matrices if necessary

View File

@ -94,12 +94,11 @@ public:
SampleBlock y(const Index i, const Index k);
ConstSampleBlock y(const Index i, const Index k) const;
// fit
SampleFitResult fit(const std::vector<const DoubleModel *> &modelVector,
Minimizer &minimizer, const DVec &init,
const FitVerbosity verbosity = FitVerbosity::Silent);
SampleFitResult fit(const DoubleModel &model, Minimizer &minimizer,
const DVec &init,
const FitVerbosity verbosity = FitVerbosity::Silent);
SampleFitResult fit(Minimizer &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &modelVector);
template <typename... Mods>
SampleFitResult fit(Minimizer &minimizer, const DVec &init,
const DoubleModel &model, const Mods... models);
private:
void setDataToSample(const Index s);
private:
@ -108,6 +107,21 @@ private:
XYStatData data_;
};
/******************************************************************************
* XYSampleData template implementation *
******************************************************************************/
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<const DoubleModel *> modelVector{&model, &models...};
return fit(minimizer, init, modelVector);
}
END_NAMESPACE
#endif // Latan_XYSampleData_hpp_

View File

@ -84,6 +84,11 @@ void XYStatData::resize(const Index nData, const Index xDim, const Index yDim)
}
}
void XYStatData::reinitChi2(const bool doReinit)
{
reinitChi2_ = doReinit;
}
Block<DMatBase> XYStatData::x(const PlaceHolder ph1 __unused,
const PlaceHolder ph2 __unused)
{
@ -205,18 +210,15 @@ ConstBlock<DMatBase> XYStatData::yxVar(const Index j, const Index i) const
}
// fit /////////////////////////////////////////////////////////////////////////
FitResult XYStatData::fit(const vector<const DoubleModel *> &modelVector,
Minimizer &minimizer, const DVec &init,
const bool reinitChi2,
const FitVerbosity verbosity)
FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
const vector<const DoubleModel *> &modelVector)
{
// initialization
chi2_.setModel(modelVector);
if (reinitChi2)
if (reinitChi2_)
{
chi2_.requestInit();
}
minimizer.setVerbosity(verbosity);
// initial parameters
const Index nPoint = getNFitPoint();
@ -251,14 +253,3 @@ FitResult XYStatData::fit(const vector<const DoubleModel *> &modelVector,
return result;
}
FitResult XYStatData::fit(const DoubleModel &model, Minimizer &minimizer,
const DVec &init, const bool reinitChi2,
const FitVerbosity verbosity)
{
vector<const DoubleModel *> modelVector(1);
modelVector[0] = &model;
return fit(modelVector, minimizer, init, reinitChi2, verbosity);
}

View File

@ -75,6 +75,7 @@ public:
// access
void resize(const Index nData, const Index xDim,
const Index yDim);
void reinitChi2(const bool doReinit = true);
Block<DMatBase> x(const PlaceHolder ph1 = _,
const PlaceHolder ph2 = _);
ConstBlock<DMatBase> x(const PlaceHolder ph1 = _,
@ -102,21 +103,36 @@ public:
Block<DMatBase> yxVar(const Index j, const Index i);
ConstBlock<DMatBase> yxVar(const Index j, const Index i) const;
// fit
FitResult fit(const std::vector<const DoubleModel *> &modelVector,
Minimizer &minimizer, const DVec &init,
const bool reinitChi2 = true,
const FitVerbosity verbosity = FitVerbosity::Silent);
FitResult fit(const DoubleModel &model, Minimizer &minimizer,
const DVec &init, const bool reinitChi2 = true,
const FitVerbosity verbosity = FitVerbosity::Silent);
FitResult fit(Minimizer &minimizer, const DVec &init,
const std::vector<const DoubleModel *> &modelVector);
template <typename... Ts>
FitResult fit(Minimizer &minimizer, const DVec &init,
const DoubleModel &model, const Ts... models);
private:
DMat x_, y_;
Mat<DMat> var_[3];
IVec isXExact_, isFitPoint_;
Chi2Function chi2_;
bool reinitChi2_{true};
};
/******************************************************************************
* XYStatData template implementation *
******************************************************************************/
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<const DoubleModel *> modelVector{&model, &models...};
return fit(minimizer, init, modelVector);
}
END_NAMESPACE
#endif // Latan_XYData_hpp_