CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
GSUtilities Class Reference

#include <GSUtilities.h>

Public Member Functions

double cdf (const double &) const
 value of integral(pdf) More...
 
double combinedMean () const
 mean value of combined state More...
 
double dpdf1 (const double &) const
 first derivative of pdf More...
 
double dpdf2 (const double &) const
 second derivative of pdf More...
 
double errorCombinedMean () const
 
float errorHighestWeight () const
 
float errorMode ()
 
float getMax (float)
 
float getMin (float)
 
 GSUtilities (const unsigned nComp, const float *weights, const float *parameters, const float *errors)
 constructor from arrays of weights, parameters and standard deviations More...
 
float maxWeight () const
 
float mode () const
 mode More...
 
double pdf (const double &) const
 value of the pdf More...
 
float quantile (const float) const
 
 ~GSUtilities ()
 

Private Member Functions

double findMode (const double) const
 
double gauss (const double &, const double &, const double &) const
 value of gaussian distribution More...
 
double gaussInt (const double &, const double &, const double &) const
 integrated value of gaussian distribution More...
 

Private Attributes

float * theErrors
 
unsigned theNComp
 
float * theParameters
 
float * theWeights
 

Detailed Description

Some utilities for analysing 1D Gaussian mixtures. Copied from ORCA's EgammaGSUtilities.

Definition at line 8 of file GSUtilities.h.

Constructor & Destructor Documentation

GSUtilities::GSUtilities ( const unsigned  nComp,
const float *  weights,
const float *  parameters,
const float *  errors 
)
inline

constructor from arrays of weights, parameters and standard deviations

Definition at line 11 of file GSUtilities.h.

References i, theErrors, theNComp, theParameters, and theWeights.

12  :
13  theNComp(nComp),
14  theWeights(0),
15  theParameters(0),
16  theErrors(0)
17  {
18  if ( theNComp ) {
19  theWeights = new float[theNComp];
20  theParameters = new float[theNComp];
21  theErrors = new float[theNComp];
22  }
23  const float* wPtr1(weights);
24  const float* pPtr1(parameters);
25  const float* ePtr1(errors);
26  float* wPtr2(theWeights);
27  float* pPtr2(theParameters);
28  float* ePtr2(theErrors);
29  for ( unsigned i=0; i<theNComp; i++ ) {
30  *(wPtr2++) = weights ? *(wPtr1++) : 1.;
31  *(pPtr2++) = *(pPtr1++);
32  *(ePtr2++) = *(ePtr1++);
33  }
34  }
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
unsigned theNComp
Definition: GSUtilities.h:80
float * theErrors
Definition: GSUtilities.h:83
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
GSUtilities::~GSUtilities ( )
inline

Definition at line 35 of file GSUtilities.h.

References theErrors, theParameters, and theWeights.

36  {
37  delete [] theWeights;
38  delete [] theParameters;
39  delete [] theErrors;
40  }
float * theErrors
Definition: GSUtilities.h:83
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82

Member Function Documentation

double GSUtilities::cdf ( const double &  x) const

value of integral(pdf)

Definition at line 157 of file GSUtilities.cc.

References gaussInt(), i, query::result, theErrors, theNComp, theParameters, and theWeights.

Referenced by errorMode(), getMax(), getMin(), and quantile().

158 {
159  double result(0.);
160  for ( unsigned i=0; i<theNComp; i++ )
162  return result;
163 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
double gaussInt(const double &, const double &, const double &) const
integrated value of gaussian distribution
Definition: GSUtilities.cc:203
float * theErrors
Definition: GSUtilities.h:83
tuple result
Definition: query.py:137
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
x
Definition: VDTMath.h:216
double GSUtilities::combinedMean ( ) const

mean value of combined state

Definition at line 210 of file GSUtilities.cc.

References i, theNComp, theParameters, and theWeights.

211 {
212  double s0(0.);
213  double s1(0.);
214  for ( unsigned i=0; i<theNComp; i++ ) {
215  s0 += theWeights[i];
216  s1 += theWeights[i]*theParameters[i];
217  }
218  return s1/s0;
219 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
double GSUtilities::dpdf1 ( const double &  x) const

first derivative of pdf

Definition at line 166 of file GSUtilities.cc.

References gauss(), i, query::result, theErrors, theNComp, theParameters, and theWeights.

Referenced by findMode().

167 {
168  double result(0.);
169  for ( unsigned i=0; i<theNComp; i++ ) {
170  double dx = (x-theParameters[i])/theErrors[i];
171  result += -theWeights[i]*dx/theErrors[i]*
173  }
174  return result;
175 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
float * theErrors
Definition: GSUtilities.h:83
tuple result
Definition: query.py:137
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
double gauss(const double &, const double &, const double &) const
value of gaussian distribution
Definition: GSUtilities.cc:190
x
Definition: VDTMath.h:216
double GSUtilities::dpdf2 ( const double &  x) const

second derivative of pdf

Definition at line 178 of file GSUtilities.cc.

References gauss(), i, query::result, theErrors, theNComp, theParameters, and theWeights.

Referenced by findMode().

179 {
180  double result(0.);
181  for ( unsigned i=0; i<theNComp; i++ ) {
182  double dx = (x-theParameters[i])/theErrors[i];
184  (dx*dx-1)*gauss(x,theParameters[i],theErrors[i]);
185  }
186  return result;
187 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
float * theErrors
Definition: GSUtilities.h:83
tuple result
Definition: query.py:137
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
double gauss(const double &, const double &, const double &) const
value of gaussian distribution
Definition: GSUtilities.cc:190
x
Definition: VDTMath.h:216
double GSUtilities::errorCombinedMean ( ) const

Definition at line 222 of file GSUtilities.cc.

References i, mathSSE::sqrt(), theNComp, and theWeights.

223 {
224  double s0(0.);
225  for ( unsigned i=0; i<theNComp; i++ ) {
226  s0 += theWeights[i];
227  }
228  return 1./(sqrt(s0));
229 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
T sqrt(T t)
Definition: SSEVec.h:46
float * theWeights
Definition: GSUtilities.h:81
float GSUtilities::errorHighestWeight ( ) const

Definition at line 73 of file GSUtilities.cc.

References i, theErrors, theNComp, and theWeights.

74 {
75  int iwMax(-1);
76  float wMax(0.);
77  for ( unsigned i=0; i<theNComp; i++ ) {
78  if ( theWeights[i]>wMax ) {
79  iwMax = i;
80  wMax = theWeights[i];
81  }
82  }
83  return theErrors[iwMax];
84 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
float * theErrors
Definition: GSUtilities.h:83
float * theWeights
Definition: GSUtilities.h:81
float GSUtilities::errorMode ( )

Definition at line 249 of file GSUtilities.cc.

References cdf(), getMax(), getMin(), Exhume::I, max(), min, text2workspace::mod, mode(), and pdf().

250 {
251  float mod = mode();
252  float min = getMin(mod);
253  float max = getMax(mod);
254  int nBins = 1000;
255  float dx = (max - min )/(float)nBins;
256 
257  float x1=mod, x2=mod, x1f=mod, x2f=mod;
258  float I=0;
259  int cnt=0;
260  while (I<.68) {
261  x1 -= dx;
262  x2 += dx;
263  if (pdf(x1)>=pdf(x2)) {
264  x1f = x1;
265  x2 -= dx;
266  } else {
267  x2f = x2;
268  x1 += dx;
269  }
270  I = cdf(x2f) - cdf(x1f);
271  cnt++;
272  // for crazy pdf's return a crazy value
273  if (cnt>2500) return 100000.;
274  }
275  return (x2f - x1f)/2;
276 }
float mode() const
mode
Definition: GSUtilities.cc:88
float getMax(float)
Definition: GSUtilities.cc:291
#define min(a, b)
Definition: mlp_lapack.h:161
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:157
const T & max(const T &a, const T &b)
const std::complex< double > I
Definition: I.h:8
list mod
Load physics model.
double pdf(const double &) const
value of the pdf
Definition: GSUtilities.cc:148
float getMin(float)
Definition: GSUtilities.cc:278
double GSUtilities::findMode ( const double  xStart) const
private

mean value of combined state double combinedMean() const; mode from starting value

Definition at line 122 of file GSUtilities.cc.

References dpdf1(), dpdf2(), alignCSCRings::e, pdf(), and vdt::x.

Referenced by mode().

123 {
124  //
125  // try with Newton
126  //
127  double y1(0.);
128  double x(xStart);
129  double y2(pdf(xStart));
130  double yd(dpdf1(xStart));
131  int nLoop(0);
132  if ( (y1+y2)<10*DBL_MIN ) return xStart;
133  while ( nLoop++<20 && fabs(y2-y1)/(y2+y1)>1.e-6 ) {
134 // std::cout << "dy = " << y2-y1 << std::endl;
135  double yd2 = dpdf2(x);
136  if ( fabs(yd2)<10*DBL_MIN ) return xStart;
137  x -= yd/dpdf2(x);
138  yd = dpdf1(x);
139  y1 = y2;
140  y2 = pdf(x);
141 // std::cout << "New x / yd = " << x << " / " << yd << std::endl;
142  }
143  if ( nLoop >= 20 ) return xStart;
144  return x;
145 }
double pdf(const double &) const
value of the pdf
Definition: GSUtilities.cc:148
double dpdf2(const double &) const
second derivative of pdf
Definition: GSUtilities.cc:178
double dpdf1(const double &) const
first derivative of pdf
Definition: GSUtilities.cc:166
x
Definition: VDTMath.h:216
double GSUtilities::gauss ( const double &  x,
const double &  mean,
const double &  sigma 
) const
private

value of gaussian distribution

Definition at line 190 of file GSUtilities.cc.

References create_public_lumi_plots::exp, Pi, query::result, and mathSSE::sqrt().

Referenced by dpdf1(), dpdf2(), and pdf().

192 {
193  const double fNorm(1./sqrt(2*TMath::Pi()));
194  double result(0.);
195 
196  double d((x-mean)/sigma);
197  if ( fabs(d)<20. ) result = exp(-d*d/2.);
198  result *= fNorm/sigma;
199  return result;
200 }
const double Pi
T sqrt(T t)
Definition: SSEVec.h:46
tuple result
Definition: query.py:137
x
Definition: VDTMath.h:216
double GSUtilities::gaussInt ( const double &  x,
const double &  mean,
const double &  sigma 
) const
private

integrated value of gaussian distribution

Definition at line 203 of file GSUtilities.cc.

Referenced by cdf().

205 {
206  return TMath::Freq((x-mean)/sigma);
207 }
x
Definition: VDTMath.h:216
float GSUtilities::getMax ( float  x)

Definition at line 291 of file GSUtilities.cc.

References cdf(), and vdt::x.

Referenced by errorMode().

292 {
293  int cnt=0;
294  float dx;
295  if (fabs(x)<2) dx = .5;
296  else dx = fabs(x)/10;
297  while( cdf(x)<.9 && cnt<1000) {
298  x += dx;
299  cnt++;
300  }
301  return x;
302 }
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:157
x
Definition: VDTMath.h:216
float GSUtilities::getMin ( float  x)

Definition at line 278 of file GSUtilities.cc.

References cdf(), and vdt::x.

Referenced by errorMode().

279 {
280  int cnt=0;
281  float dx;
282  if (fabs(x)<2) dx = .5;
283  else dx = fabs(x)/10;
284  while( cdf(x)>.1 && cnt<1000) {
285  x -= dx;
286  cnt++;
287  }
288  return x;
289 }
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:157
x
Definition: VDTMath.h:216
float GSUtilities::maxWeight ( ) const

Definition at line 232 of file GSUtilities.cc.

References i, theNComp, and theWeights.

233 {
234  // Look for the highest weight component
235  //
236  //int iwMax(-1);
237  float wMax(0.);
238  for ( unsigned i=0; i<theNComp; i++ ) {
239  if ( theWeights[i]>wMax ) {
240  //iwMax = i;
241  wMax = theWeights[i];
242  }
243  }
244  return wMax;
245 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
float * theWeights
Definition: GSUtilities.h:81
float GSUtilities::mode ( void  ) const

mode

Definition at line 88 of file GSUtilities.cc.

References findMode(), i, pdf(), theNComp, theParameters, and vdt::x.

Referenced by errorMode().

89 {
90  //
91  // start values = means of components
92  //
93  typedef std::multimap<double, double, std::greater<double> > StartMap;
94  StartMap xStart;
95  for ( unsigned i=0; i<theNComp; i++ ) {
96  xStart.insert(std::pair<double,float>(pdf(theParameters[i]),
97  theParameters[i]));
98  }
99  //
100  // now try with each start value
101  //
102  typedef std::multimap<double, double, std::greater<double> > ResultMap;
103  ResultMap xFound;
104  for ( StartMap::const_iterator i=xStart.begin();
105  i!=xStart.end(); i++ ) {
106  double x = findMode((*i).second);
107  xFound.insert(std::pair<double,double>(pdf(x),x));
108 // std::cout << "Started at " << (*i).second
109 // << " , found " << x << std::endl;
110  }
111  //
112  // results
113  //
114 // for ( ResultMap::const_iterator i=xFound.begin();
115 // i!=xFound.end(); i++ ) {
116 // std::cout << "pdf at " << (*i).second << " = " << (*i).first << std::endl;
117 // }
118  return xFound.begin()->second;
119 }
int i
Definition: DBlmapReader.cc:9
double findMode(const double) const
Definition: GSUtilities.cc:122
unsigned theNComp
Definition: GSUtilities.h:80
double pdf(const double &) const
value of the pdf
Definition: GSUtilities.cc:148
float * theParameters
Definition: GSUtilities.h:82
x
Definition: VDTMath.h:216
double GSUtilities::pdf ( const double &  x) const

value of the pdf

Definition at line 148 of file GSUtilities.cc.

References gauss(), i, query::result, theErrors, theNComp, theParameters, and theWeights.

Referenced by errorMode(), findMode(), and mode().

149 {
150  double result(0.);
151  for ( unsigned i=0; i<theNComp; i++ )
153  return result;
154 }
int i
Definition: DBlmapReader.cc:9
unsigned theNComp
Definition: GSUtilities.h:80
float * theErrors
Definition: GSUtilities.h:83
tuple result
Definition: query.py:137
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
double gauss(const double &, const double &, const double &) const
value of gaussian distribution
Definition: GSUtilities.cc:190
x
Definition: VDTMath.h:216
float GSUtilities::quantile ( const float  q) const

normalised integral from -inf to x (taking into account under- & overflows)

Definition at line 12 of file GSUtilities.cc.

References cdf(), alignCSCRings::e, i, lumiQueryAPI::q, theErrors, theNComp, theParameters, theWeights, vdt::x, and detailsBasic3DVector::y.

13 {
14  float qq = q;
15  if (q>1) qq = 1;
16  else if (q<0) qq = 0;
17  //
18  // mean and sigma of highest weight component
19  //
20  int iwMax(-1);
21  float wMax(0.);
22  for ( unsigned i=0; i<theNComp; i++ ) {
23  if ( theWeights[i]>wMax ) {
24  iwMax = i;
25  wMax = theWeights[i];
26  }
27  }
28  //
29  // Find start values: begin with mean and
30  // go towards f(x)=0 until two points on
31  // either side are found (assumes monotonously
32  // increasing function)
33  //
34  double x1(theParameters[iwMax]);
35  double y1(cdf(x1)-qq);
36  double dx = y1>0. ? -theErrors[iwMax] : theErrors[iwMax];
37  double x2(x1+dx);
38  double y2(cdf(x2)-qq);
39  int cnt =0;
40  while ( y1*y2>0. ) {
41  x1 = x2;
42  y1 = y2;
43  x2 += dx;
44  y2 = cdf(x2) - qq;
45  cnt++;
46  }
47 // std::cout << "(" << x1 << "," << y1 << ") / ("
48 // << x2 << "," << y2 << ")" << std::endl;
49  //
50  // now use bisection to find final value
51  //
52  double x(0.);
53  while ( true ) {
54  // use linear interpolation
55  x = -(x2*y1-x1*y2)/(y2-y1);
56  double y = cdf(x) - qq;
57 // std::cout << x << " " << y << std::endl;
58  if ( fabs(y)<1.e-6 ) break;
59  if ( y*y1>0. ) {
60  y1 = y;
61  x1 = x;
62  }
63  else {
64  y2 = y;
65  x2 = x;
66  }
67  }
68  return x;
69 }
int i
Definition: DBlmapReader.cc:9
double cdf(const double &) const
value of integral(pdf)
Definition: GSUtilities.cc:157
unsigned theNComp
Definition: GSUtilities.h:80
float * theErrors
Definition: GSUtilities.h:83
float * theWeights
Definition: GSUtilities.h:81
float * theParameters
Definition: GSUtilities.h:82
x
Definition: VDTMath.h:216

Member Data Documentation

float* GSUtilities::theErrors
private

Definition at line 83 of file GSUtilities.h.

Referenced by cdf(), dpdf1(), dpdf2(), errorHighestWeight(), GSUtilities(), pdf(), quantile(), and ~GSUtilities().

unsigned GSUtilities::theNComp
private
float* GSUtilities::theParameters
private

Definition at line 82 of file GSUtilities.h.

Referenced by cdf(), combinedMean(), dpdf1(), dpdf2(), GSUtilities(), mode(), pdf(), quantile(), and ~GSUtilities().

float* GSUtilities::theWeights
private