U
    Kvf                     @   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   F/tmp/pip-unpacked-wheel-2v6byqio/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rt
| jdd}|dkrt
| 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-   Znumpyr   Zscipyr   Zpandasr%   r   rA   r   r   r   r   <module>   s   n    