o
    0Gf                     @   sl  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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g dg dg dg dg d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g dgZeedddedddeZeeed dS 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   Jlib/python3.10/site-packages/statsmodels/sandbox/distributions/quantize.pyprob_bv_rectangle   s
   r	   c           	      C   s   t | tjs;ttj| } t| }g }tttj| t|kr:t	|D ]}dg| }t
d||< || | |  q#n| jd }| }tt|  ||}| }t	|D ]	}tj||d}qT|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copyZ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 ]&}td|d D ]}||f}	|d |d f}
t|
|	|||d |d f< qAq8t | rhJ |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   s    z#prob_quantize_cdf.<locals>.<lambda>	r   r   r   nanr   r   r	   Zisnanany)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 }td|d D ].}td|d D ]$}| | || f}| |d  ||d  f}	t|	||||d |d f< q0q't | r_J |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   prob_quantize_cdf_oldX   s   

r.   __main__)assert_almost_equalc                 C   s   | | S r   r   r   r   r   r   r#   w   s    r#   r   g      ?         ?)皙?r3   r3   r3            )r2   r2   r2   r2   )r
   )__doc__Zstatsmodels.compat.pythonr   Znumpyr   r	   r   r-   r.   __name__Znumpy.testingr0   Zunif_2dZarrayZarr1bZlinspaceZarr1aZarr2bZarr2aZarr3bZarr3ar   r   r   r   <module>   s>    
!
   