
7`Jc           @   s   d  d l  m Z d  d l  m Z d Z e e  Z e e  Z d  d l  Z  e d  Z d   Z d   Z	 d   Z
 d	   Z d
 d  Z d
 d  Z d   Z d S(   i(   t   exp(   t   logi   Nc         C   s   t  |   } | d k r' t d   n  | rV | d k rF t | |  St | |  Sn& | d k ro t | |  St | |  Sd S(   sP  Poisson CDF evaluater.

    This is a more stable CDF function. It can tolerate large lambda
    value. While the lambda is larger than 700, the function will be a
    little slower.

    Parameters:
    n     : your observation
    lam   : lambda of poisson distribution
    lower : if lower is False, calculate the upper tail CDF
    g        s   Lambda must > 0i  N(   t   intt	   Exceptiont   __poisson_cdf_large_lambdat   __poisson_cdft   __poisson_cdf_Q_large_lambdat   __poisson_cdf_Q(   t   nt   lamt   lowert   k(    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyt   poisson_cdf   s    c         C   sw   |  d k  r d St  |  } | } x9 t d |  d  D]$ } | } | | | } | | } q7 W| d k ro d S| Sd S(   sV   Poisson CDF For small lambda. If a > 745, this will return
    incorrect result.

    i    i   N(   R    t   xrange(   R   t   at   nextt   cdft   it   last(    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyR   1   s    c   	      C   s  |  d k  r d St  | t  } | t } t |  } t } | d 8} | } x t d |  d  D]| } | } | | | } | | } | t k s | t k ra | d k r | t 9} | t 9} | d 8} q | | 9} d } qa qa Wx t |  D] } | t 9} q W| | 9} | S(   s.   Slower poisson cdf for large lambda.
    
    i    i   (   R   t   LSTEPR    t   EXPSTEPR   t   EXPTHRES(	   R   R   t	   num_partst	   last_partt   lastexpR   R   R   R   (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyR   C   s.    






c         C   s   |  d k  r d St  |  } x/ t d |  d  D] } | } | | | } q1 Wd } |  d } x8 | d k r | } | | | } | | 7} | d 7} qb W| S(   sJ   internal Poisson CDF evaluater for upper tail with small
    lambda.

    i    i   (   R    R   (   R   R   R   R   R   R   (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyR   a   s    

c   	      C   s  |  d k  r d St  | t  } | t } t |  } t } | d 8} xq t d |  d  D]\ } | } | | | } | t k r[ | d k r | t 9} | d 8} q | | 9} d } q[ q[ Wd } |  d } x | d k r]| } | | | } | | 7} | d 7} | t k s| t k r | d k rG| t 9} | t 9} | d 8} qZ| | 9} d } q q Wx t |  D] } | t 9} qkW| | 9} | S(   sU   Slower internal Poisson CDF evaluater for upper tail with large
    lambda.
    
    i    i   (   R   R   R    R   R   R   (	   R   R   R   R   R   R   R   R   R   (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyR   w   sD    










i  c         C   s   | d k  s t   |  d k  s* |  d k r9 t d   n |  d k rI d Sd } t |  } | } x[ t d | d  D]F } | } | } | | | } | | } | |  k rv |  | k rv | Sqv W| S(   s   inverse poisson distribution.

    cdf : the CDF
    lam : the lambda of poisson distribution

    note: maxmimum return value is 1000
    and lambda must be smaller than 740.
    i  i    i   s   CDF must >= 0 and <= 1(   t   AssertionErrorR   R    R   (   R   R	   t   maximumt   sum2t   newvalR   t   sumoldR   (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyt   poisson_cdf_inv   s     	
c         C   s   | d k  s t   |  d k  s* |  d k r9 t d   n |  d k rI d Sd } t |  } | } x[ t d | d  D]F } | } | } | | | } | | } | |  k rv |  | k rv | Sqv W| S(   s   inverse poisson distribution.

    cdf : the CDF
    lam : the lambda of poisson distribution

    note: maxmimum return value is 1000
    and lambda must be smaller than 740.
    i  i    i   s   CDF must >= 0 and <= 1(   R   R   R    R   (   R   R	   R   R   R   R   R   R   (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyt   poisson_cdf_Q_inv   s     	
c         C   s2   | d k r d St  |  t | |   t |   S(   s   Poisson PDF.

    PDF(K,A) is the probability that the number of events observed in
    a unit time period will be K, given the expected number of events
    in a unit time.
    i    (   R    t   powt	   factorial(   R   R   (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyt   poisson_pdf   s    (   t   mathR    R   R   R   R   t   TrueR   R   R   R   R   R   R   R"   (    (    (    s=   /woldlab/castor/data00/home/georgi/programs/NPS-1.3.2/Prob.pyt   <module>   s   				,$