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

Last change on this file since 7477 was 7410, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 11.3 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 sum<0 or alpha<0 or the argument of sqrt<0
81// Returns 0 if s+b==0, s==0 or b==0
82//
83// Here is some eMail written by Daniel Mazin about the meaning of the arguments:
84//
85// > Ok. Here is my understanding:
86// > According to Li&Ma paper (correctly cited in MMath.cc) alpha is the
87// > scaling factor. The mathematics behind the formula 17 (and/or 9) implies
88// > exactly this. If you scale OFF to ON first (using time or using any other
89// > method), then you cannot use formula 17 (9) anymore. You can just try
90// > the formula before scaling (alpha!=1) and after scaling (alpha=1), you
91// > will see the result will be different.
92//
93// > Here are less mathematical arguments:
94//
95// > 1) the better background determination you have (smaller alpha) the more
96// > significant is your excess, thus your analysis is more sensitive. If you
97// > normalize OFF to ON first, you loose this sensitivity.
98//
99// > 2) the normalization OFF to ON has an error, which naturally depends on
100// > the OFF and ON. This error is propagating to the significance of your
101// > excess if you use the Li&Ma formula 17 correctly. But if you normalize
102// > first and use then alpha=1, the error gets lost completely, you loose
103// > somehow the criteria of goodness of the normalization.
104//
105Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
106{
107 const Double_t sum = s+b;
108
109 if (s==0 || b==0 || sum==0)
110 return 0;
111
112 if (sum<0 || alpha<=0)
113 return -1;
114
115 const Double_t l = s*TMath::Log(s/sum*(alpha+1)/alpha);
116 const Double_t m = b*TMath::Log(b/sum*(alpha+1) );
117
118 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
119}
120
121// --------------------------------------------------------------------------
122//
123// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
124// calculation has failed. Otherwise the Li/Ma significance which was
125// calculated. If s<b a negative value is returned.
126//
127Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
128{
129 const Double_t sig = SignificanceLiMa(s, b, alpha);
130 if (sig<=0)
131 return 0;
132
133 return TMath::Sign(sig, s-alpha*b);
134}
135
136// --------------------------------------------------------------------------
137//
138// Return Li/Ma (5) for the error of the excess, under the assumption that
139// the existance of a signal is already known.
140//
141Double_t MMath::SignificanceLiMaExc(Double_t s, Double_t b, Double_t alpha)
142{
143 Double_t Ns = s - alpha*b;
144 Double_t sN = s + alpha*alpha*b;
145
146 return Ns<0 || sN<0 ? 0 : Ns/TMath::Sqrt(sN);
147}
148
149// --------------------------------------------------------------------------
150//
151// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
152//
153Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
154{
155 static const Double_t sqrt2 = TMath::Sqrt(2.);
156
157 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
158
159 if (rc<0)
160 return 0;
161 if (rc>1)
162 return 1;
163
164 return rc;
165}
166
167// --------------------------------------------------------------------------
168//
169// This function reduces the precision to roughly 0.5% of a Float_t by
170// changing its bit-pattern (Be carefull, in rare cases this function must
171// be adapted to different machines!). This is usefull to enforce better
172// compression by eg. gzip.
173//
174void MMath::ReducePrecision(Float_t &val)
175{
176 UInt_t &f = (UInt_t&)val;
177
178 f += 0x00004000;
179 f &= 0xffff8000;
180}
181
182// -------------------------------------------------------------------------
183//
184// Quadratic interpolation
185//
186// calculate the parameters of a parabula such that
187// y(i) = a + b*x(i) + c*x(i)^2
188//
189// If the determinant==0 an empty TVector3 is returned.
190//
191TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
192{
193 Double_t x1 = x(0);
194 Double_t x2 = x(1);
195 Double_t x3 = x(2);
196
197 Double_t y1 = y(0);
198 Double_t y2 = y(1);
199 Double_t y3 = y(2);
200
201 const double det =
202 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
203 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
204
205
206 if (det==0)
207 return TVector3();
208
209 const double det1 = 1.0/det;
210
211 const double ai11 = x2*x3*x3 - x3*x2*x2;
212 const double ai12 = x3*x1*x1 - x1*x3*x3;
213 const double ai13 = x1*x2*x2 - x2*x1*x1;
214
215 const double ai21 = x2*x2 - x3*x3;
216 const double ai22 = x3*x3 - x1*x1;
217 const double ai23 = x1*x1 - x2*x2;
218
219 const double ai31 = x3 - x2;
220 const double ai32 = x1 - x3;
221 const double ai33 = x2 - x1;
222
223 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
224 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
225 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
226}
227
228Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
229{
230 const TVector3 c = GetParab(vx, vy);
231 return c(0) + c(1)*x + c(2)*x*x;
232}
233
234Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
235{
236 const Double_t l0 = TMath::Log10(vx(0));
237 const Double_t l1 = TMath::Log10(vx(1));
238 const Double_t l2 = TMath::Log10(vx(2));
239
240 const TVector3 vx0(l0, l1, l2);
241 return InterpolParabLin(vx0, vy, TMath::Log10(x));
242}
243
244Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
245{
246 const Double_t l0 = TMath::Cos(vx(0));
247 const Double_t l1 = TMath::Cos(vx(1));
248 const Double_t l2 = TMath::Cos(vx(2));
249
250 const TVector3 vx0(l0, l1, l2);
251 return InterpolParabLin(vx0, vy, TMath::Cos(x));
252}
253
254// --------------------------------------------------------------------------
255//
256// Analytically calculated result of a least square fit of:
257// y = A*e^(B*x)
258// Equal weights
259//
260// It returns TArrayD(2) = { A, B };
261//
262// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
263//
264TArrayD MMath::LeastSqFitExpW1(Int_t n, Double_t *x, Double_t *y)
265{
266 Double_t sumxsqy = 0;
267 Double_t sumylny = 0;
268 Double_t sumxy = 0;
269 Double_t sumy = 0;
270 Double_t sumxylny = 0;
271 for (int i=0; i<n; i++)
272 {
273 sumylny += y[i]*TMath::Log(y[i]);
274 sumxy += x[i]*y[i];
275 sumxsqy += x[i]*x[i]*y[i];
276 sumxylny += x[i]*y[i]*TMath::Log(y[i]);
277 sumy += y[i];
278 }
279
280 const Double_t dev = sumy*sumxsqy - sumxy*sumxy;
281
282 const Double_t a = (sumxsqy*sumylny - sumxy*sumxylny)/dev;
283 const Double_t b = (sumy*sumxylny - sumxy*sumylny)/dev;
284
285 TArrayD rc(2);
286 rc[0] = TMath::Exp(a);
287 rc[1] = b;
288 return rc;
289}
290
291// --------------------------------------------------------------------------
292//
293// Analytically calculated result of a least square fit of:
294// y = A*e^(B*x)
295// Greater weights to smaller values
296//
297// It returns TArrayD(2) = { A, B };
298//
299// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
300//
301TArrayD MMath::LeastSqFitExp(Int_t n, Double_t *x, Double_t *y)
302{
303 // -------- Greater weights to smaller values ---------
304 Double_t sumlny = 0;
305 Double_t sumxlny = 0;
306 Double_t sumxsq = 0;
307 Double_t sumx = 0;
308 for (int i=0; i<n; i++)
309 {
310 sumlny += TMath::Log(y[i]);
311 sumxlny += x[i]*TMath::Log(y[i]);
312
313 sumxsq += x[i]*x[i];
314 sumx += x[i];
315 }
316
317 const Double_t dev = n*sumxsq-sumx*sumx;
318
319 const Double_t a = (sumlny*sumxsq - sumx*sumxlny)/dev;
320 const Double_t b = (n*sumxlny - sumx*sumlny)/dev;
321
322 TArrayD rc(2);
323 rc[0] = TMath::Exp(a);
324 rc[1] = b;
325 return rc;
326}
327
328// --------------------------------------------------------------------------
329//
330// Analytically calculated result of a least square fit of:
331// y = A+B*ln(x)
332//
333// It returns TArrayD(2) = { A, B };
334//
335// see: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
336//
337TArrayD MMath::LeastSqFitLog(Int_t n, Double_t *x, Double_t *y)
338{
339 Double_t sumylnx = 0;
340 Double_t sumy = 0;
341 Double_t sumlnx = 0;
342 Double_t sumlnxsq = 0;
343 for (int i=0; i<n; i++)
344 {
345 sumylnx += y[i]*TMath::Log(x[i]);
346 sumy += y[i];
347 sumlnx += TMath::Log(x[i]);
348 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
349 }
350
351 const Double_t b = (n*sumylnx-sumy*sumlnx)/(n*sumlnxsq-sumlnx*sumlnx);
352 const Double_t a = (sumy-b*sumlnx)/n;
353
354 TArrayD rc(2);
355 rc[0] = a;
356 rc[1] = b;
357 return rc;
358}
359
360// --------------------------------------------------------------------------
361//
362// Analytically calculated result of a least square fit of:
363// y = A*x^B
364//
365// It returns TArrayD(2) = { A, B };
366//
367// see: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
368//
369TArrayD MMath::LeastSqFitPowerLaw(Int_t n, Double_t *x, Double_t *y)
370{
371 Double_t sumlnxlny = 0;
372 Double_t sumlnx = 0;
373 Double_t sumlny = 0;
374 Double_t sumlnxsq = 0;
375 for (int i=0; i<n; i++)
376 {
377 sumlnxlny += TMath::Log(x[i])*TMath::Log(y[i]);
378 sumlnx += TMath::Log(x[i]);
379 sumlny += TMath::Log(y[i]);
380 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
381 }
382
383 const Double_t b = (n*sumlnxlny-sumlnx*sumlny)/(n*sumlnxsq-sumlnx*sumlnx);
384 const Double_t a = (sumlny-b*sumlnx)/n;
385
386 TArrayD rc(2);
387 rc[0] = TMath::Exp(a);
388 rc[1] = b;
389 return rc;
390}
Note: See TracBrowser for help on using the repository browser.