U
    Ãmœd�  ã                   @   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                   @   sj   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   sj   || _ || _|| _|| _|| _|d ks.|dkr>tj| _d| _n(|dkrZtj	| _| jf| _n|| _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   ú[/home/sam/Atlas/atlas_env/lib/python3.8/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_obs7   s    zPredictionResults.se_obsc                 C   s   | j S r   )Úser   r   r   r   Úse_mean;   s    zPredictionResults.se_meanc                 C   s   | j S r   )r   r   r   r   r   r   ?   s    z PredictionResults.predicted_meanc                 C   s   | j S r   )r   r   r   r   r   r   D   s    zPredictionResults.var_pred_meanc                 C   s   t  | j¡S r   )r   r   r   r   r   r   r   r   I   s    zPredictionResults.seFçš™™™™™©?c                 C   sV   |r
| j n| j}| jjd|d  f| jžŽ }| 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_intN   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_framei   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Ú  |rJt | jdƒrJ|dk	rJddlm} t|tjƒr:t |¡}|| jjj	|ƒ}|dk	rÄ|dkrrt
|ddƒ}t|ƒrrd}t |¡}|jdkr¸| jjd dkr¨|ddd…f }n|dd…df }t |¡}n6| jj}|dkrât
| jddƒ}|dkrút
| jjddƒ}|dk	�rDt |¡}|jdk�rD|jdk�s<|jd |jd k�rDtd	ƒ‚|dk�rRi }| jj| j|f|Ž}|  ¡ }|t ||j¡j  d¡}	| j}
| jd
k�r¤| jd }
|dk	�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   ) ÚhasattrÚmodelZpatsyr/   Ú
isinstancer%   ZSeriesr&   ÚdataZdesign_infoÚgetattrÚcallabler   ZasarrayÚndimÚparamsÚshapeZ
atleast_2dÚexogÚsizeÚ
ValueErrorZpredictZ
cov_paramsÚdotÚTÚsumr1   Zcov_typeZcov_kwdsZuse_tr   Zdf_resid)r   r;   Z	transformr0   r   Z	pred_kwdsr/   r   Zcovbr   r
   r   r   r   r   Úget_prediction~   sZ    #




ÿÿ


 þrA   )NTNNN)	r-   Únumpyr   Zscipyr   Zpandasr%   r   rA   r   r   r   r   Ú<module>   s   n    ÿ