Changeset fb55369 in tol
- Timestamp:
- Aug 3, 2012, 9:04:16 AM (14 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- 3f203ae
- Parents:
- 902a6ae
- Location:
- tol/bmath/bprdist
- Files:
-
- 2 edited
-
prd.cpp (modified) (14 diffs)
-
tol_bprdist.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tol/bmath/bprdist/prd.cpp
r902a6ae rfb55369 2415 2415 2416 2416 //-------------------------------------------------------------------- 2417 BBinomialDist::BBinomialDist(B Int N, BDat p)2417 BBinomialDist::BBinomialDist(BDat N, BDat p) 2418 2418 2419 2419 /*! Creates an object aimed to work whith a binomial distribution. … … 2430 2430 logp_ = Log(p_); 2431 2431 logq_ = Log(q_); 2432 logGammaN_ = Log Fact(N_);2432 logGammaN_ = LogGamma(1+N_); 2433 2433 if((N_<=0)||(p_<0)||(p_>1)) 2434 2434 { … … 2451 2451 BInt k = (BInt)Floor(x).Value(); 2452 2452 if((k<0)||(k>N_)) { return(0); } 2453 return(Exp(logGammaN_-Log Fact(N_-k)-LogFact(k)+k*logp_+(N_-k)*logq_));2453 return(Exp(logGammaN_-LogGamma(1+N_-k)-LogFact(k)+k*logp_+(N_-k)*logq_)); 2454 2454 } 2455 2455 … … 2471 2471 2472 2472 s = k; 2473 xn = N_ ;2473 xn = N_.Value(); 2474 2474 pr = p_.Value(); 2475 2475 ompr = 1 - pr; … … 2494 2494 p = x.Value(); 2495 2495 q = 1-p; 2496 xn = (double) N_;2496 xn = N_.Value(); 2497 2497 pr = p_.Value(); 2498 2498 ompr = 1-pr; … … 2542 2542 2543 2543 //-------------------------------------------------------------------- 2544 BNegBinomialDist::BNegBinomialDist(B Int N, BDat P)2544 BNegBinomialDist::BNegBinomialDist(BDat N, BDat P) 2545 2545 2546 2546 /*! Creates an object aimed to work whith a negative binomial distribution. 2547 * A negative binomial distribution counts the number of failures2548 * before a fixed number of successes whith a fixed success probability.2547 * A negative binomial distribution counts the number of successes 2548 * before a fixed number of failures whith a fixed success probability. 2549 2549 * \param N Number of succeses. 2550 2550 * \param P Success probability. … … 2576 2576 2577 2577 /*! Returns NegBinomial(x,N_,P_) 2578 * = prob of getting x failures before the N_-th success2578 * = prob of getting x succeses before the N_-th failures 2579 2579 * 2580 2580 * ALGORITHM: NB(x,N_,P_) = P_ * DensBinomial(N_-1,k+N_-1,P_) … … 2583 2583 //-------------------------------------------------------------------- 2584 2584 { 2585 if(wrongParameter_ || x.IsUnknown()) { return(BDat::Unknown()); } 2586 BInt k = (BInt)Floor(x).Value(); 2587 if((k<0)) { return(0); } 2588 if((k==0)&&(N_==1)){return P_;} 2589 else{ 2590 BBinomialDist b(k+N_-1,P_); 2591 return(b.Dens(N_-1)*P_); 2592 } 2585 if(wrongParameter_ || x.IsUnknown()) { return(BDat::Unknown()); } 2586 BInt k = (BInt)Floor(x).Value(); 2587 return(gsl_ran_negative_binomial_pdf(k,P_.Value(),N_.Value())); 2593 2588 } 2594 2589 … … 2601 2596 //-------------------------------------------------------------------- 2602 2597 { 2598 if(wrongParameter_ || x.IsUnknown()) { return(BDat::Unknown()); } 2599 BInt k = (BInt)Floor(x).Value(); 2600 //return(1-gsl_sf_beta_inc (k+1, N_.Value(), 1-P_.Value())); 2601 //return(gsl_cdf_negative_binomial_P(k,P_.Value(),N_.Value())); 2602 /* */ 2603 2603 double p, q, s, xn, pr, ompr, bound; 2604 2604 int which, status; 2605 2605 s = x.Value(); 2606 xn = (double) N_;2606 xn = N_.Value(); 2607 2607 pr = P_.Value(); 2608 2608 ompr = 1-pr; … … 2613 2613 else 2614 2614 return(BDat::Unknown()); 2615 /* */ 2615 2616 } 2616 2617 … … 2623 2624 { 2624 2625 if(wrongParameter_ || x.IsUnknown()) { return(BDat::Unknown()); } 2626 //return(BDiscreteDist::Inverse(x,tolerance)); 2627 2625 2628 double p, q, s, xn, pr, ompr, bound; 2626 2629 int which, status; 2627 2630 p = x.Value(); 2628 2631 q = 1-p; 2629 xn = (double) N_;2632 xn = N_.Value(); 2630 2633 pr = P_.Value(); 2631 2634 ompr = 1-pr; … … 2641 2644 2642 2645 cumnbn(&x0,&xn,&pr,&ompr,&fx0,&q); 2643 /* may be, there is an incremental way */2644 2646 cumnbn(&x1,&xn,&pr,&ompr,&fx1,&q); 2645 2647 int K1 = 1; 2646 2648 return fabs(fx1-p)<=spmpar(&K1) ? x1 : x0; 2647 2649 } 2650 2648 2651 } 2649 2652 … … 2778 2781 //-------------------------------------------------------------------- 2779 2782 { 2783 if(wrongParameter_ || p.IsUnknown()) { return(BDat::Unknown()); } 2780 2784 return(BDiscreteDist::Inverse(p,tolerance)); 2781 2785 /* … … 2865 2869 2866 2870 2871 2872 2873 //-------------------------------------------------------------------- 2874 BGenCountDist::BGenCountDist(BDat a, BDat v) 2875 2876 /*! Creates an object aimed to deal with a Generic Counting distribution 2877 * depending just in average and variance parameters and that could be 2878 * - Binomial: average>variance 2879 * - Poisson: average=variance 2880 * - Negative Binomial: average<variance 2881 */ 2882 //-------------------------------------------------------------------- 2883 : BDiscreteDist(), a_(a), v_(v), 2884 cd_(NULL), m_(BDat::Unknown()), p_(BDat::Unknown()) 2885 { 2886 subType_ = Sign(a_-v_).Value(); 2887 m_ = a_*a_/Abs(a_-v_); 2888 p_ = 1.0 - min(v_/a_,a_/v_); 2889 if(subType_==1) 2890 { 2891 cd_ = new BBinomialDist(m_.Value(),p_); 2892 } 2893 else if(subType_==0) 2894 { 2895 cd_ = new BPoissonDist(a_); 2896 } 2897 if(subType_==-1) 2898 { 2899 cd_ = new BNegBinomialDist(m_.Value(),1-p_); 2900 } 2901 } 2902 2903 //-------------------------------------------------------------------- 2904 BGenCountDist::~BGenCountDist() 2905 //-------------------------------------------------------------------- 2906 { 2907 if(cd_) { delete cd_; } 2908 } 2909 2910 //-------------------------------------------------------------------- 2911 BDat BGenCountDist::Dens(BDat x) 2912 2913 /*! Returns PDF 2914 */ 2915 //-------------------------------------------------------------------- 2916 { 2917 BDat d; 2918 if(cd_) { d = cd_->Dens(x); } 2919 return(d); 2920 } 2921 2922 2923 //-------------------------------------------------------------------- 2924 BDat BGenCountDist::Dist(BDat x) 2925 2926 /*! Returns CDF 2927 */ 2928 //-------------------------------------------------------------------- 2929 { 2930 BDat d; 2931 if(cd_) { d = cd_->Dist(x); } 2932 return(d); 2933 } 2934 2935 //-------------------------------------------------------------------- 2936 BDat BGenCountDist::Inverse(BDat p, BDat tolerance) 2937 2938 /*! Returns the maximum x matching Poisson(x,fi) <= p 2939 */ 2940 //-------------------------------------------------------------------- 2941 { 2942 BDat i; 2943 if(cd_) { i = cd_->Inverse(p,tolerance); } 2944 return(i); 2945 } 2946 2867 2947 //-------------------------------------------------------------------- 2868 2948 BHypergeometricDist::BHypergeometricDist(BInt N, BInt P, BInt n) -
tol/bmath/bprdist/tol_bprdist.h
r902a6ae rfb55369 873 873 { 874 874 private: 875 B Int N_;875 BDat N_; 876 876 BDat p_; 877 877 BDat q_; … … 882 882 public: 883 883 // contructors and destructors: 884 BBinomialDist(B Int N, BDat p);884 BBinomialDist(BDat N, BDat p); 885 885 886 886 // Implementation: prd.cpp … … 901 901 { 902 902 private: 903 B Int N_;903 BDat N_; 904 904 BDat p_; 905 905 BDat q_; … … 912 912 public: 913 913 // contructors and destructors: 914 BNegBinomialDist(B Int N, BDat P);914 BNegBinomialDist(BDat N, BDat P); 915 915 916 916 // Implementation: prd.cpp … … 959 959 }; 960 960 961 //-------------------------------------------------------------------- 962 class TOL_API BGenCountDist : public BDiscreteDist 963 964 /*! Generic counting probability distribution definition. 965 */ 966 //-------------------------------------------------------------------- 967 { 968 private: 969 BDat a_; //average 970 BDat v_; //variance 971 int subType_; 972 BDiscreteDist* cd_; 973 BDat m_; 974 BDat p_; 975 976 public: 977 // contructors and destructors: 978 BGenCountDist(BDat a, BDat v); 979 ~BGenCountDist(); 980 981 // Implementation: prd.cpp 982 BDat Average () { return(a_); } 983 BDat Varianze () { return(v_); } 984 BDat Dist (BDat x); 985 BDat Dens (BDat x); 986 BDat Inverse (BDat prob, BDat tolerance = 0.1); 987 }; 961 988 962 989 //--------------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.
