
    hMh                         d Z ddlZddlmZ ddlmZ 	 ddeded	ed
ededefdZ	deded	edefdZ
deded	edeeef         fdZdS )ae  Project: PhiK - correlation analyzer library

Created: 2018/09/05

Description:
    Implementation of incomplete beta function

Authors:
    KPMG Advanced Analytics & Big Data team, Amstelveen, The Netherlands

Redistribution and use in source and binary forms, with or without
modification, are permitted according to the terms listed in the file
LICENSE.
    N)gammaln)Tuple  Hz>abxITMAXEPSreturnc                     d}d}d}| |z   }| dz   }	| dz
  }
d||z  |	z  z
  }t          |dz             D ]}t          |dz             }||z   }|||z
  z  |z  |
|z   | |z   z  z  }|||z  z   }|||z  z   }| |z    ||z   z  |z  |	|z   | |z   z  z  }|||z  z   }|||z  z   }|}||z  }||z  }||z  }d}t          ||z
            |t          |          z  k     r|c S t          d                    | ||                    )a  Continued fraction form of the incomplete Beta function.

    Code translated from: Numerical Recipes in C.

    Example kindly taken from blog:
    https://malishoaib.wordpress.com/2014/04/15/the-beautiful-beta-functions-in-raw-python/

    :param float a: a
    :param float b: b
    :param float x: x
    :param int ITMAX: max number of iterations, default is 5000.
    :param float EPS: epsilon precision parameter, default is 1e-7.
    :returns: continued fraction form
    :rtype: float
    g      ?   z[a={0:f} or b={1:f} too large, or ITMAX={2:d} too small to compute incomplete beta function.)rangefloatabs
ValueErrorformat)r   r   r	   r
   r   azbmamqabqapqambziemtemdapbpappbppaolds                        L/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/phik/betainc.pycontfractbetar%      sv   $ 
B	B	B
a%C
c'C
c'C	sQw}	B519  1q5\\2g!b&MA#)C!89!b&[!b&["fIr"Q&39S*AB1r6k1r6k#X#X3YrDy>>C#b''M))III * ellq%	
 	
      c                    |dk    rdS |dk    rdS t          | |z             t          |           z
  t          |          z
  | t          j        |          z  z   |t          j        d|z
            z  z   }|| dz   | |z   dz   z  k     r*t          j        |          t	          | ||          z  | z  }n/dt          j        |          t	          || d|z
            z  |z  z
  }|S )a  Evaluation of incomplete beta function.

    Code translated from: Numerical Recipes in C.

    Here a, b > 0 and 0 <= x <= 1.
    This function requires contfractbeta(a,b,x, ITMAX = 200)

    Example kindly taken from blog:
    https://malishoaib.wordpress.com/2014/04/15/the-beautiful-beta-functions-in-raw-python/

    :param float a: a
    :param float b: b
    :param float x: x
    :returns: incomplete beta function
    :rtype: float
    r   r      )r   nplogexpr%   )r   r   r	   lbetaps        r$   
incompbetar.   F   s    $ 	Avvq	
aqAENNWQZZ''!**4q26!99}Dq26RSVWRW==GXXEAEa!eai   F5MMM!Q222Q6uaAE : ::Q>>Hr&   c           	      F   |dk    rt           j         dfS |dk    rdt           j         fS t          | |z             t          |           z
  t          |          z
  | t          j        |          z  z   |t          j        d|z
            z  z   }|| dz   | |z   dz   z  k     r|t          j        |          t          | ||          z  | z  }|t          j        t          | ||                    z   t          j        |           z
  }t          j        d|z
            }ndt          j        |          t          || d|z
            |z  z  z
  }t          j        |          }|t          j        t          || d|z
                      z   t          j        |          z
  }||fS )aW  Evaluation of logarithm of incomplete beta function

    Logarithm of incomplete beta function is implemented to ensure sufficient precision
    for values very close to zero and one.

    Code translated from: Numerical Recipes in C.

    Here a, b > 0 and 0 <= x <= 1.
    This function requires contfractbeta(a,b,x, ITMAX = 200)

    Example kindly taken from blog:
    https://malishoaib.wordpress.com/2014/04/15/the-beautiful-beta-functions-in-raw-python/

    :param float a: a
    :param float b: b
    :param float x: x
    :returns: tuple of log(incb) and log(1-incb)
    :rtype: tuple
    r   r   r(   )r)   infr   r*   r+   r%   )r   r   r	   r,   r-   logplogqs          r$   log_incompbetar3   e   st   * 	Avvwz	
a26'zAENNWQZZ''!**4q26!99}Dq26RSVWRW==GXXEAEa!eai   F5MMM!Q222Q6rvmAq!44555q		Ava!e}}uq!QU!;!;a!?@@vayyrvmAq!a%88999BF1IIE:r&   )r   r   )__doc__numpyr)   scipy.specialr   typingr   r   intr%   r.   r3    r&   r$   <module>r:      s        ! ! ! ! ! !       CI/ /// /),/:?/
/ / / /d% E e     >$e $ $% $E%,4G $ $ $ $ $ $r&   