
    M/Ph                         d dl Z G d d          Z G d de          Z G d de          Z G d d	e          Z G d
 de          ZdS )    Nc                       e Zd ZdZd ZdS )RegressionEffectsat  
    Base class for regression effects used in RegressionFDR.

    Any implementation of the class must provide a method called
    'stats' that takes a RegressionFDR object and returns effect sizes
    for the model coefficients.  Greater values for these statistics
    imply greater evidence that the effect is real.

    Knockoff effect sizes are based on fitting the regression model to
    an extended design matrix [X X'], where X' is a design matrix with
    the same shape as the actual design matrix X.  The construction of
    X' guarantees that there are no true associations between the
    columns of X' and the dependent variable of the regression.  If X
    has p columns, then the effect size of covariate j is based on the
    strength of the estimated association for coefficient j compared
    to the strength of the estimated association for coefficient p+j.
    c                     t           N)NotImplementedError)selfparents     e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/knockoff_regeffects.pystatszRegressionEffects.stats   s    !!    N__name__
__module____qualname____doc__r    r   r
   r   r      s-         $" " " " "r   r   c                       e Zd ZdZd ZdS )CorrelationEffectsa  
    Marginal correlation effect sizes for FDR control.

    Parameters
    ----------
    parent : RegressionFDR
        The RegressionFDR instance to which this effect size is
        applied.

    Notes
    -----
    This class implements the marginal correlation approach to
    constructing test statistics for a knockoff analysis, as
    described under (1) in section 2.2 of the Barber and Candes
    paper.
    c                     t          j        |j        j        |j                  }t          j        |j        j        |j                  }t          j        |          t          j        |          z
  S r   )npdotexog1Tendogexog2abs)r   r	   s1s2s       r
   r   zCorrelationEffects.stats-   sK    VFLNFL11VFLNFL11vbzzBF2JJ&&r   Nr   r   r   r
   r   r      s-         "' ' ' ' 'r   r   c                       e Zd ZdZd Zd ZdS )ForwardEffectsa@  
    Forward selection effect sizes for FDR control.

    Parameters
    ----------
    parent : RegressionFDR
        The RegressionFDR instance to which this effect size is
        applied.
    pursuit : bool
        If True, 'basis pursuit' is used, which amounts to performing
        a full regression at each selection step to adjust the working
        residual vector.  If False (the default), the residual is
        adjusted by regressing out each selected variable marginally.
        Setting pursuit=True will be considerably slower, but may give
        better results when exog is not orthogonal.

    Notes
    -----
    This class implements the forward selection approach to
    constructing test statistics for a knockoff analysis, as
    described under (5) in section 2.2 of the Barber and Candes
    paper.
    c                     || _         d S r   )pursuit)r   r"   s     r
   __init__zForwardEffects.__init__L   s    r   c                    j         j        d         }j                                        fdt	          |          D             }t          j        |          }g }t	          |          D ]}t
          j        fd|D                      }t          j        |          }||z
  dz
  |||         d         <   ||         d         }	||= | j	        r5|D ]}
|	t          j
        |	|
          |
z  z  }	|                    |	           t          j
        |	          |	z  z  |d|dz           }||dz  d          }t          j        ||k    ||          t          j        ||z
            z  }|S )N   c                 6    g | ]}|j         d d |f         fS r   )exog).0ir	   s     r
   
<listcomp>z(ForwardEffects.stats.<locals>.<listcomp>R   s-    :::q&+aaad#$:::r   c           	      j    g | ]/}t          j        t          j        |d                              0S )r%   )r   r   r   )r(   xrvs     r
   r*   z(ForwardEffects.stats.<locals>.<listcomp>V   s3    ===Qrvb!A$//00===r   r      )r'   shaper   copyranger   emptyr_argmaxr"   r   appendwheresign)r   r	   nvarvlzpastr)   dpjr,   vz1z2str-   s    `            @r
   r   zForwardEffects.statsO   su   { #\  ::::eDkk:::HTNNt 
	$ 
	$A===="===>B	"A(Q,AbeAhK1aA1|  ( (A1a'AAA"&Q--!##BBqqy\tQwxx[Xb2gr2&&b)9)99	r   Nr   r   r   r   r#   r   r   r   r
   r    r    3   s<         0      r   r    c                       e Zd ZdZd ZdS )
OLSEffectsa  
    OLS regression for knockoff analysis.

    Parameters
    ----------
    parent : RegressionFDR
        The RegressionFDR instance to which this effect size is
        applied.

    Notes
    -----
    This class implements the ordinary least squares regression
    approach to constructing test statistics for a knockoff analysis,
    as described under (2) in section 2.2 of the Barber and Candes
    paper.
    c                    ddl m}  ||j        |j                  }|                                }t          |j                  dz  }t          j        |j        d|                   t          j        |j        |d                    z
  }|S )Nr   )OLSr.   )	#statsmodels.regression.linear_modelrF   r   r'   fitlenparamsr   r   )r   r	   rF   modelresultqr   s          r
   r   zOLSEffects.statsx   s    ;;;;;;FL&+..!#v}QqS)**RVFM!""4E-F-FFr   Nr   r   r   r
   rD   rD   f   s-         "    r   rD   c                   $    e Zd ZdZ	 	 ddZd ZdS )RegModelEffectsa  
    Use any regression model for Regression FDR analysis.

    Parameters
    ----------
    parent : RegressionFDR
        The RegressionFDR instance to which this effect size is
        applied.
    model_cls : class
        Any model with appropriate fit or fit_regularized
        functions
    regularized : bool
        If True, use fit_regularized to fit the model
    model_kws : dict
        Keywords passed to model initializer
    fit_kws : dict
        Dictionary of keyword arguments for fit or fit_regularized
    FNc                 N    || _         || _        ||ni | _        ||ni | _        d S r   )	model_clsregularized	model_kwsfit_kws)r   rQ   rR   rS   rT   s        r
   r#   zRegModelEffects.__init__   s5    "&&/&;")"5ww2r   c                 D    | j         |j        |j        fi | j        }| j        r |j        di | j        j        }n |j        di | j        j        }t          |          dz  }t          j        |d|                   t          j        ||d                    z
  }|S )Nr.   r   r   )rQ   r   r'   rS   rR   fit_regularizedrT   rJ   rH   rI   r   r   )r   r	   rK   rJ   rM   r   s         r
   r   zRegModelEffects.stats   s    v|V[KKDNKK 	6*U*::T\::AFFUY....5FKK1vac{##bfVABBZ&8&88r   )FNNrB   r   r   r
   rO   rO      sK         & @D> > > >    r   rO   )numpyr   r   r   r    rD   rO   r   r   r
   <module>rX      s       " " " " " " " ".' ' ' ' '* ' ' '00 0 0 0 0& 0 0 0f    "   8# # # # #' # # # # #r   