U
    Ãmœd¹  ã                   @   s.   d Z ddlZddlmZ dd„ Zd	dd„ZdS )
zZAdditional functions

prediction standard errors and confidence intervals


A: josef pktd
é    N)Ústatsc                 C   sR   t  | ¡} | jdkr&| dd…df } n(| jdkr<t  | ¡} n| jdkrNtdƒ‚| S )zM convert array_like to 2d from 1d or 0d

    not tested because not used
    é   Nr   ztoo many dimensions)ÚnpÚasarrayÚndimÚ
atleast_2dÚ
ValueError)Úx© r
   ú_/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/regression/predstd.pyÚatleast_2dcol   s    



r   çš™™™™™©?c                 C   s  |   ¡ }|dkr0| jj}| j}|dkr¢| jj}nrt |¡}|jd |jd krVtdƒ‚| j 	| j
|¡}|dkrtd}n.t |¡}|jdkr¢t|ƒ|jd kr¢tdƒ‚| j| |t ||j¡j  d¡ }t |¡}tj |d | j¡}|||  }	|||  }
||
|	fS )a  calculate standard deviation and confidence interval for prediction

    applies to WLS and OLS, not to general GLS,
    that is independently but not identically distributed observations

    Parameters
    ----------
    res : regression result instance
        results of WLS or OLS regression required attributes see notes
    exog : array_like (optional)
        exogenous variables for points to predict
    weights : scalar or array_like (optional)
        weights as defined for WLS (inverse of variance of observation)
    alpha : float (default: alpha = 0.05)
        confidence level for two-sided hypothesis

    Returns
    -------
    predstd : array_like, 1d
        standard error of prediction
        same length as rows of exog
    interval_l, interval_u : array_like
        lower und upper confidence bounds

    Notes
    -----
    The result instance needs to have at least the following
    res.model.predict() : predicted values or
    res.fittedvalues : values used in estimation
    res.cov_params() : covariance matrix of parameter estimates

    If exog is 1d, then it is interpreted as one observation,
    i.e. a row vector.

    testing status: not compared with other packages

    References
    ----------

    Greene p.111 for OLS, extended to WLS by analogy

    Nr   zwrong shape of exogg      ð?r   z+weights and exog do not have matching shapeg       @)Z
cov_paramsÚmodelÚexogZfittedvaluesÚweightsr   r   Úshaper   ZpredictÚparamsr   ÚsizeÚlenZ	mse_residÚdotÚTÚsumÚsqrtr   ÚtZisfZdf_resid)Úresr   r   ÚalphaZcovbZ	predictedZpredvarZpredstdZtppfZ
interval_uZ
interval_lr
   r
   r   Úwls_prediction_std   s*    /


$
r   )NNr   )Ú__doc__Únumpyr   Zscipyr   r   r   r
   r
   r
   r   Ú<module>   s   