
    M/Ph#                     H    d Z ddlZddlmc mZ 	 	 d
dZd Zd Zd Z	d	 Z
dS )zE
Holds files for l1 regularization of LikelihoodModel, using cvxopt.
    NFd   c           
         
!"#$%& ddl m}m} t          j        |                              d          }t          |          %t          j        |t          j        |                    & |&d%z  df          &t          j        |d                                       d          ##t          j	        %          z  ##
                                dk    sJ # %fd$#%fd!t          %          } |d	d%z  df          }
%fd
"d!"$&fd	}||j        d<   ||j        d<   d|v r|d         |j        d<   d|v r|d         |j        d<   d|v r|d         |j        d<   d|v r|d         |j        d<   |                    |||          }t          j        |d                                                   }|d%         }|d         }|d         }t          j        |#||          }|d         }|d         }|d         }t          j        |%#||||          \  }}|	rW $|          }t%          d          }t%          d          }t%          d          }|d         dk    }|d         }|||||||d} n6t          j        |d                                                   }|d%         }|	r|| fS |S )a  
    Solve the l1 regularized problem using cvxopt.solvers.cp

    Specifically:  We convert the convex but non-smooth problem

    .. math:: \min_\beta f(\beta) + \sum_k\alpha_k |\beta_k|

    via the transformation to the smooth, convex, constrained problem in twice
    as many variables (adding the "added variables" :math:`u_k`)

    .. math:: \min_{\beta,u} f(\beta) + \sum_k\alpha_k u_k,

    subject to

    .. math:: -u_k \leq \beta_k \leq u_k.

    Parameters
    ----------
    All the usual parameters from LikelhoodModel.fit
    alpha : non-negative scalar or numpy array (same size as parameters)
        The weight multiplying the l1 penalty term
    trim_mode : 'auto, 'size', or 'off'
        If not 'off', trim (set to zero) parameters that would have been zero
            if the solver reached the theoretical minimum.
        If 'auto', trim params using the Theory above.
        If 'size', trim params if they have very small absolute value
    size_trim_tol : float or 'auto' (default = 'auto')
        For use when trim_mode === 'size'
    auto_trim_tol : float
        For sue when trim_mode == 'auto'.  Use
    qc_tol : float
        Print warning and do not allow auto trim when (ii) in "Theory" (above)
        is violated by this much.
    qc_verbose : bool
        If true, print out a full QC report upon failure
    abstol : float
        absolute accuracy (default: 1e-7).
    reltol : float
        relative accuracy (default: 1e-6).
    feastol : float
        tolerance for feasibility conditions (default: 1e-7).
    refinement : int
        number of iterative refinement steps when solving KKT equations
        (default: 1).
    r   )solversmatrixF      alpha_rescaledc                 $    t          | gR  S N)_objective_func)xalphaargsfk_paramss    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/base/l1_cvxopt.py<lambda>z"fit_l1_cvxopt_cp.<locals>.<lambda>I   s    OAq(EADAAA     c                 (    t          |           S r   )_fprime)r   r   r   scores    r   r   z"fit_l1_cvxopt_cp.<locals>.<lambda>J   s    75!Xu55 r   g        c                 (    t          | |          S r   )_hessian_wrapper)r   zhessr   s     r   r   z"fit_l1_cvxopt_cp.<locals>.<lambda>M   s    %dAq(;; r   Nc                     | dfS | |            |           fS  |            |            | |          fS )Nr    )r   r   DfHf_0x0s     r   r   zfit_l1_cvxopt_cp.<locals>.FP   sW    9b5LY3q6622a55= 3q6622a55!!Aq''))r   show_progressmaxitersabstolreltolfeastol
refinementr   qc_tol
qc_verbose	trim_modesize_trim_tolauto_trim_tolnanstatusoptimal)fopt	converged
iterationsgopthopttrimmedwarnflag)NN)cvxoptr   r   nparrayravellenappendfabsonesmin_get_Goptionscpasarrayl1_solvers_common
qc_resultsdo_trim_paramsfloat)'r   r   start_paramsr   kwargsdispmaxitercallbackretallfull_outputr   r   r   Ghr   resultsr   paramsr)   r*   passedr+   r,   r-   r6   r1   r4   r5   r3   r2   r7   retvalsr   r    r   r!   r   r"   s'   `` `      `                      @@@@@@r   fit_l1_cvxopt_cprV      sj   ` '&&&&&&&8L))//44L <  H	<!6!6	7	7B	Q\1%	&	&BHV,-..44S99EBGH%%%E99;;! B
A
A
A
A
A
AC	5	5	5	5	5	5BxAsQ\1%&&A;;;;;A* * * * * * * * * (,GOO$")GOJ6$*8$4!6$*8$4!F%+I%6	"v(.|(<% jjAq!!G

73<  &&((Ayy\F HF%J)ufj2 2F {#I?+M?+M'6%	= OFG  s1vvU||U||5\\
X&)3	8$y
$7 " "
 HWS\""((**9H9  wr   c                     ddl m} t          j        |          }|d|                                         }||d         } | |g|R  ||z                                  z   }	 ||	          S )z-
    The regularized objective function.
    r   r   N)r8   r   r9   rD   r;   sum)
r   r   r   r   r   r   x_arrrS   uobjective_func_arrs
             r   r   r      s     JqMME9H9##%%FhiiA6)D)))UQYOO,=,==6$%%%r   c                     ddl m} t          j        |          }|d|                                         }t          j         | |          |          } ||dd|z  f          S )z%
    The regularized derivative.
    r   rX   Nr	   r   )r8   r   r9   rD   r;   r=   )r   r   r   r   r   rZ   rS   
fprime_arrs           r   r   r      ss     JqMME9H9##%%F 55==%00J6*q!h,/000r   c                     ddl m} t          j        |           }t          j        | | fd          }t          j        || fd          }t          j        ||fd          } ||          S )z2
    The linear inequality constraint matrix.
    r   rX   r	   axis)r8   r   r9   eyeconcatenate)r   r   IABCs         r   rA   rA      s     
xA
QBxa(((A
A2wQ'''A
1vA&&&A6!99r   c                    ddl m} t          j        |          }|d|                                         }t          j        |d                    | |          z  }t          j        |j                  }t          j        ||fd          }	t          j        ||fd          }
t          j        |	|
fd          } ||d|z  d|z  f          S )z
    Wraps the hessian up in the form for cvxopt.

    cvxopt wants the hessian of the objective function and the constraints.
        Since our constraints are linear, this part is all zeros.
    r   rX   Nr	   r`   r   )r8   r   r9   rD   r;   zerosshaperc   )r   r   r   r   r   rZ   rS   zh_xzero_matre   rf   zh_x_exts               r   r   r      s     JqMME9H9##%%F:addd6ll*Dx
##H
h'a000A
(+!444A~q!f1---H6(Q\1x<8999r   )Fr   NFFN)__doc__numpyr9   "statsmodels.base.l1_solvers_commonbaserE   rV   r   r   rA   r   r   r   r   <module>rr      s         > > > > > > > > > CF=AD D D DN& & &1 1 1  : : : : :r   