2016-03-15 16:38:57 +00:00
|
|
|
/*
|
|
|
|
* XYStatData.hpp, part of LatAnalyze 3
|
|
|
|
*
|
2020-01-13 09:57:06 +00:00
|
|
|
* Copyright (C) 2013 - 2020 Antonin Portelli
|
2016-03-15 16:38:57 +00:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_XYStatData_hpp_
|
|
|
|
#define Latan_XYStatData_hpp_
|
|
|
|
|
|
|
|
#include <LatAnalyze/Global.hpp>
|
2019-02-10 00:23:36 +00:00
|
|
|
#include <LatAnalyze/Statistics/FitInterface.hpp>
|
|
|
|
#include <LatAnalyze/Numerical/Minimizer.hpp>
|
|
|
|
#include <LatAnalyze/Functional/Model.hpp>
|
2016-03-15 16:38:57 +00:00
|
|
|
|
|
|
|
BEGIN_LATAN_NAMESPACE
|
|
|
|
|
2016-03-15 19:13:20 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* object for fit result *
|
|
|
|
******************************************************************************/
|
|
|
|
class FitResult: public DVec
|
|
|
|
{
|
|
|
|
friend class XYStatData;
|
2016-03-31 12:12:30 +01:00
|
|
|
friend class XYSampleData;
|
2016-03-15 19:13:20 +00:00
|
|
|
friend class SampleFitResult;
|
|
|
|
public:
|
|
|
|
// constructors
|
|
|
|
FitResult(void) = default;
|
|
|
|
EIGEN_EXPR_CTOR(FitResult, FitResult, Base, MatExpr)
|
|
|
|
// destructor
|
|
|
|
virtual ~FitResult(void) = default;
|
|
|
|
// access
|
|
|
|
double getChi2(void) const;
|
|
|
|
double getChi2PerDof(void) const;
|
|
|
|
double getNDof(void) const;
|
2016-03-31 12:12:30 +01:00
|
|
|
Index getNPar(void) const;
|
2016-03-15 19:13:20 +00:00
|
|
|
double getPValue(void) const;
|
2019-12-12 18:04:33 +00:00
|
|
|
double getCcdf(void) const;
|
2022-02-16 18:55:08 +00:00
|
|
|
double getSvdRangeDb(void) const;
|
2016-03-15 19:13:20 +00:00
|
|
|
const DoubleFunction & getModel(const Index j = 0) const;
|
2016-03-31 12:12:30 +01:00
|
|
|
// IO
|
|
|
|
void print(const bool printXsi = false,
|
|
|
|
std::ostream &out = std::cout) const;
|
2016-03-15 19:13:20 +00:00
|
|
|
private:
|
2022-02-16 18:55:08 +00:00
|
|
|
double chi2_{0.}, svdRangeDb_{0.};
|
2016-03-31 12:12:30 +01:00
|
|
|
Index nDof_{0}, nPar_{0};
|
2016-03-15 19:13:20 +00:00
|
|
|
std::vector<DoubleFunction> model_;
|
2016-03-31 12:12:30 +01:00
|
|
|
std::vector<std::string> parName_;
|
2016-03-15 19:13:20 +00:00
|
|
|
};
|
|
|
|
|
2016-03-15 16:38:57 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* class for X vs. Y statistical data *
|
|
|
|
******************************************************************************/
|
|
|
|
class XYStatData: public FitInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructor
|
2016-03-15 19:13:20 +00:00
|
|
|
XYStatData(void) = default;
|
2016-03-15 16:38:57 +00:00
|
|
|
// destructor
|
|
|
|
virtual ~XYStatData(void) = default;
|
|
|
|
// data access
|
2016-04-05 14:29:12 +01:00
|
|
|
double & x(const Index r, const Index i);
|
|
|
|
const double & x(const Index r, const Index i) const;
|
|
|
|
const DVec & x(const Index k) const;
|
|
|
|
double & y(const Index k, const Index j);
|
|
|
|
const double & y(const Index k, const Index j) const;
|
2016-03-15 16:38:57 +00:00
|
|
|
void setXXVar(const Index i1, const Index i2, const DMat &m);
|
|
|
|
void setYYVar(const Index j1, const Index j2, const DMat &m);
|
|
|
|
void setXYVar(const Index i, const Index j, const DMat &m);
|
|
|
|
void setXError(const Index i, const DVec &err);
|
|
|
|
void setYError(const Index j, const DVec &err);
|
2016-04-05 14:29:57 +01:00
|
|
|
template <typename... Ts>
|
|
|
|
void setUnidimData(const DMat &xData, const Ts & ...yDatas);
|
2016-03-15 16:38:57 +00:00
|
|
|
const DMat & getXXVar(const Index i1, const Index i2) const;
|
|
|
|
const DMat & getYYVar(const Index j1, const Index j2) const;
|
|
|
|
const DMat & getXYVar(const Index i, const Index j) const;
|
|
|
|
DVec getXError(const Index i) const;
|
|
|
|
DVec getYError(const Index j) const;
|
2016-04-05 14:29:12 +01:00
|
|
|
DMat getTable(const Index i, const Index j) const;
|
2022-02-16 18:55:08 +00:00
|
|
|
// get total fit variance & correlation matrices and their pseudo-inverse
|
2016-03-15 19:13:20 +00:00
|
|
|
const DMat & getFitVarMat(void);
|
|
|
|
const DMat & getFitVarMatPInv(void);
|
2022-02-16 18:55:08 +00:00
|
|
|
const DMat & getFitCorrMat(void);
|
|
|
|
const DMat & getFitCorrMatPInv(void);
|
2016-03-15 19:13:20 +00:00
|
|
|
// fit
|
2016-04-01 21:38:49 +01:00
|
|
|
FitResult fit(std::vector<Minimizer *> &minimizer, const DVec &init,
|
|
|
|
const std::vector<const DoubleModel *> &v);
|
2016-03-15 19:13:20 +00:00
|
|
|
FitResult fit(Minimizer &minimizer, const DVec &init,
|
|
|
|
const std::vector<const DoubleModel *> &v);
|
2016-03-16 13:05:56 +00:00
|
|
|
template <typename... Ts>
|
2016-04-01 21:38:49 +01:00
|
|
|
FitResult fit(std::vector<Minimizer *> &minimizer, const DVec &init,
|
|
|
|
const DoubleModel &model, const Ts... models);
|
|
|
|
template <typename... Ts>
|
2016-03-16 13:05:56 +00:00
|
|
|
FitResult fit(Minimizer &minimizer, const DVec &init,
|
|
|
|
const DoubleModel &model, const Ts... models);
|
2016-04-04 19:09:18 +01:00
|
|
|
// residuals
|
|
|
|
XYStatData getResiduals(const FitResult &fit);
|
2021-12-20 00:30:03 +00:00
|
|
|
XYStatData getNormalisedResiduals(const FitResult &fit);
|
2016-04-04 19:09:18 +01:00
|
|
|
XYStatData getPartialResiduals(const FitResult &fit, const DVec &ref,
|
|
|
|
const Index i);
|
2016-03-15 16:38:57 +00:00
|
|
|
protected:
|
|
|
|
// create data
|
2016-03-16 18:33:06 +00:00
|
|
|
virtual void createXData(const std::string name, const Index nData);
|
|
|
|
virtual void createYData(const std::string name);
|
2016-03-15 16:38:57 +00:00
|
|
|
void resizeVarMat(void);
|
|
|
|
private:
|
2016-03-15 19:13:20 +00:00
|
|
|
// schedule buffer computation
|
|
|
|
void scheduleXMapInit(void);
|
|
|
|
void scheduleChi2DataVecInit(void);
|
|
|
|
// buffer total fit variance matrix
|
2016-03-15 16:38:57 +00:00
|
|
|
void updateFitVarMat(void);
|
2016-03-15 19:13:20 +00:00
|
|
|
// buffer list of x vectors
|
2016-04-05 14:29:12 +01:00
|
|
|
void updateXMap(void) const;
|
2016-03-15 19:13:20 +00:00
|
|
|
// buffer chi^2 vectors
|
|
|
|
void updateChi2DataVec(void);
|
|
|
|
void updateChi2ModVec(const DVec p,
|
2016-04-06 18:34:33 +01:00
|
|
|
const std::vector<const DoubleModel *> &v,
|
|
|
|
const Index nPar, const Index nXDim);
|
2016-03-15 16:38:57 +00:00
|
|
|
private:
|
|
|
|
std::vector<std::map<Index, double>> yData_;
|
2016-04-06 18:34:33 +01:00
|
|
|
// no map here for fit performance
|
2016-03-15 16:38:57 +00:00
|
|
|
std::vector<DVec> xData_;
|
2016-04-06 18:34:33 +01:00
|
|
|
std::vector<DVec> xMap_;
|
2016-03-15 16:38:57 +00:00
|
|
|
Mat<DMat> xxVar_, yyVar_, xyVar_;
|
2022-02-16 18:55:08 +00:00
|
|
|
DMat fitVar_, fitVarInv_, fitCorr_, fitCorrInv_;
|
2016-03-15 19:13:20 +00:00
|
|
|
DVec chi2DataVec_, chi2ModVec_, chi2Vec_;
|
2016-03-23 17:07:31 +00:00
|
|
|
DVec xBuf_;
|
2016-03-15 19:13:20 +00:00
|
|
|
bool initXMap_{true};
|
|
|
|
bool initChi2DataVec_{true};
|
2016-03-15 16:38:57 +00:00
|
|
|
};
|
|
|
|
|
2016-03-16 13:05:56 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* XYStatData template implementation *
|
|
|
|
******************************************************************************/
|
2016-04-05 14:29:57 +01:00
|
|
|
template <typename... Ts>
|
|
|
|
void XYStatData::setUnidimData(const DMat &xData, const Ts & ...yDatas)
|
|
|
|
{
|
|
|
|
static_assert(static_or<std::is_assignable<DMat, Ts>::value...>::value,
|
|
|
|
"y data arguments are not compatible with DMat");
|
|
|
|
|
|
|
|
std::vector<const DMat *> yData{&yDatas...};
|
|
|
|
|
|
|
|
FOR_VEC(xData, r)
|
|
|
|
{
|
|
|
|
x(r, 0) = xData(r);
|
|
|
|
for (unsigned int j = 0; j < yData.size(); ++j)
|
|
|
|
{
|
|
|
|
y(r, j) = (*(yData[j]))(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:05:56 +00:00
|
|
|
template <typename... Ts>
|
2016-04-01 21:38:49 +01:00
|
|
|
FitResult XYStatData::fit(std::vector<Minimizer *> &minimizer, const DVec &init,
|
2016-03-16 13:05:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-04-01 21:38:49 +01:00
|
|
|
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...);
|
|
|
|
}
|
|
|
|
|
2016-03-15 19:13:20 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* error check macros *
|
|
|
|
******************************************************************************/
|
|
|
|
#define checkVarMat(m, var)\
|
|
|
|
if (((m).rows() != (var).rows()) or ((m).cols() != (var).cols()))\
|
|
|
|
{\
|
|
|
|
LATAN_ERROR(Size, "provided variance matrix has a wrong size"\
|
|
|
|
" (expected " + strFrom((var).rows()) + "x"\
|
|
|
|
+ strFrom((var).cols()) + ", got " + strFrom((m).rows())\
|
|
|
|
+ "x" + strFrom((m).cols()) + ")");\
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define checkErrVec(err, var)\
|
|
|
|
if ((err).size() != (var).rows())\
|
|
|
|
{\
|
|
|
|
LATAN_ERROR(Size, "provided error vector has a wrong size"\
|
|
|
|
" (expected " + strFrom((var).rows()) + ", got "\
|
|
|
|
+ strFrom((err).size()) + ")");\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define checkModelVec(v)\
|
|
|
|
if (static_cast<Index>((v).size()) != getNYDim())\
|
|
|
|
{\
|
|
|
|
LATAN_ERROR(Size, "provided model vector has a wrong size"\
|
|
|
|
" (expected " + strFrom(getNYDim()) + ", got "\
|
|
|
|
+ strFrom((v).size()) + ")");\
|
|
|
|
}\
|
|
|
|
for (unsigned int _i = 1; _i < (v).size(); ++_i)\
|
|
|
|
{\
|
|
|
|
if ((v)[_i]->getNArg() != getNXDim())\
|
|
|
|
{\
|
|
|
|
LATAN_ERROR(Size, "model " + strFrom(_i) + " has a wrong"\
|
|
|
|
+ " number of argument (expected " + strFrom(getNXDim())\
|
|
|
|
+ ", got " + strFrom((v)[_i]->getNArg()));\
|
|
|
|
}\
|
|
|
|
}\
|
|
|
|
{\
|
|
|
|
Index _nPar = (v)[0]->getNPar();\
|
|
|
|
for (unsigned int _i = 1; _i < (v).size(); ++_i)\
|
|
|
|
{\
|
|
|
|
if ((v)[_i]->getNPar() != _nPar)\
|
|
|
|
{\
|
|
|
|
LATAN_ERROR(Size, "model " + strFrom(_i) + " has a wrong"\
|
|
|
|
+ " number of parameter (expected " + strFrom(_nPar)\
|
|
|
|
+ ", got " + strFrom((v)[_i]->getNPar()));\
|
|
|
|
}\
|
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
2016-03-15 16:38:57 +00:00
|
|
|
END_LATAN_NAMESPACE
|
|
|
|
|
|
|
|
#endif // Latan_XYStatData_hpp_
|