o
    0Gf                     @   sv   d Z ddlZddlZddlmZmZ ddlZddlm	Z	m
Z
 ddlZG dd dZdd Zd	d
 Zdd Zdd ZdS )zTesting helper functions

Warning: current status experimental, mostly copy paste

Warning: these functions will be changed without warning as the need
during refactoring arises.

The first group of functions provide consistency checks

    N)Versionparse)assert_allcloseassert_c                   @   s    e Zd ZdddZdddZdS )	PytestTesterNc                 C   sR   t d}|d u r|jdd }|d u rtdtj|| _|jdd | _	d S )N   __file__zUnable to determine path__name__)
sys	_getframef_localsget
ValueErrorospathdirnamepackage_pathZpackage_name)selfr   f r   :lib/python3.10/site-packages/statsmodels/tools/_testing.py__init__   s   
zPytestTester.__init__Fc                 C   s   z?dd l }t|jtdkst|d u rddg}| jg| }tdd|  ||}|r=td|  t	
| W d S W d S  tyJ   tdw )	Nr   z3.0z
--tb=shortz--disable-pytest-warningszRunning pytest  zExit status: z"pytest>=3 required to run the test)pytestr   __version__r   ImportErrorr   printjoinmainr
   exit)r   
extra_argsr   r   cmdstatusr   r   r   __call__    s    
zPytestTester.__call__)N)NF)r	   
__module____qualname__r   r#   r   r   r   r   r      s    
	r   c                 C   s  | }t t|j}||}t|j|jdd tt |j|j	dd tt |j
|jdd t|j|jdd t| | dd t |j|j	|j|j| f}| j}t||dd tt|d ||d }|  t |j}t|j|d dd d S )N-q=rtolg|=&.>use_tr   )npeyelenparamsZt_testr   ZeffectsqueezesdZbseZtvalueZtvaluespvaluepvaluesZconf_intZcolumn_stackZsummary_framevaluesr   hasattrsummaryasarray)resultsresZmatttZ	table_resZtable2r2   r   r   r   check_ttest_tvalues1   s$   


r:   c                    s   | j tj  fddt D }t|jddd  fddt D }t|jddd du r;dnd	}t }t||v  zt	 }W n
 t
y[   Y d
S w t||v  d
S )aN  
    Check that the outputs of `res.wald_test` produces pvalues that
    match res.pvalues.

    Check that the string representations of `res.summary()` and (possibly)
    `res.summary2()` correctly label either the t or z-statistic.

    Parameters
    ----------
    results : Results

    Raises
    ------
    AssertionError
    c                    s(   g | ]}j t | d djqS )T)Zuse_fscalarZ	wald_testr+   r,   r1   .0kk_varsr8   r*   r   r   
<listcomp>b   s     z'check_ftest_pvalues.<locals>.<listcomp>r)   g}:)r(   Zatolc                    s&   g | ]}j t | d djqS )T)r;   r<   r=   )rA   r8   r   r   rB   g   s    FzP>|z|zP>|t|N)r*   r-   r.   ranger   r2   strr5   r   Zsummary2AttributeError)r7   ZpvalsZstring_use_tZsummZsumm2r   r@   r   check_ftest_pvaluesN   s(   
rF   c                 C   s   dd l }ddlm} ddlm} t| d| } t| ||fr'|dt|   | }|j	}t
|jj| |jdd t
|| dd d S )Nr   
GLMResultsDiscreteResults_resultszNot supported for r&   r'   )r   +statsmodels.genmod.generalized_linear_modelrH   #statsmodels.discrete.discrete_modelrJ   getattr
isinstanceskiptypefittedvaluesr   modelZendogZresidpredict)r7   r   rH   rJ   r8   fittedr   r   r   check_fittedz   s   rV   c                 C   s  | }t t |jjdd }ddlm} ddlm} ddl	m
}m} t| d| } t| ||frH|| ||  ||d   dS |jdd }t|||dd	 t||t | dd	 t|dd
 ||d  dd	 t|dd
 ||d dd	 tt|}||}	|jd
krtjntj}
||
||d}|	jd
krtjntj}
|
|	|d}t|tjr||| dS ||| dS )z
    Check that the `predict` method of the given results object produces the
    correct output type.

    Parameters
    ----------
    results : Results

    Raises
    ------
    AssertionError
    N   r   rG   rI   )assert_frame_equalassert_series_equalrK   r&   r'   r   )index)r+   r/   r6   rS   ZexogrL   rH   rM   rJ   Zstatsmodels.compat.pandasrX   rY   rN   rO   rT   tolistrR   r   rC   r-   ndimpdZSeriesZ	DataFrame)r7   r8   Zp_exogrH   rJ   rX   rY   rU   Z
exog_indexZ	predictedclsZpredicted_pandasZpredicted_expectedr   r   r   check_predict_types   s<   

r_   )__doc__r   r
   Zpackaging.versionr   r   Znumpyr+   Znumpy.testingr   r   Zpandasr]   r   r:   rF   rV   r_   r   r   r   r   <module>   s    ,