Announcement

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

  • #16
    Hi Daniel,

    The invcdfpoisson command you wrote appears to "hang" when the specified p-value is below a certain threshold. Here it works when the p-value is 0.007, but hangs when it is 0.006. In both cases, -qpois- in R returns 0.

    Code:
    mata : invcdfpoisson(.9 * 2.26, .0007)
    0

    Code:
    mata : invcdfpoisson(.9 * 2.26, .0006)
    I haven't been able to reproduce the error with the invcdfbinomial you wrote first, so I am hoping it's not an issue there as well.

    Thanks again for your help! Your mata code is integral to the "exact" option in a package I wrote called -funnenlinst- (available from SSC).

    Ariel

    Comment


    • #17
      poisson() returns missing if k < 0, leading to an infinite loop. Here is a fixed version:

      Code:
      version 16.1
      
      mata :
      
      real scalar invcdfpoisson(
          
          real scalar m,
          real scalar x
          
          )
      {
          real scalar mu
          real scalar sigma
          real scalar gamma
          real scalar z
          real scalar k
          
          
          if ( hasmissing((m,x)) )
              return(.)
              
          if (m < 0)
              _error(3300)
          
          if ( (x<0)|(x>1) )
              _error(3300)
          
          if (m == 0)
              return(0)
          
          if (x == 0)
              return(0)
          
          if (x == 1)
              return(.)
          
          mu    = m
          sigma = sqrt(m)
          gamma = 1/sigma
          
          z = invnormal(x)
          k  = round(mu + sigma * (z + gamma*(z*z-1)/6) + 0.5)
          k = max((0,k))
          
          if (poisson(m,k) >= x)
              while ( k & (poisson(m,k-1)>x) )
                  k--
          else
              while (poisson(m,k) <= x)
                  k++
      
          return(k)
      }
      
      end

      Comment


      • #18
        Thank you, Daniel! That fixed the problem!

        Comment

        Working...
        X