
    !g                     >    d dl mZ d dlZd Zd Zd Z G d d      Zy)    )OLSNc                    | j                   d   }t        t        |            }|j                  |       t	        j
                  |      s||   }t        | dd|f   | dd|f         }|j                  |      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          e/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/stats/regularized_covariance.py_calc_nodewise_rowr      s{    6 	

1A
uQx.CGGCL ;;uc
tAsF|T!S&\*D''e'4;;L    c                    | j                   \  }}t        t        |            }|j                  |       t	        j
                  |      s||   }t        j                  j                  | dd|f   | dd|f   j                  |      z
        dz  }t	        j                  ||z  |t        j                  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
uQx.CGGCL ;;uc

		tAsF|d1c6l&6&6|&DDEqHA
A		|Q ???@AHr   c                     t        |      }t        j                  |       }t        |      D ]1  }t	        t        |            }|j                  |       | |   |||f<   3 |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ffQiZNQx 758n#1##6sCx 7 b,QW5q888Nr   c                   $    e Zd ZdZd ZddZd Zy)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                     || _         y N)r   )selfr   s     r   __init__z!RegularizedInvCovariance.__init__   s	    	r   c                 z   | j                   j                  \  }}g }g }t        |      D ]S  }t        | j                   ||      }|j	                  |       t        | j                   |||      }|j	                  |       U t        j                  |      }t        j                  |      }t        ||      }	|	| _	        y)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     yy18 	6C-diieDL!!,/3DII|47@O$$_5	6 .1HH%67-n.?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*      s    ".>$r   r*   )#statsmodels.regression.linear_modelr   numpyr   r   r    r(   r*   r:   r   r   <module>r=      s'    3 'T'T#L6$ 6$r   