Changeset fb55369 in tol


Ignore:
Timestamp:
Aug 3, 2012, 9:04:16 AM (14 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
3f203ae
Parents:
902a6ae
Message:

Refs #1590

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@5005 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol/bmath/bprdist
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tol/bmath/bprdist/prd.cpp

    r902a6ae rfb55369  
    24152415
    24162416//--------------------------------------------------------------------
    2417 BBinomialDist::BBinomialDist(BInt N, BDat p)
     2417BBinomialDist::BBinomialDist(BDat N, BDat p)
    24182418
    24192419/*! Creates an object aimed to work whith a binomial distribution.
    … …  
    24302430    logp_ = Log(p_);
    24312431    logq_ = Log(q_);
    2432     logGammaN_ = LogFact(N_);
     2432    logGammaN_ = LogGamma(1+N_);
    24332433    if((N_<=0)||(p_<0)||(p_>1))
    24342434    {
    … …  
    24512451    BInt k = (BInt)Floor(x).Value();
    24522452    if((k<0)||(k>N_)) { return(0); }
    2453     return(Exp(logGammaN_-LogFact(N_-k)-LogFact(k)+k*logp_+(N_-k)*logq_));
     2453    return(Exp(logGammaN_-LogGamma(1+N_-k)-LogFact(k)+k*logp_+(N_-k)*logq_));
    24542454}
    24552455
    … …  
    24712471       
    24722472        s = k;
    2473         xn = N_;
     2473        xn = N_.Value();
    24742474        pr = p_.Value();
    24752475        ompr = 1 - pr;
    … …  
    24942494    p = x.Value();
    24952495    q = 1-p;
    2496     xn = (double) N_;
     2496    xn = N_.Value();
    24972497    pr = p_.Value();
    24982498    ompr = 1-pr;
    … …  
    25422542
    25432543//--------------------------------------------------------------------
    2544 BNegBinomialDist::BNegBinomialDist(BInt N, BDat P)
     2544BNegBinomialDist::BNegBinomialDist(BDat N, BDat P)
    25452545
    25462546/*! Creates an object aimed to work whith a negative binomial distribution.
    2547  *  A negative binomial distribution counts the number of failures
    2548  *  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.
    25492549 * \param N Number of succeses.
    25502550 * \param P Success probability.
    … …  
    25762576
    25772577/*! Returns NegBinomial(x,N_,P_)
    2578  *  = prob of getting x failures before the N_-th success
     2578 *  = prob of getting x succeses before the N_-th failures
    25792579 *
    25802580 *    ALGORITHM: NB(x,N_,P_) = P_ * DensBinomial(N_-1,k+N_-1,P_)
    … …  
    25832583//--------------------------------------------------------------------
    25842584{
    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()));
    25932588}
    25942589
    … …  
    26012596//--------------------------------------------------------------------
    26022597{
     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/* */
    26032603    double p, q, s, xn, pr, ompr, bound;
    26042604    int which, status;
    26052605    s = x.Value();
    2606     xn = (double) N_;
     2606    xn = N_.Value();
    26072607    pr = P_.Value();
    26082608    ompr = 1-pr;
    … …  
    26132613    else
    26142614        return(BDat::Unknown());
     2615/* */
    26152616}
    26162617
    … …  
    26232624{
    26242625  if(wrongParameter_ || x.IsUnknown()) { return(BDat::Unknown()); }
     2626//return(BDiscreteDist::Inverse(x,tolerance));
     2627
    26252628    double p, q, s, xn, pr, ompr, bound;
    26262629    int which, status;
    26272630    p = x.Value();
    26282631    q = 1-p;
    2629     xn = (double) N_;
     2632    xn = N_.Value();
    26302633    pr = P_.Value();
    26312634    ompr = 1-pr;
    … …  
    26412644       
    26422645        cumnbn(&x0,&xn,&pr,&ompr,&fx0,&q);
    2643         /* may be, there is an incremental way */
    26442646        cumnbn(&x1,&xn,&pr,&ompr,&fx1,&q);
    26452647        int K1 = 1;
    26462648        return fabs(fx1-p)<=spmpar(&K1) ? x1 : x0;
    26472649    }
     2650
    26482651}
    26492652
    … …  
    27782781//--------------------------------------------------------------------
    27792782{
     2783  if(wrongParameter_ || p.IsUnknown()) { return(BDat::Unknown()); }
    27802784  return(BDiscreteDist::Inverse(p,tolerance));
    27812785/*
    … …  
    28652869
    28662870
     2871
     2872
     2873//--------------------------------------------------------------------
     2874BGenCountDist::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//--------------------------------------------------------------------
     2911BDat 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//--------------------------------------------------------------------
     2924BDat 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//--------------------------------------------------------------------
     2936BDat 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
    28672947//--------------------------------------------------------------------
    28682948BHypergeometricDist::BHypergeometricDist(BInt N, BInt P, BInt n)
  • tol/bmath/bprdist/tol_bprdist.h

    r902a6ae rfb55369  
    873873{
    874874private:
    875   BInt N_;
     875  BDat N_;
    876876  BDat p_;
    877877  BDat q_;
    … …  
    882882public:
    883883  // contructors and destructors:
    884   BBinomialDist(BInt N, BDat p);
     884  BBinomialDist(BDat N, BDat p);
    885885
    886886    // Implementation: prd.cpp
    … …  
    901901{
    902902private:
    903   BInt N_;
     903  BDat N_;
    904904  BDat p_;
    905905  BDat q_;
    … …  
    912912public:
    913913  // contructors and destructors:
    914   BNegBinomialDist(BInt N, BDat P);
     914  BNegBinomialDist(BDat N, BDat P);
    915915
    916916  // Implementation: prd.cpp
    … …  
    959959};
    960960
     961//--------------------------------------------------------------------
     962class TOL_API BGenCountDist : public BDiscreteDist
     963
     964/*! Generic counting probability distribution definition.
     965 */
     966//--------------------------------------------------------------------
     967{
     968private:
     969  BDat a_; //average
     970  BDat v_; //variance
     971  int subType_;
     972  BDiscreteDist* cd_;
     973  BDat m_;
     974  BDat p_;
     975
     976public:
     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};
    961988
    962989//--------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.