ó
áp7]c           @   s  d  Z  d d l m Z m Z d d l Z d d l m Z m Z d d l	 m
 Z
 d d l m Z d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d  „  ƒ  YZ! e" d! k r	e ƒ  Z# e# j$ ƒ  e# j% ƒ  xA e e e  e g D]* Z& e& ƒ  Z& e& j$ ƒ  e& j% ƒ  e& j' qØWn  d S("   sy	  Tests for gam.AdditiveModel and GAM with Polynomials compared to OLS and GLM


Created on Sat Nov 05 14:16:07 2011

Author: Josef Perktold
License: BSD


Notes
-----

TODO: TestGAMGamma: has test failure (GLM looks good),
        adding log-link didn't help
        resolved: gamma doesn't fail anymore after tightening the
                  convergence criterium (rtol=1e-6)
TODO: TestGAMNegativeBinomial: rvs generation doesn't work,
        nbinom needs 2 parameters
TODO: TestGAMGaussianLogLink: test failure,
        but maybe precision issue, not completely off

        but something is wrong, either the testcase or with the link
        >>> tt3.__class__
        <class '__main__.TestGAMGaussianLogLink'>
        >>> tt3.res2.mu_pred.mean()
        3.5616368292650766
        >>> tt3.res1.mu_pred.mean()
        3.6144278964707679
        >>> tt3.mu_true.mean()
        34.821904835958122
        >>>
        >>> tt3.y_true.mean()
        2.685225067611543
        >>> tt3.res1.y_pred.mean()
        0.52991541684645616
        >>> tt3.res2.y_pred.mean()
        0.44626406889363229



one possible change
~~~~~~~~~~~~~~~~~~~
add average, integral based tests, instead of or additional to sup
    * for example mean squared error for mu and eta (predict, fittedvalues)
      or mean absolute error, what's the scale for this? required precision?
    * this will also work for real non-parametric tests

example: Gamma looks good in average bias and average RMSE (RMISE)

>>> tt3 = _estGAMGamma()
>>> np.mean((tt3.res2.mu_pred - tt3.mu_true))/tt3.mu_true.mean()
-0.0051829977497423706
>>> np.mean((tt3.res2.y_pred - tt3.y_true))/tt3.y_true.mean()
0.00015255264651864049
>>> np.mean((tt3.res1.y_pred - tt3.y_true))/tt3.y_true.mean()
0.00015255538823786711
>>> np.mean((tt3.res1.mu_pred - tt3.mu_true))/tt3.mu_true.mean()
-0.0051937668989744494
>>> np.sqrt(np.mean((tt3.res1.mu_pred - tt3.mu_true)**2))/tt3.mu_true.mean()
0.022946118520401692
>>> np.sqrt(np.mean((tt3.res2.mu_pred - tt3.mu_true)**2))/tt3.mu_true.mean()
0.022953913332599746
>>> maxabs = lambda x: np.max(np.abs(x))
>>> maxabs((tt3.res1.mu_pred - tt3.mu_true))/tt3.mu_true.mean()
0.079540546242707733
>>> maxabs((tt3.res2.mu_pred - tt3.mu_true))/tt3.mu_true.mean()
0.079578857986784574
>>> maxabs((tt3.res2.y_pred - tt3.y_true))/tt3.y_true.mean()
0.016282852522951426
>>> maxabs((tt3.res1.y_pred - tt3.y_true))/tt3.y_true.mean()
0.016288391235613865



iÿÿÿÿ(   t	   get_classt   lrangeN(   t   assert_almost_equalt   assert_equal(   t   stats(   t   AdditiveModel(   t   Model(   t   familyt   links(   t   GLM(   t   OLSt   Dummyc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR   Z   s   t   CheckAMc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   sF   t  |  j j |  j j d d ƒt  |  j j |  j j d  d d ƒd  S(   Nt   decimali   i
   (   R   t   res1t   y_predt   res2t   y_predshort(   t   self(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   test_predict_   s    c         C   sF   t  |  j j |  j j d d ƒt  |  j j |  j j d  d d ƒd  S(   NR   i   i
   (   R   R   R   R   t   fittedvaluesR   (   R   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   _est_fittedf   s    c         C   sR   t  |  j j d |  j j d d d ƒt  |  j j d |  j j d d d ƒd  S(   Ni   R   i   (   R   R   t   paramsR   (   R   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   test_paramsm   s    c         C   sX   t  |  j j ƒ  |  j j ƒ t  |  j j ƒ  |  j j ƒ t  |  j j ƒ  |  j j ƒ d  S(   N(   R   t   res_pst   df_modelR   t   df_fitt   df_resid(   R   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   _est_dfv   s    (   R   R   R   R   R   R   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR   ]   s   				t   CheckGAMc           B   s   e  Z d  „  Z RS(   c         C   s#   t  |  j j |  j j d d ƒd  S(   NR   i    (   R   R   t   mu_predR   (   R   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   test_mu~   s    (   R   R   R!   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR   |   s   t   BaseAMc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s$  d } d } d \ } } t  j | | | ƒ } t  j d | ƒ } t  j | | j ƒ  d d | f ƒ } | d  d  … d  d  … d  f t  j | d ƒ d  d  d  d  … f j | d ƒ } t | d d ƒ }	 |	 | d =| d  d  … |	 f }
 | j	 d ƒ } | |  _
 | | |
 |  _ |  _ |  _ d  S(	   Ni   iÈ   g      Ài   i   g      ð?iÿÿÿÿ(   g      Ài   (   t   npt   linspacet   sint   column_stackt   maxt   Nonet   aranget   reshapeR   t   sumt   nobst   y_truet   xt   exog(   t   clst   orderR,   t   lbt   ubt   x1t   x2R.   R/   t   idxt   exog_reducedR-   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   setup_classˆ   s    'O	(   R   R   t   classmethodR8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR"   †   s   t   TestAdditiveModelc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s}  t  t |  ƒ j ƒ  |  j } |  j |  j |  j } } } t j j	 d ƒ d } | | t j j
 | ƒ } t | ƒ } | j | ƒ | j } t | | ƒ j ƒ  }	 t ƒ  |  _ }
 |	 |  _ } | j | ƒ |
 _ |	 j j |	 j | ƒ | _ | j | d  ƒ |
 _ g  | j D]  } | j d D] } | ^ qq
} | j t g  | j D] } | j d ^ qCƒ } t j | g | ƒ |
 _ d  S(   Ni)Â… gš™™™™™¹?i
   i   (   t   superR:   R8   R,   R-   R.   R/   R#   t   randomt   seedt   randnR   t   fitt   resultsR
   R   R   R   t   predictR   t   modelR   R   t	   smootherst   alphaR+   t   array(   R0   R,   R-   R.   R/   t   sigma_noiset   yt   mt   res_gamt   res_olsR   R   t   sst   it   slopest   const(    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR8       s$    		00(   R   R   R9   R8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR:   ž   s   t   BaseGAMc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s5  |  j  } |  j |  j |  j } } } t |  d ƒ s> d } n	 |  j } |  j } | j j | ƒ |  _	 } t
 j j d ƒ t t |  j ƒ t j ƒ r¬ |  j | d | ƒ} n |  j | d | d | ƒ} t | | d | ƒ}	 |	 j | d d ƒ|	 j }
 |
 |  _ |	 |  _ t | | d | ƒj ƒ  } t ƒ  |  _ } | |  _ } | j j | j | d t ƒ| _ |
 j | ƒ | _ |
 j | d	  ƒ | _  | j j | j | d t! ƒ| _" |
 j# | _" g  |	 j$ D]  } | j d D] } | ^ qÓqÂ} |
 j% t& g  |	 j$ D] } | j d ^ qûƒ } t
 j' | g | ƒ | _ d  S(
   Nt   scalei   i)Â… t   sizeR   t   maxiterid   t   lineari
   ((   R,   R-   R.   R/   t   hasattrRP   R   t   linkt   inverset   mu_trueR#   R<   R=   t
   issubclassR    t   rvsR   t   rv_discretet   GAMR?   R@   RI   t   mod_gamR	   R   R   R   RB   RA   R   t   TrueR   R   t   FalseR    t   muRC   RD   R+   RE   (   R0   R,   R-   R.   R/   RP   t   fRW   t   y_obsRH   RI   t   res_glmR   R   RK   RL   RM   RN   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   initÃ   s6    							!!00(   R   R   R9   Rc   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyRO   Á   s   t   TestGAMPoissonc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s?   t  t |  ƒ j ƒ  t j ƒ  |  _ t j j |  _ |  j ƒ  d  S(   N(	   R;   Rd   R8   R   t   PoissonR   t   poissonRY   Rc   (   R0   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR8   õ   s    (   R   R   R9   R8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyRd   ó   s   t   TestGAMBinomialc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s?   t  t |  ƒ j ƒ  t j ƒ  |  _ t j j |  _ |  j ƒ  d  S(   N(	   R;   Rg   R8   R   t   BinomialR   t	   bernoulliRY   Rc   (   R0   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR8      s    (   R   R   R9   R8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyRg   þ   s   t   _estGAMGaussianLogLinkc           B   s   e  Z e d  „  ƒ Z RS(   c         C   sN   t  t |  ƒ j ƒ  t j t j ƒ |  _ t j j	 |  _	 d |  _
 |  j ƒ  d  S(   Ni   (   R;   Rj   R8   R   t   GaussianR   t   logR   t   normRY   RP   Rc   (   R0   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR8     s
    	(   R   R   R9   R8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyRj   	  s   	t   TestGAMGammac           B   s   e  Z e d  „  ƒ Z RS(   c         C   sH   t  t |  ƒ j ƒ  t j t j ƒ  ƒ |  _ t j j	 |  _	 |  j
 ƒ  d  S(   N(   R;   Rn   R8   R   t   GammaR   Rl   R   t   gammaRY   Rc   (   R0   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR8     s    (   R   R   R9   R8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyRn     s   t   _estGAMNegativeBinomialc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s?   t  t |  ƒ j ƒ  t j ƒ  |  _ t j j |  _ |  j ƒ  d  S(   N(	   R;   Rq   R8   R   t   NegativeBinomialR   t   nbinomRY   Rc   (   R0   (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyR8   +  s    (   R   R   R9   R8   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyRq   (  s   t   __main__((   t   __doc__t   statsmodels.compat.pythonR    R   t   numpyR#   t   numpy.testingR   R   t   scipyR   t   statsmodels.sandbox.gamR   R   R[   t   statsmodels.genmod.familiesR   R   t+   statsmodels.genmod.generalized_linear_modelR	   t#   statsmodels.regression.linear_modelR
   t   objectR   R   R   R"   R:   RO   Rd   Rg   Rj   Rn   Rq   R   t   t1R   R   t   ttR!   (    (    (    sA   lib/python2.7/site-packages/statsmodels/sandbox/tests/test_gam.pyt   <module>L   s<   
#2	

	

