U
    Kvf$                     @   s   d Z ddlZddlmZ ddlmZmZ G dd dZG dd deZ	e	 Z
G d	d
 d
eZe ZG dd deZe ZG dd deZe ZG dd deZe ZG dd deZe ZdS )z Pickand's dependence functions as generators for EV-copulas


Created on Wed Jan 27 14:33:40 2021

Author: Josef Perktold
License: BSD-3

    N)stats)_approx_fprime_cs_scalarapprox_hessc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
PickandDependencec                 O   s   | j ||S N)evaluate)selfargskwargs r   O/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/distributions/copula/depfunc_ev.py__call__   s    zPickandDependence.__call__c                 G   s   t d S r   )NotImplementedErrorr   tr	   r   r   r   r      s    zPickandDependence.evaluatec                 G   s   t |}t|| jS )zkFirst derivative of the dependence function

        implemented through numerical differentiation
        )npZ
atleast_1dr   r   r   r   r   r   deriv   s    
zPickandDependence.derivc                    sD   t |dkr&t|gj dd }nt  fdd|D }|S )zlSecond derivative of the dependence function

        implemented through numerical differentiation
           r	   r   c                    s"   g | ]}t |gj d d qS )r   )r   r   )r   r   ).0Ztir	   r   r   r   
<listcomp>)   s   z,PickandDependence.deriv2.<locals>.<listcomp>)r   sizer   r   array)r   r   r	   d2r   r   r   deriv2!   s    zPickandDependence.deriv2N)__name__
__module____qualname__r   r   r   r   r   r   r   r   r      s   r   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )AsymLogisticzasymmetric logistic model of Tawn 1988

    special case: a1=a2=1 : Gumbel

    restrictions:
     - theta in (0,1]
     - a1, a2 in [0,1]
       c                 C   s<   |dko|dk}|dko|dk}|dko.|dk}|o:|o:|S Nr   r   r   r   a1a2thetacondthZconda1Zconda2r   r   r   _check_args9   s    zAsymLogistic._check_argsc                 C   sP   d| d|  }|d| | 7 }||| d|  |d|  d|   | 7 }|S )Nr         ?r   r   r   r#   r$   r%   transfr   r   r   r   ?   s    ,zAsymLogistic.evaluatec                 C   sp   |}||| d| d   ||d|  d| d    || d|  |d|  d|   |d   | | }|S Nr   r   )r   r   r#   r$   r%   bd1r   r   r   r   J   s    2*zAsymLogistic.derivc                 C   sx   |}d| || d|   |d|  d|   || d|  |d|  d|   |d   |d| d  |d   }|S )Nr      r   )r   r   r#   r$   r%   r,   r   r   r   r   r   Q   s    **zAsymLogistic.deriv2N	r   r   r   __doc__k_argsr'   r   r   r   r   r   r   r   r   .   s   r   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )AsymNegLogisticzasymmetric negative logistic model of Joe 1990

    special case:  a1=a2=1 : symmetric negative logistic of Galambos 1978

    restrictions:
     - theta in (0,inf)
     - a1, a2 in (0,1]
    r    c                 C   s4   |dk}|dko|dk}|dko&|dk}|o2|o2|S r!   r   r"   r   r   r   r'   g   s    zAsymNegLogistic._check_argsc                 C   s<   || }}d|d|  d|  || d|   |   }|S )Nr   g      r   r)   r   r   r   r   m   s    
zAsymNegLogistic.evaluatec                 C   sh   || }}d| }|d }|| d| |  || ||   |d|  | || |  | d   }|S )Nr   r   )r   r   r#   r$   r%   m1m2r-   r   r   r   r   v   s    
" zAsymNegLogistic.derivc                 C   s   |}|| }}|d|  d|  }|| d|  }|d|  d|  }|| d|  }	|d | | ||	 |   }
|d| d  |d  || d  }|
| }|S )Nr   r3   r.   r   )r   r   r#   r$   r%   r,   Za1tpZa2tpZa1tnZa2tnt1t2r   r   r   r   r      s    
$zAsymNegLogistic.deriv2Nr/   r   r   r   r   r2   \   s   	r2   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )	AsymMixedzasymmetric mixed model of Tawn 1988

    special case:  k=0, theta in [0,1] : symmetric mixed model of
        Tiago de Oliveira 1980

    restrictions:
     - theta > 0
     - theta + 3*k > 0
     - theta + k <= 1
     - theta + 2*k <= 1
    r.   c                 C   s<   |dk}|d|  dko2|| dko2|d|  dk}||@ S )Nr   r    r   r.   r   )r   r%   kr&   cond1r   r   r   r'      s    ,zAsymMixed._check_argsc                 C   s,   d|| |  || |  ||d   }|S )Nr   r    r   )r   r   r%   r9   r*   r   r   r   r      s    (zAsymMixed.evaluatec                 C   s*   ||  d| |  d| |d   }|S )Nr.   r    r   )r   r   r%   r9   Zd_dtr   r   r   r      s    &zAsymMixed.derivc                 C   s   d| d| |  }|S )Nr.      r   )r   r   r%   r9   Zd2_dt2r   r   r   r      s    zAsymMixed.deriv2Nr/   r   r   r   r   r8      s   r8   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )AsymBiLogisticzbilogistic model of Coles and Tawn 1994, Joe, Smith and Weissman 1992

    restrictions:
     - (beta, delta) in (0,1)^2 or
     - (beta, delta) in (-inf,0)^2

    not vectorized because of numerical integration
    r.   c                 C   s8   |dko|dko|dko|dk}|dk o.|dk }||B S r!   r   )r   betadeltar:   cond2r   r   r   r'      s     zAsymBiLogistic._check_argsc                    s0    fdd}ddl m} ||ddd }|S )Nc                    sH   d  t |    d  }d t d|     }t ||S r+   )r   powermaximum)wterm1term2r=   r>   r   r   r   
_integrant   s    z+AsymBiLogistic.evaluate.<locals>._integrantr   )quadr   )Zscipy.integraterG   )r   r   r=   r>   rF   rG   r*   r   rE   r   r      s    zAsymBiLogistic.evaluateNr   r   r   r0   r1   r'   r   r   r   r   r   r<      s   r<   c                   @   s>   e Zd ZdZdZdd Zdd Zddd	Zd
d Zdd Z	dS )HRzmodel of Huesler Reiss 1989

    special case:  a1=a2=1 : symmetric negative logistic of Galambos 1978

    restrictions:
     - lambda in (0,inf)
    r   c                 C   s   |dk}|S )Nr   r   )r   lamdaZcondr   r   r   r'      s    zHR._check_argsc                 C   sR   t d| | d | }ddlm} d| |||  ||||   }|S )Nr(         ?r   )normr   )r   logscipy.statsrL   _cdf)r   r   rJ   ZtermrL   r*   r   r   r   r      s    zHR.evaluater   r.   c                 C   s  t |ttjfs.d|kr&d|kr&d}ntddtdtj  }|}td| | d | }dd| |d  |  }d| |d| | d   }|| }	tj	
|	}
t|	d  d | }| |	 }|| }tj	
|}t|d  d | }| | }|dkr,|| | |d |   | |
 }|dkr|d || |d |   |d  | d|  |  || d|  |  }|dkr|S |dkr|S |dkr||fS d S )	Nr   r.   r3   zorder should be 1, 2, or (1,2)r(   rK   )r   r3   )r.   r3   )
isinstanceintr   integer
ValueErrorsqrtpirM   r   rL   Zcdfexp)r   r   rJ   orderdnagZgd1Zgd2tpfpZfd1pZfd2ptnfnZfd1nZfd2nr-   r   r   r   r   _derivs   s>    


"



z
HR._derivsc                 C   s   |  ||dS r+   r`   r   r   rJ   r   r   r   r     s    zHR.derivc                 C   s   |  ||dS )Nr.   ra   rb   r   r   r   r   !  s    z	HR.deriv2N)rP   )
r   r   r   r0   r1   r'   r   r`   r   r   r   r   r   r   rI      s   
,rI   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )TEVz_t-EV model of Demarta and McNeil 2005

    restrictions:
     - rho in (-1,1)
     - x > 0
    r.   c                 C   s$   |}|dk}|dko|dk }|o"|S r!   r   )r   rhodfxr:   r?   r   r   r   r'   2  s    zTEV._check_argsc                 C   s   |}ddl m} t|d|  d| | }td| | d| | }td| td||   }|| }	|| }
|||	|d  d| ||
|d   }|S )Nr   )r   r(   r   )rN   r   r   r@   rU   rO   )r   r   rd   re   rf   Zstats_trC   rD   Zterm0Zz1Zz2r*   r   r   r   r   8  s     ,zTEV.evaluateNrH   r   r   r   r   rc   )  s   rc   )r0   Znumpyr   Zscipyr   Zstatsmodels.tools.numdiffr   r   r   r   Ztransform_tawnr2   Ztransform_joer8   Ztransform_tawn2r<   Ztransform_bilogisticrI   Ztransform_hrrc   Ztransform_tevr   r   r   r   <module>   s    
+4!M 