U
    Ãmœd�  ã                
   @   s‚  d Z ddlmZ ddlZdd„ Zddd„Zd	d
„ Zdd„ Ze	dk�r~ddl
mZ dd„ Zeeddgddgeƒddƒ eeddgddgeƒddƒ e ddddgddddgddddgddddgddddgg¡Zee ddd¡e ddd¡eƒZeeedƒ e dgdgdgdgg¡Zee ddd¡e ddd¡eƒZeeedƒ e ddddgg¡Zee ddd¡e ddd¡eƒZeeedƒ dS )z?Quantizing a continuous distribution in 2d

Author: josef-pktd
é    )ÚlmapNc                 C   sD   ||Ž }||d | d ƒ}|| d |d ƒ}|| Ž }|| | | S )a‰  helper function for probability of a rectangle in a bivariate distribution

    Parameters
    ----------
    lower : array_like
        tuple of lower integration bounds
    upper : array_like
        tuple of upper integration bounds
    cdf : callable
        cdf(x,y), cumulative distribution function of bivariate distribution


    how does this generalize to more than 2 variates ?
    r   é   © )ÚlowerÚupperÚcdfZprobuuZprobulZprobluZprobllr   r   úc/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/distributions/quantize.pyÚprob_bv_rectangle   s
    r	   éÿÿÿÿc           	      C   sÀ   t | tjƒsvttj| ƒ} t| ƒ}g }tttj| ƒt |¡kƒr„t	|ƒD ],}dg| }t
dƒ||< | | | | ¡ qFn| jd }| }tt| ƒƒ ||ƒ}| ¡ }t	|ƒD ]}tj||d�}q¨|S )zühelper function for probability of a rectangle grid in a multivariate distribution

    how does this generalize to more than 2 variates ?

    bins : tuple
        tuple of bin edges, currently it is assumed that they broadcast
        correctly

    Nr   )Úaxis)Ú
isinstanceÚnpZndarrayr   ÚasarrayÚlenÚallÚndimÚonesÚrangeÚsliceÚappendÚshapeÚprintÚcopyÚdiff)	Zbinsr   r   Zn_dimZbins_ÚdÚslÚ
cdf_valuesÚprobsr   r   r   Úprob_mv_grid   s"    


r   c                    sÔ   t  | ¡} t  |¡}t| ƒd }t|ƒd }t jt  ||f¡ }|| dd…df |ƒ‰ ‡ fdd„}td|d ƒD ]L}td|d ƒD ]8}||f}	|d |d f}
t|
|	|ƒ||d |d f< q‚qpt  |¡ ¡ rÐt	‚|S )z‚quantize a continuous distribution given by a cdf

    Parameters
    ----------
    binsx : array_like, 1d
        binedges

    r   Nc                    s   ˆ | |f S ©Nr   ©ÚxÚy©r   r   r   Ú<lambda>M   ó    z#prob_quantize_cdf.<locals>.<lambda>©
r   r   r   Únanr   r   r	   ÚisnanÚanyÚAssertionError)ÚbinsxÚbinsyr   ÚnxÚnyr   Zcdf_funcÚxindÚyindr   r   r   r#   r   Úprob_quantize_cdf>   s    	

 r1   c           
      C   sÂ   t  | ¡} t  |¡}t| ƒd }t|ƒd }t jt  ||f¡ }td|d ƒD ]\}td|d ƒD ]H}| | || f}| |d  ||d  f}	t|	||ƒ||d |d f< q`qNt  |¡ ¡ r¾t	‚|S )z³quantize a continuous distribution given by a cdf

    old version without precomputing cdf values

    Parameters
    ----------
    binsx : array_like, 1d
        binedges

    r   r&   )
r+   r,   r   r-   r.   r   r/   r0   r   r   r   r   r   Úprob_quantize_cdf_oldX   s    

 r2   Ú__main__)Úassert_almost_equalc                 C   s   | | S r   r   r    r   r   r   r$   w   r%   r$   r   g      à?é   g      Ð?gš™™™™™©?é   é   é   )r
   )Ú__doc__Zstatsmodels.compat.pythonr   Únumpyr   r	   r   r1   r2   Ú__name__Znumpy.testingr4   Zunif_2dÚarrayZarr1bZlinspaceZarr1aZarr2bZarr2aZarr3bZarr3ar   r   r   r   Ú<module>   s:   
!




ü ý  