Logo ROOT   6.08/07
Reference Guide
GaussianModelFunction.h
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7  * *
8  **********************************************************************/
9 
10 #ifndef MN_GaussianModelFunction_H_
11 #define MN_GaussianModelFunction_H_
12 
13 #define _USE_MATH_DEFINES
14 #include <math.h>
15 
16 
18 
19 #include "Minuit2/MnFcn.h"
20 #include "Minuit2/MnStrategy.h"
22 
23 #include <vector>
24 #include <cassert>
25 
26 namespace ROOT {
27 
28  namespace Minuit2 {
29 
30 
31 
32 
33 
34 /**
35 
36 Sample implementation of a parametric function. It can be used for
37 example for the Fumili method when minimizing with Minuit.
38 In the present case the function is a one-dimensional Gaussian,
39 which is described by its mean, standard deviation and the constant
40 term describing the amplitude. As it is used for
41 function minimization, the role of the variables (or coordinates) and
42 parameters is inversed! I.e. in the case of a one-dimensional
43 Gaussian it is x that will be the Parameter and the mean, standard
44 deviation etc will be the variables.
45 
46 @author Andras Zsenei and Lorenzo Moneta, Creation date: 26 Oct 2004
47 
48 @see <A HREF="http://mathworld.wolfram.com/NormalDistribution.html"> Definition of
49 the Normal/Gaussian distribution </A> (note: this Gaussian is normalized).
50 
51 @see ParametricFunction
52 
53 @see FumiliFCNBase
54 
55 @see FumiliMaximumLikelihoodFCN
56 
57 @ingroup Minuit
58 
59 */
60 
61 
63 
64 public:
65 
66 
67  /**
68 
69  Constructor which initializes the normalized Gaussian with x = 0.0.
70 
71  */
72 
74 
75  // setting some default values for the parameters
76  std::vector<double> param;
77  param.push_back(0.0);
78  SetParameters(param);
79 
80  }
81 
82 
83  /**
84 
85  Constructor which initializes the ParametricFunction with the
86  parameters given as input.
87 
88  @param params vector containing the initial Parameter Value.
89 
90  */
91 
92  GaussianModelFunction(const std::vector<double>& params) : ParametricFunction(params) {
93 
94  assert(params.size() == 1);
95 
96  }
97 
98 
99 
100 
102 
103 
104 
105  /**
106 
107  Calculates the Gaussian as a function of the given input.
108 
109  @param x vector containing the mean, standard deviation and amplitude.
110 
111  @return the Value of the Gaussian for the given input.
112 
113  @see <A HREF="http://mathworld.wolfram.com/NormalDistribution.html"> Definition of
114  the Normal/Gaussian distribution </A> (note: this Gaussian is normalized).
115 
116  */
117 
118  double operator()(const std::vector<double>& x) const {
119 
120  assert(x.size() == 3);
121  // commented out for speed-up (even though that is the object-oriented
122  // way to do things)
123  //std::vector<double> par = GetParameters();
124  return x[2]*exp(-0.5*(par[0]-x[0])*(par[0]-x[0])/(x[1]*x[1]))/(sqrt(2.*M_PI)*fabs(x[1]));
125  }
126 
127 
128 
129  /**
130 
131  Calculates the Gaussian as a function of the given input.
132 
133  @param x vector containing the mean, the standard deviation and the constant
134  describing the Gaussian.
135 
136  @param par vector containing the x coordinate (which is the Parameter in
137  the case of a minimization).
138 
139  @return the Value of the Gaussian for the given input.
140 
141  @see <A HREF="http://mathworld.wolfram.com/NormalDistribution.html"> Definition of
142  the Normal/Gaussian distribution </A> (note: this Gaussian is normalized).
143 
144  */
145 
146 
147  double operator()(const std::vector<double>& x, const std::vector<double>& param) const {
148 
149  assert(param.size() == 1);
150  assert(x.size() == 3);
151  return x[2]*exp(-0.5*(param[0]-x[0])*(param[0]-x[0])/(x[1]*x[1]))/(sqrt(2.*M_PI)*fabs(x[1]));
152  }
153 
154 
155 
156  /**
157 
158  THAT SHOULD BE REMOVED, IT IS ONLY HERE, BECAUSE AT PRESENT FOR GRADIENT
159  CALCULATION ONE NEEDS TO INHERIT FROM FCNBASE WHICH NEEDS THIS METHOD
160 
161  */
162 
163  virtual double Up() const { return 1.0; }
164 
165 
166 
167  std::vector<double> GetGradient(const std::vector<double>& x) const {
168 
169 
170  const std::vector<double> & param = GetParameters();
171  assert(param.size() == 1);
172  std::vector<double> grad(x.size());
173 
174  double y = (param[0]-x[0])/x[1];
175  double gaus = exp(-0.5*y*y )/(sqrt(2.*M_PI)*fabs(x[1]));
176 
177 
178  grad[0] = y/(x[1])*gaus*x[2];
179  grad[1] = x[2]*gaus*( y*y - 1.0)/x[1];
180  grad[2] = gaus;
181  //std::cout << "GRADIENT" << y << " " << gaus << " " << x[0] << " " << x[1] << " " << grad[0] << " " << grad[1] << std::endl;
182 
183  return grad;
184 
185  }
186 
187 
188 
189 };
190 
191  } // namespace Minuit2
192 
193 } // namespace ROOT
194 
195 #endif // MN_GaussianModelFunction_H_
virtual void SetParameters(const std::vector< double > &params) const
Sets the parameters of the ParametricFunction.
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
virtual double Up() const
THAT SHOULD BE REMOVED, IT IS ONLY HERE, BECAUSE AT PRESENT FOR GRADIENT CALCULATION ONE NEEDS TO INH...
double operator()(const std::vector< double > &x) const
Calculates the Gaussian as a function of the given input.
virtual const std::vector< double > & GetParameters() const
Accessor for the state of the parameters.
double sqrt(double)
Double_t x[n]
Definition: legend1.C:17
std::vector< double > GetGradient(const std::vector< double > &x) const
Member function returning the Gradient of the function with respect to its variables (but without inc...
Sample implementation of a parametric function.
GaussianModelFunction(const std::vector< double > &params)
Constructor which initializes the ParametricFunction with the parameters given as input...
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
#define M_PI
Definition: Rotated.cxx:105
Function which has parameters.
double operator()(const std::vector< double > &x, const std::vector< double > &param) const
Calculates the Gaussian as a function of the given input.
Double_t y[n]
Definition: legend1.C:17
std::vector< double > par
The vector containing the parameters of the function It is mutable for "historical reasons" as in the...
double exp(double)
GaussianModelFunction()
Constructor which initializes the normalized Gaussian with x = 0.0.