
    M/PhU                     n    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dS )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                  }|+t          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.
    N   )reshapesizenpasarrayuniqueones)hXix
num_levelskernel_valueidxs         a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/nonparametric/kernels.pyaitchison_aitkenr      s}    F 
BG		BZ	" 233
727##a':>:L
'CA,L    c                     |                     |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 
BG		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    " RU###rvQ{ladRi.H'I'IIIr   c                     ||z
  | z  }d|t          j        |          dk    <   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AbfQii!mRVAYY\)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    RU###rva!mq!tby.I'J'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/      sZ     hrwGYr]] ? ?: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      sz    immGhrwGJ E E#Ar1DDD#Ar1DDDE 	E Nr   c           	      h    d| z  dt          ||z
  | t          j        d          z  z            z   z  S )Nr   r   r   )r   r   r   r    s      r   gaussian_cdfr5      s3    7a#q2v!bgajj.9:::;;r   c                     t          |          }t          j        |          }t          j        |j                  }|j        }|D ]}||k    r|t          | |||          z  }|S r1   )intr   r
   r,   r   r   )r   r   x_ur2   r.   r   r   s          r   aitchison_aitken_cdfr9      sn    
c((CimmGhrwGJ I I88'2qZHHHHGNr   c                     t          j        |j                  }t          j        |          D ]}||k    r|t	          | ||          z  }|S r*   r+   )r   r   r8   r.   r   s        r   wang_ryzin_cdfr;      sO    hrwGYr]] , ,88z!R+++GNr   c                 B    d||z
  z  t          | ||          z  | dz  z  S )Nr   )r!   r    s      r   
d_gaussianr=      s*    Q<(1b!,,,q!t33r   c                 d    t          j        |j                  }||k    }|| 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;     727##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      s	             * * * *Z% % %PJ J J(. . .,K K K
    < < <	 	 	  4 4 4

 
 
    r   