BCGraphFitter Class Reference

A class for fitting graphs with functions. More...

#include <BCGraphFitter.h>

Inherits BCModel.

Collaboration diagram for BCGraphFitter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

Constructors and destructors



 BCGraphFitter (TGraphErrors *graph, TF1 *func)
 BCGraphFitter ()
 ~BCGraphFitter ()
Member functions (miscellaneous methods)



void DrawFit (const char *options="", bool flaglegend=false)
int Fit (TGraphErrors *graph, TF1 *func)
int Fit ()
double FitFunction (std::vector< double > x, std::vector< double > parameters)
double LogAPrioriProbability (std::vector< double > parameters)
double LogLikelihood (std::vector< double > parameters)
Member functions (get)



TGraph * GetErrorBand ()
TF1 * GetFitFunction ()
TGraphErrors * GetGraph ()
TGraph * GetGraphFitFunction ()
Member functions (set)



int SetFitFunction (TF1 *func)
int SetGraph (TGraphErrors *graph)

Private Attributes

TGraph * fErrorBand
TF1 * fFitFunction
TGraphErrors * fGraph
TGraph * fGraphFitFunction

Detailed Description

A class for fitting graphs with functions.

Author:
Daniel Kollar
Kevin Kröninger
Version:
1.0
Date:
2008 This class allows fitting of a TGraphErrors using a TF1 function. It doeasn't take the x uncertainties into account. For that look at BCGraphXFitter (not yet implemented).

Definition at line 34 of file BCGraphFitter.h.


Constructor & Destructor Documentation

BCGraphFitter::BCGraphFitter (  ) 

Default constructor

Definition at line 25 of file BCGraphFitter.cxx.

00025                              : BCModel("GraphFitter")
00026 {
00027    fGraph = 0;
00028    fFitFunction = 0;
00029    fErrorBand = 0;
00030    fGraphFitFunction = 0;
00031 
00032    this -> MCMCSetNIterationsRun(2000);
00033 
00034    this -> SetFillErrorBand();
00035 }

BCGraphFitter::BCGraphFitter ( TGraphErrors *  graph,
TF1 *  func 
)

Constructor

Parameters:
graph pointer to TGraphErrors
func pointer to TF1

Definition at line 39 of file BCGraphFitter.cxx.

00039                                                              : BCModel("GraphFitter")
00040 {
00041    fGraph = 0;
00042    fFitFunction = 0;
00043    fErrorBand = 0;
00044    fGraphFitFunction = 0;
00045 
00046    this -> MCMCSetNIterationsRun(2000);
00047 
00048    this -> SetGraph(graph);
00049    this -> SetFitFunction(func);
00050 
00051    this -> SetFillErrorBand();
00052 }

BCGraphFitter::~BCGraphFitter (  ) 

The default destructor.

Definition at line 174 of file BCGraphFitter.cxx.

00175 {}


Member Function Documentation

void BCGraphFitter::DrawFit ( const char *  options = "",
bool  flaglegend = false 
)

Draw the fit in the current pad.

Definition at line 277 of file BCGraphFitter.cxx.

00278 {
00279    if (!fGraph)
00280    {
00281       BCLog::Out(BCLog::error, BCLog::error,"BCGraphFitter::DrawFit() : TGraphErrors not defined.");
00282       return;
00283    }
00284 
00285    if (!fFitFunction)
00286    {
00287       BCLog::Out(BCLog::error, BCLog::error,"BCGraphFitter::DrawFit() : Fit function not defined.");
00288       return;
00289    }
00290 
00291    // check wheather options contain "same"
00292    TString opt = options;
00293    opt.ToLower();
00294 
00295    // if not same, draw the histogram first to get the axes
00296    if(!opt.Contains("same"))
00297       fGraph -> Draw("ap");
00298 
00299    // draw the error band as central 68% probability interval
00300    fErrorBand = this -> GetErrorBandGraph(0.16, 0.84);
00301    fErrorBand -> Draw("f same");
00302 
00303    // draw the fit function on top
00304    fGraphFitFunction = this -> GetFitFunctionGraph( this->GetBestFitParameters() );
00305    fGraphFitFunction -> SetLineColor(kRed);
00306    fGraphFitFunction -> SetLineWidth(2);
00307    fGraphFitFunction -> Draw("l same");
00308 
00309    // now draw the histogram again since it was covered by the band and
00310    // the best fit
00311    fGraph -> Draw("p same");
00312 
00313    // draw legend
00314    if (flaglegend)
00315    {
00316       TLegend * legend = new TLegend(0.25, 0.75, 0.55, 0.95);
00317       legend -> SetBorderSize(0);
00318       legend -> SetFillColor(kWhite);
00319       legend -> AddEntry(fGraph, "Data", "P");
00320       legend -> AddEntry(fGraphFitFunction, "Best fit", "L");
00321       legend -> AddEntry(fErrorBand, "Error band", "F");
00322       legend -> Draw();
00323    }
00324 
00325    gPad -> RedrawAxis();
00326 }

int BCGraphFitter::Fit ( TGraphErrors *  graph,
TF1 *  func 
)

Performs the fit of the graph with the function.

Parameters:
graph pointer to TGraphErrors object
func pointer to TF1 object
Returns:
An error code.

Definition at line 235 of file BCGraphFitter.cxx.

00236 {
00237    // set graph
00238    if (!this -> SetGraph(graph))
00239       return 0;
00240 
00241    // set function
00242    if (!this -> SetFitFunction(func))
00243       return 0;
00244 
00245    // check setup
00246    BCLog::Out(BCLog::detail,BCLog::detail,
00247          Form("Fitting %d data points with function of %d parameters",GetNDataPoints(),GetNParameters()));
00248    if(GetNDataPoints() <= (int)GetNParameters())
00249    {
00250       BCLog::Out(BCLog::warning,BCLog::warning,
00251             Form("Number of parameters (%d) lower than or equal to number of points (%d)."
00252             , GetNParameters(), GetNDataPoints()));
00253       BCLog::Out(BCLog::warning,BCLog::warning,"Fit doesn't have much meaning.");
00254    }
00255 
00256    // perform marginalization
00257    this -> MarginalizeAll();
00258 
00259    // maximize posterior probability, using the best-fit values close
00260    // to the global maximum from the MCMC
00261    this -> FindModeMinuit( this -> GetBestFitParameters(), -1);
00262 
00263    // calculate p-value from the chi2 probability
00264    // this is only valid for a product of gaussiang which is the case for
00265    // the BCGraphFitter
00266    this -> GetPvalueFromChi2(this -> GetBestFitParameters(), 3);
00267    this -> GetPvalueFromChi2NDoF(this -> GetBestFitParameters(), 3);
00268 
00269    // print summary to screen
00270    this -> PrintShortFitSummary(1);
00271 
00272    return 1;
00273 }

int BCGraphFitter::Fit (  )  [inline]

Performs the fit. The graph and the function has to beset beforehand.

Returns:
An error code.

Definition at line 117 of file BCGraphFitter.h.

00118          { return this -> Fit(fGraph, fFitFunction); };

double BCGraphFitter::FitFunction ( std::vector< double >  x,
std::vector< double >  parameters 
) [virtual]

Returns the value of the 1D fit function for a given set of parameters at a given x.

Parameters:
x point to calculate the function value at
parameters parameters of the function

Reimplemented from BCIntegrate.

Definition at line 225 of file BCGraphFitter.cxx.

00226 {
00227    // set the parameters of the function
00228    fFitFunction -> SetParameters(&params[0]);
00229 
00230    return fFitFunction -> Eval(x[0]);
00231 }

TGraph* BCGraphFitter::GetErrorBand (  )  [inline]
Returns:
pointer to the error band

Definition at line 72 of file BCGraphFitter.h.

00073          { return fErrorBand; };

TF1* BCGraphFitter::GetFitFunction (  )  [inline]
Returns:
pointer to TF1

Definition at line 67 of file BCGraphFitter.h.

00068          { return fFitFunction; };

TGraphErrors* BCGraphFitter::GetGraph (  )  [inline]
Returns:
pointer to TGraphErrors

Definition at line 62 of file BCGraphFitter.h.

00063          { return fGraph; };

TGraph* BCGraphFitter::GetGraphFitFunction (  )  [inline]
Returns:
pointer to a graph for the fit function

Definition at line 77 of file BCGraphFitter.h.

00078          { return fGraphFitFunction; };

double BCGraphFitter::LogAPrioriProbability ( std::vector< double >  parameters  )  [virtual]

The log of the prior probability. It is set to be flat in all parameters.

Parameters:
parameters vector containing the parameter values

Reimplemented from BCModel.

Definition at line 179 of file BCGraphFitter.cxx.

00180 {
00181    // using flat probability in all parameters
00182    double logprob = 0.;
00183    for(unsigned int i=0; i < this -> GetNParameters(); i++)
00184       logprob -= log(this -> GetParameter(i) -> GetRangeWidth());
00185 
00186    return logprob;
00187 }

double BCGraphFitter::LogLikelihood ( std::vector< double >  parameters  )  [virtual]

The log of the conditional probability.

Parameters:
parameters vector containing the parameter values

Reimplemented from BCModel.

Definition at line 191 of file BCGraphFitter.cxx.

00192 {
00193    // initialize probability
00194    double logl = 0.;
00195 
00196    // set the parameters of the function
00197    // passing the pointer to first element of the vector is
00198    // not completely safe as there might be an implementation where
00199    // the vector elements are not stored consecutively in memory.
00200    // however it is much faster than copying the contents, especially
00201    // for large number of parameters
00202    fFitFunction -> SetParameters(&params[0]);
00203 
00204    // loop over all data points
00205    for (int i = 0; i < this -> GetNDataPoints(); i++)
00206    {
00207       std::vector <double> x = GetDataPoint(i) -> GetValues();
00208 
00209       // her we ignore the errors on x even when they're available
00210       // i.e. we treat them just as the region specifiers
00211       double y = x[1];
00212       double yerr = x[3];
00213       double yexp = this -> FitFunction(x,params);
00214 
00215       // calculate log of probability assuming
00216       // a Gaussian distribution for each point
00217       logl += BCMath::LogGaus(y, yexp, yerr, true);
00218    }
00219 
00220    return logl;
00221 }

int BCGraphFitter::SetFitFunction ( TF1 *  func  ) 
Parameters:
func pointer to TF1 object

Definition at line 134 of file BCGraphFitter.cxx.

00135 {
00136    if(!func)
00137    {
00138       BCLog::Out(BCLog::error,BCLog::error,"BCGraphFitter::SetFitFunction() : TF1 not created.");
00139       return 0;
00140    }
00141 
00142    // get the new number of parameters
00143    int npar = func -> GetNpar();
00144    if(!npar)
00145    {
00146       BCLog::Out(BCLog::error,BCLog::error,"BCGraphFitter::SetFitFunction() : TF1 has zero parameters. Not able to fit.");
00147       return 0;
00148    }
00149 
00150    // set the function
00151    fFitFunction = func;
00152 
00153    // update the model name to contain the function name
00154    this -> SetName(TString::Format("GraphFitter with %s",fFitFunction->GetName()));
00155 
00156    // reset parameters
00157    fParameterSet -> clear();
00158 
00159    // add parameters
00160    for (int i = 0; i < npar; ++i)
00161    {
00162       double xmin;
00163       double xmax;
00164       fFitFunction -> GetParLimits(i, xmin, xmax);
00165 
00166       this -> AddParameter(fFitFunction->GetParName(i), xmin, xmax);
00167    }
00168 
00169    return this -> GetNParameters();
00170 }

int BCGraphFitter::SetGraph ( TGraphErrors *  graph  ) 
Parameters:
graph pointer to TGraphErrors object

Definition at line 56 of file BCGraphFitter.cxx.

00057 {
00058    if(!graph)
00059    {
00060       BCLog::Out(BCLog::error,BCLog::error,"BCGraphFitter::SetGraph() : TGraphErrors not created.");
00061       return 0;
00062    }
00063 
00064    int npoints = graph -> GetN();
00065    if(!npoints)
00066    {
00067       BCLog::Out(BCLog::error,BCLog::error,"BCGraphFitter::SetGraph() : TGraphErrors is empty.");
00068       return 0;
00069    }
00070    else if(npoints==1)
00071    {
00072       BCLog::Out(BCLog::error,BCLog::error,"BCGraphFitter::SetGraph() : TGraphErrors has only one point. Not able to fit.");
00073       return 0;
00074    }
00075 
00076    fGraph = graph;
00077 
00078    double * x = fGraph -> GetX();
00079    double * y = fGraph -> GetY();
00080    double * ex = fGraph -> GetEX();
00081    double * ey = fGraph -> GetEY();
00082 
00083    if(!ey)
00084    {
00085       BCLog::Out(BCLog::error,BCLog::error,"BCGraphFitter::SetGraph() : TGraphErrors has NO errors set on Y. Not able to fit.");
00086       return 0;
00087    }
00088 
00089    BCDataSet * ds = new BCDataSet();
00090 
00091    // fill the dataset
00092    // find x and y boundaries for the error band calculation
00093    double xmin=x[0];
00094    double xmax=x[0];
00095    double ymin=y[0];
00096    double ymax=y[0];
00097    for (int i = 0; i < npoints; ++i)
00098    {
00099       // if x errors are not set, set them to zero
00100       double errx = ex ? ex[i] : 0.;
00101 
00102       // create the data point
00103       BCDataPoint * dp = new BCDataPoint(4);
00104       dp -> SetValue(0, x[i]);
00105       dp -> SetValue(1, y[i]);
00106       dp -> SetValue(2, errx);
00107       dp -> SetValue(3, ey[i]);
00108       ds -> AddDataPoint(dp);
00109 
00110       if(x[i]-errx < xmin)
00111          xmin = x[i]-errx;
00112       else if(x[i]+errx > xmax)
00113          xmax = x[i]+errx;
00114 
00115       if(y[i] - 5.*ey[i] < ymin)
00116          ymin = y[i] - 5.*ey[i];
00117       else if(y[i] + 5.*ey[i] > ymax)
00118          ymax = y[i] + 5.*ey[i];
00119    }
00120 
00121    this -> SetDataSet(ds);
00122 
00123    // set boundaries for the error band calculation
00124    this -> SetDataBoundaries(0, xmin, xmax);
00125    this -> SetDataBoundaries(1, ymin, ymax);
00126 
00127    this -> SetFitFunctionIndices(0, 1);
00128 
00129    return this -> GetNDataPoints();
00130 }


Member Data Documentation

TGraph* BCGraphFitter::fErrorBand [private]

Pointer to the error band (for legend)

Definition at line 145 of file BCGraphFitter.h.

The fit function

Definition at line 141 of file BCGraphFitter.h.

TGraphErrors* BCGraphFitter::fGraph [private]

The graph containing the data.

Definition at line 137 of file BCGraphFitter.h.

Pointer to a graph for displaying the fit function

Definition at line 149 of file BCGraphFitter.h.


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

Generated on Tue Oct 6 09:48:21 2009 for Bayesian Analysis Toolkit by  doxygen 1.6.1