o
    0Gfg                     @   sL  d Z ddlZddlmZmZ dZdRddZdRdd	Zd
d Z	dj
ede	_ dd Zdj
ede_ dd Zdj
ede_ dd Zdj
ede_ dd Zdj
ede_ dd Zdj
ede_ dd Zdd  Zd!d" Zd#j
ede_ d$d% Zd&j
ede_ d'd( Zd)j
ede_ d*d+ Zd,j
ede_ d-d. Zd/j
ede_ d0d1 Zd2d3 Zd4j
ede_ d5d6 Zd7j
ede_ d8d9 Zd:d; Zd<j
ede_ d=d> Zd?j
ede_ d@dA ZdBj
ede_ dCdD ZdEj
ede_ dFdG ZdHj
ede_ dIdJ Z dKdL Z!dMj
ede!_ dNdO Z"dPj
ede"_ eeeeeeeeee"dQ
Z#e	eeeeeeeee!dQ
Z$dS )SuD  Asymmetric kernels for R+ and unit interval

References
----------

.. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
   Asymmetric Kernel Density Estimators and Smoothed Histograms with
   Application to Income Data.” Econometric Theory 21 (2): 390–412.

.. [2] Chen, Song Xi. 1999. “Beta Kernel Estimators for Density Functions.”
   Computational Statistics & Data Analysis 31 (2): 131–45.
   https://doi.org/10.1016/S0167-9473(99)00010-9.

.. [3] Chen, Song Xi. 2000. “Probability Density Function Estimation Using
   Gamma Kernels.”
   Annals of the Institute of Statistical Mathematics 52 (3): 471–80.
   https://doi.org/10.1023/A:1004165218295.

.. [4] Jin, Xiaodong, and Janusz Kawczak. 2003. “Birnbaum-Saunders and
   Lognormal Kernel Estimators for Modelling Durations in High Frequency
   Financial Data.” Annals of Economics and Finance 4: 103–24.

.. [5] Micheaux, Pierre Lafaye de, and Frédéric Ouimet. 2020. “A Study of Seven
   Asymmetric Kernels for the Estimation of Cumulative Distribution Functions,”
   November. https://arxiv.org/abs/2011.14893v1.

.. [6] Mombeni, Habib Allah, B Masouri, and Mohammad Reza Akhoond. 2019.
   “Asymmetric Kernels for Boundary Modification in Distribution Function
   Estimation.” REVSTAT, 1–27.

.. [7] Scaillet, O. 2004. “Density Estimation Using Inverse and Reciprocal
   Inverse Gaussian Kernels.”
   Journal of Nonparametric Statistics 16 (1–2): 217–26.
   https://doi.org/10.1080/10485250310001624819.


Created on Mon Mar  8 11:12:24 2021

Author: Josef Perktold
License: BSD-3

    N)specialstatsa[  Parameters
    ----------
    x : array_like, float
        Points for which density is evaluated. ``x`` can be scalar or 1-dim.
    sample : ndarray, 1-d
        Sample from which kde is computed.
    bw : float
        Bandwidth parameter, there is currently no default value for it.

    Returns
    -------
    Components for kernel estimation
   c                       t |r|nt| |d }t| t |k rCt| dkr,t| dddf } |  }du r=|d}|S | }|S du rRttt |t }t| | }	t| |	}
t	 fdd|
D }|S )ac  Density estimate based on asymmetric kernel.

    Parameters
    ----------
    x : array_like, float
        Points for which density is evaluated. ``x`` can be scalar or 1-dim.
    sample : ndarray, 1-d
        Sample from which kernel estimate is computed.
    bw : float
        Bandwidth parameter, there is currently no default value for it.
    kernel_type : str or callable
        Kernel name or kernel function.
        Currently supported kernel names are "beta", "beta2", "gamma",
        "gamma2", "bs", "invgamma", "invgauss", "lognorm", "recipinvgauss" and
        "weibull".
    weights : None or ndarray
        If weights is not None, then kernel for sample points are weighted
        by it. No weights corresponds to uniform weighting of each component
        with 1 / nobs, where nobs is the size of `sample`.
    batch_size : float
        If x is an 1-dim array, then points can be evaluated in vectorized
        form. To limit the amount of memory, a loop can work in batches.
        The number of batches is determined so that the intermediate array
        sizes are limited by

        ``np.size(batch) * len(sample) < batch_size * 1000``.

        Default is to have at most 10000 elements in intermediate arrays.

    Returns
    -------
    pdf : float or ndarray
        Estimate of pdf at points x. ``pdf`` has the same size or shape as x.
         Nc                    (   g | ]}|d d d f   qS N .0ZxibwZkfuncsampleweightsr   Llib/python3.10/site-packages/statsmodels/nonparametric/kernels_asymmetric.py
<listcomp>{        z#pdf_kernel_asym.<locals>.<listcomp>)
callablekernel_dict_pdfnpsizelenasarraymeanonesarray_splitconcatenate)xr   r   kernel_typer   
batch_sizepdfipdfknx_splitr   r   r   pdf_kernel_asym>   ,   $
r'   c                    r   )av  Estimate of cumulative distribution based on asymmetric kernel.

    Parameters
    ----------
    x : array_like, float
        Points for which density is evaluated. ``x`` can be scalar or 1-dim.
    sample : ndarray, 1-d
        Sample from which kernel estimate is computed.
    bw : float
        Bandwidth parameter, there is currently no default value for it.
    kernel_type : str or callable
        Kernel name or kernel function.
        Currently supported kernel names are "beta", "beta2", "gamma",
        "gamma2", "bs", "invgamma", "invgauss", "lognorm", "recipinvgauss" and
        "weibull".
    weights : None or ndarray
        If weights is not None, then kernel for sample points are weighted
        by it. No weights corresponds to uniform weighting of each component
        with 1 / nobs, where nobs is the size of `sample`.
    batch_size : float
        If x is an 1-dim array, then points can be evaluated in vectorized
        form. To limit the amount of memory, a loop can work in batches.
        The number of batches is determined so that the intermediate array
        sizes are limited by

        ``np.size(batch) * len(sample) < batch_size * 1000``.

        Default is to have at most 10000 elements in intermediate arrays.

    Returns
    -------
    cdf : float or ndarray
        Estimate of cdf at points x. ``cdf`` has the same size or shape as x.
    r   r   Nr   c                    r	   r
   r   r   r   r   r   r      r   z#cdf_kernel_asym.<locals>.<listcomp>)
r   kernel_dict_cdfr   r   r   r   r   r   r   r   )r   r   r   r    r   r!   cdfiZcdfr$   r%   r&   r   r   r   cdf_kernel_asym   r(   r+   c                 C   $   t j|| | d d|  | d S Nr   )r   betar#   r   r   r   r   r   r   kernel_pdf_beta      $r0   u      Beta kernel for density, pdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 1999. “Beta Kernel Estimators for Density Functions.”
       Computational Statistics & Data Analysis 31 (2): 131–45.
       https://doi.org/10.1016/S0167-9473(99)00010-9.
    )
doc_paramsc                 C   r,   r-   )r   r.   sfr/   r   r   r   kernel_cdf_beta   r1   r4   u#      Beta kernel for cumulative distribution, cdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 1999. “Beta Kernel Estimators for Density Functions.”
       Computational Statistics & Data Analysis 31 (2): 131–45.
       https://doi.org/10.1016/S0167-9473(99)00010-9.
    c                 C     d|d  d }d|d  d|d   d }t | dkrw| d| k r@|t || d  | |   }tj||d|  | }|S | dd|  krgd|  }|t ||d  ||   }tj|| | |}|S tj|| | d|  | }|S | | }d|  | }	| d| k }
| |
 }|t ||d  ||   ||
< | dd|  k}d| |  }|t ||d  ||   |	|< tj|||	}|S N   g      @      g      @r   )r   r   sqrtr   r.   r#   r   r   r   Za1Za2ar#   Zx_Zalphar.   Zmask_lowZmask_uppr   r   r   kernel_pdf_beta2   s0   ""r=   u-      Beta kernel for density, pdf, estimation with boundary corrections.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 1999. “Beta Kernel Estimators for Density Functions.”
       Computational Statistics & Data Analysis 31 (2): 131–45.
       https://doi.org/10.1016/S0167-9473(99)00010-9.
    c                 C   r5   r6   )r   r   r:   r   r.   r3   r;   r   r   r   kernel_cdf_beta2&  s0   ""r>   u"      Beta kernel for cdf estimation with boundary correction.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 1999. “Beta Kernel Estimators for Density Functions.”
       Computational Statistics & Data Analysis 31 (2): 131–45.
       https://doi.org/10.1016/S0167-9473(99)00010-9.
    c                 C      t jj|| | d |d}|S Nr   Zscaler   gammar#   )r   r   r   r"   r   r   r   kernel_pdf_gamma\  s   rD   u-      Gamma kernel for density, pdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 2000. “Probability Density Function Estimation Using
       Gamma Krnels.”
       Annals of the Institute of Statistical Mathematics 52 (3): 471–80.
       https://doi.org/10.1023/A:1004165218295.
    c                 C   r?   r@   r   rC   r3   )r   r   r   r*   r   r   r   kernel_cdf_gammat  s   rF   u=      Gamma kernel for cumulative distribution, cdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 2000. “Probability Density Function Estimation Using
       Gamma Krnels.”
       Annals of the Institute of Statistical Mathematics 52 (3): 471–80.
       https://doi.org/10.1023/A:1004165218295.
    c                 C      t jj|| | |dS )zGamma kernel for pdf, without boundary corrected part.

    drops `+ 1` in shape parameter

    It should be possible to use this if probability in
    neighborhood of zero boundary is small.

    rA   rB   r/   r   r   r   _kernel_pdf_gamma     	rH   c                 C   rG   )zGamma kernel for cdf, without boundary corrected part.

    drops `+ 1` in shape parameter

    It should be possible to use this if probability in
    neighborhood of zero boundary is small.

    rA   rE   r/   r   r   r   _kernel_cdf_gamma  rI   rJ   c                 C   t   t | dkr| d| k r| | d d }n| | }n| | }| d| k }|| d d ||< tjj|||d}|S Nr   r7   rA   )r   r   r   rC   r#   r   r   r   r<   maskr#   r   r   r   kernel_pdf_gamma2     
rO   uF      Gamma kernel for density, pdf, estimation with boundary correction.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 2000. “Probability Density Function Estimation Using
       Gamma Krnels.”
       Annals of the Institute of Statistical Mathematics 52 (3): 471–80.
       https://doi.org/10.1023/A:1004165218295.
    c                 C   rK   rL   )r   r   r   rC   r3   rM   r   r   r   kernel_cdf_gamma2  rP   rQ   u<      Gamma kernel for cdf estimation with boundary correction.

    {doc_params}

    References
    ----------
    .. [1] Bouezmarni, Taoufik, and Olivier Scaillet. 2005. “Consistency of
       Asymmetric Kernel Density Estimators and Smoothed Histograms with
       Application to Income Data.” Econometric Theory 21 (2): 390–412.

    .. [2] Chen, Song Xi. 2000. “Probability Density Function Estimation Using
       Gamma Krnels.”
       Annals of the Institute of Statistical Mathematics 52 (3): 471–80.
       https://doi.org/10.1023/A:1004165218295.
    c                 C      t jj|d| d | | dS r@   )r   invgammar#   r/   r   r   r   kernel_pdf_invgamma     rT   u      Inverse gamma kernel for density, pdf, estimation.

    Based on cdf kernel by Micheaux and Ouimet (2020)

    {doc_params}

    References
    ----------
    .. [1] Micheaux, Pierre Lafaye de, and Frédéric Ouimet. 2020. “A Study of
       Seven Asymmetric Kernels for the Estimation of Cumulative Distribution
       Functions,” November. https://arxiv.org/abs/2011.14893v1.
    c                 C   rR   r@   )r   rS   r3   r/   r   r   r   kernel_cdf_invgamma  rU   rV   u_      Inverse gamma kernel for cumulative distribution, cdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Micheaux, Pierre Lafaye de, and Frédéric Ouimet. 2020. “A Study of
       Seven Asymmetric Kernels for the Estimation of Cumulative Distribution
       Functions,” November. https://arxiv.org/abs/2011.14893v1.
    c                 C   "   | }d| }t jj||| |dS r@   )r   invgaussr#   r   r   r   mZlamr   r   r   kernel_pdf_invgauss     r[   uZ      Inverse gaussian kernel for density, pdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Scaillet, O. 2004. “Density Estimation Using Inverse and Reciprocal
       Inverse Gaussian Kernels.”
       Journal of Nonparametric Statistics 16 (1–2): 217–26.
       https://doi.org/10.1080/10485250310001624819.
    c                 C   sT   dt dt j | |d   t dd| |   ||  d | |    }|dS )zJInverse gaussian kernel density, explicit formula.

    Scaillet 2004
    r   r7      r   )r   r:   piexpr   r   r   r   r#   r   r   r   kernel_pdf_invgauss_&  s   (
ra   c                 C   rW   r@   )r   rX   r3   rY   r   r   r   kernel_cdf_invgauss0  r\   rb   uj      Inverse gaussian kernel for cumulative distribution, cdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Scaillet, O. 2004. “Density Estimation Using Inverse and Reciprocal
       Inverse Gaussian Kernels.”
       Journal of Nonparametric Statistics 16 (1–2): 217–26.
       https://doi.org/10.1080/10485250310001624819.
    c                 C   .   d| |  }d| }t jj||| d| dS r@   )r   recipinvgaussr#   rY   r   r   r   kernel_pdf_recipinvgaussE     re   ue      Reciprocal inverse gaussian kernel for density, pdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Scaillet, O. 2004. “Density Estimation Using Inverse and Reciprocal
       Inverse Gaussian Kernels.”
       Journal of Nonparametric Statistics 16 (1–2): 217–26.
       https://doi.org/10.1080/10485250310001624819.
    c                 C   sT   dt dt j | |  t | |  d|  | | |  d | | |   }|S )zUReciprocal inverse gaussian kernel density, explicit formula.

    Scaillet 2004
    r   r7   )r   r:   r^   r_   r`   r   r   r   kernel_pdf_recipinvgauss_]  s   $
rg   c                 C   rc   r@   )r   rd   r3   rY   r   r   r   kernel_cdf_recipinvgaussi  rf   rh   u[      Reciprocal inverse gaussian kernel for cdf estimation.

    {doc_params}

    References
    ----------
    .. [1] Scaillet, O. 2004. “Density Estimation Using Inverse and Reciprocal
       Inverse Gaussian Kernels.”
       Journal of Nonparametric Statistics 16 (1–2): 217–26.
       https://doi.org/10.1080/10485250310001624819.
    c                 C      t jj||| dS NrA   )r   fatiguelifer#   r/   r   r   r   kernel_pdf_bs     rl   uZ      Birnbaum Saunders (normal) kernel for density, pdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Jin, Xiaodong, and Janusz Kawczak. 2003. “Birnbaum-Saunders and
       Lognormal Kernel Estimators for Modelling Durations in High Frequency
       Financial Data.” Annals of Economics and Finance 4: 103–24.
    c                 C   ri   rj   )r   rk   r3   r/   r   r   r   kernel_cdf_bs  rm   rn   u      Birnbaum Saunders (normal) kernel for cdf estimation.

    {doc_params}

    References
    ----------
    .. [1] Jin, Xiaodong, and Janusz Kawczak. 2003. “Birnbaum-Saunders and
       Lognormal Kernel Estimators for Modelling Durations in High Frequency
       Financial Data.” Annals of Economics and Finance 4: 103–24.
    .. [2] Mombeni, Habib Allah, B Masouri, and Mohammad Reza Akhoond. 2019.
       “Asymmetric Kernels for Boundary Modification in Distribution Function
       Estimation.” REVSTAT, 1–27.
    c                 C   *   t dt d|  }tjj||| dS Nr8   r   rA   )r   r:   logr   lognormr#   r   r   r   Zbw_r   r   r   kernel_pdf_lognorm     	rt   u      Log-normal kernel for density, pdf, estimation.

    {doc_params}

    Notes
    -----
    Warning: parameterization of bandwidth will likely be changed

    References
    ----------
    .. [1] Jin, Xiaodong, and Janusz Kawczak. 2003. “Birnbaum-Saunders and
       Lognormal Kernel Estimators for Modelling Durations in High Frequency
       Financial Data.” Annals of Economics and Finance 4: 103–24.
    c                 C   ro   rp   )r   r:   rq   r   rr   r3   rs   r   r   r   kernel_cdf_lognorm  ru   rv   u      Log-normal kernel for cumulative distribution, cdf, estimation.

    {doc_params}

    Notes
    -----
    Warning: parameterization of bandwidth will likely be changed

    References
    ----------
    .. [1] Jin, Xiaodong, and Janusz Kawczak. 2003. “Birnbaum-Saunders and
       Lognormal Kernel Estimators for Modelling Durations in High Frequency
       Financial Data.” Annals of Economics and Finance 4: 103–24.
    c                 C   sX   dt d|  }dt |t j  | t t | t | d  |  }|dS )z]Log-normal kernel for density, pdf, estimation, explicit formula.

    Jin, Kawczak 2003
       r   r7   r   )r   rq   r:   r^   r_   r   )r   r   r   Ztermr#   r   r   r   kernel_pdf_lognorm_  s
   "
rx   c                 C   $   t jj|d| | td|  dS r@   )r   weibull_minr#   r   rC   r/   r   r   r   kernel_pdf_weibull     r{   u\      Weibull kernel for density, pdf, estimation.

    Based on cdf kernel by Mombeni et al. (2019)

    {doc_params}

    References
    ----------
    .. [1] Mombeni, Habib Allah, B Masouri, and Mohammad Reza Akhoond. 2019.
       “Asymmetric Kernels for Boundary Modification in Distribution Function
       Estimation.” REVSTAT, 1–27.
    c                 C   ry   r@   )r   rz   r3   r   rC   r/   r   r   r   kernel_cdf_weibull  r|   r}   u:      Weibull kernel for cumulative distribution, cdf, estimation.

    {doc_params}

    References
    ----------
    .. [1] Mombeni, Habib Allah, B Masouri, and Mohammad Reza Akhoond. 2019.
       “Asymmetric Kernels for Boundary Modification in Distribution Function
       Estimation.” REVSTAT, 1–27.
    )
r.   Zbeta2bsrC   Zgamma2rS   rX   rr   rd   Zweibull)Nr   )%__doc__Znumpyr   Zscipyr   r   r2   r'   r+   r0   formatr4   r=   r>   rD   rF   rH   rJ   rO   rQ   rT   rV   r[   ra   rb   re   rg   rh   rl   rn   rt   rv   rx   r{   r}   r)   r   r   r   r   r   <module>   s    +

CC%%




		

