CMS 3D CMS Logo

LinearFitErrorsIn2Coord Class Reference

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

#include <CommonTools/Statistics/interface/LinearFitErrorsIn2Coord.h>

List of all members.

Public Member Functions

float intercept (const vector< float > &x, const vector< float > &y, int ndat, const vector< float > &sigx, const vector< float > &sigy) const
 Approached intercept computed with approached slope.
float slope (const vector< float > &x, const vector< float > &y, int ndat, const vector< float > &sigx, const 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.


Private Member Functions

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


Detailed Description

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

Definition at line 11 of file LinearFitErrorsIn2Coord.h.


Member Function Documentation

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

Approached intercept computed with approached slope.

Definition at line 33 of file LinearFitErrorsIn2Coord.cc.

References i, and slope().

00036 {
00037   
00038   float fs = slope(x, y, ndat, sigx, sigy);
00039   float fi = 0;
00040   float sumWi = 0;
00041   for (int i = 0; i != ndat; i++) {
00042     float wi = 1./(sigy[i] + fs*fs*sigx[i]);
00043     fi += wi*(y[i] - fs*x[i]);
00044     sumWi += wi;
00045   }
00046 
00047   return fi / sumWi;
00048 
00049 }

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

Approached slope:

Definition at line 5 of file LinearFitErrorsIn2Coord.cc.

References LinearFit::fit(), i, scale, funct::sqrt(), and variance().

Referenced by intercept().

00008 {
00009 
00010   // scale y and sigy, compute scaled errors
00011   float scale = sqrt(variance(x, ndat) / variance(y, ndat));
00012   vector<float> yScaled = y;
00013   vector<float> sigyScaled = sigy;
00014   vector<float> sig(ndat);
00015   for (int i = 0; i != ndat; i++) {
00016     yScaled[i] *= scale; 
00017     sigyScaled[i] *= scale;
00018     sig[i] = sqrt(sigx[i]*sigx[i] + sigyScaled[i]*sigyScaled[i]);
00019   }
00020 
00021   // usual linear fit
00022   LinearFit lf;
00023   float fs, fi, covss, covii, covsi;
00024   lf.fit(x, yScaled, ndat, sig, fs, fi, covss, covii, covsi);
00025 
00026   // unscale result
00027   fs /= scale;
00028   return fs;
00029 
00030 }

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

Definition at line 53 of file LinearFitErrorsIn2Coord.cc.

References i, and m1.

Referenced by slope().

00054 {
00055   double m1 = 0., m2 = 0.;
00056   for (int i = 0; i != ndat; i++) {
00057     m1 += x[i];
00058     m2 += x[i]*x[i];
00059   }
00060   m1 /= ndat;
00061   m2 /= ndat;
00062 
00063   return float(m2 - m1*m1);
00064 
00065 }


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:27:44 2009 for CMSSW by  doxygen 1.5.4