25 #ifndef LEAST_SQ_FITTER_1D 26 #define LEAST_SQ_FITTER_1D 63 template <
class FittingFunc>
81 double func1d(
double value,
int exponent);
87 Eigen::JacobiSVD<Eigen::MatrixXd> _svd;
103 template <
class FittingFunc>
106 : _x(x), _y(y), _s(s), _order(order) {
112 if (_nData != static_cast<int>(_y.
size())) {
115 if (_nData != static_cast<int>(_s.
size())) {
119 if (_nData < _order) {
125 Eigen::MatrixXd design(_nData, _order);
126 Eigen::VectorXd rhs(_nData);
127 for (
int i = 0; i < _nData; ++i) {
128 rhs[i] = y[i] / _s[i];
129 for (
int j = 0; j < _order; ++j) {
130 design(i, j) = func1d(_x[i], j) / _s[i];
133 _svd.compute(design, Eigen::ComputeThinU | Eigen::ComputeThinV);
134 _par = _svd.solve(rhs);
138 template <
class FittingFunc>
140 Eigen::VectorXd vec = Eigen::VectorXd::Zero(_order);
141 for (
int i = 0; i < _order; ++i) {
148 template <
class FittingFunc>
151 for (
int i = 0; i < _order; ++i) {
152 variance[i] = _svd.matrixV().row(i).dot(
153 (_svd.singularValues().array().inverse().square() * _svd.matrixV().col(i).array()).matrix());
155 return variance.sqrt().matrix();
159 template <
class FittingFunc>
163 FittingFunc func(_order - 1);
165 for (
int i = 0; i < _order; ++i) {
166 func.setParameter(i, _par(i));
172 template <
class FittingFunc>
181 template <
class FittingFunc>
188 for (
int i = 0; i < _nData; ++i) {
198 template <
class FittingFunc>
203 for (
int i = 0; i < _nData; ++i) {
204 double val = _y[i] - f(_x[i]);
206 chisq += pow(val, 2);
217 template <
class FittingFunc>
219 return getChiSq() / (double)(_nData - _order);
224 template <
class FittingFunc>
232 for (
int i = 0; i < _order; ++i) {
240 template <
class FittingFunc>
242 return (*_funcArray[exponent])(value);
double getReducedChiSq()
Return a measure of the goodness of fit.
std::vector< double > residuals()
Return a vector of residuals of the fit (i.e the difference between the input y values, and the value of the fitting function at that point.
Eigen::VectorXd getParams()
Return the best fit parameters as an Eigen::Matrix.
A base class for image defects.
Eigen::VectorXd getErrors()
Return the 1 sigma uncertainties in the best fit parameters as an Eigen::Matrix.
double valueAt(double x)
Calculate the value of the function at a given point.
FittingFunc getBestFitFunction()
Return the best fit polynomial as a lsst::afw::math::Function1 object.
LeastSqFitter1d(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &s, int order)
Fit a 1d polynomial to a set of data points z(x, y)
table::Key< table::Array< double > > coeff
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
double getChiSq()
Return a measure of the goodness of fit.
Reports errors that are due to events beyond the control of the program.
Fit an lsst::afw::math::Function1 object to a set of data points in one dimension.