26 #ifndef LEAST_SQ_FITTER_1D
27 #define LEAST_SQ_FITTER_1D
32 #include "boost/shared_ptr.hpp"
40 namespace except = lsst::pex::exceptions;
41 namespace pexLogging = lsst::pex::logging;
72 const std::vector<double> &s,
88 double func1d(
double value,
int exponent);
94 Eigen::JacobiSVD<Eigen::MatrixXd>
_svd;
111 const std::vector<double> &
y,
const std::vector<double> &s,
unsigned int order) :
112 _x(x), _y(y), _s(s), _order(order) {
115 throw LSST_EXCEPT(except::RuntimeError,
"Fit order must be >= 1");
119 if (
_nData != static_cast<int>(
_y.size())) {
120 throw LSST_EXCEPT(except::RuntimeError,
"x and y vectors of different lengths");
122 if (
_nData != static_cast<int>(
_s.size())) {
123 throw LSST_EXCEPT(except::RuntimeError,
"x and s vectors of different lengths");
127 throw LSST_EXCEPT(except::RuntimeError,
"Fewer data points than parameters");
133 Eigen::VectorXd rhs(
_nData);
134 for (
int i = 0; i <
_nData; ++i) {
135 rhs[i] = y[i] /
_s[i];
136 for (
int j = 0; j <
_order; ++j) {
140 _svd.compute(design, Eigen::ComputeThinU | Eigen::ComputeThinV);
149 Eigen::VectorXd vec = Eigen::VectorXd::Zero(_order);
150 for (
int i = 0; i < _order; ++i) {
159 Eigen::ArrayXd variance(_order);
160 for (
int i = 0; i < _order; ++i) {
161 variance[i] =
_svd.matrixV().row(i).dot(
162 (
_svd.singularValues().array().inverse().square() *
_svd.matrixV().col(i).array()).matrix()
165 return variance.sqrt().matrix();
174 FittingFunc func(_order - 1);
176 for (
int i = 0; i < _order; ++i) {
177 func.setParameter(i, _par(i));
185 FittingFunc f = getBestFitFunction();
195 std::vector<double> out;
198 FittingFunc f = getBestFitFunction();
200 for (
int i = 0; i < _nData; ++i) {
201 out.push_back(_y[i] - f(_x[i]));
212 FittingFunc f = getBestFitFunction();
215 for (
int i = 0; i < _nData; ++i) {
216 double val = _y[i] - f(_x[i]);
218 chisq += pow(val, 2);
231 return getChiSq()/(double) (_nData - _order);
240 _funcArray.reserve(_order);
242 std::vector<double> coeff;
243 coeff.reserve( _order);
245 coeff.push_back(1.0);
246 for (
int i = 0; i < _order; ++i) {
247 boost::shared_ptr<FittingFunc> p(
new FittingFunc(coeff));
248 _funcArray.push_back(p);
250 coeff.push_back(1.0);
255 return (*_funcArray[exponent])(value);
Eigen::VectorXd getErrors()
Return the 1 sigma uncertainties in the best fit parameters as an Eigen::Matrix.
FittingFunc getBestFitFunction()
Return the best fit polynomial as a lsst::afw::math::Function1 object.
definition of the Trace messaging facilities
double getReducedChiSq()
Return a measure of the goodness of fit. Where is the number of data points, and is the number of ...
Define a collection of useful Functions.
LeastSqFitter1d(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &s, unsigned int order)
Eigen::JacobiSVD< Eigen::MatrixXd > _svd
double func1d(double value, int exponent)
#define LSST_EXCEPT(type,...)
std::vector< double > residuals()
double valueAt(double x)
Calculate the value of the function at a given point.
Eigen::JacobiSVD< Eigen::MatrixXd > _svd
std::vector< boost::shared_ptr< FittingFunc > > _funcArray
Eigen::VectorXd getParams()
Return the best fit parameters as an Eigen::Matrix.
double getChiSq()
Return a measure of the goodness of fit. .
Fit an lsst::afw::math::Function1 object to a set of data points in one dimension.