o
    0Gfw                     @   s@   d Z ddlZddlmZ ddlZG dd dZ		d	ddZdS )
zM
Created on Fri Dec 19 11:29:18 2014

Author: Josef Perktold
License: BSD-3

    N)statsc                   @   sl   e Zd ZdZ	dddZedd Zedd Zed	d
 Zedd Z	edd Z
dddZdddZdS )PredictionResultsaE  
    Results class for predictions.

    Parameters
    ----------
    predicted_mean : ndarray
        The array containing the prediction means.
    var_pred_mean : ndarray
        The array of the variance of the prediction means.
    var_resid : ndarray
        The array of residual variances.
    df : int
        The degree of freedom used if dist is 't'.
    dist : {'norm', 't', object}
        Either a string for the normal or t distribution or another object
        that exposes a `ppf` method.
    row_labels : list[str]
        Row labels used in summary frame.
    Nc                 C   sn   || _ || _|| _|| _|| _|d u s|dkr tj| _d| _d S |dkr/tj	| _| jf| _d S || _d| _d S )Nnorm t)
	predictedvar_preddf	var_resid
row_labelsr   r   dist	dist_argsr   )selfpredicted_meanvar_pred_meanr
   r	   r   r   r   r   Blib/python3.10/site-packages/statsmodels/regression/_prediction.py__init__$   s   

zPredictionResults.__init__c                 C   s   t | j| j S N)npsqrtr   r
   r   r   r   r   se_obs6   s   zPredictionResults.se_obsc                 C      | j S r   )ser   r   r   r   se_mean:   s   zPredictionResults.se_meanc                 C   r   r   )r   r   r   r   r   r   >      z PredictionResults.predicted_meanc                 C   r   r   )r   r   r   r   r   r   C   r   zPredictionResults.var_pred_meanc                 C   s   t | jS r   )r   r   r   r   r   r   r   r   H   s   zPredictionResults.seF皙?c                 C   sX   |r| j n| j}| jjd|d  g| jR  }| j||  }| j||  }t||fS )a.  
        Returns the confidence interval of the value, `effect` of the
        constraint.

        This is currently only available for t and z tests.

        Parameters
        ----------
        alpha : float, optional
            The significance level for the confidence interval.
            ie., The default `alpha` = .05 returns a 95% confidence interval.

        Returns
        -------
        ci : ndarray, (k_constraints, 2)
            The array has the lower and the upper limit of the confidence
            interval in the columns.
           g       @)r   r   r   Zppfr   r   r   Zcolumn_stack)r   obsalphar   qlowerupperr   r   r   conf_intM   s
   zPredictionResults.conf_intc                 C   s   | j |dd}| j |dd}i }| j|d< | j|d< |d d df |d< |d d df |d	< |d d df |d
< |d d df |d< || _tj|| j| d}|S )NT)r   r   FZmeanZmean_ser   Zmean_ci_lowerr   Zmean_ci_upperZobs_ci_lowerZobs_ci_upper)indexcolumns)r#   r   r   tablepd	DataFramer   keys)r   r   Zci_obsZci_meanZ
to_includeresr   r   r   summary_frameh   s   


zPredictionResults.summary_frame)NNN)Fr   )r   )__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r#   r+   r   r   r   r   r      s     






r   Tc                 C   s  |r%t | jdr%|dur%ddlm} t|tjrt|}|| jjj	|}|durb|du r9t
|dd}t|r9d}t|}|jdkr\| jjd dkrT|dddf }n|dddf }t|}n| jj}|du rqt
| jdd}|du r}t
| jjdd}|durt|}|jdkr|jdks|jd |jd krtd	|du ri }| jj| j|fi |}|  }|t||jj d}	| j}
| jd
kr| jd }
|dur|
| }
ddg| j }t||	|
| j||dS )a  
    Compute prediction results.

    Parameters
    ----------
    exog : array_like, optional
        The values for which you want to predict.
    transform : bool, optional
        If the model was fit via a formula, do you want to pass
        exog through the formula. Default is True. E.g., if you fit
        a model y ~ log(x1) + log(x2), and transform is True, then
        you can pass a data structure that contains x1 and x2 in
        their original form. Otherwise, you'd need to log the data
        first.
    weights : array_like, optional
        Weights interpreted as in WLS, used for the variance of the predicted
        residual.
    row_labels : list
        A list of row labels to use.  If not provided, read `exog` is
        available.
    **kwargs
        Some models can take additional keyword arguments, see the predict
        method of the model for the details.

    Returns
    -------
    linear_model.PredictionResults
        The prediction results instance contains prediction and prediction
        variance and can on demand calculate confidence intervals and summary
        tables for the prediction of the mean and of new observations.
    ZformulaNr   )dmatrixr$   r   weightsr   zweights has wrong shapezfixed scalescaler   r   )r	   r   r   ) hasattrZmodelZpatsyr1   
isinstancer'   ZSeriesr(   dataZdesign_infogetattrcallabler   ZasarrayndimparamsshapeZ
atleast_2dexogsize
ValueErrorZpredictZ
cov_paramsdotTsumr3   Zcov_typeZcov_kwdsZuse_tr   Zdf_resid)r   r<   Z	transformr2   r   Z	pred_kwdsr1   r   Zcovbr   r
   r   r   r   r   get_prediction}   sR   #






rB   )NTNNN)	r/   Znumpyr   Zscipyr   Zpandasr'   r   rB   r   r   r   r   <module>   s    n