
    M/PhB                     @   d Z ddlZ G d d          Z G d de          Z G d de          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z	 G d d          Z
 G d de
          Z G d d          Z G 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                   ,    e Zd ZdZddZd Zd Zd ZdS )	Penaltya,  
    A class for representing a scalar-value penalty.

    Parameters
    ----------
    weights : 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                 "    || _         d| _        d S Nr   weightsalphaselfr	   s     [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/base/_penalties.py__init__zPenalty.__init__'   s    


    c                     t           )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.
        NotImplementedErrorr   paramss     r   funczPenalty.func+   
     "!r   c                     t           )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`.
        r   r   s     r   derivzPenalty.deriv;   r   r   c                 r    t          j        | j                  dk    rt          |          dk    r | j        S )zwork around for Null model

        This will not be needed anymore when we can use `self._null_drop_keys`
        as in DiscreteModels.
        TODO: check other models
           )npsizer	   lenr   s     r   _null_weightszPenalty._null_weightsK   s9     74<  1$$6{{a |r   Nr   )__name__
__module____qualname____doc__r   r   r   r    r   r   r   r      s_            " " " " " "     r   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )NonePenaltyz+
    A penalty that does not penalize.
    c                     t                                                       |rdd l}|                    d           d S d S )Nr   z keyword arguments are be ignored)superr   warningswarn)r   kwdsr)   	__class__s      r   r   zNonePenalty.__init___   sM     	>OOOMM<=====	> 	>r   c                 ^    |j         dk    r!t          j        |j        dd                    S dS )N   r   r   )ndimr   zerosshaper   s     r   r   zNonePenalty.funce   s/    ;!8FL,---1r   c                 4    t          j        |j                  S Nr   r0   r1   r   s     r   r   zNonePenalty.derivk   s    x%%%r   c                 @    t          j        |j        d                   S )Nr   r4   r   s     r   deriv2zNonePenalty.deriv2n   s    xQ(((r   	r    r!   r"   r#   r   r   r   r6   __classcell__r,   s   @r   r&   r&   Z   so         > > > > >  & & &) ) ) ) ) ) )r   r&   c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )L2z!
    The L2 (ridge) penalty.
    r   c                 J    t                                          |           d S r3   )r(   r   )r   r	   r,   s     r   r   zL2.__init__x   s!    !!!!!r   c                 P    t          j        | j        | j        z  |dz  z            S Nr.   )r   sumr	   r
   r   s     r   r   zL2.func{   s$    vdlTZ/&!);<<<r   c                 ,    d| j         z  | j        z  |z  S r>   r   r   s     r   r   zL2.deriv~   s    4<$*,v55r   c                 j    d| j         z  | j        z  t          j        t	          |                    z  S r>   )r	   r
   r   onesr   r   s     r   r6   z	L2.deriv2   s+    4<$*,rws6{{/C/CCCr   r   r7   r9   s   @r   r;   r;   s   s{         " " " " " "= = =6 6 6D D D D D D Dr   r;   c                   ,    e Zd ZdZddZd Zd Zd ZdS )L2Univariatez;
    The L2 (ridge) penalty applied to each parameter.
    Nc                 *    |	d| _         d S || _         d S r   r	   r   s     r   r   zL2Univariate.__init__   s    ?DLLL"DLLLr   c                     | j         |dz  z  S r>   rF   r   s     r   r   zL2Univariate.func   s    |fai''r   c                     d| j         z  |z  S r>   rF   r   s     r   r   zL2Univariate.deriv   s    4<&((r   c                 Z    d| j         z  t          j        t          |                    z  S r>   )r	   r   rB   r   r   s     r   r6   zL2Univariate.deriv2   s$    4<"'#f++"6"666r   r3   )r    r!   r"   r#   r   r   r   r6   r$   r   r   rD   rD      s_         # # # #( ( () ) )7 7 7 7 7r   rD   c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )PseudoHuberz#
    The pseudo-Huber penalty.
    r   c                 X    t                                          |           || _        d S r3   )r(   r   dlt)r   rM   r	   r,   s      r   r   zPseudoHuber.__init__   s&    !!!r   c                     t          j        d|| j        z  dz  z             }|dz  }|| j        dz  z  }t          j        | j        | j        z  |z  d          S )Nr   r.   r   )r   sqrtrM   r?   r	   r
   r   r   vs      r   r   zPseudoHuber.func   s[    GA$(*Q..//	Q	TXq[vdlTZ/!3Q777r   c                 p    t          j        d|| j        z  dz  z             }|| j        z  | j        z  |z  S )Nr   r.   )r   rO   rM   r	   r
   rP   s      r   r   zPseudoHuber.deriv   s;    GA$(*Q..//$tz1A55r   c                 l    t          j        d|| j        z  dz  z   d          }| j        | j        z  |z  S )Nr   r.   g      )r   powerrM   r	   r
   rP   s      r   r6   zPseudoHuber.deriv2   s8    HQ&48+a//66|dj(1,,r   r   r7   r9   s   @r   rK   rK      st              8 8 86 6 6- - - - - - -r   rK   c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )	SCADu  
    The SCAD penalty of Fan and Li.

    The SCAD penalty is linear around zero as a L1 penalty up to threshold tau.
    The SCAD penalty is constant for values larger than c*tau.
    The middle segment is quadratic and connect the two segments with a continuous
    derivative.
    The penalty is symmetric around zero.

    Parameterization follows Boo, Johnson, Li and Tan 2011.
    Fan and Li use lambda instead of tau, and a instead of c. Fan and Li
    recommend setting c=3.7.

    f(x) = { tau |x|                                        if 0 <= |x| < tau
           { -(|x|^2 - 2 c tau |x| + tau^2) / (2 (c - 1))   if tau <= |x| < c tau
           { (c + 1) tau^2 / 2                              if c tau <= |x|

    Parameters
    ----------
    tau : float
        slope and threshold for linear segment
    c : float
        factor for second threshold which is c * tau
    weights : None or array
        weights for penalty of each parameter. If an entry is zero, then the
        corresponding parameter will not be penalized.

    References
    ----------
    Buu, Anne, Norman J. Johnson, Runze Li, and Xianming Tan. "New variable
    selection methods for zero‐inflated count data with applications to the
    substance abuse field."
    Statistics in medicine 30, no. 18 (2011): 2326-2340.

    Fan, Jianqing, and Runze Li. "Variable selection via nonconcave penalized
    likelihood and its oracle properties."
    Journal of the American statistical Association 96, no. 456 (2001):
    1348-1360.
    皙@r   c                 f    t                                          |           || _        || _        d S r3   )r(   r   tauc)r   rY   rZ   r	   r,   s       r   r   zSCAD.__init__   s-    !!!r   c                     | j         }t          j        t          j        |                    }t          j        |j        |j                  }|                    t          j                   ||k     }|| j	        |z  k    }|||         z  ||<   | | z  }||         }|dz  d| j	        z  |z  |z  z
  |dz  z   }	|	 d| j	        dz
  z  z  ||<   | j	        dz   |dz  z  dz  ||<   | j
        |z                      d          S )Nr.   r   g       @r   )rY   r   
atleast_1dabsemptyr1   dtypefillnanrZ   r	   r?   )
r   r   rY   p_absresmask1mask3mask2p_abs2tmps
             r   r   z	SCAD.func   s    hbfVnn--hu{EK00#%5<'E
%uqy1tv:+f44sAv=TQ$&1*-.E
fqjCF*R/E
s"''***r   c                    | j         }t          j        |          }t          j        |          }t          j        |          }t          j        |j                  }|                    t          j                   ||k     }|| j	        |z  k    }| | z  }	||         |z  ||<   ||	         ||	         | j	        |z  z
  z  }
|
 | j	        dz
  z  ||	<   d||<   | j
        |z  S )Nr   r   )rY   r   r\   r]   signr^   r1   r`   ra   rZ   r	   )r   r   rY   prb   p_signrc   rd   re   rf   rh   s              r   r   z
SCAD.deriv   s     hM&!!q		hu{###%%E]S(E
UmuU|dfsl:;TTVaZ(E
E
|c!!r   c                     | j         }t          j        |          }t          j        |          }t          j        |j                  }||k     }|| j        |z  k    }| | z  }d| j        dz
  z  ||<   | j        |z  S )zSecond derivative of function

        This returns scalar or vector in same shape as params, not a square
        Hessian. If the return is 1 dimensional, then it is the diagonal of
        the Hessian.
        r   )rY   r   r\   r]   r0   r1   rZ   r	   )	r   r   rY   rk   rb   rc   rd   re   rf   s	            r   r6   zSCAD.deriv2  s     hM&!!q		hu{###%%46A:&E
|c!!r   )rW   r   r7   r9   s   @r   rV   rV      su        & &P     
+ + +$" " "(" " " " " " "r   rV   c                   B     e Zd ZdZd	 fd	Z fdZ fdZ fdZ xZS )
SCADSmoothedad  
    The SCAD penalty of Fan and Li, quadratically smoothed around zero.

    This follows Fan and Li 2001 equation (3.7).

    Parameterization follows Boo, Johnson, Li and Tan 2011
    see docstring of SCAD

    Parameters
    ----------
    tau : float
        slope and threshold for linear segment
    c : float
        factor for second threshold
    c0 : float
        threshold for quadratically smoothed segment
    restriction : None or array
        linear constraints for

    Notes
    -----
    TODO: Use delegation instead of subclassing, so smoothing can be added to
    all penalty classes.
    rW   Nr   c                    t                                          |||           || _        || _        ||n|dz  | _        | j        |k    rt          d          | j        }| j        }d| _        t                                          |          }t                                          |          }|| _        |d|z  |z  z
  | _	        d|z  |z  | _
        || _        d S )N)rZ   r	   g?zc0 cannot be larger than taur   g      ?)r(   r   rY   rZ   c0
ValueErrorr	   r   r   aq1aq2restriction)	r   rY   rZ   rr   r	   rv   deriv_c0value_c0r,   s	           r   r   zSCADSmoothed.__init__6  s    7333""C#I7S==;<<< W,77==$$77<<##cHnr11>B&&r   c                    |                      |          }| j        2t          j        |          dk    r| j                            |          }| j        }d| _        t                                          |d                   }|| _        || j        z  }t          j	        t          j
        |                    }|| j        k     }||         }| j        |dz  z  ||<   ||z                      d          S )Nr   r   )N.r.   r   )r   rv   r   r   dotr	   r(   r   rt   r\   r]   rr   ru   r?   )	r   r   r	   self_weightsvaluerb   maskp_abs_maskedr,   s	           r   r   zSCADSmoothed.funcK  s    $$V,,'BGFOOa,?,?%))&11F |VI.//# 	bfVnn--twT{hq0d%$$Q'''r   c                    |                      |          }| j        2t          j        |          dk    r| j                            |          }| j        }d| _        t                                          |          }|| _        t          j        |          }t          j	        |          | j
        k     }d| j        z  ||         z  ||<   | j        5t          j        |          dk    r||                    | j                  z  S ||z  S Nr   r   r.   )r   rv   r   r   rz   r	   r(   r   r\   r]   rr   ru   r   r   r	   r{   r|   rk   r}   r,   s          r   r   zSCADSmoothed.derivb  s    $$V,,'BGFOOa,?,?%))&11F|f%%# M&!!vayy47"$(lQtW,d'BGFOOa,?,?UYYt'78888U?"r   c                 $   |                      |          }| j        2t          j        |          dk    r| j                            |          }| j        }d| _        t                                          |          }|| _        t          j        |          }t          j	        |          | j
        k     }d| j        z  ||<   | j        Bt          j        |          dk    r*| j        j        ||z  z                      | j                  S ||z  S r   )r   rv   r   r   rz   r	   r(   r6   r\   r]   rr   ru   Tr   s          r   r6   zSCADSmoothed.deriv2w  s    $$V,,'BGFOOa,?,?%))&11F|v&&# M&!!vayy47"$(ld'BGFOOa,?,? $&'E/:c$*++, U?"r   )rW   Nr   Nr7   r9   s   @r   rp   rp     s         2' ' ' ' ' '*( ( ( ( (.# # # # #*# # # # # # # # #r   rp   c                   0    e Zd ZdZddZd Zd ZeZd ZdS )ConstraintsPenaltya  
    Penalty applied to linear transformation of parameters

    Parameters
    ----------
    penalty: instance of penalty function
        currently this requires an instance of a univariate, vectorized
        penalty class
    weights : None or ndarray
        weights for adding penalties of transformed params
    restriction : None or ndarray
        If it is not None, then restriction defines a linear transformation
        of the parameters. The penalty function is applied to each transformed
        parameter independently.

    Notes
    -----
    `restrictions` allows us to impose penalization on contrasts or stochastic
    constraints of the original parameters.
    Examples for these contrast are difference penalities or all pairs
    penalties.
    Nc                 p    || _         |d| _        n|| _        |t          j        |          }|| _        d S r   )penaltyr	   r   asarrayrv   )r   r   r	   rv   s       r   r   zConstraintsPenalty.__init__  sA    ?DLL"DL"*[11K&r   c                     | j         | j                             |          }| j                            |          }| j        |j        z  j                            d          S )a  evaluate penalty function at params

        Parameter
        ---------
        params : ndarray
            array of parameters at which derivative is evaluated

        Returns
        -------
        deriv2 : ndarray
            value(s) of penalty function
        Nr   )rv   rz   r   r   r	   r   r?   r   r   r|   s      r   r   zConstraintsPenalty.func  sX     '%))&11F!!&))uw&)--a000r   c                     | j         | j                             |          }| j                            |          }| j         '| j        |j                            | j                   z  S | j        |j        z  S )a#  first derivative of penalty function w.r.t. params

        Parameter
        ---------
        params : ndarray
            array of parameters at which derivative is evaluated

        Returns
        -------
        deriv2 : ndarray
            array of first partial derivatives
        )rv   rz   r   r   r	   r   r   s      r   r   zConstraintsPenalty.deriv  sn     '%))&11F""6**'<%'++d.>"?"???L57*+r   c                 &   | j         | j                             |          }| j                            |          }| j         2| j         j        |z  | j        z  }|                    | j                   }nt          j        | j        |z            }|S )a  second derivative of penalty function w.r.t. params

        Parameter
        ---------
        params : ndarray
            array of parameters at which derivative is evaluated

        Returns
        -------
        deriv2 : ndarray, 2-D
            second derivative matrix
        )rv   rz   r   r6   r   r	   r   diag)r   r   r|   rQ   s       r   r6   zConstraintsPenalty.deriv2  s     '%))&11F##F++' !#e+dl:AEE$*++EEGDL5011Er   )NN)	r    r!   r"   r#   r   r   r   gradr6   r$   r   r   r   r     sf         .' ' ' '1 1 1,, , ,. D    r   r   c                   $     e Zd ZdZd fd	Z xZS )L2ConstraintsPenaltyzAconvenience class of ConstraintsPenalty with L2 penalization
    Nc                     |t          d          t                      }t                                          |||           d S )Nz"sigma_prior is not implemented yet)r	   rv   )r   rD   r(   r   )r   r	   rv   sigma_priorr   r,   s        r   r   zL2ConstraintsPenalty.__init__  sX    "%&JKKK..'>I 	 	K 	K 	K 	K 	Kr   )NNN)r    r!   r"   r#   r   r8   r9   s   @r   r   r     sQ         K K K K K K K K K Kr   r   c                        e Zd Zd Zd Zd ZdS )CovariancePenaltyc                     || _         d S r3   )weight)r   r   s     r   r   zCovariancePenalty.__init__  s    r   c                     t           )z
        Parameters
        ----------
        mat : square matrix
            The matrix to be penalized.
        mat_inv : square matrix
            The inverse of `mat`.

        Returns
        -------
        A scalar penalty value
        r   r   matmat_invs      r   r   zCovariancePenalty.func  s
     "!r   c                     t           )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`.
        r   r   s      r   r   zCovariancePenalty.deriv&  s
     "!r   N)r    r!   r"   r   r   r   r$   r   r   r   r     sA          " " "" " " " "r   r   c                       e Zd ZdZd Z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                    	 t           j                            |          }n&# t           j        j        $ r t           j        cY S w xY wd| j        z  t          j        t          j        t          j        |                              z  S )N)	r   linalgcholeskyLinAlgErrorinfr   r?   logr   )r   r   r   cys       r   r   zPSD.func?  sw    	##C((BBy$ 	 	 	6MMM	DK"&)<)<"="===s   "  AAc                     |                                 }d|z  t          j        t          j        |                    z
  }t          j        |j        d                   \  }}| j         |||f         z  S )Nr.   r   )copyr   r   tril_indicesr1   r   )r   r   r   r   ijs         r   r   z	PSD.derivF  s_    \\^^rTBGBGBKK(((ocil++!|b1g%%r   N)r    r!   r"   r#   r   r   r$   r   r   r   r   8  s<         > > >& & & & &r   r   )r#   numpyr   r   r&   r;   rD   rK   rV   rp   r   r   r   r   r$   r   r   <module>r      s   (    ? ? ? ? ? ? ? ?D) ) ) ) )' ) ) )2D D D D D D D D$7 7 7 7 77 7 7 7*- - - - -' - - -0g" g" g" g" g"7 g" g" g"Tq# q# q# q# q#4 q# q# q#ho o o o o o o odK K K K K- K K K$" $" $" $" $" $" $" $"N& & & & &
 & & & & &r   