B
    Z1&                 @   s@   d Z ddlZddlmZ G dd deZ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               @   sX   e Zd ZdddZedd Zedd Zedd	 ZdddZdddZ	dddZ
dS )PredictionResultsNc	       	      C   sv   || _ || _|| _|| _|| _|| _|| _|d ks:|dkrJtj| _	d| _
n(|dkrftj| _	| jf| _
n|| _	d| _
d S )Nnorm t)predicted_meanvar_pred_meandf	var_resid
row_labelslinpredlinkr   r   dist	dist_argsr   )	selfr   r   r
   r	   r   r   r   r   r   r   =lib/python3.7/site-packages/statsmodels/genmod/_prediction.py__init__   s    zPredictionResults.__init__c             C   s   t t| j| j S )N)NotImplementedErrornpsqrtr   r
   )r   r   r   r   se_obs%   s    zPredictionResults.se_obsc             C   s   t | jS )N)r   r   r   )r   r   r   r   se_mean*   s    zPredictionResults.se_meanc             C   s   | j | j S )N)r   r   )r   r   r   r   tvalues.   s    zPredictionResults.tvaluesr   	two-sidedc             C   s   | j | | j }|dkr8| jjt|f| j d }nD|dkrV| jj|f| j }n&|dkrt| jj|f| j }ntd||fS )a3  z- or t-test for hypothesis that mean is equal to value

        Parameters
        ----------
        value : array_like
            value under the null hypothesis
        alternative : string
            'two-sided', 'larger', 'smaller'

        Returns
        -------
        stat : ndarray
            test statistic
        pvalue : ndarray
            p-value of the hypothesis test, the distribution is given by
            the attribute of the instance, specified in `__init__`. Default
            if not specified is the normal distribution.

        )z	two-sidedz2-sidedZ2s   )Zlargerl)Zsmallerszinvalid alternative)	r   r   r   Zsfr   absr   Zcdf
ValueError)r   valueZalternativestatZpvaluer   r   r   t_test3   s     zPredictionResults.t_testendpoint皙?c             K   s   t ddd}| j||k }|dkrL|sL| jj|dd}| j|}nX|dksX|r| 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.

        kwds : extra keyword arguments
            currently ignored, only for compatibility, consistent signature

        Returns
        -------
        ci : ndarray, (k_constraints, 2)
            The array has the lower and the upper limit of the confidence
            interval in the columns.

        r         r"   F)alphaZobsZdeltag       @)r   Zlinspacer   inverseallr   conf_intr   r   Zppfr   r   Zcolumn_stack)r   methodr&   kwdsZtmpZ	is_linearZ	ci_linearZciZseqlowerupperr   r   r   r)   W   s    zPredictionResults.conf_intr(   c             C   s   dd l }ddlm} | j|d}| }| j|d< | j|d< |d d df |d< |d d df |d< || _|j|| j|	 d	}|S )
Nr   )OrderedDict)r&   ZmeanZmean_seZmean_ci_lowerr$   Zmean_ci_upper)indexcolumns)
ZpandasZstatsmodels.compat.collectionsr/   r)   r   r   tableZ	DataFramer   keys)r   Zwhatr&   Zpdr/   Zci_meanZ
to_includeresr   r   r   summary_frame~   s    


zPredictionResults.summary_frame)NNNNNN)r   r   )r"   r#   )r(   r#   )__name__
__module____qualname__r   propertyr   r   r   r!   r)   r5   r   r   r   r   r      s    

$
'r   Tc          
   C   s  |r6t | jdr6|dk	r6ddlm} || jjjj|}|dk	r|dkr^t|dd}t|r^d}t	
|}|jdkr| jjjdks| jjjd dkr|dddf }t	|}n6| jj}|dkrt| jdd}|dkrt| jjdd}|dk	r.t	
|}|jdkr.|jdks&|jd |jd kr.td	d
|d< | jj| j|f|}	|  }
| jjj|j}|d |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.
    args, kwargs :
        Some models can take additional arguments or keywords, see the
        predict method of the model for the details.

    Returns
    -------
    prediction_results : generalized_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   )dmatrixr0   r$   weightsr   zweights has wrong shapeFZlinearr   zfixed scalescaler   r   )r	   r   r   r   r   )!hasattrZmodelZpatsyr:   dataZdesign_infoZbuildergetattrcallabler   asarrayndimexogshapeZ
atleast_2dsizer   Zpredictparams
cov_paramsZfamilyr   inverse_derivr   dotTsumr<   Zcov_typeZcov_kwdsZuse_tr   Zdf_resid)r   rC   	transformr;   r   r   r   Z	pred_kwdsr:   r   Zcovb
link_derivr   r
   r   r   r   r   get_prediction_glm   sL    !


""

rN   c             C   s   ddl m} |dkr$|dkr$| }|dkr<t| dr<| j}t| } || }|| }|d t	| }t
j}	t| t	||	|| d}
t|||	||
|d}|S )aJ  
    results for univariate, nonlinear, monotonicaly transformed parameters

    This provides transformed values, standard errors and confidence interval
    for transformations of parameters, for example in calculating rates with
    `exp(params)` in the case of Poisson or other models with exponential
    mean function.

    r   )linksNr0   r   )r   r   r   )r   r   r   r   )Zstatsmodels.genmod.familiesrO   ZLogr=   r0   r   rA   r'   rH   Zdiagr   r   r   Zidentity)rF   rG   r   rL   r   rO   r   rM   r   r   r   r4   r   r   r   params_transform_univariate   s    


rP   )NTNNNNN)NNN)	__doc__Znumpyr   Zscipyr   objectr   rN   rP   r   r   r   r   <module>   s     
X 