00001 #include "CommonTools/Statistics/interface/LinearFitErrorsIn2Coord.h"
00002 #include "CommonTools/Statistics/interface/LinearFit.h"
00003 #include <cmath>
00004
00005 float LinearFitErrorsIn2Coord::slope(
00006 const vector<float> & x, const vector<float> & y, int ndat,
00007 const vector<float> & sigx, const vector<float> & sigy) const
00008 {
00009
00010
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
00022 LinearFit lf;
00023 float fs, fi, covss, covii, covsi;
00024 lf.fit(x, yScaled, ndat, sig, fs, fi, covss, covii, covsi);
00025
00026
00027 fs /= scale;
00028 return fs;
00029
00030 }
00031
00032
00033 float LinearFitErrorsIn2Coord::intercept(
00034 const vector<float> & x, const vector<float> & y, int ndat,
00035 const vector<float> & sigx, const vector<float> & sigy) const
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 }
00050
00051
00052 float
00053 LinearFitErrorsIn2Coord::variance(const vector<float> & x, int ndat) const
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 }