B
    ZB              
   @   s  d Z ddlmZ ddlmZmZ ddlZdd Zddd	Z	d
d Z
dd Zedkrd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
    )print_function)range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   Ilib/python3.7/site-packages/statsmodels/sandbox/distributions/quantize.pyprob_bv_rectangle	   s
    r   c       	      C   s   t | tjszttj| } t| }g }tttj| t|krxHt	|D ],}dg| }t
d||< || | |  qHW n| jd }| }tt|  ||}| }xt	|D ]}tj||d}qW |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onesr   sliceappendshapeprintcopyZdiff)	Zbinsr	   r   Zn_dimZbins_dZ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}xbtd|d D ]P}xJtd|d D ]8}||f}	|d |d f}
t|
|	|||d |d f< qW qrW t | 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>N   s    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    	

$r+   c       
      C   s   t | } t |}t| d }t|d }t jt ||f }xrtd|d D ]`}xZtd|d D ]H}| | || f}| |d  ||d  f}	t|	||||d |d f< qdW qPW t | 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"   r#   r$   )
r%   r&   r	   r'   r(   r   r)   r*   r   r   r   r   r
   prob_quantize_cdf_oldY   s    

$r,   __main__)assert_almost_equalc             C   s   | | S )Nr   )r   r   r   r   r
   r    x   s    r    r   g      ?   g      ?g?         )r   )__doc__Z
__future__r   Zstatsmodels.compat.pythonr   r   Znumpyr   r   r   r+   r,   __name__Znumpy.testingr.   Zunif_2dZarrayZarr1bZlinspaceZarr1aZarr2bZarr2aZarr3bZarr3ar   r   r   r
   <module>   s8   
!



 
  