
    M/Ph                     F    d dl mZ d dlZd Zd Zd Z G d d          ZdS )    )OLSNc                 8   | j         d         }t          t          |                    }|                    |           t	          j        |          s||         }t          | dd|f         | dd|f                   }|                    |          j        }|S )a  calculates the nodewise_row values for the idxth variable, used to
    estimate approx_inv_cov.

    Parameters
    ----------
    exog : array_like
        The weighted design matrix for the current partition.
    idx : scalar
        Index of the current variable.
    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.

    Returns
    -------
    An array-like object of length p-1

    Notes
    -----

    nodewise_row_i = arg min 1/(2n) ||exog_i - exog_-i gamma||_2^2
                             + alpha ||gamma||_1
       N)alpha)	shapelistrangepopnpisscalarr   fit_regularizedparams)exogidxr   pindtmodnodewise_rows          h/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/regularized_covariance.py_calc_nodewise_rowr      s    6 	
1A
uQxx..CGGCLLL ;u c
tAAAsF|T!!!S&\**D''e'44;L    c                    | j         \  }}t          t          |                    }|                    |           t	          j        |          s||         }t          j                            | dd|f         | dd|f                             |          z
            dz  }t	          j	        ||z  |t          j                            |d          z  z             }|S )a1  calculates the nodewise_weightvalue for the idxth variable, used to
    estimate approx_inv_cov.

    Parameters
    ----------
    exog : array_like
        The weighted design matrix for the current partition.
    nodewise_row : array_like
        The nodewise_row values for the current variable.
    idx : scalar
        Index of the current variable
    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.

    Returns
    -------
    A scalar

    Notes
    -----

    nodewise_weight_i = sqrt(1/n ||exog,i - exog_-i nodewise_row||_2^2
                             + alpha ||nodewise_row||_1)
    N   r   )
r   r   r	   r
   r   r   linalgnormdotsqrt)r   r   r   r   nr   r   ds           r   _calc_nodewise_weightr    /   s    : :DAq
uQxx..CGGCLLL ;u c

	tAAAsF|d111c6l&6&6|&D&DDEEqHA
A	|Q ? ???@@AHr   c                    t          |          }t          j        |           }t          |          D ]@}t	          t          |                    }|                    |           | |         |||f<   A|d|dddf         dz  z  z  }|S )a  calculates the approximate inverse covariance matrix

    Parameters
    ----------
    nodewise_row_l : list
        A list of array-like object where each object corresponds to
        the nodewise_row values for the corresponding variable, should
        be length p.
    nodewise_weight_l : list
        A list of scalars where each scalar corresponds to the nodewise_weight
        value for the corresponding variable, should be length p.

    Returns
    ------
    An array-like object, p x p matrix

    Notes
    -----

    nwr = nodewise_row
    nww = nodewise_weight

    approx_inv_cov_j = - 1 / nww_j [nwr_j,1,...,1,...nwr_j,p]
    Nr   )lenr   eyer	   r   r
   )nodewise_row_lnodewise_weight_lr   approx_inv_covr   r   s         r   _calc_approx_inv_covr(   Y   s    4 	AfQiiZNQxx 7 7588nn#1##6sCx  b,QQQW5q888Nr   c                   &    e Zd ZdZd ZddZd ZdS )RegularizedInvCovarianceaU  
    Class for estimating regularized inverse covariance with
    nodewise regression

    Parameters
    ----------
    exog : array_like
        A weighted design matrix for covariance

    Attributes
    ----------
    exog : array_like
        A weighted design matrix for covariance
    alpha : scalar
        Regularizing constant
    c                     || _         d S N)r   )selfr   s     r   __init__z!RegularizedInvCovariance.__init__   s    			r   r   c                 |   | j         j        \  }}g }g }t          |          D ]Y}t          | j         ||          }|                    |           t          | j         |||          }|                    |           Zt          j        |          }t          j        |          }t          ||          }	|	| _	        dS )zestimates the regularized inverse covariance using nodewise
        regression

        Parameters
        ----------
        alpha : scalar
            Regularizing constant
        N)
r   r   r	   r   appendr    r   arrayr(   _approx_inv_cov)
r-   r   r   r   r%   r&   r   r   nodewise_weightr'   s
             r   fitzRegularizedInvCovariance.fit   s     y188 	6 	6C-dieDDL!!,///3DI|47@ @O$$_5555.11H%677-n.?A A  .r   c                     | j         S r,   )r2   )r-   s    r   r'   z'RegularizedInvCovariance.approx_inv_cov   s    ##r   N)r   )__name__
__module____qualname____doc__r.   r4   r'    r   r   r*   r*      sP         "  . . . .>$ $ $ $ $r   r*   )#statsmodels.regression.linear_modelr   numpyr   r   r    r(   r*   r:   r   r   <module>r=      s    3 3 3 3 3 3    ' ' 'T' ' 'T# # #L6$ 6$ 6$ 6$ 6$ 6$ 6$ 6$ 6$ 6$r   