1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-09-20 05:25:37 +01:00

New base class for fitting objects

This commit is contained in:
Antonin Portelli 2014-03-12 19:17:15 +00:00
parent 2e4ac4fa5a
commit 1eb081749a
5 changed files with 267 additions and 91 deletions

178
latan/FitInterface.cpp Normal file
View File

@ -0,0 +1,178 @@
/*
* FitInterface.cpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2014 Antonin Portelli
*
* 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/>.
*/
#include <latan/FitInterface.hpp>
#include <latan/includes.hpp>
using namespace std;
using namespace Latan;
/******************************************************************************
* FitInterface implementation *
******************************************************************************/
// constructors ////////////////////////////////////////////////////////////////
FitInterface::FitInterface(const Index nData, const Index xDim,
const Index yDim)
{
resize(nData, xDim, yDim);
}
// access //////////////////////////////////////////////////////////////////////
void FitInterface::assumeXExact(const Index i, const bool isExact)
{
isXExact_(i) = (isExact) ? 1 : 0;
}
void FitInterface::assumeXXCorrelated(const Index i1, const Index i2,
const bool isCorrelated)
{
isXXCorr_(i1, i2) = (isCorrelated) ? 1 : 0;
}
void FitInterface::assumeYYCorrelated(const Index j1, const Index j2,
const bool isCorrelated)
{
isYYCorr_(j1, j2) = (isCorrelated) ? 1 : 0;
}
void FitInterface::assumeYXCorrelated(const Index j, const Index i,
const bool isCorrelated)
{
isYXCorr_(j, i) = (isCorrelated) ? 1 : 0;
}
void FitInterface::assumeDataCorrelated(const Index k1, const Index k2,
const bool isCorrelated)
{
isDataCorr_(k1, k2) = (isCorrelated) ? 1 : 0;
}
void FitInterface::fitPoint(const Index i, const bool isFitPoint)
{
isFitPoint_(i) = (isFitPoint) ? 1 : 0;
}
void FitInterface::fitPointRange(const Index k1, const Index k2,
const bool isFitPoint)
{
int size = static_cast<int>(k2-k1+1);
isFitPoint_.segment(k1, size) = IVec::Constant(size, (isFitPoint) ? 1 : 0);
}
void FitInterface::fitAllPoints(const bool isFitPoint)
{
fitPointRange(0, getNData()-1, isFitPoint);
}
Index FitInterface::getNData(void) const
{
return isFitPoint_.size();
}
Index FitInterface::getNFitPoint(void) const
{
return isFitPoint_.sum();
}
Index FitInterface::getXDim(void) const
{
return isXXCorr_.rows();
}
Index FitInterface::getYDim(void) const
{
return isYYCorr_.rows();
}
Index FitInterface::getStatXDim(void) const
{
return isXExact_.size() - isXExact_.sum();
}
void FitInterface::setFitInterface(const FitInterface &fitInterface)
{
if (&fitInterface != this)
{
resize(fitInterface.getNData(), fitInterface.getXDim(),
fitInterface.getYDim());
isXExact_ = fitInterface.isXExact_;
isFitPoint_ = fitInterface.isFitPoint_;
isXXCorr_ = fitInterface.isXXCorr_;
isYYCorr_ = fitInterface.isYYCorr_;
isYXCorr_ = fitInterface.isYXCorr_;
isDataCorr_ = fitInterface.isDataCorr_;
}
}
void FitInterface::setNData(const Index nData)
{
resize(nData, getXDim(), getYDim());
}
void FitInterface::setXDim(const Index xDim)
{
resize(getNData(), xDim, getYDim());
}
void FitInterface::setYDim(const Index yDim)
{
resize(getNData(), getXDim(), yDim);
}
void FitInterface::resize(const Index nData, const Index xDim, const Index yDim)
{
isXExact_.setConstant(xDim, 0);
isFitPoint_.setConstant(nData, 0);
isXXCorr_.setIdentity(xDim, xDim);
isYYCorr_.setIdentity(xDim, xDim);
isYXCorr_.setConstant(yDim, xDim, 0);
isDataCorr_.setIdentity(nData, nData);
}
// test ////////////////////////////////////////////////////////////////////////
bool FitInterface::isFitPoint(const Index k) const
{
return (isFitPoint_[k] == 1);
}
bool FitInterface::isXExact(const Index i) const
{
return (isXExact_[i] == 1);
}
bool FitInterface::isXXCorrelated(const Index i1, const Index i2) const
{
return (isXXCorr_(i1, i2) == 1);
}
bool FitInterface::isYYCorrelated(const Index j1, const Index j2) const
{
return (isYYCorr_(j1, j2) == 1);
}
bool FitInterface::isYXCorrelated(const Index j, const Index i) const
{
return (isYXCorr_(j, i) == 1);
}
bool FitInterface::isDataCorrelated(const Index k1, const Index k2) const
{
return (isDataCorr_(k1, k2) == 1);
}

79
latan/FitInterface.hpp Normal file
View File

@ -0,0 +1,79 @@
/*
* FitInterface.hpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2014 Antonin Portelli
*
* 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_FitInterface_hpp_
#define Latan_FitInterface_hpp_
#include <latan/Global.hpp>
#include <latan/Minimizer.hpp>
BEGIN_NAMESPACE
/******************************************************************************
* base class for data fit *
******************************************************************************/
class FitInterface
{
public:
typedef Minimizer::Verbosity FitVerbosity;
public:
// constructors
FitInterface(void) = default;
FitInterface(const Index nData, const Index xDim, const Index yDim);
// destructor
virtual ~FitInterface(void) = default;
// access
void assumeXExact(const Index i, const bool isExact = true);
void assumeXXCorrelated(const Index i1, const Index i2,
const bool isCorrelated = true);
void assumeYYCorrelated(const Index j1, const Index j2,
const bool isCorrelated = true);
void assumeYXCorrelated(const Index j, const Index i,
const bool isCorrelated = true);
void assumeDataCorrelated(const Index k1, const Index k2,
const bool isCorrelated = true);
void fitPoint(const Index k, const bool isFitPoint = true);
void fitPointRange(const Index k1, const Index k2,
const bool isFitPoint = true);
void fitAllPoints(const bool isFitPoint = true);
Index getNData(void) const;
Index getNFitPoint(void) const;
Index getXDim(void) const;
Index getYDim(void) const;
Index getStatXDim(void) const;
void setFitInterface(const FitInterface &fitInterface);
void setNData(const Index nData);
void setXDim(const Index xDim);
void setYDim(const Index yDim);
void resize(const Index nData, const Index xDim, const Index yDim);
// test
bool isFitPoint(const Index k) const;
bool isXExact(const Index i) const;
bool isXXCorrelated(const Index i1, const Index i2) const;
bool isYYCorrelated(const Index j1, const Index j2) const;
bool isYXCorrelated(const Index j, const Index i) const;
bool isDataCorrelated(const Index k1, const Index k2) const;
private:
IVec isXExact_, isFitPoint_;
IMat isXXCorr_, isYYCorr_, isYXCorr_, isDataCorr_;
};
END_NAMESPACE
#endif // Latan_FitInterface_hpp_

View File

@ -35,6 +35,7 @@ liblatan_la_SOURCES = \
Global.cpp \
includes.hpp \
File.cpp \
FitInterface.cpp \
Mat.cpp \
Math.cpp \
MathInterpreter.cpp \
@ -53,6 +54,7 @@ liblatan_la_HEADERS = \
Chi2Function.hpp \
CompiledFunction.hpp\
Dataset.hpp \
FitInterface.hpp \
Function.hpp \
Global.hpp \
File.hpp \

View File

@ -32,6 +32,11 @@ double FitResult::getChi2(void) const
return chi2_;
}
Index FitResult::getNDof(void) const
{
return nDof_;
}
double FitResult::getChi2PerDof(void) const
{
return chi2_/static_cast<double>(nDof_);
@ -57,69 +62,6 @@ XYStatData::XYStatData(const Index nData, const Index xDim, const Index yDim)
}
// access //////////////////////////////////////////////////////////////////////
void XYStatData::assumeXExact(const Index i, const bool isExact)
{
isXExact_[i] = (isExact) ? 1 : 0;
}
void XYStatData::fitPoint(const Index i, const bool isFitPoint)
{
isFitPoint_[i] = (isFitPoint) ? 1 : 0;
}
void XYStatData::fitPointRange(const Index k1, const Index k2,
const bool isFitPoint)
{
int size = static_cast<int>(k2-k1+1);
isFitPoint_.segment(k1, size) = IVec::Constant(size, (isFitPoint) ? 1 : 0);
}
void XYStatData::fitAllPoints(const bool isFitPoint)
{
fitPointRange(0, getNData()-1, isFitPoint);
}
Index XYStatData::getNData(void) const
{
return x_.rows();
}
Index XYStatData::getNFitPoint(void) const
{
return isFitPoint_.sum();
}
Index XYStatData::getXDim(void) const
{
return x_.cols();
}
Index XYStatData::getYDim(void) const
{
return y_.cols();
}
Index XYStatData::getStatXDim(void) const
{
return isXExact_.size() - isXExact_.sum();
}
void XYStatData::setNData(const Index nData)
{
resize(nData, getXDim(), getYDim());
}
void XYStatData::setXDim(const Index xDim)
{
resize(getNData(), xDim, getYDim());
}
void XYStatData::setYDim(const Index yDim)
{
resize(getNData(), getXDim(), yDim);
}
void XYStatData::resize(const Index nData, const Index xDim, const Index yDim)
{
x_.conservativeResize(nData, xDim);
@ -263,17 +205,6 @@ ConstBlock<DMatBase> XYStatData::yxVar(const Index j, const Index i) const
return FULL_BLOCK(var_[yx](j, i));
}
// test ////////////////////////////////////////////////////////////////////////
bool XYStatData::isFitPoint(const Index k) const
{
return (isFitPoint_[k] == 1);
}
bool XYStatData::isXExact(const Index i) const
{
return (isXExact_[i] == 1);
}
// fit /////////////////////////////////////////////////////////////////////////
FitResult XYStatData::fit(const vector<const DoubleModel *> &modelVector,
Minimizer &minimizer, const DVec &init,

View File

@ -22,6 +22,7 @@
#include <latan/Global.hpp>
#include <latan/Chi2Function.hpp>
#include <latan/FitInterface.hpp>
#include <latan/Function.hpp>
#include <latan/Mat.hpp>
#include <latan/Minimizer.hpp>
@ -44,6 +45,7 @@ public:
virtual ~FitResult(void) = default;
// access
double getChi2(void) const;
Index getNDof(void) const;
double getChi2PerDof(void) const;
const DoubleFunction & getModel(const Index j = 0) const;
private:
@ -64,7 +66,6 @@ public:
yy = 1,
yx = 2
};
typedef Minimizer::Verbosity FitVerbosity;
public:
// constructors
XYStatData(void);
@ -72,19 +73,6 @@ public:
// destructor
virtual ~XYStatData(void) = default;
// access
void assumeXExact(const Index i, const bool isExact = true);
void fitPoint(const Index k, const bool isFitPoint = true);
void fitPointRange(const Index k1, const Index k2,
const bool isFitPoint = true);
void fitAllPoints(const bool isFitPoint = true);
Index getNData(void) const;
Index getNFitPoint(void) const;
Index getXDim(void) const;
Index getYDim(void) const;
Index getStatXDim(void) const;
void setNData(const Index nData);
void setXDim(const Index xDim);
void setYDim(const Index yDim);
void resize(const Index nData, const Index xDim,
const Index yDim);
Block<DMatBase> x(const PlaceHolder ph1, const PlaceHolder ph2);
@ -109,9 +97,6 @@ public:
ConstBlock<DMatBase> yyVar(const Index j1, const Index j2) const;
Block<DMatBase> yxVar(const Index j, const Index i);
ConstBlock<DMatBase> yxVar(const Index j, const Index i) const;
// test
bool isFitPoint(const Index k) const;
bool isXExact(const Index i) const;
// fit
FitResult fit(const std::vector<const DoubleModel *> &modelVector,
Minimizer &minimizer, const DVec &init,
@ -120,6 +105,7 @@ public:
FitResult fit(const DoubleModel &model, Minimizer &minimizer,
const DVec &init, const bool reinitChi2 = true,
const FitVerbosity verbosity = FitVerbosity::Silent);
private:
DMat x_, y_;
Mat<DMat> var_[3];