CMS 3D CMS Logo

Public Member Functions | Private Member Functions

LinearFitErrorsIn2Coord Class Reference

#include <LinearFitErrorsIn2Coord.h>

List of all members.

Public Member Functions

float intercept (const std::vector< float > &x, const std::vector< float > &y, int ndat, const std::vector< float > &sigx, const std::vector< float > &sigy) const
float slope (const std::vector< float > &x, const std::vector< float > &y, int ndat, const std::vector< float > &sigx, const std::vector< float > &sigy) const

Private Member Functions

float variance (const std::vector< float > &x, int ndat) const

Detailed Description

Straight line fit for data with errors on both coordinates source: Numerical Recipes

Definition at line 10 of file LinearFitErrorsIn2Coord.h.


Member Function Documentation

float LinearFitErrorsIn2Coord::intercept ( const std::vector< float > &  x,
const std::vector< float > &  y,
int  ndat,
const std::vector< float > &  sigx,
const std::vector< float > &  sigy 
) const

Approached intercept computed with approached slope

Definition at line 33 of file LinearFitErrorsIn2Coord.cc.

References i, and slope().

{
  
  float fs = slope(x, y, ndat, sigx, sigy);
  float fi = 0;
  float sumWi = 0;
  for (int i = 0; i != ndat; i++) {
    float wi = 1./(sigy[i] + fs*fs*sigx[i]);
    fi += wi*(y[i] - fs*x[i]);
    sumWi += wi;
  }

  return fi / sumWi;

}
float LinearFitErrorsIn2Coord::slope ( const std::vector< float > &  x,
const std::vector< float > &  y,
int  ndat,
const std::vector< float > &  sigx,
const std::vector< float > &  sigy 
) const

Approached slope:

  • rescale y and sigy by var(x)/var(y)
  • fit a straight line with weights derived from the scaled sum sigx^2 + sigy^2

Definition at line 5 of file LinearFitErrorsIn2Coord.cc.

References LinearFit::fit(), i, pileupReCalc_HLTpaths::scale, mathSSE::sqrt(), variance(), and detailsBasic3DVector::y.

Referenced by intercept().

{

  // scale y and sigy, compute scaled errors
  float scale = sqrt(variance(x, ndat) / variance(y, ndat));
  std::vector<float> yScaled = y;
  std::vector<float> sigyScaled = sigy;
  std::vector<float> sig(ndat);
  for (int i = 0; i != ndat; i++) {
    yScaled[i] *= scale; 
    sigyScaled[i] *= scale;
    sig[i] = sqrt(sigx[i]*sigx[i] + sigyScaled[i]*sigyScaled[i]);
  }

  // usual linear fit
  LinearFit lf;
  float fs, fi, covss, covii, covsi;
  lf.fit(x, yScaled, ndat, sig, fs, fi, covss, covii, covsi);

  // unscale result
  fs /= scale;
  return fs;

}
float LinearFitErrorsIn2Coord::variance ( const std::vector< float > &  x,
int  ndat 
) const [private]

Definition at line 53 of file LinearFitErrorsIn2Coord.cc.

References i.

Referenced by slope().

{
  double m1 = 0., m2 = 0.;
  for (int i = 0; i != ndat; i++) {
    m1 += x[i];
    m2 += x[i]*x[i];
  }
  m1 /= ndat;
  m2 /= ndat;

  return float(m2 - m1*m1);

}