source: trunk/MagicSoft/Mars/mbase/MMath.cc@ 7929

Last change on this file since 7929 was 7899, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 14.6 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MMath
28//
29// Mars - Math package (eg Significances, etc)
30//
31/////////////////////////////////////////////////////////////////////////////
32#include "MMath.h"
33
34#ifndef ROOT_TVector3
35#include <TVector3.h>
36#endif
37
38#ifndef ROOT_TArrayD
39#include <TArrayD.h>
40#endif
41// --------------------------------------------------------------------------
42//
43// Calculate Significance as
44// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
45//
46// s: total number of events in signal region
47// b: number of background events in signal region
48//
49Double_t MMath::Significance(Double_t s, Double_t b)
50{
51 const Double_t k = b==0 ? 0 : s/b;
52 const Double_t f = s+k*k*b;
53
54 return f==0 ? 0 : (s-b)/TMath::Sqrt(f);
55}
56
57// --------------------------------------------------------------------------
58//
59// Symmetrized significance - this is somehow analog to
60// SignificanceLiMaSigned
61//
62// Returns Significance(s,b) if s>b otherwise -Significance(b, s);
63//
64Double_t MMath::SignificanceSym(Double_t s, Double_t b)
65{
66 return s>b ? Significance(s, b) : -Significance(b, s);
67}
68
69// --------------------------------------------------------------------------
70//
71// calculates the significance according to Li & Ma
72// ApJ 272 (1983) 317, Formula 17
73//
74// s // s: number of on events
75// b // b: number of off events
76// alpha = t_on/t_off; // t: observation time
77//
78// The significance has the same (positive!) value for s>b and b>s.
79//
80// Returns -1 if s<0 or b<0 or alpha<0 or the argument of sqrt<0
81//
82// Here is some eMail written by Daniel Mazin about the meaning of the arguments:
83//
84// > Ok. Here is my understanding:
85// > According to Li&Ma paper (correctly cited in MMath.cc) alpha is the
86// > scaling factor. The mathematics behind the formula 17 (and/or 9) implies
87// > exactly this. If you scale OFF to ON first (using time or using any other
88// > method), then you cannot use formula 17 (9) anymore. You can just try
89// > the formula before scaling (alpha!=1) and after scaling (alpha=1), you
90// > will see the result will be different.
91//
92// > Here are less mathematical arguments:
93//
94// > 1) the better background determination you have (smaller alpha) the more
95// > significant is your excess, thus your analysis is more sensitive. If you
96// > normalize OFF to ON first, you loose this sensitivity.
97//
98// > 2) the normalization OFF to ON has an error, which naturally depends on
99// > the OFF and ON. This error is propagating to the significance of your
100// > excess if you use the Li&Ma formula 17 correctly. But if you normalize
101// > first and use then alpha=1, the error gets lost completely, you loose
102// > somehow the criteria of goodness of the normalization.
103//
104Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
105{
106 const Double_t sum = s+b;
107
108 if (s<0 || b<0 || alpha<=0)
109 return -1;
110
111 const Double_t l = s==0 ? 0 : s*TMath::Log(s/sum*(alpha+1)/alpha);
112 const Double_t m = b==0 ? 0 : b*TMath::Log(b/sum*(alpha+1) );
113
114 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
115}
116
117// --------------------------------------------------------------------------
118//
119// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
120// calculation has failed. Otherwise the Li/Ma significance which was
121// calculated. If s<b a negative value is returned.
122//
123Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
124{
125 const Double_t sig = SignificanceLiMa(s, b, alpha);
126 if (sig<=0)
127 return 0;
128
129 return TMath::Sign(sig, s-alpha*b);
130}
131
132// --------------------------------------------------------------------------
133//
134// Return Li/Ma (5) for the error of the excess, under the assumption that
135// the existance of a signal is already known.
136//
137Double_t MMath::SignificanceLiMaExc(Double_t s, Double_t b, Double_t alpha)
138{
139 Double_t Ns = s - alpha*b;
140 Double_t sN = s + alpha*alpha*b;
141
142 return Ns<0 || sN<0 ? 0 : Ns/TMath::Sqrt(sN);
143}
144
145// --------------------------------------------------------------------------
146//
147// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
148//
149Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
150{
151 static const Double_t sqrt2 = TMath::Sqrt(2.);
152
153 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
154
155 if (rc<0)
156 return 0;
157 if (rc>1)
158 return 1;
159
160 return rc;
161}
162
163// ------------------------------------------------------------------------
164//
165// Return the "median" (at 68.3%) value of the distribution of
166// abs(a[i]-Median)
167//
168template <class Size, class Element>
169Double_t MMath::MedianDevImp(Size n, const Element *a, Double_t &med)
170{
171 static const Double_t prob = 0.682689477208650697; //MMath::.GaissProb(1.0);
172
173 // Sanity check
174 if (n <= 0 || !a)
175 return 0;
176
177 // Get median of distribution
178 med = TMath::Median(n, a);
179
180 // Create the abs(a[i]-med) distribution
181 Double_t arr[n];
182 for (int i=0; i<n; i++)
183 arr[i] = TMath::Abs(a[i]-med);
184
185 // FIXME: GausProb() is a workaround. It should be taken into account in Median!
186 //return TMath::Median(n, arr);
187
188 // Sort distribution
189 Long64_t idx[n];
190 TMath::SortImp(n, arr, idx, kTRUE);
191
192 // Define where to divide
193 const Int_t div = TMath::Nint(n*prob);
194
195 // Calculate result
196 Double_t dev = TMath::KOrdStat(n, arr, div, idx);
197 if (n%2 == 0)
198 {
199 dev += TMath::KOrdStat(n, arr, div-1, idx);
200 dev /= 2;
201 }
202
203 return dev;
204}
205
206// ------------------------------------------------------------------------
207//
208// Return the "median" (at 68.3%) value of the distribution of
209// abs(a[i]-Median)
210//
211Double_t MMath::MedianDev(Long64_t n, const Short_t *a, Double_t &med)
212{
213 return MedianDevImp(n, a, med);
214}
215
216// ------------------------------------------------------------------------
217//
218// Return the "median" (at 68.3%) value of the distribution of
219// abs(a[i]-Median)
220//
221Double_t MMath::MedianDev(Long64_t n, const Int_t *a, Double_t &med)
222{
223 return MedianDevImp(n, a, med);
224}
225
226// ------------------------------------------------------------------------
227//
228// Return the "median" (at 68.3%) value of the distribution of
229// abs(a[i]-Median)
230//
231Double_t MMath::MedianDev(Long64_t n, const Float_t *a, Double_t &med)
232{
233 return MedianDevImp(n, a, med);
234}
235
236// ------------------------------------------------------------------------
237//
238// Return the "median" (at 68.3%) value of the distribution of
239// abs(a[i]-Median)
240//
241Double_t MMath::MedianDev(Long64_t n, const Double_t *a, Double_t &med)
242{
243 return MedianDevImp(n, a, med);
244}
245
246// ------------------------------------------------------------------------
247//
248// Return the "median" (at 68.3%) value of the distribution of
249// abs(a[i]-Median)
250//
251Double_t MMath::MedianDev(Long64_t n, const Long_t *a, Double_t &med)
252{
253 return MedianDevImp(n, a, med);
254}
255
256// ------------------------------------------------------------------------
257//
258// Return the "median" (at 68.3%) value of the distribution of
259// abs(a[i]-Median)
260//
261Double_t MMath::MedianDev(Long64_t n, const Long64_t *a, Double_t &med)
262{
263 return MedianDevImp(n, a, med);
264}
265
266Double_t MMath::MedianDev(Long64_t n, const Short_t *a) { Double_t med; return MedianDevImp(n, a, med); }
267Double_t MMath::MedianDev(Long64_t n, const Int_t *a) { Double_t med; return MedianDevImp(n, a, med); }
268Double_t MMath::MedianDev(Long64_t n, const Float_t *a) { Double_t med; return MedianDevImp(n, a, med); }
269Double_t MMath::MedianDev(Long64_t n, const Double_t *a) { Double_t med; return MedianDevImp(n, a, med); }
270Double_t MMath::MedianDev(Long64_t n, const Long_t *a) { Double_t med; return MedianDevImp(n, a, med); }
271Double_t MMath::MedianDev(Long64_t n, const Long64_t *a) { Double_t med; return MedianDevImp(n, a, med); }
272
273// --------------------------------------------------------------------------
274//
275// This function reduces the precision to roughly 0.5% of a Float_t by
276// changing its bit-pattern (Be carefull, in rare cases this function must
277// be adapted to different machines!). This is usefull to enforce better
278// compression by eg. gzip.
279//
280void MMath::ReducePrecision(Float_t &val)
281{
282 UInt_t &f = (UInt_t&)val;
283
284 f += 0x00004000;
285 f &= 0xffff8000;
286}
287
288// -------------------------------------------------------------------------
289//
290// Quadratic interpolation
291//
292// calculate the parameters of a parabula such that
293// y(i) = a + b*x(i) + c*x(i)^2
294//
295// If the determinant==0 an empty TVector3 is returned.
296//
297TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
298{
299 Double_t x1 = x(0);
300 Double_t x2 = x(1);
301 Double_t x3 = x(2);
302
303 Double_t y1 = y(0);
304 Double_t y2 = y(1);
305 Double_t y3 = y(2);
306
307 const double det =
308 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
309 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
310
311
312 if (det==0)
313 return TVector3();
314
315 const double det1 = 1.0/det;
316
317 const double ai11 = x2*x3*x3 - x3*x2*x2;
318 const double ai12 = x3*x1*x1 - x1*x3*x3;
319 const double ai13 = x1*x2*x2 - x2*x1*x1;
320
321 const double ai21 = x2*x2 - x3*x3;
322 const double ai22 = x3*x3 - x1*x1;
323 const double ai23 = x1*x1 - x2*x2;
324
325 const double ai31 = x3 - x2;
326 const double ai32 = x1 - x3;
327 const double ai33 = x2 - x1;
328
329 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
330 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
331 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
332}
333
334Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
335{
336 const TVector3 c = GetParab(vx, vy);
337 return c(0) + c(1)*x + c(2)*x*x;
338}
339
340Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
341{
342 const Double_t l0 = TMath::Log10(vx(0));
343 const Double_t l1 = TMath::Log10(vx(1));
344 const Double_t l2 = TMath::Log10(vx(2));
345
346 const TVector3 vx0(l0, l1, l2);
347 return InterpolParabLin(vx0, vy, TMath::Log10(x));
348}
349
350Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
351{
352 const Double_t l0 = TMath::Cos(vx(0));
353 const Double_t l1 = TMath::Cos(vx(1));
354 const Double_t l2 = TMath::Cos(vx(2));
355
356 const TVector3 vx0(l0, l1, l2);
357 return InterpolParabLin(vx0, vy, TMath::Cos(x));
358}
359
360// --------------------------------------------------------------------------
361//
362// Analytically calculated result of a least square fit of:
363// y = A*e^(B*x)
364// Equal weights
365//
366// It returns TArrayD(2) = { A, B };
367//
368// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
369//
370TArrayD MMath::LeastSqFitExpW1(Int_t n, Double_t *x, Double_t *y)
371{
372 Double_t sumxsqy = 0;
373 Double_t sumylny = 0;
374 Double_t sumxy = 0;
375 Double_t sumy = 0;
376 Double_t sumxylny = 0;
377 for (int i=0; i<n; i++)
378 {
379 sumylny += y[i]*TMath::Log(y[i]);
380 sumxy += x[i]*y[i];
381 sumxsqy += x[i]*x[i]*y[i];
382 sumxylny += x[i]*y[i]*TMath::Log(y[i]);
383 sumy += y[i];
384 }
385
386 const Double_t dev = sumy*sumxsqy - sumxy*sumxy;
387
388 const Double_t a = (sumxsqy*sumylny - sumxy*sumxylny)/dev;
389 const Double_t b = (sumy*sumxylny - sumxy*sumylny)/dev;
390
391 TArrayD rc(2);
392 rc[0] = TMath::Exp(a);
393 rc[1] = b;
394 return rc;
395}
396
397// --------------------------------------------------------------------------
398//
399// Analytically calculated result of a least square fit of:
400// y = A*e^(B*x)
401// Greater weights to smaller values
402//
403// It returns TArrayD(2) = { A, B };
404//
405// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
406//
407TArrayD MMath::LeastSqFitExp(Int_t n, Double_t *x, Double_t *y)
408{
409 // -------- Greater weights to smaller values ---------
410 Double_t sumlny = 0;
411 Double_t sumxlny = 0;
412 Double_t sumxsq = 0;
413 Double_t sumx = 0;
414 for (int i=0; i<n; i++)
415 {
416 sumlny += TMath::Log(y[i]);
417 sumxlny += x[i]*TMath::Log(y[i]);
418
419 sumxsq += x[i]*x[i];
420 sumx += x[i];
421 }
422
423 const Double_t dev = n*sumxsq-sumx*sumx;
424
425 const Double_t a = (sumlny*sumxsq - sumx*sumxlny)/dev;
426 const Double_t b = (n*sumxlny - sumx*sumlny)/dev;
427
428 TArrayD rc(2);
429 rc[0] = TMath::Exp(a);
430 rc[1] = b;
431 return rc;
432}
433
434// --------------------------------------------------------------------------
435//
436// Analytically calculated result of a least square fit of:
437// y = A+B*ln(x)
438//
439// It returns TArrayD(2) = { A, B };
440//
441// see: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
442//
443TArrayD MMath::LeastSqFitLog(Int_t n, Double_t *x, Double_t *y)
444{
445 Double_t sumylnx = 0;
446 Double_t sumy = 0;
447 Double_t sumlnx = 0;
448 Double_t sumlnxsq = 0;
449 for (int i=0; i<n; i++)
450 {
451 sumylnx += y[i]*TMath::Log(x[i]);
452 sumy += y[i];
453 sumlnx += TMath::Log(x[i]);
454 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
455 }
456
457 const Double_t b = (n*sumylnx-sumy*sumlnx)/(n*sumlnxsq-sumlnx*sumlnx);
458 const Double_t a = (sumy-b*sumlnx)/n;
459
460 TArrayD rc(2);
461 rc[0] = a;
462 rc[1] = b;
463 return rc;
464}
465
466// --------------------------------------------------------------------------
467//
468// Analytically calculated result of a least square fit of:
469// y = A*x^B
470//
471// It returns TArrayD(2) = { A, B };
472//
473// see: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
474//
475TArrayD MMath::LeastSqFitPowerLaw(Int_t n, Double_t *x, Double_t *y)
476{
477 Double_t sumlnxlny = 0;
478 Double_t sumlnx = 0;
479 Double_t sumlny = 0;
480 Double_t sumlnxsq = 0;
481 for (int i=0; i<n; i++)
482 {
483 sumlnxlny += TMath::Log(x[i])*TMath::Log(y[i]);
484 sumlnx += TMath::Log(x[i]);
485 sumlny += TMath::Log(y[i]);
486 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
487 }
488
489 const Double_t b = (n*sumlnxlny-sumlnx*sumlny)/(n*sumlnxsq-sumlnx*sumlnx);
490 const Double_t a = (sumlny-b*sumlnx)/n;
491
492 TArrayD rc(2);
493 rc[0] = TMath::Exp(a);
494 rc[1] = b;
495 return rc;
496}
Note: See TracBrowser for help on using the repository browser.