Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Poisson inverse cumulative distribution function that returns k instead of m?

    Hello,

    Stata's invpoisson() function "returns the Poisson mean such that the cumulative Poisson distribution evaluated at k is p: if poisson(m,k) = p, then invpoisson(k,p) = m," according to the user guide. Is there an inverse Poisson CDF function that returns k for a given (m,p)? I understand the difficulty in inverting the Poisson CDF, but R has this function (QPOIS), so I thought it might be possible in Stata.

    Thanks for your time,

    Bob

  • #2
    This is adapted from:
    http://www.mrexcel.com/forum/excel-q...e-poisson.html


    Code:
    discard
    capture program drop invpoisk
    *For a Poisson process with mean m
    *returns the largest integer such that the CDF <= p
    *E.g.,  disp %18.15f poisson(10,5) returns 0.067085962879032
    *invpoisk(10, 0.067085962879032) returns 5
    *Adapted from http://www.mrexcel.com/forum/excel-questions/507508-reverse-poisson.html
    
    program invpoisk, rclass
    syntax ,[ m(real 1) p(real 1)]
        scalar dm = exp(-`m')
        scalar dCDF = 0
        scalar dFact =1
        scalar dPower = 1
        scalar iX = 0
        while dCDF < `p' {
            scalar dCDF = dCDF + dm*dPower/ dFact
            scalar iX = iX + 1
            scalar dFact = dFact* iX
            scalar dPower = dPower * `m'
        }
        scalar k = iX - cond(dCDF/`p' >1, 2,1)
        return scalar k = k
        disp "k = " k " for a given mean and probability(`m', `p')"
    end
    Code:
    . invpoisk, m(10) p(.067085962879032)
    k = 5 for a given mean and probability(10, .067085962879032)
    
    . invpoisk, m(4.28) p(.2)
    k = 2 for a given mean and probability(4.28, .2)

    Comment


    • #3
      This is awesome. Thank you for your help!

      -Bob

      Comment

      Working...
      X