ó
áp7]c           @   sA   d  Z  d d l Z d d l m Z d „  Z d d d d „ Z d S(   sZ   Additional functions

prediction standard errors and confidence intervals


A: josef pktd
iÿÿÿÿN(   t   statsc         C   sz   t  j |  ƒ }  |  j d k r7 |  d d … d f }  n? |  j d k rX t  j |  ƒ }  n |  j d k rv t d ƒ ‚ n  |  S(   sM    convert array_like to 2d from 1d or 0d

    not tested because not used
    i   Ni    s   too many dimensions(   t   npt   asarrayt   ndimt   Nonet
   atleast_2dt
   ValueError(   t   x(    (    sE   lib/python2.7/site-packages/statsmodels/sandbox/regression/predstd.pyt   atleast_2dcol   s    gš™™™™™©?c         C   sz  |  j  ƒ  } | d k rK |  j j } |  j } | d k rö |  j j } qö n« t j | ƒ } | j d | j d k rƒ t	 d ƒ ‚ n  |  j j
 |  j | ƒ } | d k r° d } nF t j | ƒ } | j d k rö t | ƒ | j d k rö t	 d ƒ ‚ n  |  j | | t j | | j ƒ j j d ƒ } t j | ƒ } t j j | d |  j ƒ } | | | }	 | | | }
 | |
 |	 f S(   s  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

    i   s   wrong shape of exogg      ð?i    s+   weights and exog do not have matching shapeg       @N(   t
   cov_paramsR   t   modelt   exogt   fittedvaluest   weightsR   R   t   shapeR   t   predictt   paramsR   t   sizet   lent	   mse_residt   dott   Tt   sumt   sqrtR    t   tt   isft   df_resid(   t   resR   R   t   alphat   covbt	   predictedt   predvart   predstdt   tppft
   interval_ut
   interval_l(    (    sE   lib/python2.7/site-packages/statsmodels/sandbox/regression/predstd.pyt   wls_prediction_std   s*    /		(0(   t   __doc__t   numpyR   t   scipyR    R   R   R$   (    (    (    sE   lib/python2.7/site-packages/statsmodels/sandbox/regression/predstd.pyt   <module>   s   	