
    M/Ph&                     b    d Z ddlZddlmZ ddlmZ  G d de          Z G d de          ZdS )	z_
Penalty classes for Generalized Additive Models

Author: Luca Puggini
Author: Josef Perktold

    N)
block_diag)Penaltyc                   :    e Zd ZdZd	dZd
dZd
dZd
dZd
dZdS )UnivariateGamPenaltya  
    Penalty for smooth term in Generalized Additive Models

    Parameters
    ----------
    univariate_smoother : instance
        instance of univariate smoother or spline class
    alpha : float
        default penalty weight, alpha can be provided to each method
    weights:
        TODO: not used and verified, might be removed

    Attributes
    ----------
    Parameters are stored, additionally
    nob s: The number of samples used during the estimation
    n_columns : number of columns in smoother basis
       c                 t    || _         || _        || _        | j        j        | _        | j        j        | _        d S N)weightsalphaunivariate_smoothernobs	dim_basis	n_columns)selfr   r   r
   s       ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/gam/gam_penalties.py__init__zUnivariateGamPenalty.__init__"   s6    
#6 ,1	1;    Nc                     || j         }|                    | j        j                            |                    }||z  | j        z  S )aR  evaluate penalization at params

        Parameters
        ----------
        params : ndarray
            coefficients for the spline basis in the regression model
        alpha : float
            default penalty weight

        Returns
        -------
        func : float
            value of the penalty evaluated at params
        )r   dotr   cov_der2r   )r   paramsr   fs       r   funczUnivariateGamPenalty.func)   sF     =JEJJt/8<<VDDEEqy49$$r   c                 v    || j         }d|z  t          j        | j        j        |          z  }|| j        z  }|S )a}  evaluate derivative of penalty with respect to params

        Parameters
        ----------
        params : ndarray
            coefficients for the spline basis in the regression model
        alpha : float
            default penalty weight

        Returns
        -------
        deriv : ndarray
            derivative, gradient of the penalty with respect to params
        N   )r   npr   r   r   r   )r   r   r   ds       r   derivzUnivariateGamPenalty.deriv>   s@     =JEIt7@&III	TYr   c                 P    || j         }d|z  | j        j        z  }|| j        z  }|S )a  evaluate second derivative of penalty with respect to params

        Parameters
        ----------
        params : ndarray
            coefficients for the spline basis in the regression model
        alpha : float
            default penalty weight

        Returns
        -------
        deriv2 : ndarray, 2-Dim
            second derivative, hessian of the penalty with respect to params
        Nr   )r   r   r   r   )r   r   r   d2s       r   deriv2zUnivariateGamPenalty.deriv2T   s4     =JEY1::
di	r   c                 2    || j         }|| j        j        z  S )a  penalty matrix for the smooth term of a GAM

        Parameters
        ----------
        alpha : list of floats or None
            penalty weights

        Returns
        -------
        penalty matrix
            square penalty matrix for quadratic penalization. The number
            of rows and columns are equal to the number of columns in the
            smooth terms, i.e. the number of parameters for this smooth
            term in the regression model
        )r   r   r   )r   r   s     r   penalty_matrixz#UnivariateGamPenalty.penalty_matrixj   s!      =JEt/888r   )r   r   r	   	__name__
__module____qualname____doc__r   r   r   r!   r#    r   r   r   r      s         &< < < <% % % %*   ,   ,9 9 9 9 9 9r   r   c                   >    e Zd ZdZ	 	 d	dZd
dZd
dZd
dZd
dZdS )MultivariateGamPenaltya^  
    Penalty for Generalized Additive Models

    Parameters
    ----------
    multivariate_smoother : instance
        instance of additive smoother or spline class
    alpha : list of float
        default penalty weight, list with length equal to the number of smooth
        terms. ``alpha`` can also be provided to each method.
    weights : array_like
        currently not used
        is a list of doubles of the same length as alpha or a list
        of ndarrays where each component has the length equal to the number
        of columns in that component
    start_idx : int
        number of parameters that come before the smooth terms. If the model
        has a linear component, then the parameters for the smooth components
        start at ``start_index``.

    Attributes
    ----------
    Parameters are stored, additionally
    nob s: The number of samples used during the estimation

    dim_basis : number of columns of additive smoother. Number of columns
        in all smoothers.
    k_variables : number of smooth terms
    k_params : total number of parameters in the regression model
    Nr   c                     t          |j                  t          |          k    r5dt          |j                  t          |          fz  }t          |          | _         j        j         _         j        j         _         j        j         _        | _        | _        | j        z    _	        |$d t           j                  D              _        n dd l}|                    d           | _         fdt           j                  D              _        |}t           j        j                  D ])\  }}	d j        |         |||	j        z   <   ||	j        z  }*g  _        t           j                  D ]T}t#           j        j        |          j        |          j        |                   }
 j                            |
           Ud S )NzTall the input values should be of the same length. len(smoothers)=%d, len(alphas)=%dc                     g | ]}d S )g      ?r)   ).0_s     r   
<listcomp>z3MultivariateGamPenalty.__init__.<locals>.<listcomp>   s    @@@1B@@@r   r   zweights is currently ignoredc                 P    g | ]"}t          j        j        t                     #S ))dtype)r   zerosk_paramsbool)r.   r/   r   s     r   r0   z3MultivariateGamPenalty.__init__.<locals>.<listcomp>   s:     7 7 7 Xdm4888 7 7 7r   T)r
   r   )len	smoothers
ValueErrormultivariate_smootherr   k_variablesr   r   	start_idxr4   ranger
   warningswarnmask	enumerategpr   append)r   r9   r   r
   r;   msgr=   param_countismootherrA   s   `          r   r   zMultivariateGamPenalty.__init__   s    $.//3u::==8,677U<EEC S//!%:"3=5A.3	
"!DN2 ? A@d.>(?(?@@@DLLOOOMM8999"DL7 7 7 7#D$4557 7 7	$T%?%IJJ 	. 	.KAx KODIaLkH4F&FFG8--KKt'(( 	 	A%d&@&J1&M.2l1o,0JqM; ; ;B GNN2		 	r   c                     |dg| j         z  }d}t          | j                   D ]@}|| j        |                  }|| j        |                             |||                   z  }A|S )aH  evaluate penalization at params

        Parameters
        ----------
        params : ndarray
            coefficients in the regression model
        alpha : float or list of floats
            penalty weights

        Returns
        -------
        func : float
            value of the penalty evaluated at params
        Nr   r   )r:   r<   r?   rA   r   )r   r   r   costrE   params_is         r   r   zMultivariateGamPenalty.func   sr     =FT--Et'(( 	> 	>Adil+HDGAJOOHE!HO===DDr   c                 B   |dg| j         z  }t          j        | j                  g}t	          | j                   D ]P}|| j        |                  }|                    | j        |                             |||                              Qt          j	        |          S )ar  evaluate derivative of penalty with respect to params

        Parameters
        ----------
        params : ndarray
            coefficients in the regression model
        alpha : list of floats or None
            penalty weights

        Returns
        -------
        deriv : ndarray
            derivative, gradient of the penalty with respect to params
        NrH   )
r:   r   r3   r;   r<   r?   rB   rA   r   concatenate)r   r   r   gradrE   rJ   s         r   r   zMultivariateGamPenalty.deriv   s     =FT--E(()t'(( 	D 	DAdil+HKK
((q(BBCCCC~d###r   c                 :   |dg| j         z  }t          j        | j        | j        f          g}t	          | j                   D ]P}|| j        |                  }|                    | j        |                             |||                              Qt          | S )a  evaluate second derivative of penalty with respect to params

        Parameters
        ----------
        params : ndarray
            coefficients in the regression model
        alpha : list of floats or None
            penalty weights

        Returns
        -------
        deriv2 : ndarray, 2-Dim
            second derivative, hessian of the penalty with respect to params
        NrH   )
r:   r   r3   r;   r<   r?   rB   rA   r!   r   )r   r   r   r!   rE   rJ   s         r   r!   zMultivariateGamPenalty.deriv2   s     =FT--E(DNDN;<<=t'(( 	G 	GAdil+HMM$'!*++HE!H+EEFFFF6""r   c                 
   || j         }t          j        | j        | j        f          g}t	          | j                  D ]<}|                    | j        |                             ||                              =t          | S )a  penalty matrix for generalized additive model

        Parameters
        ----------
        alpha : list of floats or None
            penalty weights

        Returns
        -------
        penalty matrix
            block diagonal, square penalty matrix for quadratic penalization.
            The number of rows and columns are equal to the number of
            parameters in the regression model ``k_params``.

        Notes
        -----
        statsmodels does not support backwards compatibility when keywords are
        used as positional arguments. The order of keywords might change.
        We might need to add a ``params`` keyword if the need arises.
        NrH   )
r   r   r3   r;   r<   r:   rB   rA   r#   r   )r   r   s_allrE   s       r   r#   z%MultivariateGamPenalty.penalty_matrix  s    * =JE4>4>:;;<t'(( 	D 	DALL22q2BBCCCC5!!r   )Nr   r	   r$   r)   r   r   r+   r+      s         > >B) ) ) )V   2$ $ $ $2# # # #2" " " " " "r   r+   )	r(   numpyr   scipy.linalgr   statsmodels.base._penaltiesr   r   r+   r)   r   r   <module>rT      s         # # # # # # / / / / / /o9 o9 o9 o9 o97 o9 o9 o9dr" r" r" r" r"W r" r" r" r" r"r   