ó
áp7]c           @   s¥   d  Z  d d l m Z d d l Z d d l m Z d d „ Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s°  
Module of kernels that are able to handle continuous as well as categorical
variables (both ordered and unordered).

This is a slight deviation from the current approach in
statsmodels.nonparametric.kernels where each kernel is a class object.

Having kernel functions rather than classes makes extension to a multivariate
kernel density estimation much easier.

NOTE: As it is, this module does not interact with the existing API
iÿÿÿÿ(   t   divisionN(   t   erfc         C   s€   | j  | j ƒ } | d k r< t j t j | ƒ j ƒ } n  t j | j ƒ |  | d } | | k } | d |  | | | <| S(   s  
    The Aitchison-Aitken kernel, used for unordered discrete random variables.

    Parameters
    ----------
    h : 1-D ndarray, shape (K,)
        The bandwidths used to estimate the value of the kernel function.
    Xi : 2-D ndarray of ints, shape (nobs, K)
        The value of the training set.
    x: 1-D ndarray, shape (K,)
        The value at which the kernel density is being estimated.
    num_levels: bool, optional
        Gives the user the option to specify the number of levels for the
        random variable.  If False, the number of levels is calculated from
        the data.

    Returns
    -------
    kernel_value : ndarray, shape (nobs, K)
        The value of the kernel function at each training point for each var.

    Notes
    -----
    See p.18 of [2]_ for details.  The value of the kernel L if :math:`X_{i}=x`
    is :math:`1-\lambda`, otherwise it is :math:`\frac{\lambda}{c-1}`.
    Here :math:`c` is the number of levels plus one of the RV.

    References
    ----------
    .. [*] J. Aitchison and C.G.G. Aitken, "Multivariate binary discrimination
           by the kernel method", Biometrika, vol. 63, pp. 413-420, 1976.
    .. [*] Racine, Jeff. "Nonparametric Econometrics: A Primer," Foundation
           and Trends in Econometrics: Vol 3: No 1, pp1-88., 2008.
    i   N(   t   reshapet   sizet   Nonet   npt   asarrayt   uniquet   ones(   t   ht   Xit   xt
   num_levelst   kernel_valuet   idx(    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   aitchison_aitken   s    #c         C   sX   | j  | j ƒ } d d |  |  t | | ƒ } | | k } | d |  | | | <| S(   s•  
    The Wang-Ryzin kernel, used for ordered discrete random variables.

    Parameters
    ----------
    h : scalar or 1-D ndarray, shape (K,)
        The bandwidths used to estimate the value of the kernel function.
    Xi : ndarray of ints, shape (nobs, K)
        The value of the training set.
    x : scalar or 1-D ndarray of shape (K,)
        The value at which the kernel density is being estimated.

    Returns
    -------
    kernel_value : ndarray, shape (nobs, K)
        The value of the kernel function at each training point for each var.

    Notes
    -----
    See p. 19 in [1]_ for details.  The value of the kernel L if
    :math:`X_{i}=x` is :math:`1-\lambda`, otherwise it is
    :math:`\frac{1-\lambda}{2}\lambda^{|X_{i}-x|}`, where :math:`\lambda` is
    the bandwidth.

    References
    ----------
    .. [*] Racine, Jeff. "Nonparametric Econometrics: A Primer," Foundation
           and Trends in Econometrics: Vol 3: No 1, pp1-88., 2008.
           http://dx.doi.org/10.1561/0800000009
    .. [*] M.-C. Wang and J. van Ryzin, "A class of smooth estimators for
           discrete distributions", Biometrika, vol. 68, pp. 301-309, 1981.
    g      à?i   (   R   R   t   abs(   R	   R
   R   R   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt
   wang_ryzinF   s
    ! c         C   s:   d t  j d t  j ƒ t  j | | d |  d d ƒ S(   sø  
    Gaussian Kernel for continuous variables
    Parameters
    ----------
    h : 1-D ndarray, shape (K,)
        The bandwidths used to estimate the value of the kernel function.
    Xi : 1-D ndarray, shape (K,)
        The value of the training set.
    x : 1-D ndarray, shape (K,)
        The value at which the kernel density is being estimated.

    Returns
    -------
    kernel_value : ndarray, shape (nobs, K)
        The value of the kernel function at each training point for each var.

    g      ð?i   g       @(   R   t   sqrtt   pit   exp(   R	   R
   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   gaussiann   s    c         C   s:   d t  j d t  j ƒ t  j | | d |  d d ƒ S(   s,    Calculates the Gaussian Convolution Kernel g      ð?i   i   g      @(   R   R   R   R   (   R	   R
   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   gaussian_convolutionƒ   s    c         C   sV   t  j | j ƒ } x= t  j | ƒ D], } | t |  | | ƒ t |  | | ƒ 7} q" W| S(   N(   R   t   zerosR   R   R   (   R	   R
   t   Xjt   orderedR   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   wang_ryzin_convolutionˆ   s    *c      	   C   sq   t  j | ƒ } t  j | j ƒ } | j } x@ | D]8 } | t |  | | d | ƒt |  | | d | ƒ7} q1 W| S(   NR   (   R   R   R   R   R   (   R	   R
   R   t   Xi_valsR   R   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   aitchison_aitken_convolution“   s    	c         C   s+   d |  d t  | | |  t j d ƒ ƒ S(   Ng      à?i   i   (   R   R   R   (   R	   R
   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   gaussian_cdfž   s    c         C   sv   t  | ƒ } t j | ƒ } t j | j ƒ } | j } x9 | D]1 } | | k r= | t |  | | d | ƒ7} q= q= W| S(   NR   (   t   intR   R   R   R   R   (   R	   R
   t   x_uR   R   R   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   aitchison_aitken_cdf¢   s    	#c         C   sU   t  j | j ƒ } x< t  j | ƒ D]+ } | | k r" | t |  | | ƒ 7} q" q" W| S(   N(   R   R   R   R   R   (   R	   R
   R   R   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   wang_ryzin_cdf®   s
    c         C   s$   d | | t  |  | | ƒ |  d S(   Ni   (   R   (   R	   R
   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt
   d_gaussian·   s    c         C   s:   t  j | j ƒ } | | k } | |  } | | | | <| S(   sr   
    A version for the Aitchison-Aitken kernel for nonparametric regression.

    Suggested by Li and Racine.
    (   R   R   R   (   R	   R
   R   R   t   ixt   inDom(    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   aitchison_aitken_reg¼   s
    
c         C   s   |  t  | | ƒ S(   sw   
    A version for the Wang-Ryzin kernel for nonparametric regression.

    Suggested by Li and Racine in [1] ch.4
    (   R   (   R	   R
   R   (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   wang_ryzin_regÉ   s    (   t   __doc__t
   __future__R    t   numpyR   t   scipy.specialR   R   R   R   R   R   R   R   R   R    R!   R"   R%   R&   (    (    (    s@   lib/python2.7/site-packages/statsmodels/nonparametric/kernels.pyt   <module>   s   -	(										