
    !gU                     l    d 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 Zy)a  
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
    N)erfc                    |j                  |j                        }|2t        j                  t        j                  |      j                        }t        j
                  |j                        | z  |dz
  z  }||k(  }|d| z
  z  |   ||<   |S )a!  
    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.
       )reshapesizenpasarrayuniqueones)hXix
num_levelskernel_valueidxs         ^/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/nonparametric/kernels.pyaitchison_aitkenr      s    F 
BGG	BZZ		" 2 23
77277#a':>:L
'CA,L    c                     |j                  |j                        }dd| z
  z  | t        ||z
        z  z  }||k(  }|d| z
  z  |   ||<   |S )a  
    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.
          ?r   )r   r   abs)r   r   r   r   r   s        r   
wang_ryzinr   D   s\    B 
BGG	B!a%=AR!V$45L
'CA,Lr   c                     dt        j                  dt         j                  z        z  t        j                  ||z
  dz   | dz  dz  z        z  S )a  
    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       @r   sqrtpiexpr   r   r   s      r   gaussianr!   l   sE    " RUU##rvvQ{ladRi.H'IIIr   c                     ||z
  | z  }d|t        j                  |      dkD  <   ddt        j                  |      dz  z
  dz  z  S )a  
    Tricube 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.
    r   r   g?   )r   r   )r   r   r   us       r   tricuber%      sH    " 
a1AAbffQi!mRVVAY\)A---r   c                     dt        j                  dt         j                  z        z  t        j                  ||z
  dz   | dz  dz  z        z  S )z, Calculates the Gaussian Convolution Kernel r      r   g      @r   r    s      r   gaussian_convolutionr(      sC    RUU##rvva!mq!tby.I'JJJr   c                     t        j                  |j                        }t        j                  |      D ]   }|t	        | ||      t	        | ||      z  z  }" |S Nr   zerosr   r
   r   )r   r   Xjorderedr   s        r   wang_ryzin_convolutionr/      sU     hhrwwGYYr] ?:aQ'*QA*>>>? Nr   c           	          t        j                  |      }t        j                  |j                        }|j                  }|D ]$  }|t	        | |||      t	        | |||      z  z  }& |S N)r   )r   r
   r,   r   r   )r   r   r-   Xi_valsr.   r   r   s          r   aitchison_aitken_convolutionr3      so    iimGhhrwwGJ E#Ar1D#Ar1DE 	EE Nr   c           	      b    d| z  dt        ||z
  | t        j                  d      z  z        z   z  S )Nr   r   r   )r   r   r   r    s      r   gaussian_cdfr5      s0    7a#q2v!bggaj.9::;;r   c                     t        |      }t        j                  |      }t        j                  |j                        }|j                  }|D ]  }||k  s	|t        | |||      z  } |S r1   )intr   r
   r,   r   r   )r   r   x_ur2   r.   r   r   s          r   aitchison_aitken_cdfr9      sh    
c(CiimGhhrwwGJ I8'2qZHHGI Nr   c                     t        j                  |j                        }t        j                  |      D ]  }||k  s	|t	        | ||      z  } |S r*   r+   )r   r   r8   r.   r   s        r   wang_ryzin_cdfr;      sL    hhrwwGYYr] ,8z!R++G, Nr   c                 :    d||z
  z  t        | ||      z  | dz  z  S )Nr   )r!   r    s      r   
d_gaussianr=      s'    Q<(1b!,,q!t33r   c                 h    t        j                  |j                        }||k7  }|| z  }||   ||<   |S )zr
    A version for the Aitchison-Aitken kernel for nonparametric regression.

    Suggested by Li and Racine.
    )r   r   r   )r   r   r   r   ixinDoms         r   aitchison_aitken_regrA      s<     77277#L	qBFERyLr   c                 $    | t        ||z
        z  S )zw
    A version for the Wang-Ryzin kernel for nonparametric regression.

    Suggested by Li and Racine in [1] ch.4
    )r   r    s      r   wang_ryzin_regrC      s     BFr   r*   )__doc__numpyr   scipy.specialr   r   r   r!   r%   r(   r/   r3   r5   r9   r;   r=   rA   rC    r   r   <module>rH      sW     *Z%PJ(.,K
<	4

r   