
    'YHh*                        d Z ddlmZ ddlZddlmZmZ ddlm	Z	 ddl
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  G d d      Z G d d      Z G d de      Z G d d      Z G d dee      Z G d dee      Z G d de      Z G d de      Ze
j<                  j?                  dde        G d de             Z! G d  d!e      Z"e
j<                  j?                  d"de#       G d# d$e             Z$y)%a|	  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 did not help
        resolved: gamma does not fail anymore after tightening the
                  convergence criterium (rtol=1e-6)
TODO: TestGAMNegativeBinomial: rvs generation does not 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



    )lrangeN)assert_almost_equalassert_equal)stats)AdditiveModel)Model)familylinks)GLM)OLSc                       e Zd Zy)DummyN)__name__
__module____qualname__     [/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/sandbox/tests/test_gam.pyr   r   Z   s    r   r   c                       e Zd Zd Zej
                  j                  ded      d        Zd Z	ej
                  j                  dde
      d	        Zy
)CheckAMc                     t        | j                  j                  | j                  j                  d       t        | j                  j                  | j                  j                  d d d       y N   decimal
   )r   res1y_predres2y_predshortselfs    r   test_predictzCheckAM.test_predict_   sO    DII,, II,,a	9DII11 II,,Sb11	>r   z&Unknown, results do not match expectedT)reasonraisesstrictc                     t        | j                  j                  | j                  j                  d       t        | j                  j
                  | j                  j                  d d d       y r   )r   r   r   r   fittedvaluesr    r!   s    r   test_fittedzCheckAM.test_fittede   sR     	DII,, II22A	?DII11 II223B7	Dr   c                     t        | j                  j                  dd  | j                  j                  dd  d       t        | j                  j                  d   | j                  j                  d   d       y )N   r   r   )r   r   paramsr   r!   s    r   test_paramszCheckAM.test_paramsn   sd     	DII,,QR0 II,,QR0!	= 	DII,,Q/ II,,Q/	<r   zres_ps attribute does not existr$   r&   r%   c                 T   t        | j                  j                         | j                  j                         t        | j                  j	                         | j                  j                         t        | j                  j                         | j                  j
                         y N)r   res_psdf_modelr   df_fitdf_residr!   s    r   test_dfzCheckAM.test_dfw   sg     	T[[))+TYY-?-?@T[['')499+=+=>T[[))+TYY-?-?@r   N)r   r   r   r#   pytestmarkxfailAssertionErrorr)   r-   AttributeErrorr5   r   r   r   r   r   ]   sp    > [[F,T  ;D;D< [[?">  ;A;Ar   r   c                       e Zd Zd Zd Zy)CheckGAMc                 p    t        | j                  j                  | j                  j                  d       y )Nr   r   )r   r   mu_predr   r!   s    r   test_muzCheckGAM.test_mu   s%    DII-- II--q	:r   c                 v    t        | j                  j                  | j                  j                  d d d       y )Nr   r   r   )r   r   r    r   r   r!   s    r   test_predictionzCheckGAM.test_prediction   s,    DII11 II,,Sb11	>r   N)r   r   r   r?   rA   r   r   r   r<   r<      s    :
>r   r<   c                       e Zd Zed        Zy)BaseAMc                    d}d}d\  }}t        j                  |||      }t        j                  d|z        }t        j                  ||j	                         z  dz  d|z  f      }|d d d d d f   t        j
                  |dz         d d d d f   z  j                  |d      }t        |dz   dz        }	|	|dz   = |d d |	f   }
|j                  d      }|| _	        |||
c| _
        | _        | _        y )N      )g      rE   r   r+   g      ?)nplinspacesincolumn_stackmaxarangereshaper   sumnobsy_truexexog)clsorderrP   lbubx1x2rR   rS   idxexog_reducedrQ   s               r   setup_classzBaseAM.setup_class   s     B[[R&VVAbD\OOR[]BrE23!Ad(RYYuQw/dA>>GGbQeAgq[!aLAcE{! &,a#
CE38r   N)r   r   r   classmethodr\   r   r   r   rC   rC      s    > >r   rC   c                   2     e Zd Ze fd       Z fdZ xZS )TestAdditiveModelc                    t         |           | j                  }| j                  | j                  | j
                  }}}t        j                  j                  d       d}||t        j                  j                  |      z  z   }t        |      }|j                  |       |j                  }t        ||      j                         }	t               x| _        }
|	x| _        }|j#                  |      |
_        |	j&                  j#                  |	j(                  |      |_        |j#                  |d d       |
_        |j,                  D cg c]  }|j(                  dd  D ]  }|  }}}|j.                  t1        |j,                  D cg c]  }|j(                  d    c}      z   }t        j2                  |g|z         |
_        y c c}}w c c}w )N) g?r   r+   )superr\   rP   rQ   rR   rS   rH   randomseedrandnr   fitresultsr   r   r   r   predictr   modelr,   r    	smoothersalpharO   array)rT   rP   rQ   rR   rS   sigma_noiseymres_gamres_olsr   r   ssislopesconst	__class__s                   r   r\   zTestAdditiveModel.setup_class   si   xx**ceeSXX4
		w[299??4#888!	a))a,""$  '!4!!4ooa(mm++GNNDA"??1Sb62kkA299QR=Aa!A!AAAKK$HbRYYq\$H IIhhw/0	 B$Hs   G	G
c                 "    t         |           y r0   rb   r)   r"   rv   s    r   r)   zTestAdditiveModel.test_fitted   s     	r   )r   r   r   r]   r\   r)   __classcell__rv   s   @r   r_   r_      s    1 1> r   r_   c                       e Zd Zed        Zy)BaseGAMc                    | j                   }| j                  | j                  | j                  }}}t	        | d      sd}n| j
                  }| j                  }|j                  j                  |      x| _	        }t        j                  j                  d       	 | j                  |||      }t        |||      }	|	j!                  |d       |	j"                  }
|
| _        |	| _        t)        |||      j!                         }t+               x| _        }|x| _        }|j0                  j3                  |j4                  |d	
      |_        |
j3                  |      |_        |
j3                  |d d       |_        |j0                  j3                  |j4                  |d
      |_        |
j<                  |_        |	j>                  D cg c]  }|j4                  dd  D ]  }|  }}}|
j@                  tC        |	j>                  D cg c]  }|j4                  d    c}      z   }t        jD                  |g|z         |_        y # t        $ r | j                  ||      }Y w xY wc c}}w c c}w )Nscaler+   ra   )r   size)r   )r	   d   )maxiterlinear)whichr   mean)#rP   rQ   rR   rS   hasattrr   r	   linkinversemu_truerH   rc   rd   rvs	TypeErrorGAMrf   rg   rp   mod_gamr   r   r   r   ri   rh   r,   r   r    r>   murj   rk   rO   rl   )rT   rP   rQ   rR   rS   r   fr   y_obsro   rp   res_glmr   r   rr   rs   rt   ru   s                     r   initzBaseGAM.init   s   xx**ceeSXX4sG$EIIEJJ !v 66g
		w	0GGG5tG<E q#	eS!))eT!,002  '!4!!4 mm++GNND+Qooa("??1Sb62 }},,W^^T,Pzz  kkA299QR=Aa!A!AAAKK$HbRYYq\$H IIhhw/09  	0GGG$G/E	04 B$Hs   I I'I-
I$#I$N)r   r   r   r]   r   r   r   r   r}   r}      s    ,1 ,1r   r}   c                   (     e Zd Ze fd       Z xZS )TestGAMPoissonc                     t         |           t        j                         | _        t        j
                  j                  | _        | j                          y r0   )rb   r\   r	   Poissonr   poissonr   r   rT   rv   s    r   r\   zTestGAMPoisson.setup_class   s6    ^^%
--##
r   r   r   r   r]   r\   rz   r{   s   @r   r   r           r   r   c                   (     e Zd Ze fd       Z xZS )TestGAMBinomialc                     t         |           t        j                         | _        t        j
                  j                  | _        | j                          y r0   )rb   r\   r	   Binomialr   	bernoullir   r   r   s    r   r\   zTestGAMBinomial.setup_class  s6    __&
//%%
r   r   r{   s   @r   r   r   	  r   r   r   z'Unknown, results do not match expected.Tr.   c                   (     e Zd Ze fd       Z xZS )TestGAMGaussianLogLinkc                     t         |           t        j                  t	        j
                               | _        t        j                  j                  | _        d| _	        | j                          y )N   )rb   r\   r	   Gaussianr
   Logr   normr   r   r   r   s    r   r\   z"TestGAMGaussianLogLink.setup_class   sC    __UYY[1
**..	
r   r   r{   s   @r   r   r     s      r   r   c                   (     e Zd Ze fd       Z xZS )TestGAMGammac                     t         |           t        j                  t	        j
                               | _        t        j                  j                  | _        | j                          y r0   )
rb   r\   r	   Gammar
   r   r   gammar   r   r   s    r   r\   zTestGAMGamma.setup_class-  s<    \\%))+.
++//
r   r   r{   s   @r   r   r   +  r   r   r   6Passing wrong number of args/kwargs to _parse_args_rvsc                        e Zd Ze fd       Zej                  j                  dde       fd       Z	ej                  j                  dde       fd       Z
 xZS )TestGAMNegativeBinomialc                     t         |           t        j                         | _        t        j
                  j                  | _        | j                          y r0   )rb   r\   r	   NegativeBinomialr   nbinomr   r   r   s    r   r\   z#TestGAMNegativeBinomial.setup_class=  s8    ,,.
,,""
r   r   Tr.   c                 "    t         |           y r0   rx   ry   s    r   r)   z#TestGAMNegativeBinomial.test_fittedF  s     	r   c                 "    t         |           y r0   )rb   r5   ry   s    r   r5   zTestGAMNegativeBinomial.test_dfN  s     	r   )r   r   r   r]   r\   r6   r7   r8   r   r)   r5   rz   r{   s   @r   r   r   7  s{       [[ 3"9  66
 [[ 3"9  66r   r   )%__doc__statsmodels.compat.pythonr   numpyrH   numpy.testingr   r   scipyr   r6   statsmodels.sandbox.gamr   r   r   statsmodels.genmod.familiesr	   r
   +statsmodels.genmod.generalized_linear_modelr   #statsmodels.regression.linear_modelr   r   r   r<   rC   r_   r}   r   r   r7   r8   r9   r   r   r   r   r   r   r   <module>r      s  JV -  ;   1 0 5 ; 3	 	 A  AF
>w 
>> >.% %P/1fh /1d	W 		g 	 C~  7W 7(	7 	  /y  2g 2r   