U
    md                  
   @   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rt	|D ],}dg| }t
d||< || | |  qFn| jd }| }tt|  ||}| }t	|D ]}tj||d}q|S )zhelper 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< qqpt | rt	|S )zquantize 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 | rt	|S )zquantize 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:   
!




   