
    Vfd(                         d Z ddlZddlmZ ddlmZ ddlmZ  G d de          Z	 G d d	e	          Z
 G d
 de	          ZdS )zb
Created on Fri Jan 29 19:19:45 2021

Author: Josef Perktold
Author: Pamphile Roy
License: BSD-3

    N)stats)multivariate_t)Copulac                   D    e Zd ZdZd ZddZddZddZdd	Zd
 Z	d Z
dS )EllipticalCopulaa3  Base class for elliptical copula

    This class requires subclassing and currently does not have generic
    methods based on an elliptical generator.

    Notes
    -----
    Elliptical copulas require that copula parameters are set when the
    instance is created. Those parameters currently cannot be provided in the
    call to methods. (This will most likely change in future versions.)
    If non-empty ``args`` are provided in methods, then a ValueError is raised.
    The ``args`` keyword is provided for a consistent interface across
    copulas.

    c                 8    |dk    r|d}t          |          |S )N zaMethods in elliptical copulas use copula parameters in attributes. `arg` in the method is ignored)
ValueError)selfargsmsgs      Klib/python3.11/site-packages/statsmodels/distributions/copula/elliptical.py_handle_argszEllipticalCopula._handle_args"   s)    2::$*ACS//!K       r	   Nc                     |                      |           | j                            ||          }| j                            |          S )N)sizerandom_state)r   distr_mvrvsdistr_uvcdf)r   nobsr   r   xs        r   r   zEllipticalCopula.rvs*   sF    $M4lCC}  ###r   c                     |                      |           | j                            |          }| j                            |          }|t          j        | j                            |          d          z  S )N)axis)r   r   ppfr   pdfnpprod)r   ur   r   
mv_pdf_ppfs        r   r   zEllipticalCopula.pdf/   sj    $m""]&&s++
BGDM$5$5c$:$:DDDDDr   c                     |                      |           | j                            |          }| j                            |          S N)r   r   r   r   r   )r   r"   r   r   s       r   r   zEllipticalCopula.cdf6   sA    $m""}  %%%r   c                     || j         }|j        dk    r|d         }dt          j        |          z  t          j        z  }|S )a  Bivariate kendall's tau based on correlation coefficient.

        Parameters
        ----------
        corr : None or float
            Pearson correlation. If corr is None, then the correlation will be
            taken from the copula attribute.

        Returns
        -------
        Kendall's tau that corresponds to pearson correlation in the
        elliptical copula.
        N   r(   r   r   r(   corrshaper    arcsinpi)r   r+   rhos      r   tauzEllipticalCopula.tau;   sD     <9D::D")D//!BE)
r   c                 N    t          j        |t           j        z  dz            }|S )aP  Pearson correlation from kendall's tau.

        Parameters
        ----------
        tau : array_like
            Kendall's tau correlation coefficient.

        Returns
        -------
        Pearson correlation coefficient for given tau in elliptical
        copula. This can be used as parameter for an elliptical copula.
        r(   )r    sinr.   )r   r0   r+   s      r   corr_from_tauzEllipticalCopula.corr_from_tauP   s"     vcBEkAo&&r   c                    t          j        |          }|j        d         dk    r0t          j        |dddf         |dddf                   d         }n|| j        }t          j        |          }t          |          D ]Q}t          |dz   |          D ];}t          j        |d|f         |d|f                   d         }|x|||f<   |||f<   <R|                     |          S )a  Copula correlation parameter using Kendall's tau of sample data.

        Parameters
        ----------
        data : array_like
            Sample data used to fit `theta` using Kendall's tau.

        Returns
        -------
        corr_param : float
            Correlation parameter of the copula, ``theta`` in Archimedean and
            pearson correlation in elliptical.
            If k_dim > 2, then average tau is used.
        r   r(   Nr   .)	r    asarrayr,   r   
kendalltauk_dimeyerange_arg_from_tau)r   datar   r0   kijtau_ijs           r   fit_corr_paramzEllipticalCopula.fit_corr_param`   s     Jt71:??"1QQQT7AaaadG44Q7CC
A&))C1XX 3 3qsA 3 3A"-aQi36CCAFF,22C1IAqD		3 !!#&&&r   )r   r	   Nr	   r%   )__name__
__module____qualname____doc__r   r   r   r   r0   r3   r@   r	   r   r   r   r      s           $ $ $ $
E E E E& & & &
   *   ' ' ' ' 'r   r   c                   2     e Zd ZdZd fd	Zd	dZd Z xZS )
GaussianCopulaa  Gaussian copula.

    It is constructed from a multivariate normal distribution over
    :math:`\mathbb{R}^d` by using the probability integral transform.

    For a given correlation matrix :math:`R \in[-1, 1]^{d \times d}`,
    the Gaussian copula with parameter matrix :math:`R` can be written
    as:

    .. math::

        C_R^{\text{Gauss}}(u) = \Phi_R\left(\Phi^{-1}(u_1),\dots,
        \Phi^{-1}(u_d) \right),

    where :math:`\Phi^{-1}` is the inverse cumulative distribution function
    of a standard normal and :math:`\Phi_R` is the joint cumulative
    distribution function of a multivariate normal distribution with mean
    vector zero and covariance matrix equal to the correlation
    matrix :math:`R`.

    Parameters
    ----------
    corr : scalar or array_like
        Correlation or scatter matrix for the elliptical copula. In the
        bivariate case, ``corr` can be a scalar and is then considered as
        the correlation coefficient. If ``corr`` is None, then the scatter
        matrix is the identity matrix.
    k_dim : int
        Dimension, number of components in the multivariate random variable.
    allow_singular : bool
        Allow singular correlation matrix.
        The behavior when the correlation matrix is singular is determined by
        `scipy.stats.multivariate_normal`` and might not be appropriate for
        all copula or copula distribution metnods. Behavior might change in
        future versions.

    Notes
    -----
    Elliptical copulas require that copula parameters are set when the
    instance is created. Those parameters currently cannot be provided in the
    call to methods. (This will most likely change in future versions.)
    If non-empty ``args`` are provided in methods, then a ValueError is raised.
    The ``args`` keyword is provided for a consistent interface across
    copulas.

    References
    ----------
    .. [1] Joe, Harry, 2014, Dependence modeling with copulas. CRC press.
        p. 163

    Nr(   Fc                    t                                          |           |t          j        |          }n8|dk    r2t          j        |          dk    rt          j        d|g|dgg          }t          j        |          | _        | j        f| _        t          j
        | _        t          j        ||          | _        d S )Nr7   r(   r         ?)covallow_singular)super__init__r    r8   r   arrayr5   r+   r   r   normr   multivariate_normalr   )r   r+   r7   rL   	__class__s       r   rN   zGaussianCopula.__init__   s    u%%%<6%==DDaZZBGDMMQ..8b$Z$455DJt$$	YL	
1^5 5 5r   c                     dS )a  
        Bivariate tail dependence parameter.

        Joe (2014) p. 182

        Parameters
        ----------
        corr : any
            Tail dependence for Gaussian copulas is always zero.
            Argument will be ignored

        Returns
        -------
        Lower and upper tail dependence coefficients of the copula with given
        Pearson correlation coefficient.
        )r   r   r	   )r   r+   s     r   dependence_tailzGaussianCopula.dependence_tail   s	    $ tr   c                 ,    |                      |          S r%   r3   r   r0   s     r   r:   zGaussianCopula._arg_from_tau   s    !!#&&&r   )Nr(   Fr%   )rB   rC   rD   rE   rN   rT   r:   __classcell__rR   s   @r   rG   rG   ~   sk        2 2h5 5 5 5 5 5   (' ' ' ' ' ' 'r   rG   c                   B     e Zd ZdZd
 fd	ZddZddZddZd	 Z xZ	S )StudentTCopulaa  Student t copula.

    Parameters
    ----------
    corr : scalar or array_like
        Correlation or scatter matrix for the elliptical copula. In the
        bivariate case, ``corr` can be a scalar and is then considered as
        the correlation coefficient. If ``corr`` is None, then the scatter
        matrix is the identity matrix.
    df : float (optional)
        Degrees of freedom of the multivariate t distribution.
    k_dim : int
        Dimension, number of components in the multivariate random variable.

    Notes
    -----
    Elliptical copulas require that copula parameters are set when the
    instance is created. Those parameters currently cannot be provided in the
    call to methods. (This will most likely change in future versions.)
    If non-empty ``args`` are provided in methods, then a ValueError is raised.
    The ``args`` keyword is provided for a consistent interface across
    copulas.

    References
    ----------
    .. [1] Joe, Harry, 2014, Dependence modeling with copulas. CRC press.
        p. 181
    Nr(   c                    t                                          |           |t          j        |          }n8|dk    r2t          j        |          dk    rt          j        d|g|dgg          }|| _        t          j        |          | _        ||f| _	        t          j        |          | _        t          ||          | _        d S )NrI   r(   r   rJ   )df)r,   r]   )rM   rN   r    r8   r   rO   r]   r5   r+   r   r   tr   r   r   )r   r+   r]   r7   rR   s       r   rN   zStudentTCopula.__init__   s    u%%%<6%==DDaZZBGDMMQ..8b$Z$455DJt$$	2J	2&Tb999r   r	   c                      t          d          )Nz!CDF not available in closed form.)NotImplementedError)r   r"   r   s      r   r   zStudentTCopula.cdf  s    !"EFFFr   c                     || j         }|j        dk    r|d         }dt          j        |dz            z  t          j        z  }|S )a  
        Bivariate Spearman's rho based on correlation coefficient.

        Joe (2014) p. 182

        Parameters
        ----------
        corr : None or float
            Pearson correlation. If corr is None, then the correlation will be
            taken from the copula attribute.

        Returns
        -------
        Spearman's rho that corresponds to pearson correlation in the
        elliptical copula.
        Nr'   r)      r(   r*   )r   r+   r0   s      r   spearmans_rhozStudentTCopula.spearmans_rho  sJ    " <9D::D")D1H%%%-
r   c                     || j         }|j        dk    r|d         }| j        }t          j        |dz   d|z
  z  dz  |z              }dt
          j                            ||dz             z  }||fS )a  
        Bivariate tail dependence parameter.

        Joe (2014) p. 182

        Parameters
        ----------
        corr : None or float
            Pearson correlation. If corr is None, then the correlation will be
            taken from the copula attribute.

        Returns
        -------
        Lower and upper tail dependence coefficients of the copula with given
        Pearson correlation coefficient.
        Nr'   r)   r   r(   )r+   r,   r]   r    sqrtr   r^   r   )r   r+   r]   r^   lams        r   rT   zStudentTCopula.dependence_tail$  s}    " <9D::DWgrAv!d(+a/$6777%'++aa(((Cxr   c                 ,    |                      |          S r%   rV   rW   s     r   r:   zStudentTCopula._arg_from_tau@  s     !!#&&&r   )NNr(   rA   r%   )
rB   rC   rD   rE   rN   r   rc   rT   r:   rX   rY   s   @r   r[   r[      s         :: : : : : :G G G G   2   8' ' ' ' ' ' 'r   r[   )rE   numpyr    scipyr   statsmodels.compat.scipyr   (statsmodels.distributions.copula.copulasr   r   rG   r[   r	   r   r   <module>rl      s               3 3 3 3 3 3 ; ; ; ; ; ;i' i' i' i' i'v i' i' i'XX' X' X' X' X'% X' X' X'vj' j' j' j' j'% j' j' j' j' j'r   