Changeset abafec5 in tol
- Timestamp:
- Jan 13, 2011, 6:13:10 PM (16 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- a419435
- Parents:
- e0b334d
- File:
-
- 1 edited
-
tol/bmath/bprdist/prd.cpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tol/bmath/bprdist/prd.cpp
re0b334d rabafec5 403 403 } 404 404 405 //-------------------------------------------------------------------- 406 BDat BDiscreteDist::Inverse (BDat prob, BDat tolerance) 407 //-------------------------------------------------------------------- 408 { 409 BText _MID = "BDiscreteDist::Inverse"; 410 if(wrongParameter_|| prob.IsUnknown()) { return(BDat::Unknown());} 411 if(prob+1.0<1.0) { return(BDat::Unknown()); } 412 if(prob-1.0>0.0) { return(BDat::Unknown()); } 413 if(prob+1.0==1.0) { return(min_); } 414 if(prob-1.0==0.0) { return(max_); } 415 bool has_lower_bound = !min_.IsNegInf(); 416 bool has_upper_bound = !max_.IsPosInf(); 417 double a = min_.Value(); 418 double b = max_.Value(); 419 double pa = has_lower_bound ? Dist(a).Value() : 0; 420 if(prob<=pa) { return(a); } 421 double pb = 1.0; 422 double h = 1.0; 423 double h_factor = 10.0; 424 if(!has_lower_bound || !has_upper_bound) 425 { 426 double avr = round(Average().Value()); 427 if(!IS_FINITE(avr)) 428 { 429 Error(_MID+"Cannot handle with distribution which average is not finite."); 430 return(BDat::Unknown()); 431 } 432 double pdf_avr = Dist(avr).Value(); 433 if(prob+1==pdf_avr+1) { return(avr); } 434 else if(pdf_avr>prob) 435 { 436 b = avr; 437 pb = pdf_avr; 438 if(!has_lower_bound) 439 { 440 a = b; 441 pa = pb; 442 } 443 } 444 else // if(pdf_avr<prob) 445 { 446 a = avr; 447 pa = pdf_avr; 448 if(!has_upper_bound) 449 { 450 b = a; 451 pb = pa; 452 } 453 } 454 }; 455 has_lower_bound = IS_FINITE(a)!=0; 456 has_upper_bound = IS_FINITE(b)!=0; 457 if(!has_lower_bound || !has_upper_bound) 458 { 459 Error(_MID+"Sorry, cannot find a finite searching interval for p="+prob); 460 return(BDat::Unknown()); 461 } 462 while(pa>prob) 463 { 464 a -= h; 465 pa = Dist(a).Value(); 466 if(prob+1==pa+1) { return(a); } 467 h *= h_factor; 468 } 469 while(pb<prob) 470 { 471 b += h; 472 pb = Dist(b).Value(); 473 if(prob+1==pb+1) { return(b); } 474 h *= h_factor; 475 } 476 double pa1 = (min_>=a-1)?0.0:Dist(a-1).Value(); 477 double pb1 = Dist(b-1).Value(); 478 if((pa1 < prob) && (prob <=pa)) { return(a); } 479 else if((pb1 < prob) && (prob <=pb)) { return(b); } 480 481 if((a>=b)||(pa>=pb)||(pa>prob)||(pb<prob)) 482 { 483 Error(_MID+"Sorry, cannot find a valid searching interval for p="+prob); 484 return(BDat::Unknown()); 485 } 486 double x; 487 double px; 488 double px1; 489 while(a<b-1) 490 { 491 x = round((a+b)/2); 492 px = Dist(x).Value(); 493 px1 = Dist(x-1).Value(); 494 if((px1 < prob) && (prob <=px)) { return(x); } 495 else if(prob> px1) { a = x; pa = px; pa1 = px1; } 496 else { b = x; pb = px; pb1 = px1; } 497 } 498 if((pa1 < prob) && (prob <= pa)) { return(a); } 499 else if((pb1 < prob) && (prob <= pb)) { return(b); } 500 else 501 { 502 Error(_MID+"Sorry, cannot find a valid searching interval for p="+prob); 503 return(BDat::Unknown()); 504 } 505 506 } 405 507 406 508 //-------------------------------------------------------------------- … … 2055 2157 return(BDat::Unknown()); 2056 2158 } 2159 2057 2160 2058 2161 … … 2428 2531 double p, q, s, xlam, bound; 2429 2532 int which, status; 2430 s = (double) x.Value();2533 s = round((double) x.Value()); 2431 2534 xlam = (double) fi_.Value(); 2432 2535 which = 1; … … 2474 2577 2475 2578 //-------------------------------------------------------------------- 2476 BDat BPoissonDist::Inverse(BDat x, BDat tolerance)2477 2478 /*! Returns Inv(Sum{0..x}.Poisson(i,fi))2479 */ 2480 //-------------------------------------------------------------------- 2481 { 2482 //return(BDiscreteDist::Inverse(x,tolerance));2483 2579 BDat BPoissonDist::Inverse(BDat p, BDat tolerance) 2580 2581 /*! Returns the maximum x matching Poisson(x,fi) <= p 2582 */ 2583 //-------------------------------------------------------------------- 2584 { 2585 return(BDiscreteDist::Inverse(p,tolerance)); 2586 /* 2484 2587 if(wrongParameter_|| x.IsUnknown()) { return(BDat::Unknown());} 2588 2589 2485 2590 double p, q, s, xlam, bound=0.0; 2486 2591 int which, status=0; … … 2493 2598 if(status==0) 2494 2599 { 2495 return( round(s));2600 return(s); 2496 2601 } 2497 2602 else if(status==1) 2498 2603 { 2499 return(0.0); 2604 Warning(BText("[BPoissonDist::Dist] the answer of cdfpoi appears to be lower than lowest search bound ")+ 2605 "\n p="+x.Name()+ 2606 "\n s="+s+ 2607 "\n status="+status+ 2608 "\n bound="+bound); 2609 return(s); 2500 2610 } 2501 2611 else if(status==2) 2502 2612 { 2613 Warning(BText("[BPoissonDist::Dist] the answer of cdfpoi appears to be higher than greatest search bound ")+ 2614 "\n p="+x.Name()+ 2615 "\n s="+s+ 2616 "\n status="+status+ 2617 "\n bound="+bound); 2503 2618 return(BDat::PosInf()); 2504 2619 } … … 2507 2622 Error(BText("[BPoissonDist::Dist] cdfpoi fails at ")+ 2508 2623 "\n p="+x.Name()+ 2624 "\n s="+s+ 2509 2625 "\n status="+status+ 2510 2626 "\n bound="+bound);
Note: See TracChangeset
for help on using the changeset viewer.
