
    M/Ph                     2    d Z ddlZddlmZ ddZd Zd ZdS )	z(
Holds common functions for l1 solvers.
    N)ConvergenceWarningFc                    t          j        |                                           rJ | |                     d          k                                    s
J d             ||           }t          |           }t          j        dg|z            }t          |          D ]>}||         dk    r0t          ||                   ||         z
  ||         z  |k    rd||<   ?t          ||| |          }	|                                }
|
sT| 
                                }d||fz  }|dz  }|r|t          |	          z  }dd	l}|                    |t                     |
S )
a  
    Theory dictates that one of two conditions holds:
        i) abs(score[i]) == alpha[i]  and  params[i] != 0
        ii) abs(score[i]) <= alpha[i]  and  params[i] == 0
    qc_results checks to see that (ii) holds, within qc_tol

    qc_results also checks for nan or results of the wrong shape.

    Parameters
    ----------
    params : ndarray
        model parameters.  Not including the added variables x_added.
    alpha : ndarray
        regularization coefficients
    score : function
        Gradient of unregularized objective function
    qc_tol : float
        Tolerance to hold conditions (i) and (ii) to for QC check.
    qc_verbose : bool
        If true, print out a full QC report upon failure

    Returns
    -------
    passed : bool
        True if QC check passed
    qc_dict : Dictionary
        Keys are fprime, alpha, params, passed_array

    Prints
    ------
    Warning message if QC check fails.
    Fz#params should have already been 1-dTr   F)fprimealphaparamspassed_arrayz1QC check did not pass for %d out of %d parametersz\
Try increasing solver accuracy or number of iterations, decreasing alpha, or switch solversN)npisnanmaxravelminlenarrayrangeabsdictsum_get_verbose_addonwarningswarnr   )r   r   scoreqc_tol
qc_verboser   k_paramsr	   iqc_dictpassed
num_failedmessager   s                 b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/base/l1_solvers_common.py
qc_resultsr"   
   s   D x##%%%%%fll3''',,.. . .-. . . U6]]F6{{H8TFX-..L8__ ( (8a<<F1Iq)U1X5>>"'QU6N N NGF 
3#m((**
EI" " 4 	4 	3)'222Gg1222M    c                     | d         }| d         }| d         }| d         }d}d}|ddz  z  }|d	z  }t          t          |                    D ]&}|d
||         ||         ||         ||         fz  z  }'|S )Nr   r   r   r	   z-
------ verbose QC printout -----------------z7
------ Recall the problem was rescaled by 1 / nobs ---z
|%-10s|%-10s|%-10s|%-10s|)r   r   r   paramz-
--------------------------------------------z 
|%-10s|%-10.3e|%-10.3e|%-10.3e|)r   r   )r   r   r   r   r	   addonr   s          r!   r   r   L   s    GEXFXF>*L<EFE	* .. . .E	==E3u:: A A4Qq6!9fQi8A A 	ALr#   c                    dg|z  }|dk    rt          j        dg|z            }n|dk    r<|s:ddl}	d}
|	                    |
t                     t          j        dg|z            }n|dk    ra|r_ ||           }t          |          D ]C}||         dk    r5||         t          ||                   z
  ||         z  |k    r
d| |<   d||<   DnZ|d	k    rBt          |          D ]1}||         dk    r#t          | |                   |k     r
d| |<   d||<   2nt          d
|z            | t          j        |          fS )a  
    Trims (set to zero) params that are zero at the theoretical minimum.
    Uses heuristics to account for the solver not actually finding the minimum.

    In all cases, if alpha[i] == 0, then do not trim the ith param.
    In all cases, do nothing with the added variables.

    Parameters
    ----------
    params : ndarray
        model parameters.  Not including added variables.
    k_params : Int
        Number of parameters
    alpha : ndarray
        regularization coefficients
    score : Function.
        score(params) should return a 1-d vector of derivatives of the
        unpenalized objective function.
    passed : bool
        True if the QC check passed
    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.

    Returns
    -------
    params : ndarray
        Trimmed model parameters
    trimmed : ndarray of booleans
        trimmed[i] == True if the ith parameter was trimmed.
    Foffautor   NzoCould not trim params automatically due to failed QC check. Trimming using trim_mode == 'size' will still work.g        Tsizez(trim_mode == %s, which is not recognized)	r
   r   r   r   r   r   r   
ValueErrorasarray)r   r   r   r   r   	trim_modesize_trim_tolauto_trim_toltrimmedr   msgr   r   s                r!   do_trim_paramsr2   ]   s   V g GE(E7X-..	f		V	Dc-...(E7X-..	f			vx 	& 	&AQx1}}!Hs6!9~~-q9MII #F1I!%GAJ		&
 
f		x 	& 	&AQx1}}vay>>M11 #F1I!%GAJ		& 6)DF F 	F 2:g&&&&r#   )F)__doc__numpyr
   statsmodels.tools.sm_exceptionsr   r"   r   r2    r#   r!   <module>r7      sq         > > > > > >? ? ? ?D  "F' F' F' F' F'r#   