ó
áp7]c        	   @  s:  d  Z  d d l m Z d d l m Z m Z d d l Z d „  Z d d „ Z	 d „  Z
 d „  Z e d	 k r6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 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 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 d d d d g g ƒ Z e
 e j d d d ƒ e j d d d ƒ e ƒ Z e e e d ƒ n  d S(   s?   Quantizing a continuous distribution in 2d

Author: josef-pktd
iÿÿÿÿ(   t   print_function(   t   ranget   lmapNc         C  sV   | | Œ  } | | d |  d ƒ } | |  d | d ƒ } | |  Œ  } | | | | S(   s‰  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 ?
    i    i   (    (   t   lowert   uppert   cdft   probuut   probult   problut   probll(    (    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyt   prob_bv_rectangle	   s
    c   	      C  s  t  |  t j ƒ s¬ t t j |  ƒ }  t |  ƒ } g  } t t t j |  ƒ t j | ƒ k ƒ r¿ xI t	 | ƒ D]8 } d g | } t d ƒ | | <| j |  | | ƒ qj Wq¿ n |  j d } |  } t t |  ƒ ƒ | | ƒ } | j ƒ  } x) t	 | ƒ D] } t j | d | ƒ} qô W| S(   sü   helper 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

    i    t   axisN(   t
   isinstancet   npt   ndarrayR   t   asarrayt   lent   allt   ndimt   onesR   t   Nonet   slicet   appendt   shapet   printt   copyt   diff(	   t   binsR   R   t   n_dimt   bins_t   dt   slt
   cdf_valuest   probs(    (    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyt   prob_mv_grid   s"    
'c           s(  t  j |  ƒ }  t  j | ƒ } t |  ƒ d } t | ƒ d } t  j t  j | | f ƒ } | |  d d … d f | ƒ ‰  ‡  f d †  } x} t d | d ƒ D]h } x_ t d | d ƒ D]J } | | f }	 | d | d f }
 t |
 |	 | ƒ | | d | d f <q¶ Wqœ Wt  j | ƒ j	 ƒ  s$t
 ‚ | S(   s‚   quantize a continuous distribution given by a cdf

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

    i   Nc           s   ˆ  |  | f S(   N(    (   t   xt   y(   R    (    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyt   <lambda>N   t    (   R   R   R   t   nanR   R   R   R
   t   isnant   anyt   AssertionError(   t   binsxt   binsyR   t   nxt   nyR!   t   cdf_funct   xindt   yindR   R   (    (   R    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyt   prob_quantize_cdf?   s    	,c   
      C  s
  t  j |  ƒ }  t  j | ƒ } t |  ƒ d } t | ƒ d } t  j t  j | | f ƒ } x t d | d ƒ D]x } xo t d | d ƒ D]Z } |  | | | f } |  | d | | d f }	 t |	 | | ƒ | | d | d f <qˆ Wqn Wt  j | ƒ j ƒ  st	 ‚ | S(   s³   quantize a continuous distribution given by a cdf

    old version without precomputing cdf values

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

    i   (
   R   R   R   R'   R   R   R
   R(   R)   R*   (
   R+   R,   R   R-   R.   R!   R0   R1   R   R   (    (    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyt   prob_quantize_cdf_oldY   s    ,t   __main__(   t   assert_almost_equalc         C  s   |  | S(   N(    (   R#   R$   (    (    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyR%   x   R&   i    i   g      à?i   g      Ð?gš™™™™™©?i   i   i   (   t   __doc__t
   __future__R    t   statsmodels.compat.pythonR   R   t   numpyR   R
   R"   R2   R3   t   __name__t   numpy.testingR5   t   unif_2dt   arrayt   arr1bt   linspacet   arr1at   arr2bt   arr2at   arr3bt   arr3a(    (    (    sI   lib/python2.7/site-packages/statsmodels/sandbox/distributions/quantize.pyt   <module>   s8   	!			((000