Ë
    ¢�Dj…3  ã                   ó¾   — d dl Zd dlmZ d dlmc mZ d dlm	Z	 	 d„ Z
	 	 	 	 dd„Z	 dd„Z G d„ de«      Z G d	„ d
ej                  «      Z ej                   ee«       y)é    N)ÚResults)Úcache_readonlyc                 óH   ‡ ‡‡‡‡‡— ˆˆˆ ˆfd„}ˆˆˆ ˆfd„}ˆˆˆˆ fd„}|||fS )ao  
    Negative penalized log-likelihood functions.

    Returns the negative penalized log-likelihood, its derivative, and
    its Hessian.  The penalty only includes the smooth (L2) term.

    All three functions have argument signature (x, model), where
    ``x`` is a point in the parameter space and ``model`` is an
    arbitrary statsmodels regression model.
    c                 óÄ   •— |j                   }‰‰   d‰z
  z  t        j                  | dz  «      z  dz  } |j                  t        j                  |    fi ‰¤Ž}| |z  |z   S )Né   é   )ÚnobsÚnpÚsumÚloglikeÚr_)	ÚparamsÚmodelr	   Úpen_llfÚllfÚL1_wtÚalphaÚkÚloglike_kwdss	        €€€€ú`C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/base/elastic_net.pyÚ	nploglikez_gen_npfuncs.<locals>.nploglike)   sc   ø€ Ø�z‰zˆØ˜‘(˜a %™iÑ(¬2¯6©6°&¸!±)Ó+<Ñ<¸qÑ@ˆØˆe�m‰mœBŸE™E &™MÑ:¨\Ñ:ˆØˆu�t‰|˜gÑ%Ð%ó    c                 ó˜   •— |j                   }‰‰   d‰z
  z  | z  } |j                  t        j                  |    fi ‰¤Žd    |z  }||z   S )Nr   r   )r	   Úscorer
   r   )	r   r   r	   Úpen_gradÚgrr   r   r   Ú
score_kwdss	        €€€€r   Únpscorez_gen_npfuncs.<locals>.npscore/   sW   ø€ Ø�z‰zˆØ˜‘8˜q 5™yÑ)¨FÑ2ˆØˆe�k‰kœ"Ÿ%™% ™-Ñ6¨:Ñ6°qÑ9Ð9¸DÑ@ˆØ�H‰}Ðr   c                 ó’   •— |j                   }‰‰   d‰z
  z  } |j                  t        j                  |    fi ‰¤Žd    |z  |z   }|S )Nr   )r   r   )r	   Úhessianr
   r   )	r   r   r	   Úpen_hessÚhr   r   Ú	hess_kwdsr   s	        €€€€r   Únphessz_gen_npfuncs.<locals>.nphess5   sR   ø€ Ø�z‰zˆØ˜‘8˜q 5™yÑ)ˆØˆU�]‰]œ2Ÿ5™5 ™=Ñ6¨IÑ6°tÑ<Ð<¸tÑCÀhÑNˆØˆr   © )	r   r   r   r   r   r#   r   r   r$   s	   ``````   r   Ú_gen_npfuncsr&      s!   ý€ ÷&÷÷ð �g˜vÐ%Ð%r   c                 ón	  — | j                   j                  d   }|
€i n|
}
|€i n|}|€i n|}t        j                  |«      r|t        j                  |«      z  }|€t        j
                  |«      }n|j                  «       }d}t        j
                  t        |«      t        ¬«      }| j                  «       }d|d<   |j                  dd«      }d|v rS|d   �N|€%t        j                  |j                  d«      «      }n'|t        j                  |j                  d«      «      z  }t        |«      D �cg c]  }t        ||||
||«      ‘Œ }}d}t        |«      D �]  }|j                  «       }t        |«      D ]É  }||   rŒ	|j                  «       }d	||<   t        j                  | j                   |«      }|�||z  } | j                  | j                   | j                   dd…|f   fd|i|¤Ž}||   \  }}}t#        ||||||   ||   |z  ||	¬
«      ||<   |d	kD  sŒ¤t        j$                  ||   «      |k  sŒÀd||<   d||<   ŒË t        j&                  t        j$                  ||z
  «      «      }||k  s�Œd} n d	|t        j$                  |«      |k  <   |st)        | |«      }||_        t-        |«      S t        j.                  |«      } t        j
                  ||f«      }!| j0                  D �ci c]  }|t3        | |d«      “Œ }}t        | «      d	kD  rr | j                  | j                   | j                   dd…| f   fi |¤Ž}"|"j5                  «       }#|#j6                  || <   |#j8                  |!t        j:                  | | «      <   nA | j                  | j                   | j                   dd…d	f   fi |¤Ž}"|"j5                  d	¬«      }#t=        |#j                  t>        j@                  «      r|#jB                  j                  }$n|#j                  }$tE        |#d«      r|#jF                  }%nd}%| jH                  | jJ                  }'}&t        | «      | _$        | jL                  | jH                  z
  | _%         |$| ||!|%¬«      }d|_'        ||_        ||_(        ddz   i|_)        |&|'c| _$        | _%        |S c c}w c c}w )a—	  
    Return an elastic net regularized fit to a regression model.

    Parameters
    ----------
    model : model object
        A statsmodels object implementing ``loglike``, ``score``, and
        ``hessian``.
    method : {'coord_descent'}
        Only the coordinate descent algorithm is implemented.
    maxiter : int
        The maximum number of iteration cycles (an iteration cycle
        involves running coordinate descent on all variables).
    alpha : scalar or array_like
        The penalty weight.  If a scalar, the same penalty weight
        applies to all variables in the model.  If a vector, it
        must have the same length as `params`, and contains a
        penalty weight for each coefficient.
    L1_wt : scalar
        The fraction of the penalty given to the L1 penalty term.
        Must be between 0 and 1 (inclusive).  If 0, the fit is
        a ridge fit, if 1 it is a lasso fit.
    start_params : array_like
        Starting values for `params`.
    cnvrg_tol : scalar
        If `params` changes by less than this amount (in sup-norm)
        in one iteration cycle, the algorithm terminates with
        convergence.
    zero_tol : scalar
        Any estimated coefficient smaller than this value is
        replaced with zero.
    refit : bool
        If True, the model is refit using only the variables that have
        non-zero coefficients in the regularized fit.  The refitted
        model is not regularized.
    check_step : bool
        If True, confirm that the first step is an improvement and search
        further if it is not.
    loglike_kwds : dict-like or None
        Keyword arguments for the log-likelihood function.
    score_kwds : dict-like or None
        Keyword arguments for the score function.
    hess_kwds : dict-like or None
        Keyword arguments for the Hessian function.

    Returns
    -------
    Results
        A results object.

    Notes
    -----
    The ``elastic net`` penalty is a combination of L1 and L2
    penalties.

    The function that is minimized is:

    -loglike/n + alpha*((1-L1_wt)*|params|_2^2/2 + L1_wt*|params|_1)

    where |*|_1 and |*|_2 are the L1 and L2 norms.

    The computational approach used here is to obtain a quadratic
    approximation to the smooth part of the target function:

    -loglike/n + alpha*(1-L1_wt)*|params|_2^2/2

    then repeatedly optimize the L1 penalized version of this function
    along coordinate axes.
    r   Ng-Cëâ6?)ÚdtypeFÚhasconstÚoffsetÚexposurer   )ÚtolÚ
check_stepTç        )ÚmaxiterÚscaleç      ð?)r0   Ú	iteration)*ÚexogÚshaper
   ÚisscalarÚonesÚzerosÚcopyÚlenÚboolÚ_get_init_kwdsÚpopÚlogÚranger&   ÚdotÚ	__class__ÚendogÚ_opt_1dÚabsÚmaxÚRegularizedResultsÚ	convergedÚRegularizedResultsWrapperÚflatnonzeroÚ
_init_keysÚgetattrÚfitr   Únormalized_cov_paramsÚix_Ú
issubclassÚwrapÚResultsWrapperÚ_resultsÚhasattrr0   Údf_modelÚdf_residr	   ÚregularizedÚmethodÚfit_history)(r   rV   r/   r   r   Ústart_paramsÚ	cnvrg_tolÚzero_tolÚrefitr-   r   r   r#   Úk_exogr   ÚbtolÚparams_zeroÚ	init_argsÚmodel_offsetr   Úfgh_listrF   ÚitrÚparams_saveÚparams0r*   Ú
model_1varÚfuncÚgradÚhessÚpchangeÚresultsÚiiÚcovÚmodel1ÚrsltÚklassr0   ÚpÚqs(                                           r   Úfit_elasticnetrr   >   s—  € ðT �Z‰Z×Ñ˜aÑ €Fà%Ð-‘2°<€LØ!Ð)‘¨z€JØÐ'‘¨Y€Iä	‡{�{�5ÔØœŸ™ ›Ñ'ˆð ÐÜ—‘˜&Ó!‰à×"Ñ"Ó$ˆà€DÜ—(‘(œ3˜v›;¬dÔ3€Kà×$Ñ$Ó&€Ià!€IˆjÑØ—=‘= ¨4Ó0€LØ�YÑ 9¨ZÑ#8Ð#DØÐÜŸ6™6 )§-¡-°
Ó";Ó<‰LàœBŸF™F 9§=¡=°Ó#<Ó=Ñ=ˆLô �v“ö àô 	�Q˜˜u l°JÀ	ÕJð €Hð  ð €Iä�W‹~ó (ˆð —k‘k“mˆÜ�v“ò 	ˆAð
 ˜1Š~Øð
 —k‘k“mˆGØˆG�A‰JÜ—V‘V˜EŸJ™J¨Ó0ˆFØÐ'Ø˜,Ñ&�ð )˜Ÿ™Ø—‘˜UŸZ™Zª¨1¨Ñ-ñKØ6<ðKØ@IñKˆJð  (¨™{ÑˆD�$˜ÜØ�d˜D *¨f°Q©i¸¸q¹À%¹Ø Zô1ˆF�1‰Ið
 �Q‹wœ2Ÿ6™6 &¨¡)Ó,¨xÓ7Ø!%�˜A‘Ø��q’	ð=	ôB —&‘&œŸ™ ¨Ñ 4Ó5Ó6ˆØ�YÔØˆIÙðQ(ðV )*€FŒ2�6‰6�&‹>˜HÑ$Ñ%áÜ$ U¨FÓ3ˆØ%ˆÔÜ(¨Ó1Ð1ô 
�‰˜Ó	€BÜ
�(‰(�F˜FÐ#Ó
$€CØ5:×5EÑ5EÖF°�”G˜E 1 dÓ+Ñ+ÐF€IÐFÜ
ˆ2ƒw�‚{Ø �—‘Ø�K‰K˜Ÿ™¢A r EÑ*ñ9Ø.7ñ9ˆà�z‰z‹|ˆØ—[‘[ˆˆr‰
Ø"×8Ñ8ˆŒB�F‰F�2�r‹NÒð
 !�—‘ §¡¨e¯j©jº¸A¸Ñ.>ÑLÀ)ÑLˆØ�z‰z !ˆzÓ$ˆô �$—.‘.¤$×"5Ñ"5Ô6Ø—‘×'Ñ'‰à—‘ˆô ˆt�WÔØ—
‘
‰àˆð �>‰>˜5Ÿ>™>€q€AÜ˜“W€E„NØ—Z‘Z %§.¡.Ñ0€E„Nñ �%˜ ¨EÔ2€EØ€EÔØ€E„OØ€E„LØ$ c¨A¡gÐ.€EÔð &'¨Ð"€E„N�E”Nà€LùòS ùòx Gs   Ä2R-Ë/R2c                 ó¼  — |} | ||«      }	 |||«      }
 |||«      }|
||z  z
  }|t        j                  |«      kD  ry|dk\  r	||
z
  |z  }n|dk  r
||
z    |z  }nt         j                  S |s||z   S  | ||z   |«      |t        j                  ||z   «      z  z   }||	|t        j                  |«      z  z   dz   k  r||z   S ddlm}  || |f|dz
  |dz   f|¬«      }|S )az  
    One-dimensional helper for elastic net.

    Parameters
    ----------
    func : function
        A smooth function of a single variable to be optimized
        with L1 penaty.
    grad : function
        The gradient of `func`.
    hess : function
        The Hessian of `func`.
    model : statsmodels model
        The model being fit.
    start : real
        A starting value for the function argument
    L1_wt : non-negative real
        The weight for the L1 penalty function.
    tol : non-negative real
        A convergence threshold.
    check_step : bool
        If True, check that the first step is an improvement and
        use bisection if it is not.  If False, return after the
        first step regardless.

    Notes
    -----
    ``func``, ``grad``, and ``hess`` have argument signature (x,
    model), where ``x`` is a point in the parameter space and
    ``model`` is the model being fit.

    If the log-likelihood for the model is exactly quadratic, the
    global minimum is returned in one step.  Otherwise numerical
    bisection is used.

    Returns
    -------
    The argmin of the objective function.
    r.   r   g»½×Ùß|Û=)Úbrentr   )ÚargsÚbrackr,   )r
   rC   ÚnanÚscipy.optimizert   )rf   rg   rh   r   Ústartr   r,   r-   ÚxÚfÚbÚcÚdr"   Úf1rt   Úx_opts                    r   rB   rB     s  € ðj 	€AÙˆQ�‹€AÙˆQ�‹€AÙˆQ�‹€AØ	ˆAˆa‰C‰€Að Œr�v‰v�a‹yÒØð 	ˆA‚vØ�Q‰Y˜!‰O‰Ø	
ˆQŠØ�a‰iˆL˜1Ñ‰ä�v‰vˆñ
 Ø�1‰uˆÙ	ˆa�!‰e�UÓ	˜e¤B§F¡F¨1¨q©5£MÑ1Ñ	1€BØ	ˆQ�”r—v‘v˜a“y‘Ñ  5Ñ(Ò(Ø�1‰uˆõ %Ù�$˜e˜X¨a°©c°1°Q±3¨Z¸SÔA€EØ€Lr   c                   ó2   ‡ — e Zd ZdZˆ fd„Zed„ «       Zˆ xZS )rE   zí
    Results for models estimated using regularization

    Parameters
    ----------
    model : Model
        The model instance used to estimate the parameters.
    params : ndarray
        The estimated (regularized) parameters.
    c                 ó&   •— t         ‰| �  ||«       y )N)ÚsuperÚ__init__)Úselfr   r   r@   s      €r   r„   zRegularizedResults.__init__q  s   ø€ Ü‰Ñ˜ Õ'r   c                 óL   — | j                   j                  | j                  «      S )zR
        The predicted values from the model at the estimated parameters.
        )r   Úpredictr   )r…   s    r   ÚfittedvalueszRegularizedResults.fittedvaluest  s   € ð
 �z‰z×!Ñ! $§+¡+Ó.Ð.r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r„   r   rˆ   Ú__classcell__)r@   s   @r   rE   rE   f  s!   ø„ ñ	ô(ð ñ/ó ô/r   rE   c                   ó   — e Zd ZddddœZeZy)rG   ÚcolumnsÚrows)r   Úresidrˆ   N)r‰   rŠ   r‹   Ú_attrsÚ_wrap_attrsr%   r   r   rG   rG   |  s   „ àØØñ€Fð
 �Kr   rG   )Úcoord_descentéd   r.   r1   NgH¯¼šò×z>g:Œ0âŽyE>FTNNN)T)Únumpyr
   Ústatsmodels.base.modelr   Ústatsmodels.base.wrapperÚbaseÚwrapperrO   Ústatsmodels.tools.decoratorsr   r&   rr   rB   rE   rP   rG   Úpopulate_wrapperr%   r   r   ú<module>r�      sv   ðÛ Ý *ß 'Ð 'Ý 7ðò.&ðB ;>ØDHØ:>ØAEóOðf óSôl/˜ô /ô, × 3Ñ 3ô ð €× Ñ Ð/Ø(õ*r   