
    !g                        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       yy)z?Quantizing a continuous distribution in 2d

Author: josef-pktd
    )lmapNc                 h     || } ||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          g/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/sandbox/distributions/quantize.pyprob_bv_rectangler      sR     %[Fq58$Fq58$F%[FF?V#f,,    c                 :   t        | t        j                        st        t        j                  |       } t        |       }g }t        t        t        j                  |       t        j                  |      k(        rMt        |      D ]-  }dg|z  }t        d      ||<   |j                  | |   |          / n| j                  d   }| }t        t        |               ||      }|j                         }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     dBJJ'BJJ%D	tBGGT"bggen4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 ]<  }t        d|dz         D ](  }||f}	|dz
  |dz
  f}
t        |
|	|      ||dz
  |dz
  f<   * > t        j                  |      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     JJuEJJuE	UaB	UaBFFRWWb"X&&EU1T6]E*J+HaA M!RTN 	MD4LE!VT!V$E#4UE8#LE$q&a- 		MM xx""$$$Lr   c                    t        j                  |       } t        j                  |      }t        |       dz
  }t        |      dz
  }t         j                  t        j                  ||f      z  }t        d|dz         D ]H  }t        d|dz         D ]4  }| |   ||   f}| |dz
     ||dz
     f}	t        |	||      ||dz
  |dz
  f<   6 J t        j                  |      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   r0   )
r4   r5   r	   r6   r7   r(   r9   r:   r   r   s
             r   prob_quantize_cdf_oldr=   X   s     JJuEJJuE	UaB	UaBFFRWWb"X&&EaA H!RTN 	HD4[%+.E46]E$q&M2E#4UE3#GE$q&a- 		HH xx""$$$Lr   __main__)assert_almost_equalc                     | |z  S r,   r   )r-   r.   s     r   r/   r/   w   s
    !A# r   r   g      ?         ?)皙?rC   rC   rC            )rB   rB   rB   rB   ))__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      s   + -*B4: z1G)1Q%!C'BCL)1Q%#cGDdBOBHH23333	5 6E kbkk!Aa0+"++a!2DgNEub)BHHgggg  E kbkk!Aa0+"++a!2DgNEub)BHH234Ekbkk!Aa0+"++a!2DgNEub)1 r   