← Documentation
ADS-IDAC Engine 5.0.0
Probability.cpp
Go to the documentation of this file.
1// Probability.cpp: implementation of the Probability class.
2//
4
5#include "Probability.h"
6#include <boost/serialization/export.hpp>
7#include <boost/archive/binary_oarchive.hpp>
8#include <boost/archive/binary_iarchive.hpp>
9BOOST_CLASS_EXPORT_IMPLEMENT(Probability)
10using namespace std;
11
13// Construction/Destruction
15
17 probability = -1.0;
18 alpha_parameter = 2.0;
19 beta_parameter = 100.0;
20 failure_rate = 0.0;
21}
22
24
25}
33}
35 fout << probability << ' '
36 << alpha_parameter << ' '
37 << beta_parameter << '\n';
38}
40 fin >> probability
43}
48
50//
51// Purpose:
52//
53// R8_EPSILON returns the R8 round off unit.
54//
55// Discussion:
56//
57// R8_EPSILON is a number R which is a power of 2 with the property that,
58// to the precision of the computer's arithmetic,
59// 1 < 1 + R
60// but
61// 1 = ( 1 + R / 2 )
62//
63// Modified:
64//
65// 06 May 2003
66//
67// Author:
68//
69// John Burkardt
70//
71// Parameters:
72//
73// Output, double R8_EPSILON, the floating point round-off unit.
74//
75{
76 double r;
77
78 r = 1.0;
79
80 while (1.0 < (double) (1.0 + r)) {
81 r = r / 2.0;
82 }
83
84 return (2.0 * r);
85}
86
88//
89// Purpose:
90//
91// R8_HUGE returns a "huge" R8.
92//
93// Discussion:
94//
95// HUGE_VAL is the largest representable legal real number, and is usually
96// defined in math.h, or sometimes in stdlib.h.
97//
98// Modified:
99//
100// 08 May 2003
101//
102// Author:
103//
104// John Burkardt
105//
106// Parameters:
107//
108// Output, double R8_HUGE, a "huge" real value.
109//
110{
111 return HUGE_VAL;
112}
113
114double Probability::beta(double x, double y)
115//
116// Purpose:
117//
118// BETA returns the value of the Beta function.
119//
120// Formula:
121//
122// BETA(X,Y) = ( GAMMA(X) * GAMMA(Y) ) / GAMMA(X+Y)
123//
124// Properties:
125//
126// BETA(X,Y) = BETA(Y,X).
127// BETA(X,Y) = Integral ( 0 <= T <= 1 ) T**(X-1) (1-T)**(Y-1) dT.
128// BETA(X,Y) = GAMMA(X) * GAMMA(Y) / GAMMA(X+Y)
129//
130// Restrictions:
131//
132// Both X and Y must be greater than 0.
133//
134// Modified:
135//
136// 09 May 2003
137//
138// Author:
139//
140// John Burkardt
141//
142// Parameters:
143//
144// Input, double X, Y, the two parameters that define the Beta function.
145// X and Y must be greater than 0.
146//
147// Output, double BETA, the value of the Beta function.
148//
149{
150 if (x <= 0.0 || y <= 0.0) {
151 cout << "\n";
152 cout << "BETA - Fatal error!\n";
153 cout << " Both X and Y must be greater than 0.\n";
154 exit(1);
155 }
156
157 return (exp(gamma_log(x) + gamma_log(y) - gamma_log(x + y)));
158}
159double Probability::beta_inc(double a, double b, double x)
160//
161// Purpose:
162//
163// BETA_INC returns the value of the incomplete Beta function.
164//
165// Discussion:
166//
167// This calculation requires an iteration. In some cases, the iteration
168// may not converge rapidly, or may become inaccurate.
169//
170// Formula:
171//
172// BETA_INC(A,B,X)
173//
174// = Integral ( 0 <= T <= X ) T**(A-1) (1-T)**(B-1) dT
175// / Integral ( 0 <= T <= 1 ) T**(A-1) (1-T)**(B-1) dT
176//
177// = Integral ( 0 <= T <= X ) T**(A-1) (1-T)**(B-1) dT
178// / BETA(A,B)
179//
180// Modified:
181//
182// 10 October 2004
183//
184// Author:
185//
186// Majumder and Bhattacharjee.
187//
188// C++ translation by John Burkardt.
189//
190// Reference:
191//
192// Majumder, Bhattacharjee,
193// Algorithm AS63,
194// Applied Statistics,
195// 1973, volume 22, number 3.
196//
197// Parameters:
198//
199// Input, double A, B, the parameters of the function.
200// 0.0 < A,
201// 0.0 < B.
202//
203// Input, double X, the argument of the function.
204// Normally, 0.0 <= X <= 1.0.
205//
206// Output, double BETA_INC, the value of the function.
207//
208{
209 double cx = 0.0;
210 int i = 0;
211 int it = 0;
212 int it_max = 1000;
213 bool indx = false;
214 int ns = 0;
215 double pp = 0;
216 double psq = 0.0;
217 double qq = 0.0;
218 double rx = 0.0;
219 double temp = 0.0;
220 double term = 0.0;
221 double tol = 1.0E-07;
222 double value = 0.0;
223 double xx = 0.0;
224
225 if (a <= 0.0) {
226 cout << "\n";
227 cout << "BETA_INC - Fatal error!\n";
228 cout << " A <= 0.\n";
229 exit(1);
230 }
231
232 if (b <= 0.0) {
233 cout << "\n";
234 cout << "BETA_INC - Fatal error!\n";
235 cout << " B <= 0.\n";
236 exit(1);
237 }
238
239 if (x <= 0.0) {
240 value = 0.0;
241 return value;
242 } else if (1.0 <= x) {
243 value = 1.0;
244 return value;
245 }
246//
247// Change tail if necessary and determine S.
248//
249 psq = a + b;
250
251 if (a < (a + b) * x) {
252 xx = 1.0 - x;
253 cx = x;
254 pp = b;
255 qq = a;
256 indx = true;
257 } else {
258 xx = x;
259 cx = 1.0 - x;
260 pp = a;
261 qq = b;
262 indx = false;
263 }
264
265 term = 1.0;
266 i = 1;
267 value = 1.0;
268
269 //ns = ( int ) ( qq + cx * ( a + b ) );
270 ns = static_cast<int>( qq + cx * (a + b));
271//
272// Use Soper's reduction formulas.
273//
274 rx = xx / cx;
275
276 //temp = qq - ( double ) i;
277 temp = qq - static_cast<double> (i);
278 if (ns == 0) {
279 rx = xx;
280 }
281
282 it = 0;
283
284 for (;;) {
285 it = it + 1;
286
287 if (it_max < it) {
288 cout << "\n";
289 cout << "BETA_INC - Fatal error!\n";
290 cout << " Maximum number of iterations exceeded!\n";
291 cout << " IT_MAX = " << it_max << "\n";
292 exit(1);
293 }
294
295 //term = term * temp * rx / ( pp + ( double ) ( i ) );
296 term = term * temp * rx / (pp + static_cast<double>( i ));
297 value = value + term;
298 temp = fabs(term);
299
300 if (temp <= tol && temp <= tol * value) {
301 break;
302 }
303
304 i = i + 1;
305 ns = ns - 1;
306
307 if (0 <= ns) {
308 //temp = qq - ( double ) i;
309 temp = qq - static_cast<double> (i);
310 if (ns == 0) {
311 rx = xx;
312 }
313 } else {
314 temp = psq;
315 psq = psq + 1.0;
316 }
317 }
318//
319// Finish calculation.
320//
321 value = value * exp(pp * log(xx)
322 + (qq - 1.0) * log(cx)) / (beta(a, b) * pp);
323
324 if (indx) {
325 value = 1.0 - value;
326 }
327
328 return value;
329}
330
331double Probability::beta_pdf(double x, double a, double b)
332//
333//
334// Purpose:
335//
336// BETA_PDF evaluates the Beta PDF.
337//
338// Formula:
339//
340// PDF(A,B;X) = X**(A-1) * (1-X)**(B-1) / BETA(A,B).
341//
342// Discussion:
343//
344// A = B = 1 yields the Uniform distribution on [0,1].
345// A = B = 1/2 yields the Arcsin distribution.
346// B = 1 yields the power function distribution.
347// A = B -> Infinity tends to the Normal distribution.
348//
349// Modified:
350//
351// 10 October 2004
352//
353// Author:
354//
355// John Burkardt
356//
357// Parameters:
358//
359// Input, double X, the argument of the PDF.
360// 0.0 <= X <= 1.0.
361//
362// Input, double A, B, the parameters of the PDF.
363// 0.0 < A,
364// 0.0 < B.
365//
366// Output, double BETA_PDF, the value of the PDF.
367//
368{
369 double pdf;
370
371 if (x < 0.0 || 1.0 < x) {
372 pdf = 0.0;
373 } else {
374 pdf = pow(x, (a - 1.0)) * pow((1.0 - x), (b - 1.0)) / beta(a, b);
375 }
376
377 return pdf;
378}
379
380double Probability::beta_cdf(double x, double a, double b)
381//
382//
383// Purpose:
384//
385// BETA_CDF evaluates the Beta CDF.
386//
387// Modified:
388//
389// 10 October 2004
390//
391// Author:
392//
393// John Burkardt
394//
395// Parameters:
396//
397// Input, double X, the argument of the CDF.
398//
399// Input, double A, B, the parameters of the PDF.
400// 0.0 < A,
401// 0.0 < B.
402//
403// Output, double BETA_CDF, the value of the CDF.
404//
405{
406 double cdf;
407
408 if (x <= 0.0) {
409 cdf = 0.0;
410 } else if (x <= 1.0) {
411 cdf = beta_inc(a, b, x);
412 } else {
413 cdf = 1.0;
414 }
415
416 return cdf;
417}
418
419double Probability::beta_cdf_inv(double _cdf, double _a, double _b)
420// find the inverse of the cumulative beta distribution
421{
422 double p_low, p_high, p_est, F_est;
423 double err, err_max;
424 int max_count = 1000; // set maximum number of iterations
425 p_low = 0.0;
426 p_high = 1.0;
427 err = 100.0;
428 err_max = 1.0e-8; // set maximum error
429 //p_est = a/(a+b);
430 while (fabs(err) > err_max && max_count != 0) {
431 p_est = 0.5 * (p_low + p_high);
432 F_est = beta_cdf(p_est, _a, _b);
433 err = F_est - _cdf;
434 if (err <= 0.0) {
435 p_low = fmax(0.0, p_low + 0.5 * fabs(p_est - p_low));
436 } else {
437 p_high = fmin(1.0, p_high - 0.5 * fabs(p_high - p_est));
438 }
439 max_count -= 1;
440 }
441 return p_est;
442}
443
445//
446// Purpose:
447//
448// GAMMA_LOG calculates the natural logarithm of GAMMA ( X ) for positive X.
449//
450// Discussion:
451//
452// The program uses rational functions that theoretically approximate
453// LOG(GAMMA(X)) to at least 18 significant decimal digits. The
454// approximation for 12 < X is from Hart et al, while approximations
455// for X < 12.0 are similar to those in Cody and Hillstrom, but are
456// unpublished.
457//
458// The accuracy achieved depends on the arithmetic system, the compiler,
459// intrinsic functions, and proper selection of the machine-dependent
460// constants.
461//
462// Modified:
463//
464// 12 May 2003
465//
466// Author:
467//
468// William Cody and L. Stoltz
469// Argonne National Laboratory
470//
471// C++ translation by John Burkardt.
472//
473// Reference:
474//
475// William Cody, Kenneth Hillstrom,
476// Chebyshev Approximations for the Natural Logarithm of the Gamma Function,
477// Mathematics of Computation,
478// Volume 21, 1967, pages 198-203.
479//
480// Kenneth Hillstrom,
481// ANL/AMD Program ANLC366S, DGAMMA/DLGAMA,
482// May 1969.
483//
484// Hart, Ward Cheney, Charles Lawson, Maehly,
485// Charles Mesztenyi, John Rice, Thacher, Witzgall,
486// Computer Approximations,
487// Wiley and sons, New York, 1968.
488//
489// Parameters:
490//
491// Input, double X, the argument of the Gamma function. X must be positive.
492//
493// Output, double GAMMA_LOG, the logarithm of the Gamma function of X.
494// If X <= 0.0, or if overflow would occur, the program returns the
495// value XINF, the largest representable floating point number.
496//
497// Machine-dependent constants:
498//
499// BETA - radix for the floating-point representation.
500//
501// MAXEXP - the smallest positive power of BETA that overflows.
502//
503// XBIG - largest argument for which LN(GAMMA(X)) is representable
504// in the machine, i.e., the solution to the equation
505// LN(GAMMA(XBIG)) = BETA**MAXEXP.
506//
507// FRTBIG - Rough estimate of the fourth root of XBIG
508//
509//
510// Approximate values for some important machines are:
511//
512// BETA MAXEXP XBIG
513//
514// CRAY-1 (S.P.) 2 8191 9.62E+2461
515// Cyber 180/855
516// under NOS (S.P.) 2 1070 1.72E+319
517// IEEE (IBM/XT,
518// SUN, etc.) (S.P.) 2 128 4.08E+36
519// IEEE (IBM/XT,
520// SUN, etc.) (D.P.) 2 1024 2.55D+305
521// IBM 3033 (D.P.) 16 63 4.29D+73
522// VAX D-Format (D.P.) 2 127 2.05D+36
523// VAX G-Format (D.P.) 2 1023 1.28D+305
524//
525//
526// FRTBIG
527//
528// CRAY-1 (S.P.) 3.13E+615
529// Cyber 180/855
530// under NOS (S.P.) 6.44E+79
531// IEEE (IBM/XT,
532// SUN, etc.) (S.P.) 1.42E+9
533// IEEE (IBM/XT,
534// SUN, etc.) (D.P.) 2.25D+76
535// IBM 3033 (D.P.) 2.56D+18
536// VAX D-Format (D.P.) 1.20D+9
537// VAX G-Format (D.P.) 1.89D+76
538//
539{
540 double c[7] = {
541 -1.910444077728E-03,
542 8.4171387781295E-04,
543 -5.952379913043012E-04,
544 7.93650793500350248E-04,
545 -2.777777777777681622553E-03,
546 8.333333333333333331554247E-02,
547 5.7083835261E-03};
548 double corr;
549 double d1 = -5.772156649015328605195174E-01;
550 double d2 = 4.227843350984671393993777E-01;
551 double d4 = 1.791759469228055000094023;
552 double frtbig = 1.42E+09;
553 int i;
554 double p1[8] = {
555 4.945235359296727046734888,
556 2.018112620856775083915565E+02,
557 2.290838373831346393026739E+03,
558 1.131967205903380828685045E+04,
559 2.855724635671635335736389E+04,
560 3.848496228443793359990269E+04,
561 2.637748787624195437963534E+04,
562 7.225813979700288197698961E+03};
563 double p2[8] = {
564 4.974607845568932035012064,
565 5.424138599891070494101986E+02,
566 1.550693864978364947665077E+04,
567 1.847932904445632425417223E+05,
568 1.088204769468828767498470E+06,
569 3.338152967987029735917223E+06,
570 5.106661678927352456275255E+06,
571 3.074109054850539556250927E+06};
572 double p4[8] = {
573 1.474502166059939948905062E+04,
574 2.426813369486704502836312E+06,
575 1.214755574045093227939592E+08,
576 2.663432449630976949898078E+09,
577 2.940378956634553899906876E+010,
578 1.702665737765398868392998E+011,
579 4.926125793377430887588120E+011,
580 5.606251856223951465078242E+011};
581 double pnt68 = 0.6796875;
582 double q1[8] = {
583 6.748212550303777196073036E+01,
584 1.113332393857199323513008E+03,
585 7.738757056935398733233834E+03,
586 2.763987074403340708898585E+04,
587 5.499310206226157329794414E+04,
588 6.161122180066002127833352E+04,
589 3.635127591501940507276287E+04,
590 8.785536302431013170870835E+03};
591 double q2[8] = {
592 1.830328399370592604055942E+02,
593 7.765049321445005871323047E+03,
594 1.331903827966074194402448E+05,
595 1.136705821321969608938755E+06,
596 5.267964117437946917577538E+06,
597 1.346701454311101692290052E+07,
598 1.782736530353274213975932E+07,
599 9.533095591844353613395747E+06};
600 double q4[8] = {
601 2.690530175870899333379843E+03,
602 6.393885654300092398984238E+05,
603 4.135599930241388052042842E+07,
604 1.120872109616147941376570E+09,
605 1.488613728678813811542398E+010,
606 1.016803586272438228077304E+011,
607 3.417476345507377132798597E+011,
608 4.463158187419713286462081E+011};
609 double res;
610 double sqrtpi = 0.9189385332046727417803297;
611 double xbig = 4.08E+36;
612 double xden;
613 double xm1;
614 double xm2;
615 double xm4;
616 double xnum;
617 double xsq;
618//
619// Return immediately if the argument is out of range.
620//
621 if (x <= 0.0 || xbig < x) {
622 return r8_huge();
623 }
624
625 if (x <= r8_epsilon()) {
626 res = -log(x);
627 } else if (x <= 1.5) {
628 if (x < pnt68) {
629 corr = -log(x);
630 xm1 = x;
631 } else {
632 corr = 0.0;
633 xm1 = (x - 0.5) - 0.5;
634 }
635
636 if (x <= 0.5 || pnt68 <= x) {
637 xden = 1.0;
638 xnum = 0.0;
639
640 for (i = 0; i < 8; i++) {
641 xnum = xnum * xm1 + p1[i];
642 xden = xden * xm1 + q1[i];
643 }
644
645 res = corr + (xm1 * (d1 + xm1 * (xnum / xden)));
646 } else {
647 xm2 = (x - 0.5) - 0.5;
648 xden = 1.0;
649 xnum = 0.0;
650 for (i = 0; i < 8; i++) {
651 xnum = xnum * xm2 + p2[i];
652 xden = xden * xm2 + q2[i];
653 }
654
655 res = corr + xm2 * (d2 + xm2 * (xnum / xden));
656
657 }
658 } else if (x <= 4.0) {
659 xm2 = x - 2.0;
660 xden = 1.0;
661 xnum = 0.0;
662 for (i = 0; i < 8; i++) {
663 xnum = xnum * xm2 + p2[i];
664 xden = xden * xm2 + q2[i];
665 }
666
667 res = xm2 * (d2 + xm2 * (xnum / xden));
668 } else if (x <= 12.0) {
669 xm4 = x - 4.0;
670 xden = -1.0;
671 xnum = 0.0;
672 for (i = 0; i < 8; i++) {
673 xnum = xnum * xm4 + p4[i];
674 xden = xden * xm4 + q4[i];
675 }
676
677 res = d4 + xm4 * (xnum / xden);
678 } else {
679 res = 0.0;
680
681 if (x <= frtbig) {
682
683 res = c[6];
684 xsq = x * x;
685
686 for (i = 0; i < 6; i++) {
687 res = res / xsq + c[i];
688 }
689
690 }
691
692 res = res / x;
693 corr = log(x);
694 res = res + sqrtpi - 0.5 * corr;
695 res = res + x * (corr - 1.0);
696
697 }
698
699 return res;
700}
701
703 double cdf, p;
704/*
705 cdf = static_cast<double>(rand())/ static_cast<double>(RAND_MAX);
706 //cout << cdf << '\n';
707 if (cdf < 1.0e-6)
708 {
709 cdf = 1.0e-6;
710 }
711 if (cdf > 0.999999)
712 {
713 cdf = 0.999999;
714 }
715 p = beta_cdf_inv (cdf, alpha_parameter, beta_parameter);
716*/
718 return p;
719}
721 return failure_rate;
722}
723void Probability::setFailureRate(double failure_rate) {
725}
727 double p;
728 p = exp(-failure_rate*deltaT);
729 return p;
730}
double getProbability()
Definition: Probability.cpp:27
void copyProbability(Probability &)
Definition: Probability.cpp:28
double r8_epsilon(void)
Definition: Probability.cpp:49
void setBeta_Parameter(double)
Definition: Probability.cpp:46
double gamma_log(double)
double r8_huge()
Definition: Probability.cpp:87
double alpha_parameter
Definition: Probability.h:54
double beta_pdf(double, double, double)
virtual ~Probability()
Definition: Probability.cpp:23
double beta_cdf(double, double, double)
double probability
Definition: Probability.h:56
double getFailureRate() const
double getBeta_Parameter()
Definition: Probability.cpp:47
double failure_rate
Definition: Probability.h:55
double beta_cdf_inv(double, double, double)
void setProbability(double)
Definition: Probability.cpp:26
double beta_inc(double, double, double)
double getAlpha_Parameter()
Definition: Probability.cpp:45
double beta_parameter
Definition: Probability.h:53
double calculateProbability()
double beta(double, double)
double calculateSurvivalProbability(double)
void saveProbabilityToFile(std::ofstream &)
Definition: Probability.cpp:34
void retrieveProbabilityFromFile(std::ifstream &)
Definition: Probability.cpp:39
void setFailureRate(double failure_rate)
void setAlpha_Parameter(double)
Definition: Probability.cpp:44
bool log
Enable/disable rank-specific logging.