
    M/PhR                        d Z ddlmZ ddlZddlZddlmZ ddlZ	ddl
mc mZ ddlmZ ddlmZmZmZmZ ddlmc mZ ddlmZmZ ddlmZ dd	lmZ dd
l m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddZ* G d de          Z+ G d de          Z, ej-        e,e+            G d de#e          Z. G d de#e          Z/d Z0d Z1dS )za
Generalized Additive Models

Author: Luca Puggini
Author: Josef Perktold

created on 08/07/2015
    )IterableN)optimize)Logit)GLM
GLMResultsGLMResultsWrapper_check_convergence)PerfectSeparationErrorValueWarning)cache_readonly)_is_using_pandas)matrix_sqrt)PenalizedMixin)MultivariateGamPenalty)MultivariateGAMCVPath)KFoldc                 z   t          |d          }|r|j        nd}|t          | j        dd          }||ddlm} t          |t          j                  rst          |d          rJt          |j
        t                    r0|j
        |                                v rt          j        |          }nt          j        |          j        }t          |          }t          |t                     } |||d          }|t          |          k    r9|s7ddl}||                    dt&                     n|                    |          }|j        }|et+          j        |          }|j        d	k    r2| j        j        d	k    s| j        j        d	         d	k    r|dddf         }t+          j        |          }||fS )
ztransform exog for predict using design_info

    Note: this is copied from base.model.Results.predict and converted to
    standalone function with additional options.
    Ndesign_infor   )dmatrixname	dataframe)return_typeznan values have been dropped   )r   indexgetattrdatapatsyr   
isinstancepdSerieshasattrr   strdescribe	DataFrameTlendictwarningswarnr   reindexnpasarrayndimexogshape
atleast_2d)	modelr.   r   	is_pandas
exog_indexr   orig_exog_lenis_dictr(   s	            j/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/gam/generalized_additive_model.py_transform_predict_exogr7   %   s    !t,,I(2dJej->>D$4!!!!!!dBI&& 	,f%% ,*TY*D*D ,I!5!5!7!777|D)) |D))+D		T4((w{DkBBB3t99$$W$OOO!<lKKKK||J//Z
z$9>>uz!33$z/2a774=D}T""    c                        e Zd ZdZ fdZ	 	 ddZd fd	Zd fd	ZddZ	 	 dd
Z	d Z
ddZed             Zed             Zed             Zed             Zed             Z xZS )GLMGamResultsa&  Results class for generalized additive models, GAM.

    This inherits from GLMResults.

    Warning: some inherited methods might not correctly take account of the
    penalization

    GLMGamResults inherits from GLMResults
    All methods related to the loglikelihood function return the penalized
    values.

    Attributes
    ----------

    edf
        list of effective degrees of freedom for each column of the design
        matrix.
    hat_matrix_diag
        diagonal of hat matrix
    gcv
        generalized cross-validation criterion computed as
        ``gcv = scale / (1. - hat_matrix_trace / self.nobs)**2``
    cv
        cross-validation criterion computed as
        ``cv = ((resid_pearson / (1 - hat_matrix_diag))**2).sum() / nobs``

    Notes
    -----
    status: experimental
    c                    || _         || _        || _        || _        | j                                        }|dz
  | _        | j         j        j        d         |z
  | _	        | j        | j         _        | j	        | j         _	        | j
        }| j                             |          x| _        } t                      j        ||||fi | d S )Nr   r   )r1   paramsnormalized_cov_paramsscaleedfsumdf_modelendogr/   df_residfittedvaluesestimate_scalesuper__init__)	selfr1   r<   r=   r>   kwdsr?   mu	__class__s	           r6   rG   zGLMGamResults.__init__s   s     
%:"
hllnna
(.q1C7 #m
"m
!Z66r:::
U60%	
 	
;?	
 	
 	
 	
 	
r8   NTc                 v   |t          j        |          }d}|du r!||}n||}nt          j        ||f          }ny|8t          | j        d          r#t          | j        || j        j                  \  }}|;| j        j                            |          }||}nt          j        ||f          }n|}||fS )a  Transform original explanatory variables for prediction

        Parameters
        ----------
        exog : array_like, optional
            The values for the linear explanatory variables.
        exog_smooth : array_like
            values for the variables in the smooth terms
        transform : bool, optional
            If transform is False, then ``exog`` is returned unchanged and
            ``x`` is ignored. It is assumed that exog contains the full
            design matrix for the predict observations.
            If transform is True, then the basis representation of the smooth
            term will be constructed from the provided ``x``.

        Returns
        -------
        exog_transformed : ndarray
            design matrix for the prediction
        NFdesign_info_linear)	r+   r,   column_stackr!   r1   r7   rM   smoother	transform)rH   r.   exog_smoothrP   r3   ex	ex_smooths          r6   _tranform_predict_exogz$GLMGamResults._tranform_predict_exog   s    , "*[11K
"<$BB$)<==BB GDJ8L$M$M#:Jdj&C$E $E j & J/99+FF	<"BB $	):;;BB:~r8   c                    |                      |||          \  }} t                      j        |fddi|}|Gt          |d          s7|j        dk    rt          j        ||          S t          j        ||          S |S )a  "
        compute prediction

        Parameters
        ----------
        exog : array_like, optional
            The values for the linear explanatory variables
        exog_smooth : array_like
            values for the variables in the smooth terms
        transform : bool, optional
            If transform is True, then the basis representation of the smooth
            term will be constructed from the provided ``exog``.
        kwargs :
            Some models can take additional arguments or keywords, see the
            predict method of the model for the details.

        Returns
        -------
        prediction : ndarray, pandas.Series or pandas.DataFrame
            predicted values
        r.   rQ   rP   rP   FNpredicted_valuesr   )r   )rT   rF   predictr!   r-   r   r    r$   )	rH   r.   rQ   rP   kwargsrR   r3   predict_resultsrK   s	           r6   rX   zGLMGamResults.predict   s    , 44$AL?H 5 J JJ *%''/"HHHHH!'!3+5 +5!#q((y
CCCC|O:FFFF""r8   c                 r    |                      |||          \  }} t                      j        |fddi|S )a  compute prediction results

        Parameters
        ----------
        exog : array_like, optional
            The values for which you want to predict.
        exog_smooth : array_like
            values for the variables in the smooth terms
        transform : bool, optional
            If transform is True, then the basis representation of the smooth
            term will be constructed from the provided ``x``.
        kwargs :
            Some models can take additional arguments or keywords, see the
            predict method of the model for the details.

        Returns
        -------
        prediction_results : generalized_linear_model.PredictionResults
            The prediction results instance contains prediction and prediction
            variance and can on demand calculate confidence intervals and
            summary tables for the prediction of the mean and of new
            observations.
        rV   rP   F)rT   rF   get_prediction)rH   r.   rQ   rP   rY   rR   r3   rK   s          r6   r\   zGLMGamResults.get_prediction   sS    2 44$AL?H 5 J JJ &uww%bDDEDVDDDr8   c                 :   |}| j         j        }|j        |         }| j         j        }|t	          j        |          d         z   }|j        dd|f         }| j         j        j        }	|r/|	-t	          j	        |	g|f          }| j         j
        dd|f         }t	          j        || j        |                   }
|                     |          }|}|t	          j        ||j                  j        z                      d          }t	          j        |          }|
|fS )aN  contribution of a smooth term to the linear prediction

        Warning: This will be replaced by a predict method

        Parameters
        ----------
        smooth_index : int
            index of the smooth term within list of smooth terms
        include_constant : bool
            If true, then the estimated intercept is added to the prediction
            and its standard errors. This avoids that the confidence interval
            has zero width at the imposed identification constraint, e.g.
            either at a reference point or at the mean.

        Returns
        -------
        predicted : nd_array
            predicted value of linear term.
            This is not the expected response if the link function is not
            linear.
        se_pred : nd_array
            standard error of linear prediction
        r   N)columnr   )r1   rO   maskk_exog_linearr+   nonzerobasisr   	const_idxconcatenater.   dotr<   
cov_paramsr%   r@   sqrt)rH   smooth_indexinclude_constantvariablerO   r_   	start_idxidx	exog_partrc   linpredpartial_cov_paramscovbvarses                  r6   partial_valueszGLMGamResults.partial_values  s   0  :&}X&J,	"*T**1-- N111d7+	JO-	 	0	 5.9+s!344C
3/I&DK$455!__C_88!26$	4466;;A>>WS\\{r8   Fc                 f   ddl m}m}  |             |}|                     ||          \  }	}
| j        j        }|j        |         j        }t          j	        |          }||         }|	|         }	|
|         }
 ||          \  }}|r*| j
        |         }|	|z   }|                    ||d           |                    ||	dd           |r>|                    ||	d	|
z  z   d
d           |                    ||	d	|
z  z
  d
d           |                    |j        |         j                   |S )aP  plot the contribution of a smooth term to the linear prediction

        Parameters
        ----------
        smooth_index : int
            index of the smooth term within list of smooth terms
        plot_se : bool
            If plot_se is true, then the confidence interval for the linear
            prediction will be added to the plot.
        cpr : bool
            If cpr (component plus residual) is true, then a scatter plot of
            the partial working residuals will be added to the plot.
        include_constant : bool
            If true, then the estimated intercept is added to the prediction
            and its standard errors. This avoids that the confidence interval
            has zero width at the imposed identification constraint, e.g.
            either at a reference point or at the mean.
        ax : None or matplotlib axis instance
           If ax is not None, then the plot will be added to it.

        Returns
        -------
        Figure
            If `ax` is None, the created figure. Otherwise, the Figure to which
            `ax` is connected.
        r   )_import_mplcreate_mpl_ax)ri      )sblue   )clwg\(\?-)r{   )statsmodels.graphics.utilsru   rv   rs   r1   rO   	smoothersxr+   argsortresid_workingscatterplot
set_xlabelvariable_name)rH   rh   plot_secprri   axru   rv   rj   y_estrr   rO   r   
sort_indexfigresidualcpr_s                    r6   plot_partialzGLMGamResults.plot_partial2  sk   8 	JIIIIIII''9I ( K K	r:&x(*Z]]
jMj!
^-##R 	% )*5H8#DJJq$!J$$$
5Fq))) 	9GGAutby(#G888GGAutby(#G888
h(2@AAA
r8   c                    |}| j         j        }| j         j        }t          | j                  }|j        |         }|                                }|t          j        |          d         d         z   }t          j	        |||          }	| j
        |||z                                            }
|                     |	|
          S )a  hypothesis test that a smooth component is zero.

        This calls `wald_test` to compute the hypothesis test, but uses
        effective degrees of freedom.

        Parameters
        ----------
        smooth_index : int
            index of the smooth term within list of smooth terms

        Returns
        -------
        wald_test : ContrastResults instance
            the results instance created by `wald_test`
        r   )df_constraints)r1   rO   r`   r&   r<   r_   r@   r+   ra   eyer?   	wald_test)rH   rh   rj   rO   rk   k_paramsr_   k_constraintsrl   constraintsr   s              r6   test_significancezGLMGamResults.test_significancem  s    "  :&J,	t{##}X&

"*T**1-a00f]Hc::#s]':":;??AA~~k.~IIIr8   r   c                 4   | j                             | j        | j        |          }t	          j        |          dddf         | j         j        z  }| j        | j        z  }||                    |j	                  j	        z  
                    |          }|S )a  
        Compute the diagonal of the hat matrix

        Parameters
        ----------
        observed : bool
            If true, then observed hessian is used in the hat matrix
            computation. If false, then the expected hessian is used.
            In the case of a canonical link function both are the same.
            This is only relevant for models that implement both observed
            and expected Hessian, which is currently only GLM. Other
            models only use the observed Hessian.
        _axis : int
            This is mainly for internal use. By default it returns the usual
            diagonal of the hat matrix. If _axis is zero, then the result
            corresponds to the effective degrees of freedom, ``edf`` for each
            column of exog.

        Returns
        -------
        hat_matrix_diag : ndarray
            The diagonal of the hat matrix computed from the observed
            or expected hessian.
        )r>   observedN)axis)r1   hessian_factorr<   r>   r+   rg   r.   r=   re   r%   r@   )rH   r   _axisweightswexoghess_invhds          r6   get_hat_matrix_diagz!GLMGamResults.get_hat_matrix_diag  s    2 *++DKtz5= , ? ?  D)DJO; -
: hll57++--222>>	r8   c                 .    |                      d          S )Nr   )r   r   rH   s    r6   r?   zGLMGamResults.edf  s    ''a'000r8   c                 4    | j                                         S N)hat_matrix_diagr@   r   s    r6   hat_matrix_tracezGLMGamResults.hat_matrix_trace  s    #'')))r8   c                 .    |                      d          S )NT)r   r   r   s    r6   r   zGLMGamResults.hat_matrix_diag  s    '''666r8   c                 <    | j         d| j        | j        z  z
  dz  z  S N      ?rz   )r>   r   nobsr   s    r6   gcvzGLMGamResults.gcv  s#    zR$"7$)"CCaGGGr8   c                 h    | j         d| j        z
  z  dz                                  }|| j        z  }|S r   )resid_pearsonr   r@   r   )rH   cv_s     r6   cvzGLMGamResults.cv  s9    "b4+?&?@1DIIKKty
r8   )NNT)T)TFTN)Tr   )__name__
__module____qualname____doc__rG   rT   rX   r\   rs   r   r   r   r   r?   r   r   r   r   __classcell__rK   s   @r6   r:   r:   S   s        >
 
 
 
 
. =A)-4 4 4 4l!# !# !# !# !# !#FE E E E E E</ / / /b <A/39 9 9 9vJ J J>& & & &P 1 1 ^1 * * ^* 7 7 ^7 H H ^H   ^    r8   r:   c                       e Zd ZdS )GLMGamResultsWrapperN)r   r   r    r8   r6   r   r     s        Dr8   r   c                   p     e Zd ZdZeZeZ	 	 d fd	Zd Z		 	 	 d fd	Z
	 	 	 ddZ	 	 	 ddZ	 	 ddZ xZS )GLMGama  
    Generalized Additive Models (GAM)

    This inherits from `GLM`.

    Warning: Not all inherited methods might take correctly account of the
    penalization. Not all options including offset and exposure have been
    verified yet.

    Parameters
    ----------
    endog : array_like
        The response variable.
    exog : array_like or None
        This explanatory variables are treated as linear. The model in this
        case is a partial linear model.
    smoother : instance of additive smoother class
        Examples of smoother instances include Bsplines or CyclicCubicSplines.
    alpha : float or list of floats
        Penalization weights for smooth terms. The length of the list needs
        to be the same as the number of smooth terms in the ``smoother``.
    family : instance of GLM family
        See GLM.
    offset : None or array_like
        See GLM.
    exposure : None or array_like
        See GLM.
    missing : 'none'
        Missing value handling is not supported in this class.
    **kwargs
        Extra keywords are used in call to the super classes.

    Notes
    -----
    Status: experimental. This has full unit test coverage for the core
    results with Gaussian and Poisson (without offset and exposure). Other
    options and additional results might not be correctly supported yet.
    (Binomial with counts, i.e. with n_trials, is most likely wrong in pirls.
    User specified var or freq weights are most likely also not correct for
    all results.)
    Nr   nonec	           
         |	                     dd           }
d }t          |d          r|j        | _        | j        j        }t          |d           }|                     ||||
          | _        || j        j        }|| j        j	        }|j
        d         }nd }d}|| _        || _        || _        |j        | _        |                     |          | _        t%          || j        |          }|	                    dd            |t)          j        ||j        f          }n|j        }|g }|| j        j        z   }|r#|!t1          j        || j        j        |          } t7                      j        |f||||||d|	 |s|| j        d d <   t          | j        d          r| j        `t          | d	          r| j        | _         d | _        | `d S d S )
Nhasconstr   r   r   )alphark   penal)r   columns)r.   familyoffsetexposurer   missingformula)!getr!   r   rM   column_namesr   _handle_datadata_linearxnamesr.   r/   r`   exog_linearrO   k_variables	k_smooths_check_alphar   r   popr+   rN   rb   	col_namesr   r$   
row_labelsrF   rG   
exog_namesr   r   formula_linear)rH   rB   r.   rO   r   r   r   r   r   rY   r   xnames_linearr2   r   r`   r   r   rK   s                    r6   rG   zGLMGam.__init__   s>    ::j$//4'' 	A&*&6D# 3@M$T400	  ,,UD'8LL  ,3M*/K'-a0MMKM* ' !-&&u--
&xtz1>@ @ @

7D!!!"?K#@AADD>D
  M!88 	00<D,<,G(.0 0 0D 	 	AT& &$g	A 	A9?	A 	A 	A  	(!'DOAAA 49m,, 	&	%4## 	"&,DDL	 	r8   c                     t          |t                    s|gt          | j        j                  z  }n$t          |t
                    st          |          }|S )aB  check and convert alpha to required list format

        Parameters
        ----------
        alpha : scalar, list or array_like
            penalization weight

        Returns
        -------
        alpha : list
            penalization weight, list with length equal to the number of
            smooth terms
        )r   r   r&   rO   r   list)rH   r   s     r6   r   zGLMGam._check_alphaG  sS     %** 	 Gc$-"9:::EEE4(( 	 KKEr8     pirls:0yE>	nonrobustTF   c                 H   t          | d          r| j        | _        | `|                                dv r | j        | j        f|||||||d|}nP|dk    r%|# | j        | j        f|||||||d|}|j        }~ t                      j        d|||||||||	|
dd|}|S )a  estimate parameters and create instance of GLMGamResults class

        Parameters
        ----------
        most parameters are the same as for GLM
        method : optimization method
            The special optimization method is "pirls" which uses a penalized
            version of IRLS. Other methods are gradient optimizers as used in
            base.model.LikelihoodModel.

        Returns
        -------
        res : instance of wrapped GLMGamResults
        r   )r   irls)start_paramsmaxitertolr>   cov_typecov_kwdsuse_tr   N)r   r   methodr   r>   r   r   r   full_outputdispmax_start_irlsr   )	r!   r   r   lower
_fit_pirlsr   r<   rF   fit)rH   r   r   r   r   r>   r   r   r   r   r   r   rY   resrK   s                 r6   r   z
GLMGam.fit\  s#   ( 4## 	"&,D
 <<>>...!$/$* 9<*1s%+3h(-9 9 289 9CC
 !!|';%dodj =|.<#,1/7(,1	= = 6<	= =
  #z%''+ (<&-f"%U'/($)*5D-.( ( !'( (C 
r8   d   c
           	      v   | j         }
| j        }| j                            |          }|j        \  }}|	t          j        dg|z            | _        n|	| _        t          | d          sd| _	        || _
        d| _        |5| j                            |
          }| j                            |          }n7t          j        ||          | j	        z   }| j                            |          }| j                            |
|          }t%          d|gt
          j        |g          }d}|d	         }|dk    rL| j                            |          }|                     |          | _        t+          j        | |d          }d}t/          |          D ]4}| j        | j                            |          z  | _        || j        j                            |          |
|z
  z  z   | j	        z
  }t7          |||| j                  }t          j        ||j                                                  }|| j	        z  }| j                            |          }|                     |||          }|
                                j         dk    r)t          j!        ||
z
  d          rd
}tE          |          tG          |||d          }|r n6|| _$        |                     |          | _        tK          | |j        |j&        | j        |||          }d|_'        |dz   |d<   ||_(        ||_)        tU          |          S )z:fit model with penalized reweighted least squares
        r   Nr   _offset_exposurer   r   )r<   devianceFr   z2Perfect separation detected, results not available)r   r   r   PIRLS	iteration)+rB   r.   r   penalty_matrixr/   r+   arraydata_weightsr!   r   	scaletyper>   r   starting_murX   re   fittedr   r'   infrE   lmRegressionResultsranger   linkderivpenalized_wlsr<   ravel_update_historysqueezer-   allcloser
   r	   rJ   r:   r=   r   fit_history	convergedr   )rH   r   r   r   r   r>   r   r   r   r   rB   wlsexogspl_sr   	n_columnsrJ   lin_preddevhistoryr  	criterionwls_resultsr   wlsendogmsgglm_resultss                             r6   r   zGLMGam._fit_pirls  s;    
)
)))66!-i ? "" 5 5D 'Dt/00 	&$%D! 
((//B{**2..HHvg|44t7LLH##H--Bk""5"--t\2bfc]KKK	J'	 a<<##H--B,,R00DJ.t\4HHKIw 	 	I  ,t{/B/B2/F/FFDL !4;#3#9#9"#=#=#LL/0H ('5$,OOKvg{'9::@@BBH--H##H--B
 **;GDDG}}#q((R[eQ-G-G(J,S111 +9iaHHI ((,,
#D+*<$/$E$(J-5*/	1 1 1 %(1}") )#K000r8   aicbasinhoppingc                      j         } j        }t          j         j                  }|t	          j         j                  }nt	          j        d|z             }i g d<   |gd<   g d<    fd}	|dk    rCt          dd	d
          }
|
	                    |           t          j        |	|fi |
}|d         }n}|dk    rGt          dddddd          }
|
	                    |           t          j        |	|fi |
}|j        }n0|dk    rt          j        |	|fi |}|j        }nt          d          d         d= t	          j        |          }| _         | _        | _        ||fS )a	  find alpha by minimizing results criterion

        The objective for the minimization can be results attributes like
        ``gcv``, ``aic`` or ``bic`` where the latter are based on effective
        degrees of freedom.

        Warning: In many case the optimization might converge to a local
        optimum or near optimum. Different start_params or using a global
        optimizer is recommended, default is basinhopping.

        Parameters
        ----------
        criterion='aic'
            name of results attribute to be minimized.
            Default is 'aic', other options are 'gcv', 'cv' or 'bic'.
        start_params : None or array
            starting parameters for alpha in the penalization weight
            minimization. The parameters are internally exponentiated and
            the minimization is with respect to ``exp(alpha)``
        start_model_params : None or array
            starting parameter for the ``model._fit_pirls``.
        method : 'basinhopping', 'nm' or 'minimize'
            'basinhopping' and 'nm' directly use the underlying scipy.optimize
            functions `basinhopping` and `fmin`. 'minimize' provides access
            to the high level interface, `scipy.optimize.minimize`.
        fit_kwds : keyword arguments
            additional keyword arguments will be used in the call to the
            scipy optimizer. Which keywords are supported depends on the
            scipy optimization function.

        Returns
        -------
        alpha : ndarray
            penalization parameter found by minimizing the criterion.
            Note that this can be only a local (near) optimum.
        fit_res : tuple
            results returned by the scipy optimization routine. The
            parameters in the optimization problem are `log(alpha)`
        history : dict
            history of calls to pirls and contains alpha, the fit
            criterion and the parameters to which pirls converged to for the
            given alpha.

        Notes
        -----
        In the test cases Nelder-Mead and bfgs often converge to local optima,
        see also https://github.com/statsmodels/statsmodels/issues/5381.

        This does not use any analytical derivatives for the criterion
        minimization.

        Status: experimental, It is possible that defaults change if there
        is a better way to find a global optimum. API (e.g. type of return)
        might also change.
        Ng#B;r   r<   r  c                 ,   t          j        |           }                    d         d         |          }d                             |           d                             t          j        |j                             t          |          S )Nr<   )r   r   r   )r+   expr   appendr,   r<   r   )pares_r  r  rH   s      r6   funz$GLMGam.select_penweight.<locals>.fun=  s    q		A??0A"0E)* # , ,DG##A&&&H$$RZ%<%<===4+++r8   nmTr   i  )r   r   maxfunr   r  zNelder-Meadr   i  )r   maxfev)r   options
   )minimizer_kwargsniterminimizezmethod not recognized)r>   r   copyr   r+   zerosr   logr'   updater   fminr  r   r&  
ValueErrorr  )rH   r  r   start_model_paramsr   fit_kwds
scale_keepscaletype_keep
alpha_keepr  rI   fit_resoptr   r  s   ``            @r6   select_penweightzGLMGam.select_penweight  s   v Z
Ytz**
8DN33LL6%,"677L/0!	, 	, 	, 	, 	, 	, 	, T>>D$tDDDDKK!!!mC>>>>G!*CC~%%M/2c#B#B*D *D " " "D KK!!!+CFFFFG)CCz!!'\FFXFFG)CC4555Ha s  
'
gw&&r8         c           	         |d }| fdt          | j                  D             }|t          |d          }t          | j        |t
          || j        | j        |          }|                                }|j	        |fS )ao  find alphas by k-fold cross-validation

        Warning: This estimates ``k_folds`` models for each point in the
            grid of alphas.

        Parameters
        ----------
        alphas : None or list of arrays
        cv_iterator : instance
            instance of a cross-validation iterator, by default this is a
            KFold instance
        cost : function
            default is mean squared error. The cost function to evaluate the
            prediction error for the left out sample. This should take two
            arrays as argument and return one float.
        k_folds : int
            number of folds if default Kfold iterator is used.
            This is ignored if ``cv_iterator`` is not None.

        Returns
        -------
        alpha_cv : list of float
            Best alpha in grid according to cross-validation
        res_cv : instance of MultivariateGAMCVPath
            The instance was used for cross-validation and holds the results

        Notes
        -----
        The default alphas are defined as
        ``alphas = [np.logspace(0, 7, k_grid) for _ in range(k_smooths)]``
        Nc                 f    t           j                            | |z
            t          |           z  S r   )r+   linalgnormr&   )x1x2s     r6   costz+GLMGam.select_penweight_kfold.<locals>.cost  s%    y~~b2g..R88r8   c                 <    g | ]}t          j        d d          S )r      )r+   logspace).0_k_grids     r6   
<listcomp>z1GLMGam.select_penweight_kfold.<locals>.<listcomp>  s'    OOOAbk!Q//OOOr8   T)k_foldsshuffle)rO   alphasgamr=  rB   r.   cv_iterator)
r   r   r   r   rO   r   rB   r   r   alpha_cv)rH   rG  rI  r=  rE  rC  gam_cv
gam_cv_ress        `  r6   select_penweight_kfoldzGLMGam.select_penweight_kfoldb  s    D <9 9 9 >OOOOt~9N9NOOOF>>>K&f+1DJ,0,<3>@ @ @ ZZ\\
"J..r8   )NNr   NNNr   )Nr   r   r   Nr   NNTFr   )Nr   r   Nr   NNN)r  NNr  )NNNr5  r6  )r   r   r   r   r:   _results_classr   _results_class_wrapperrG   r   r   r   r4  rM  r   r   s   @r6   r   r     s       ( (T #N1HL5;E E E E E EN  * HLCG9:1 1 1 1 1 1h EIJNa1 a1 a1 a1F >B,0 .m' m' m' m'^ JN132/ 2/ 2/ 2/ 2/ 2/ 2/ 2/r8   r   c                   "     e Zd ZdZ fdZ xZS )LogitGamzGeneralized Additive model for discrete Logit

    This subclasses discrete_model Logit.

    Warning: not all inherited methods might take correctly account of the
    penalization

    not verified yet.
    c                    t          |t                    s*t          j        |gt	          |j                  z            }|| _        || _        d| _        t          ||          } t                      j        ||j        g|R d|i| d S )Nr   r   r   )r   r   r+   r   r&   r   rO   r   
pen_weightr   rF   rG   rb   )rH   rB   rO   r   argsrY   r   rK   s          r6   rG   zLogitGam.__init__  s    %** 	@HeWs8+='>'>>??E 
&xu===MdMMMeMfMMMMMr8   )r   r   r   r   rG   r   r   s   @r6   rQ  rQ    sK         	N 	N 	N 	N 	N 	N 	N 	N 	Nr8   rQ  c                     | ||}}}t          ||d|z  |          \  }}}	t          j        |||	                                          }
|
j                                        |
_        |
S )a
  weighted least squares with quadratic penalty

    Parameters
    ----------
    endog : ndarray
        response or endogenous variable
    exog : ndarray
        design matrix, matrix of exogenous or explanatory variables
    penalty_matrix : ndarray, 2-Dim square
        penality matrix for quadratic penalization. Note, the penalty_matrix
        is multiplied by two to match non-pirls fitting methods.
    weights : ndarray
        weights for WLS

    Returns
    -------
    results : Results instance of WLS
    rz   )make_augmented_matrixr   WLSr   r<   r  )rB   r.   r   r   yr   rx   aug_yaug_xaug_weightsr  s              r6   r  r    sm    & T>!qA 5aAE7 K KE5+&{337799K %+1133Kr8   c                 F   | ||}}}|j         d         }t          |          }t          j        ||g          }	|	j         d         }
t          j        dg|
z            }||d|<   t          j        dg|j         d         z            }t          j        ||g          }||	|fS )aM  augment endog, exog and weights with stochastic restriction matrix

    Parameters
    ----------
    endog : ndarray
        response or endogenous variable
    exog : ndarray
        design matrix, matrix of exogenous or explanatory variables
    penalty_matrix : ndarray, 2-Dim square
        penality matrix for quadratic penalization
    weights : ndarray
        weights for WLS

    Returns
    -------
    endog_aug : ndarray
        augmented response variable
    exog_aug : ndarray
        augmented design matrix
    weights_aug : ndarray
        augmented weights for WLS
    r   g        Nr   )r/   r   r+   vstackr   rd   )rB   r.   r   r   rX  r   rx   r   rsr;  n_samp1es_x1y1id1w1s                 r6   rV  rV    s    . dN!qA71:D 
QB	Ar7		B8A;L	2$%	&	&BBuuI
(B4"(1+%
&
&C	#	'	'Br2:r8   r   )2r   collections.abcr   r'  numpyr+   scipyr   pandasr   statsmodels.base.wrapperbasewrapperwrap#statsmodels.discrete.discrete_modelr   +statsmodels.genmod.generalized_linear_modelr   r   r   r	   #statsmodels.regression.linear_model
regressionlinear_modelr   statsmodels.tools.sm_exceptionsr
   r   statsmodels.tools.decoratorsr   statsmodels.tools.datar   statsmodels.tools.linalgr   statsmodels.base._penalizedr   statsmodels.gam.gam_penaltiesr   9statsmodels.gam.gam_cross_validation.gam_cross_validationr   5statsmodels.gam.gam_cross_validation.cross_validatorsr   r7   r:   r   populate_wrapperr   rQ  r  rV  r   r8   r6   <module>ry     s    % $ $ $ $ $                ' ' ' ' ' ' ' ' ' 5 5 5 5 5 5< < < < < < < < < < < < 0 0 0 0 0 0 0 0 0; ; ; ; ; ; ; ; 7 7 7 7 7 7 3 3 3 3 3 3 0 0 0 0 0 0 6 6 6 6 6 6 @ @ @ @ @ @      G G G G G G+ + + +\u u u u uJ u u up	 	 	 	 	, 	 	 	  *M : : :B/ B/ B/ B/ B/^S B/ B/ B/JN N N N N~u N N N,  B& & & & &r8   