1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-14 01:45:35 +00:00
LatAnalyze/lib/XYStatData.old.cpp

316 lines
8.5 KiB
C++
Raw Normal View History

2014-03-03 12:41:48 +00:00
/*
* XYStatData.cpp, part of LatAnalyze 3
*
2015-06-11 14:04:54 +01:00
* Copyright (C) 2013 - 2015 Antonin Portelli
2014-03-03 12:41:48 +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/>.
*/
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/XYStatData.hpp>
#include <LatAnalyze/includes.hpp>
#include <LatAnalyze/Math.hpp>
2014-03-03 12:41:48 +00:00
using namespace std;
using namespace Latan;
using namespace Math;
2014-03-03 12:41:48 +00:00
/******************************************************************************
* FitResult implementation *
******************************************************************************/
// access //////////////////////////////////////////////////////////////////////
double FitResult::getChi2(void) const
{
return chi2_;
}
2014-09-18 17:15:56 +01:00
double FitResult::getChi2PerDof(void) const
2014-03-12 19:17:15 +00:00
{
2014-09-18 17:15:56 +01:00
return chi2_/getNDof();
2014-03-12 19:17:15 +00:00
}
2014-09-18 17:15:56 +01:00
double FitResult::getNDof(void) const
2014-03-03 12:41:48 +00:00
{
2014-09-18 17:15:56 +01:00
return static_cast<double>(nDof_);
2014-03-03 12:41:48 +00:00
}
double FitResult::getPValue(void) const
{
return chi2PValue(getChi2(), getNDof());;
}
const DoubleFunction & FitResult::getModel(const Index j) const
{
return model_[static_cast<unsigned int>(j)];
}
2014-03-03 12:41:48 +00:00
/******************************************************************************
* XYStatData implementation *
******************************************************************************/
// constructor /////////////////////////////////////////////////////////////////
XYStatData::XYStatData(void)
: chi2_(*this)
{}
XYStatData::XYStatData(const Index nData, const Index xDim, const Index yDim)
: XYStatData()
{
resize(nData, xDim, yDim);
}
// access //////////////////////////////////////////////////////////////////////
void XYStatData::resize(const Index nData, const Index xDim, const Index yDim)
{
FitInterface::resize(nData, xDim, yDim);
x_.resize(nData, xDim);
y_.resize(nData, yDim);
var_[xx].resize(xDim, xDim);
var_[yy].resize(yDim, yDim);
var_[yx].resize(yDim, xDim);
2014-03-03 12:41:48 +00:00
FOR_MAT(var_[xx], i1, i2)
{
var_[xx](i1, i2).resize(nData, nData);
2014-03-03 12:41:48 +00:00
}
FOR_MAT(var_[yy], j1, j2)
{
var_[yy](j1, j2).resize(nData, nData);
2014-03-03 12:41:48 +00:00
}
FOR_MAT(var_[yx], j, i)
{
var_[yx](j, i).resize(nData, nData);
2014-03-03 12:41:48 +00:00
}
}
2014-04-03 09:03:46 +01:00
void XYStatData::reinitChi2(const bool doReinit)
{
reinitChi2_ = doReinit;
}
Block<MatBase<double>> XYStatData::x(const PlaceHolder ph1 __dumb,
const PlaceHolder ph2 __dumb)
2014-03-03 18:57:53 +00:00
{
return x_.block(0, 0, getNData(), getXDim());
}
ConstBlock<MatBase<double>> XYStatData::x(const PlaceHolder ph1 __dumb,
const PlaceHolder ph2 __dumb)
2015-02-23 18:11:37 +00:00
const
2014-03-03 18:57:53 +00:00
{
return x_.block(0, 0, getNData(), getXDim());
2014-03-03 12:41:48 +00:00
}
2015-02-23 18:11:37 +00:00
Block<MatBase<double>> XYStatData::x(const Index i,
const PlaceHolder ph2 __dumb)
2014-03-03 12:41:48 +00:00
{
2014-03-03 18:57:53 +00:00
return x_.block(0, i, getNData(), 1);
2014-03-03 12:41:48 +00:00
}
2015-02-23 18:11:37 +00:00
ConstBlock<MatBase<double>> XYStatData::x(const Index i,
const PlaceHolder ph2 __dumb)
2015-02-23 18:11:37 +00:00
const
2014-03-03 12:41:48 +00:00
{
2014-03-03 18:57:53 +00:00
return x_.block(0, i, getNData(), 1);
2014-03-03 12:41:48 +00:00
}
Block<MatBase<double>> XYStatData::x(const PlaceHolder ph1 __dumb,
2015-02-23 18:11:37 +00:00
const Index k)
2014-03-03 12:41:48 +00:00
{
2014-03-03 18:57:53 +00:00
return x_.block(k, 0, 1, getXDim());
2014-03-03 12:41:48 +00:00
}
ConstBlock<MatBase<double>> XYStatData::x(const PlaceHolder ph1 __dumb,
2015-02-23 18:11:37 +00:00
const Index k) const
2014-03-03 12:41:48 +00:00
{
2014-03-03 18:57:53 +00:00
return x_.block(k, 0, 1, getXDim());
2014-03-03 12:41:48 +00:00
}
2014-03-03 18:57:53 +00:00
double & XYStatData::x(const Index i, const Index k)
{
return x_(k, i);
}
const double & XYStatData::x(const Index i, const Index k) const
{
return x_(k, i);
}
Block<MatBase<double>> XYStatData::y(const PlaceHolder ph1 __dumb,
const PlaceHolder ph2 __dumb)
2014-03-03 18:57:53 +00:00
{
return y_.block(0, 0, getNData(), getYDim());
}
ConstBlock<MatBase<double>> XYStatData::y(const PlaceHolder ph1 __dumb,
const PlaceHolder ph2 __dumb)
2015-02-23 18:11:37 +00:00
const
2014-03-03 18:57:53 +00:00
{
return y_.block(0, 0, getNData(), getYDim());
}
2015-02-23 18:11:37 +00:00
Block<MatBase<double>> XYStatData::y(const Index j,
const PlaceHolder ph2 __dumb)
2014-03-03 18:57:53 +00:00
{
return y_.block(0, j, getNData(), 1);
}
2015-02-23 18:11:37 +00:00
ConstBlock<MatBase<double>> XYStatData::y(const Index j,
const PlaceHolder ph2 __dumb)
2015-02-23 18:11:37 +00:00
const
2014-03-03 18:57:53 +00:00
{
return y_.block(0, j, getNData(), 1);
}
Block<MatBase<double>> XYStatData::y(const PlaceHolder ph1 __dumb, const Index k)
2014-03-03 18:57:53 +00:00
{
return y_.block(k, 0, 1, getYDim());
}
ConstBlock<MatBase<double>> XYStatData::y(const PlaceHolder ph1 __dumb,
2015-02-23 18:11:37 +00:00
const Index k) const
2014-03-03 18:57:53 +00:00
{
return y_.block(k, 0, 1, getYDim());
}
double & XYStatData::y(const Index j, const Index k)
{
return y_(k, j);
}
const double & XYStatData::y(const Index j, const Index k) const
{
return y_(k, j);
}
#define FULL_BLOCK(m) (m).block(0, 0, (m).rows(), (m).cols())
2015-02-23 18:11:37 +00:00
Block<MatBase<double>> XYStatData::xxVar(const Index i1, const Index i2)
2014-03-03 12:41:48 +00:00
{
return FULL_BLOCK(var_[xx](i1, i2));
}
2015-02-23 18:11:37 +00:00
ConstBlock<MatBase<double>> XYStatData::xxVar(const Index i1,
const Index i2) const
2014-03-03 12:41:48 +00:00
{
return FULL_BLOCK(var_[xx](i1, i2));
}
2015-02-23 18:11:37 +00:00
Block<MatBase<double>> XYStatData::yyVar(const Index j1, const Index j2)
2014-03-03 12:41:48 +00:00
{
return FULL_BLOCK(var_[yy](j1, j2));
}
2015-02-23 18:11:37 +00:00
ConstBlock<MatBase<double>> XYStatData::yyVar(const Index j1,
const Index j2) const
2014-03-03 12:41:48 +00:00
{
return FULL_BLOCK(var_[yy](j1, j2));
}
2015-02-23 18:11:37 +00:00
Block<MatBase<double>> XYStatData::yxVar(const Index j, const Index i)
2014-03-03 12:41:48 +00:00
{
return FULL_BLOCK(var_[yx](j, i));
}
2015-02-23 18:11:37 +00:00
ConstBlock<MatBase<double>> XYStatData::yxVar(const Index j,
const Index i) const
2014-03-03 12:41:48 +00:00
{
return FULL_BLOCK(var_[yx](j, i));
}
// fit /////////////////////////////////////////////////////////////////////////
2014-04-03 09:03:46 +01:00
FitResult XYStatData::fit(Minimizer &minimizer, const DVec &init,
const vector<const DoubleModel *> &modelVector)
2014-03-03 12:41:48 +00:00
{
// initialization
chi2_.setModel(modelVector);
2014-04-03 09:03:46 +01:00
if (reinitChi2_)
2014-03-03 12:41:48 +00:00
{
chi2_.requestInit();
}
// initial parameters
const Index nPoint = getNFitPoint();
DVec fullInit = init;
Index is = 0, kf = 0;
2014-03-03 12:41:48 +00:00
fullInit.conservativeResize(chi2_.getNArg());
for (Index i = 0; i < getXDim(); ++i)
{
if (!isXExact(i))
2014-03-03 12:41:48 +00:00
{
kf = 0;
for (Index k = 0; k < getNData(); ++k)
{
if (isFitPoint(k))
{
fullInit(chi2_.getNPar() + nPoint*is + kf) = x(i, k);
kf++;
}
}
is++;
2014-03-03 12:41:48 +00:00
}
}
minimizer.setInit(fullInit);
2014-03-03 12:41:48 +00:00
// fit
2015-02-24 17:00:19 +00:00
DoubleFunction chi2 = chi2_.makeFunction(false);
FitResult result;
2015-02-24 17:00:19 +00:00
result = minimizer(chi2);
result.chi2_ = chi2(result);
result.nDof_ = chi2_.getNDof();
result.model_.resize(modelVector.size());
for (unsigned int j = 0; j < modelVector.size(); ++j)
{
2015-01-28 17:19:57 +00:00
result.model_[j] = modelVector[j]->fixPar(result);
}
2014-03-03 12:41:48 +00:00
return result;
}
// residuals ///////////////////////////////////////////////////////////////////
XYStatData XYStatData::getResiduals(const FitResult &fit) const
{
XYStatData res(*this);
for (Index j = 0; j < res.getYDim(); ++j)
{
const DoubleFunction &f = fit.getModel(j);
for (Index k = 0; k < res.getNData(); ++k)
{
res.y(j, k) -= f(res.x(_, k).transpose());
}
}
return res;
}
XYStatData XYStatData::getPartialResiduals(const FitResult &fit, const DVec &x,
const Index i) const
{
XYStatData res(*this);
DVec buf(x), xk;
for (Index j = 0; j < res.getYDim(); ++j)
{
const DoubleFunction &f = fit.getModel(j);
for (Index k = 0; k < res.getNData(); ++k)
{
buf(i) = res.x(i, k);
res.y(j, k) -= f(res.x(_, k).transpose()) - f(buf);
}
}
return res;
}