Ë
    £�Dj7  ã                   ó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         úeC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/nonparametric/kernels.pyÚaitchison_aitkenr      s   € ðF 
�‰�B—G‘GÓ	€BØÐÜ—Z‘Z¤§	¡	¨"£× 2Ñ 2Ó3ˆ
ä—7‘7˜2Ÿ7™7Ó# 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 
�‰�B—G‘GÓ	€BØ˜!˜a™%‘= A¬¨R°!©V«Ñ$4Ñ5€LØ
�‰'€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   € ð" ”—‘˜œRŸU™U™Ó#Ñ#¤r§v¡v°°Q±¸©{¨l¸aÀ¹dÀR¹iÑ.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Ø€A„b‡f�fˆQƒi�!�mÑØ˜œRŸV™V A›Y¨™\Ñ)¨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   € à”—‘˜œRŸU™U™Ó#Ñ#¤r§v¡v°°a±¸!±¨m¸qÀ!¹tÀb¹yÑ.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   € ô �h‰h�r—w‘wÓ€GÜ�Y‰Y�r‹]ò ?ˆØ”:˜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   € Ü�i‰i˜‹m€GÜ�h‰h�r—w‘wÓ€GØ—‘€JØò EˆØÔ# A r¨1¸ÔDÜ# A r¨1¸Ô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   € Ø�‰7�aœ#˜q 2™v¨!¬b¯g©g°a«j©.Ñ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Ü�i‰i˜‹m€GÜ�h‰h�r—w‘wÓ€GØ—‘€JØò IˆØ�‹8ØÔ'¨¨2¨q¸ZÔHÑH‰Gð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   € Ü�h‰h�r—w‘wÓ€GÜ�Y‰Y�r‹]ò ,ˆØ�‹8Ø”z ! R¨Ó+Ñ+‰Gð,ð €Nr   c                 ó:   — d||z
  z  t        | ||«      z  | dz  z  S )Nr   )r!   r    s      r   Ú
d_gaussianr=   Ê   s'   € à��Q‘‰<œ( 1 b¨!Ó,Ñ,¨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<   € ô —7‘7˜2Ÿ7™7Ó#€LØ	ˆq‰€BØ�‰F€EØ˜R‘y€L�ÑØÐ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   