B
    £ŠãZ„  ã               @   s`   d Z ddlZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZdS )a  
A collection of smooth penalty functions.

Penalties on vectors take a vector argument and return a scalar
penalty.  The gradient of the penalty is a vector with the same shape
as the input value.

Penalties on covariance matrices take two arguments: the matrix and
its inverse, both in unpacked (square) form.  The returned penalty is
a scalar, and the gradient is returned as a vector that contains the
gradient with respect to the free elements in the lower triangle of
the covariance matrix.

All penalties are subtracted from the log-likelihood, so greater
penalty values correspond to a greater degree of penalization.

The penaties should be smooth so that they can be subtracted from log
likelihood functions and optimized using standard methods (i.e. L1
penalties do not belong here).
é    Nc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚPenaltya)  
    A class for representing a scalar-value penalty.

    Parameters
    ----------
    wts : array-like
        A vector of weights that determines the weight of the penalty
        for each parameter.


    Notes
    -----
    The class has a member called `alpha` that scales the weights.
    c             C   s   || _ d| _d S )Ng      ð?)ÚwtsÚalpha)Úselfr   © r   ú:lib/python3.7/site-packages/statsmodels/base/_penalties.pyÚ__init__(   s    zPenalty.__init__c             C   s   t ‚dS )a  
        A penalty function on a vector of parameters.

        Parameters
        ----------
        params : array-like
            A vector of parameters.

        Returns
        -------
        A scalar penaty value; greater values imply greater
        penalization.
        N)ÚNotImplementedError)r   Úparamsr   r   r   Úfunc,   s    zPenalty.funcc             C   s   t ‚dS )a  
        The gradient of a penalty function.

        Parameters
        ----------
        params : array-like
            A vector of parameters

        Returns
        -------
        The gradient of the penalty with respect to each element in
        `params`.
        N)r	   )r   r
   r   r   r   Úgrad<   s    zPenalty.gradN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s   r   c               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚL2z!
    The L2 (ridge) penalty.
    Nc             C   s    |d krd| _ n|| _ d| _d S )Ng      ð?)r   r   )r   r   r   r   r   r   R   s    zL2.__init__c             C   s   t  | j| j |d  ¡S )Né   )ÚnpÚsumr   r   )r   r
   r   r   r   r   Y   s    zL2.funcc             C   s   d| j  | j | S )Nr   )r   r   )r   r
   r   r   r   r   \   s    zL2.grad)N)r   r   r   r   r   r   r   r   r   r   r   r   M   s   
r   c               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚPseudoHuberz#
    The pseudo-Huber penalty.
    Nc             C   s&   || _ |d krd| _n|| _d| _d S )Ng      ð?)Údltr   r   )r   r   r   r   r   r   r   e   s
    zPseudoHuber.__init__c             C   sD   t  d|| j d  ¡}|d8 }|| jd 9 }t  | j| j | ¡S )Né   r   )r   Úsqrtr   r   r   r   )r   r
   Úvr   r   r   r   m   s    zPseudoHuber.funcc             C   s,   t  d|| j d  ¡}|| j | j | S )Nr   r   )r   r   r   r   r   )r   r
   r   r   r   r   r   s   s    zPseudoHuber.grad)N)r   r   r   r   r   r   r   r   r   r   r   r   `   s   
r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚCovariancePenaltyc             C   s
   || _ d S )N)Úwt)r   r   r   r   r   r   z   s    zCovariancePenalty.__init__c             C   s   t ‚dS )zõ
        Parameters
        ----------
        mat : square matrix
            The matrix to be penalized.
        mat_inv : square matrix
            The inverse of `mat`.

        Returns
        -------
        A scalar penalty value
        N)r	   )r   ÚmatÚmat_invr   r   r   r   }   s    zCovariancePenalty.funcc             C   s   t ‚dS )a[  
        Parameters
        ----------
        mat : square matrix
            The matrix to be penalized.
        mat_inv : square matrix
            The inverse of `mat`.

        Returns
        -------
        A vector containing the gradient of the penalty
        with respect to each element in the lower triangle
        of `mat`.
        N)r	   )r   r   r   r   r   r   r   Œ   s    zCovariancePenalty.gradN)r   r   r   r   r   r   r   r   r   r   r   x   s   r   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚPSDzž
    A penalty that converges to +infinity as the argument matrix
    approaches the boundary of the domain of symmetric, positive
    definite matrices.
    c             C   sL   yt j |¡}W n t jjk
r*   t jS X d| j t  t  t  |¡¡¡ S )Néþÿÿÿ)	r   ZlinalgZcholeskyZLinAlgErrorÚinfr   r   ÚlogÚdiag)r   r   r   Úcyr   r   r   r   ¥   s
    zPSD.funcc             C   sH   |  ¡ }d| t t |¡¡ }t |jd ¡\}}| j |||f  S )Nr   r   )Úcopyr   r"   Ztril_indicesÚshaper   )r   r   r   r#   ÚiÚjr   r   r   r   ¬   s    zPSD.gradN)r   r   r   r   r   r   r   r   r   r   r   ž   s   r   )	r   Znumpyr   Úobjectr   r   r   r   r   r   r   r   r   Ú<module>   s   5&