LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | List of all members
lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc > Class Template Reference

Fit an lsst::afw::math::Function1 object to a set of data points in two dimensions. More...

#include <LeastSqFitter2d.h>

Public Member Functions

 LeastSqFitter2d (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const std::vector< double > &s, int order)
 Fit a 2d polynomial to a set of data points z(x, y) More...
 
Eigen::MatrixXd getParams ()
 Build up a triangular matrix of the parameters. More...
 
Eigen::MatrixXd getErrors ()
 Companion function to getParams(). Returns uncertainties in the parameters as a matrix. More...
 
double valueAt (double x, double y)
 Return the value of the best fit function at a given position (x,y) More...
 
std::vector< double > residuals ()
 Return a vector of residuals of the fit (i.e the difference between the input z values, and the value of the fitting function at that point. More...
 
double getChiSq ()
 Return a measure of the goodness of fit. More...
 
double getReducedChiSq ()
 Return a measure of the goodness of fit. More...
 

Detailed Description

template<class FittingFunc>
class lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >

Fit an lsst::afw::math::Function1 object to a set of data points in two dimensions.

The class is templated over the kind of object to fit. Note that we fit the 1d object in each dimension, not the 2d one.

Input is a list of x and y ordinates for a set of points, the z coordinate, and the uncertainties, s. order is order of the polynomial to fit (e.g if the templated function is lsst::afw::math::PolynomialFunction1, then order=3 => fit a function of the form \(ax^2+bx+c\)

Template Parameters
FittingFuncThe 1d function to fit in both dimensions. Must inherit from lsst::afw::math::Function1
Parameters
xOrdinate of points to fit
yOrdinate of points to fit
zCo-ordinate of pionts to fit
s1 \(\sigma\) uncertainties in z
orderPolynomial order to fit
See also
LeastSqFitter1d

Definition at line 65 of file LeastSqFitter2d.h.

Constructor & Destructor Documentation

◆ LeastSqFitter2d()

template<class FittingFunc >
lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::LeastSqFitter2d ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< double > &  z,
const std::vector< double > &  s,
int  order 
)

Fit a 2d polynomial to a set of data points z(x, y)

Template Parameters
FittingFuncThe type of function to fit in each dimension. This function should extend the base class of lsst::afw::math::Function1. Note that this is a 1d function
Parameters
xvector of x positions of data
yvector of y positions of data
zValue of data for a given x,y. \(z = z_i = z_i(x_i, y_i)\)
sVector of measured uncertainties in the values of z
orderOrder of 2d function to fit

Definition at line 109 of file LeastSqFitter2d.h.

112  : _x(x), _y(y), _z(z), _s(s), _order(order), _nPar(0), _par(1) {
113  //_nPar, the number of terms to fix (x^2, xy, y^2 etc.) is \Sigma^(order+1) 1
114  _nPar = 0;
115  for (int i = 0; i < order; ++i) {
116  _nPar += i + 1;
117  }
118 
119  // Check input vectors are the same size
120  _nData = _x.size();
121  if (_nData != static_cast<int>(_y.size())) {
122  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "x and y vectors of different lengths");
123  }
124  if (_nData != static_cast<int>(_s.size())) {
125  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "x and s vectors of different lengths");
126  }
127  if (_nData != static_cast<int>(_z.size())) {
128  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "x and z vectors of different lengths");
129  }
130 
131  for (int i = 0; i < _nData; ++i) {
132  if (_s[i] == 0.0) {
133  std::string msg = "Illegal zero value for fit weight encountered.";
134  throw LSST_EXCEPT(pex::exceptions::RuntimeError, msg);
135  }
136  }
137 
138  if (_nData < _order) {
139  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Fewer data points than parameters");
140  }
141 
142  initFunctions();
143 
144  Eigen::MatrixXd design(_nData, _nPar);
145  Eigen::VectorXd rhs(_nData);
146  for (int i = 0; i < _nData; ++i) {
147  rhs[i] = z[i] / s[i];
148  for (int j = 0; j < _nPar; ++j) {
149  design(i, j) = func2d(_x[i], _y[i], j) / _s[i];
150  }
151  }
152  _svd.compute(design, Eigen::ComputeThinU | Eigen::ComputeThinV);
153  _par = _svd.solve(rhs);
154 }
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
double z
Definition: Match.cc:44
int y
Definition: SpanSet.cc:48
T size(T... args)
table::Key< int > order

Member Function Documentation

◆ getChiSq()

template<class FittingFunc >
double lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getChiSq

Return a measure of the goodness of fit.

\[ \chi^2 = \sum \left( \frac{z_i - f(x_i, y_i)}{s_i} \right)^2 \]

Definition at line 186 of file LeastSqFitter2d.h.

186  {
187  double chisq = 0;
188  for (int i = 0; i < _nData; ++i) {
189  double val = _z[i] - valueAt(_x[i], _y[i]);
190  val /= _s[i];
191  chisq += pow(val, 2);
192  }
193 
194  return chisq;
195 }
double valueAt(double x, double y)
Return the value of the best fit function at a given position (x,y)
T pow(T... args)
ImageT val
Definition: CR.cc:146

◆ getErrors()

template<class FittingFunc >
Eigen::MatrixXd lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getErrors

Companion function to getParams(). Returns uncertainties in the parameters as a matrix.

Definition at line 235 of file LeastSqFitter2d.h.

235  {
236  Eigen::ArrayXd variance(_nPar);
237  for (int i = 0; i < _nPar; ++i) {
238  variance[i] = _svd.matrixV().row(i).dot(
239  (_svd.singularValues().array().inverse().square() * _svd.matrixV().col(i).array()).matrix());
240  }
241  return expandParams(variance.sqrt().matrix());
242 }
afw::table::Key< afw::table::Array< VariancePixelT > > variance

◆ getParams()

template<class FittingFunc >
Eigen::MatrixXd lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getParams

Build up a triangular matrix of the parameters.

The shape of the matrix is such that the values correspond to the coefficients of the following polynomials
1 y y^2 y^3
x xy xy^2 0
x^2 x^2y 0 0
x^3 0 0 0
(order==4)
where row x column < order

Definition at line 166 of file LeastSqFitter2d.h.

166  {
167  return expandParams(_par);
168 }

◆ getReducedChiSq()

template<class FittingFunc >
double lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::getReducedChiSq

Return a measure of the goodness of fit.

\[ \chi_r^2 = \sum \left( \frac{z_i - f(x_i, y_i)}{s} \right)^2 \div (N-p) \]

Where \( N \) is the number of data points, and \( p \) is the number of parameters in the fit

Definition at line 203 of file LeastSqFitter2d.h.

203  {
204  return getChiSq() / (double)(_nData - _nPar);
205 }
double getChiSq()
Return a measure of the goodness of fit.

◆ residuals()

template<class FittingFunc >
std::vector< double > lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::residuals

Return a vector of residuals of the fit (i.e the difference between the input z values, and the value of the fitting function at that point.

Definition at line 222 of file LeastSqFitter2d.h.

222  {
224  out.reserve(_nData);
225 
226  for (int i = 0; i < _nData; ++i) {
227  out.push_back(_z[i] - valueAt(_x[i], _y[i]));
228  }
229 
230  return out;
231 }
T push_back(T... args)
T reserve(T... args)

◆ valueAt()

template<class FittingFunc >
double lsst::meas::astrom::sip::LeastSqFitter2d< FittingFunc >::valueAt ( double  x,
double  y 
)

Return the value of the best fit function at a given position (x,y)

Definition at line 209 of file LeastSqFitter2d.h.

209  {
210  double val = 0;
211 
212  // Sum the values of the different orders to get the value of the fitted function
213  for (int i = 0; i < _nPar; ++i) {
214  val += _par[i] * func2d(x, y, i);
215  }
216  return val;
217 }

The documentation for this class was generated from the following file: