LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc > Class Template Reference

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

#include <LeastSqFitter1d.h>

Public Member Functions

 LeastSqFitter1d (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &s, unsigned int order)
 
Eigen::VectorXd getParams ()
 Return the best fit parameters as an Eigen::Matrix. More...
 
Eigen::VectorXd getErrors ()
 Return the 1 sigma uncertainties in the best fit parameters as an Eigen::Matrix. More...
 
FittingFunc getBestFitFunction ()
 Return the best fit polynomial as a lsst::afw::math::Function1 object. More...
 
double valueAt (double x)
 Calculate the value of the function at a given point. More...
 
std::vector< double > residuals ()
 
double getChiSq ()
 Return a measure of the goodness of fit.

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

. More...

 
double getReducedChiSq ()
 Return a measure of the goodness of fit.

\[ \chi_r^2 = \sum \left( \frac{y_i - f(x_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. More...

 

Private Member Functions

void initFunctions ()
 
double func1d (double value, int exponent)
 

Private Attributes

std::vector< double > _x
 
std::vector< double > _y
 
std::vector< double > _s
 
int _order
 
int _nData
 
Eigen::JacobiSVD< Eigen::MatrixXd > _svd
 
Eigen::VectorXd _par
 
std::vector< boost::shared_ptr
< FittingFunc > > 
_funcArray
 

Detailed Description

template<class FittingFunc>
class lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >

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

The class is templated over the kind of object to fit.

Input is a list of x ordinates for a set of points, the y 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
yCo-ordinate of pionts to fit
s1 \(\sigma\) uncertainties in z
orderPolynomial order to fit
See Also
LeastSqFitter1d

Definition at line 69 of file LeastSqFitter1d.h.

Constructor & Destructor Documentation

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

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

Template Parameters
FittingFuncThe type of function to fit. This function extends the base class of lsst::afw::math::Function1
Parameters
xvector of x positions of data
yvector of y positions of data
sVector of measured uncertainties in the values of z
orderOrder of 2d function to fit

Definition at line 110 of file LeastSqFitter1d.h.

111  :
112  _x(x), _y(y), _s(s), _order(order) {
113 
114  if (order == 0) {
115  throw LSST_EXCEPT(except::RuntimeError, "Fit order must be >= 1");
116  }
117 
118  _nData = _x.size();
119  if (_nData != static_cast<int>(_y.size())) {
120  throw LSST_EXCEPT(except::RuntimeError, "x and y vectors of different lengths");
121  }
122  if (_nData != static_cast<int>(_s.size())) {
123  throw LSST_EXCEPT(except::RuntimeError, "x and s vectors of different lengths");
124  }
125 
126  if (_nData < _order) {
127  throw LSST_EXCEPT(except::RuntimeError, "Fewer data points than parameters");
128  }
129 
130  initFunctions();
131 
132  Eigen::MatrixXd design(_nData, _order);
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) {
137  design(i, j) = func1d(_x[i], j) / _s[i];
138  }
139  }
140  _svd.compute(design, Eigen::ComputeThinU | Eigen::ComputeThinV);
141  _par = _svd.solve(rhs);
142 }
Eigen::JacobiSVD< Eigen::MatrixXd > _svd
double func1d(double value, int exponent)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46

Member Function Documentation

template<class FittingFunc >
double lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::func1d ( double  value,
int  exponent 
)
private

Definition at line 254 of file LeastSqFitter1d.h.

254  {
255  return (*_funcArray[exponent])(value);
256 }
std::vector< boost::shared_ptr< FittingFunc > > _funcArray
template<class FittingFunc >
FittingFunc lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::getBestFitFunction ( )

Return the best fit polynomial as a lsst::afw::math::Function1 object.

Definition at line 170 of file LeastSqFitter1d.h.

170  {
171 
172  //FittingFunc and LeastSqFitter disagree on the definition of order of a function.
173  //LSF says that a linear function is order 2 (two coefficients), FF says only 1
174  FittingFunc func(_order - 1);
175 
176  for (int i = 0; i < _order; ++i) {
177  func.setParameter(i, _par(i));
178  }
179  return func;
180 }
template<class FittingFunc >
double lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::getChiSq ( )

Return a measure of the goodness of fit.

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

.

Definition at line 211 of file LeastSqFitter1d.h.

211  {
212  FittingFunc f = getBestFitFunction();
213 
214  double chisq = 0;
215  for (int i = 0; i < _nData; ++i) {
216  double val = _y[i] - f(_x[i]);
217  val /= _s[i];
218  chisq += pow(val, 2);
219  }
220 
221  return chisq;
222 }
FittingFunc getBestFitFunction()
Return the best fit polynomial as a lsst::afw::math::Function1 object.
bool val
double chisq
template<class FittingFunc >
Eigen::VectorXd lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::getErrors ( )

Return the 1 sigma uncertainties in the best fit parameters as an Eigen::Matrix.

Definition at line 158 of file LeastSqFitter1d.h.

158  {
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()
163  );
164  }
165  return variance.sqrt().matrix();
166 }
Eigen::JacobiSVD< Eigen::MatrixXd > _svd
template<class FittingFunc >
Eigen::VectorXd lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::getParams ( )

Return the best fit parameters as an Eigen::Matrix.

Definition at line 147 of file LeastSqFitter1d.h.

147  {
148 
149  Eigen::VectorXd vec = Eigen::VectorXd::Zero(_order);
150  for (int i = 0; i < _order; ++i) {
151  vec(i) = _par(i);
152  }
153  return vec;
154 }
template<class FittingFunc >
double lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::getReducedChiSq ( )

Return a measure of the goodness of fit.

\[ \chi_r^2 = \sum \left( \frac{y_i - f(x_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 230 of file LeastSqFitter1d.h.

230  {
231  return getChiSq()/(double) (_nData - _order);
232 }
double getChiSq()
Return a measure of the goodness of fit. .
template<class FittingFunc >
void lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::initFunctions ( )
private

Initialise the array of functions. _funcArray[i] is a object of type math::Function1 of order _norder

Definition at line 238 of file LeastSqFitter1d.h.

238  {
239 
240  _funcArray.reserve(_order);
241 
242  std::vector<double> coeff;
243  coeff.reserve( _order);
244 
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);
249  coeff[i] = 0.0;
250  coeff.push_back(1.0); //coeff now looks like [0,0,...,0,1]
251  }
252 }
std::vector< boost::shared_ptr< FittingFunc > > _funcArray
template<class FittingFunc >
std::vector< double > lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::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.

Definition at line 194 of file LeastSqFitter1d.h.

194  {
195  std::vector<double> out;
196  out.reserve(_nData);
197 
198  FittingFunc f = getBestFitFunction();
199 
200  for (int i = 0; i < _nData; ++i) {
201  out.push_back(_y[i] - f(_x[i]));
202  }
203 
204  return out;
205 }
FittingFunc getBestFitFunction()
Return the best fit polynomial as a lsst::afw::math::Function1 object.
template<class FittingFunc >
double lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::valueAt ( double  x)

Calculate the value of the function at a given point.

Definition at line 184 of file LeastSqFitter1d.h.

184  {
185  FittingFunc f = getBestFitFunction();
186 
187  return f(x);
188 }
FittingFunc getBestFitFunction()
Return the best fit polynomial as a lsst::afw::math::Function1 object.
double x

Member Data Documentation

template<class FittingFunc >
std::vector<boost::shared_ptr<FittingFunc> > lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_funcArray
private

Definition at line 97 of file LeastSqFitter1d.h.

template<class FittingFunc >
int lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_nData
private

Definition at line 92 of file LeastSqFitter1d.h.

template<class FittingFunc >
int lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_order
private

Definition at line 91 of file LeastSqFitter1d.h.

template<class FittingFunc >
Eigen::VectorXd lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_par
private

Definition at line 95 of file LeastSqFitter1d.h.

template<class FittingFunc >
std::vector<double> lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_s
private

Definition at line 90 of file LeastSqFitter1d.h.

template<class FittingFunc >
Eigen::JacobiSVD<Eigen::MatrixXd> lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_svd
private

Definition at line 94 of file LeastSqFitter1d.h.

template<class FittingFunc >
std::vector<double> lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_x
private

Definition at line 90 of file LeastSqFitter1d.h.

template<class FittingFunc >
std::vector<double> lsst::meas.astrom.sip::LeastSqFitter1d< FittingFunc >::_y
private

Definition at line 90 of file LeastSqFitter1d.h.


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