
$Oc           @   s  d  d l  m Z d  d l Td Z e d  a d d d d d	 d
 g a d a d   Z d   Z	 d   Z
 d d d  Z d d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d  Z e d k rd Ge d  d   GHd! Ge d  d"  GHd# Ge d  d   Ge d  d   GHd$ Ge d%  GHd& Ge d'  Ge d%  GHd( Ge d)  GHd* Ge d  GHd+ Ge d, d-  Ge d, d-  GHd. Ge d) d/  GHn  d0 S(1   i(   t   division(   t   *s   Too many iterations: g       @gSvS@g+WUg8@g뒦~4gM&S?g1!e[־g%@c         C   sw   |  d } | d } | d t  |  | } d } x0 t d  D]" } | d } | t | | } q? W| t  t |  S(   s    Logarithm of the gamma function.g      ?g      @g      ?i   (   t   logt   ranget
   gammln_coft
   gammln_stp(   t   xxt   xt   tmpt   sert   j(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   gammln/   s    


c         C   s   t  t |    S(   s   Gamma function.(   t   expR   (   R   (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   gamma=   s    c         C   s(   t  t |   t |  t |  |   S(   s   Beta function.(   R   R   (   t   at   b(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   betaC   s    i  gv!>c   	      C   s   t  |   } | d k  r$ t |  n  | d k r4 d S|  } d |  } | } d } x | | k r | d } | | | } | | } t |  t |  | k  r | t | |  t |  |  | f S| d } qS Wt t t |  t |  | f   d S(   s1   Series approx'n to the incomplete gamma function.g        g      ?i   N(   R   t   bad_argt   absR   R   t	   max_iterst   str(	   R   R   t   itmaxt   epst   glnt   apt   sumt   deltat   n(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   gserH   s"    


'i   c         C   s@  t  |   } d } d } | } d } d }	 d }
 d } x | | k r| } | |  } | | | |
 } |	 | | |
 } | |
 } | | | | } | | | |	 }	 | d k rd | }
 |	 |
 } t | | |  | k  r| t | |  t |  |  | f S| } n  | d } q9 Wt t t | | |    d S(   s=   Continued fraction approx'n of the incomplete gamma function.g        g      ?i   N(   R   R   R   R   R   R   (   R   R   R   R   R   t   goldt   a0t   a1t   b0t   b1t   facR   t   ant   anat   anft   g(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   gcf]   s0    



'	c         C   sd   | d k  s |  d k r* t  |  | f  n  | |  d k  rK t |  |  d Sd t |  |  d Sd S(   s   Incomplete gamma function.g        g      ?i    N(   t
   ValueErrorR   R'   (   R   R   (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   gammpy   s
    c         C   sj   | d k  s |  d k r0 t  t |  | f   n  | |  d k  rU d t |  |  d St |  |  d Sd S(   s   Incomplete gamma function.g        g      ?i    N(   R(   t   reprR   R'   (   R   R   (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   gammq   s
    c      	   C   s   d } d } d } d } d } d } d } |  d k rI d	 } d	 |  }  n  d
 t  |   } t |  }	 |	 | |	 | |	 | d	 |	 | |	 | |	 | }
 | |
 S(   s   Inverse of the normal CDF.gA|`@g
O?g>]ݱ&?g!?gĒr9?gobHN&nU?g      g      ?g      ?g       (   R   t   sqrt(   t   pt   c0t   c1t   c2t   d1t   d2t   d3t   signt   argt   tR&   (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   ncdf_inv   s    6c         C   s   t  |   } d d d | } | t | | d | d | d | d | d | d | d	 | d
 | d | d  } |  d k r | Sd | Sd S(   s   Complementary error function.g      ?g      ?gś??g5 ?g`yg?gƸ?gꪂIǿg#v?g9)gS?gޅ1Ogv(?g        g       @N(   R   R   (   R   t   zR6   t   r(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   erfcc   s    c         C   s   d d t  |  t  S(   s   Cumulative normal dist'n.g      ?g      ?(   R:   t   rt2(   R   (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   ncdf   s    c         C   s   d t  |  t  S(   sg   Cummulative normal dist'n inside nsig sigmas.
	ncdf_sig = 1 - 2 * (upper tail) = 1 - erfc(sigfac/rt(2))g      ?(   R:   R;   (   t   nsig(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   ncdf_sig   s    c         C   s!   d | } d |  } t  | |  S(   s`   Lower tail area of the chi**2 dist'n with nu dof.
	Note that chisq is *not* the reduced chis**2!g      ?(   R)   (   t   chisqt   nut   hnut   hchi(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   pchisq   s    

c         C   s!   d | } d |  } t  | |  S(   s`   Upper tail area of the chi**2 dist'n with nu dof.
	Note that chisq is *not* the reduced chis**2!g      ?(   R+   (   R?   R@   RA   RB   (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   qchisq   s    

gh㈵>c         C   s   |  t  |  t d |   } t | |   } x] t d | |   | d | } | | | | } t | |   } t | |  | k r0 | Sq0 d S(   s5   Critical chi**2 with lower tail area of p for nu dof.g       @gjt?gMbP?N(   R7   R,   RC   R   (   R@   R-   t   tolt   chit   pcurt   dfdc(    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt
   chisq_crit   s    t   __main__s   gser: g      @s   gcf:  g      @s   gammp, gammq: s   erfcc: g      ?s
   ncdf_inv: g$C?s   ncdf: g      ?s
   ncdf_sig: s   q & p chisq: g      @i   s   chisq_crit: gI+?N(   t
   __future__R    t   mathR   R,   R;   R   R   R   R   R   R   R'   R)   R+   R7   R:   R<   R>   RC   RD   RI   t   __name__(    (    (    sF   /homepages/25/d321765456/htdocs/pub_http_internet/python/sp_funcs_m.pyt   <module>!   s:   
				
					
		