U
    Kvfg                     @   sL  d Z ddlZddlmZmZ dZdRddZdSd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 )TuD  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                    s   t |r|nt| |d }t| t |k rt| dkrXt| dddf } |  }dkrx|d}q| }n`dkrt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                    s(   g | ] }|d d d f   qS N .0xibwZkfuncsampleweightsr	   P/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/nonparametric/kernels_asymmetric.py
<listcomp>|   s   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?   s(    $
r%   c                    s   t |r|nt| |d }t| t |k rt| dkrXt| dddf } |  }dkrx|d}q| }n`dkrttt |t }t| | }	t| |	}
t	 fdd|
D }|S )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                    s(   g | ] }|d d d f   qS r   r	   r
   r   r	   r   r      s   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   s(    $
r(   c                 C   s$   t j|| | d d|  | d S Nr   )r   betar!   r   r   r   r	   r	   r   kernel_pdf_beta   s    r,   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   s$   t j|| | d d|  | d S r)   )r   r*   sfr+   r	   r	   r   kernel_cdf_beta   s    r/   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   s  d|d  d }d|d  d|d   d }t | dkr| d| k r~|t || d  | |   }tj||d|  | }nh| dd|  krd|  }|t ||d  ||   }tj|| | |}ntj|| | d|  | }n| | }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_alphar*   Zmask_lowZmask_uppr	   r	   r   kernel_pdf_beta2   s*    ""r8   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   s  d|d  d }d|d  d|d   d }t | dkr| d| k r~|t || d  | |   }tj||d|  | }nh| dd|  krd|  }|t ||d  ||   }tj|| | |}ntj|| | d|  | }n| | }d|  | }	| d| k }
| |
 }|t ||d  ||   ||
< | dd|  k}d| |  }|t ||d  ||   |	|< tj|||	}|S r0   )r   r   r4   r   r*   r.   r5   r	   r	   r   kernel_cdf_beta2'  s*    ""r9   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   s   t jj|| | d |d}|S Nr   Zscaler   gammar!   )r   r   r   r    r	   r	   r   kernel_pdf_gamma]  s    r>   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   s   t jj|| | d |d}|S r:   r   r=   r.   )r   r   r   r'   r	   r	   r   kernel_cdf_gammau  s    r@   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   s   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.

    r;   r<   r+   r	   r	   r   _kernel_pdf_gamma  s    	rA   c                 C   s   t jj|| | |dS )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.

    r;   r?   r+   r	   r	   r   _kernel_cdf_gamma  s    	rB   c                 C   st   t | dkr6| d| k r,| | d d }q^| | }n(| | }| d| k }|| d d ||< tjj|||d}|S Nr   r1   r;   )r   r   r   r=   r!   r   r   r   r6   maskr!   r	   r	   r   kernel_pdf_gamma2  s    
rF   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   st   t | dkr6| d| k r,| | d d }q^| | }n(| | }| d| k }|| d d ||< tjj|||d}|S rC   )r   r   r   r=   r.   rD   r	   r	   r   kernel_cdf_gamma2  s    
rG   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   s   t jj|d| d | | dS r:   )r   invgammar!   r+   r	   r	   r   kernel_pdf_invgamma  s    rI   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   s   t jj|d| d | | dS r:   )r   rH   r.   r+   r	   r	   r   kernel_cdf_invgamma   s    rJ   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   s"   | }d| }t jj||| |dS r:   )r   invgaussr!   r   r   r   mZlamr	   r	   r   kernel_pdf_invgauss  s    rN   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   r1      r   )r   r4   piexpr   r   r   r   r!   r	   r	   r   kernel_pdf_invgauss_'  s    (rS   c                 C   s"   | }d| }t jj||| |dS r:   )r   rK   r.   rL   r	   r	   r   kernel_cdf_invgauss1  s    rT   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   s.   d| |  }d| }t jj||| d| dS r:   )r   recipinvgaussr!   rL   r	   r	   r   kernel_pdf_recipinvgaussF  s    rV   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   r1   )r   r4   rP   rQ   rR   r	   r	   r   kernel_pdf_recipinvgauss_^  s    $
rW   c                 C   s.   d| |  }d| }t jj||| d| dS r:   )r   rU   r.   rL   r	   r	   r   kernel_cdf_recipinvgaussj  s    rX   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   s   t jj||| dS Nr;   )r   fatiguelifer!   r+   r	   r	   r   kernel_pdf_bs  s    r[   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   s   t jj||| dS rY   )r   rZ   r.   r+   r	   r	   r   kernel_cdf_bs  s    r\   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   s*   t dt d|  }tjj||| dS Nr2   r   r;   )r   r4   logr   lognormr!   r   r   r   Zbw_r	   r	   r   kernel_pdf_lognorm  s    	ra   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   s*   t dt d|  }tjj||| dS r]   )r   r4   r^   r   r_   r.   r`   r	   r	   r   kernel_cdf_lognorm  s    	rb   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   r1   r   )r   r^   r4   rP   rQ   r   )r   r   r   Ztermr!   r	   r	   r   kernel_pdf_lognorm_  s
    "rd   c                 C   s$   t jj|d| | td|  dS r:   )r   weibull_minr!   r   r=   r+   r	   r	   r   kernel_pdf_weibull  s    rf   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   s$   t jj|d| | td|  dS r:   )r   re   r.   r   r=   r+   r	   r	   r   kernel_cdf_weibull  s    rg   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bsr=   Zgamma2rH   rK   r_   rU   Zweibull)Nr   )Nr   )%__doc__Znumpyr   Zscipyr   r   r-   r%   r(   r,   formatr/   r8   r9   r>   r@   rA   rB   rF   rG   rI   rJ   rN   rS   rT   rV   rW   rX   r[   r\   ra   rb   rd   rf   rg   r&   r   r	   r	   r	   r   <module>   s   +
C
C%%




		
