
    M/Ph                        d Z ddlmZ ddlZd ZddZd Zd Ze	d	k    r;dd
l
mZ 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j        g dg dg dg dg dg          Z e ej        ddd           ej        ddd          e          Z eeed            ej        dgdgdgdgg          Z e ej        ddd           ej        ddd          e          Z eeed            ej        g dg          Z e ej        ddd           ej        ddd          e          Z eeed           dS dS )z?Quantizing a continuous distribution in 2d

Author: josef-pktd
    )lmapNc                      || } ||d         | d                   } || d         |d                   } ||  }||z
  |z
  |z   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cdfprobuuprobulprobluproblls          j/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/distributions/quantize.pyprob_bv_rectangler      s`     S%[FSq58$$FSq58$$FS%[FF?V#f,,    c                    t          | t          j                  st          t          j        |           } t          |           }g }t          t          t          j        |           t          j        |          k              rKt          |          D ];}dg|z  }t          d          ||<   |                    | |         |                    <n| j        d         }| }t          t          |                       ||          }|                                }t          |          D ]}t          j        ||          }|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npndarrayr   asarraylenallndimonesrangesliceappendshapeprintcopydiff)	binsr	   r   n_dimbins_dsl
cdf_valuesprobss	            r   prob_mv_gridr*      s    dBJ'' BJ%%D		tBGT""bgenn455 	*5\\ * *VE\d1T!WR[))))
1	#d))UJOOE5\\ ' 'A&&&Lr   c                    t          j        |           } t          j        |          }t          |           dz
  }t          |          dz
  }t           j        t          j        ||f          z  } || dddf         |          fd}t          d|dz             D ]B}t          d|dz             D ],}||f}	|dz
  |dz
  f}
t          |
|	|          ||dz
  |dz
  f<   -Ct          j        |                                          rJ |S )zquantize a continuous distribution given by a cdf

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

    r   Nc                     | |f         S Nr   )xyr(   s     r   <lambda>z#prob_quantize_cdf.<locals>.<lambda>M   s    JqsO r   	r   r   r   nanr   r   r   isnanany)binsxbinsyr	   nxnyr)   cdf_funcxindyindr   r   r(   s              @r   prob_quantize_cdfr<   >   s-    JuEJuE	UaB	UaBFRWb"X&&&EU111T6]E**J++++HaA M M!RTNN 	M 	MD4LE!VT!V$E#4UE8#L#LE$q&a-  		M x""$$$$$Lr   c                    t          j        |           } t          j        |          }t          |           dz
  }t          |          dz
  }t           j        t          j        ||f          z  }t          d|dz             D ]Z}t          d|dz             D ]D}| |         ||         f}| |dz
           ||dz
           f}	t          |	||          ||dz
  |dz
  f<   E[t          j        |                                          rJ |S )zquantize a continuous distribution given by a cdf

    old version without precomputing cdf values

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

    r   r1   )
r5   r6   r	   r7   r8   r)   r:   r;   r   r   s
             r   prob_quantize_cdf_oldr>   X   s    JuEJuE	UaB	UaBFRWb"X&&&EaA H H!RTNN 	H 	HD4[%+.E46]E$q&M2E#4UE3#G#GE$q&a-  		H x""$$$$$Lr   __main__)assert_almost_equalc                     | |z  S r-   r   )r.   r/   s     r   r0   r0   w   s
    !A# r   r   g      ?         ?)皙?rD   rD   rD            )rC   rC   rC   rC   )r   )__doc__statsmodels.compat.pythonr   numpyr   r   r*   r<   r>   __name__numpy.testingr@   unif_2darrayarr1blinspacearr1aarr2barr2aarr3barr3ar   r   r   <module>rV      si    + * * * * *    - - -*   B  4  : z111111oG))1Q%!C'BBCLLL))1Q%#cGDDdBOOOBH222333333333333	5 6 6E kbk!Aa00+"+a!2D2DgNNEub)))BHgggg    E kbk!Aa00+"+a!2D2DgNNEub)))BH222344Ekbk!Aa00+"+a!2D2DgNNEub)))))1 r   