
    >h=                         S r SSKrSSKJr  SSKJr  SSKJr  SSK	J
r
Jr  SSKrSrSS jr " S	 S
5      r " S S5      r " S S5      r " S S\\5      rg)a_  
Generalized additive models



Requirements for smoothers
--------------------------

smooth(y, weights=xxx) : ? no return ? alias for fit
predict(x=None) : smoothed values, fittedvalues or for new exog
df_fit() : degress of freedom of fit ?


Notes
-----
- using PolySmoother works for AdditiveModel, and GAM with Poisson and Binomial
- testfailure with Gamma, no other families tested
- there is still an indeterminacy in the split up of the constant across
  components (smoothers) and alpha, sum, i.e. constant, looks good.
  - role of offset, that I have not tried to figure out yet

Refactoring
-----------
currently result is attached to model instead of other way around
split up Result in class for AdditiveModel and for GAM,
subclass GLMResults, needs verification that result statistics are appropriate
how much inheritance, double inheritance?
renamings and cleanup
interface to other smoothers, scipy splines

basic unittests as support for refactoring exist, but we should have a test
case for gamma and the others. Advantage of PolySmoother is that we can
benchmark against the parametric GLM results.

    N)families)PolySmoother)GLM)IterationLimitWarningiteration_limit_docFc                 2   [         R                  " U 5      nU R                  S   nUS:  a  UnGO[         R                  " S5      [         R                  " S5      -  n[         R                  " S5      [         R                  " S5      -  n[         R                  " S5      [         R                  " S5      -  n[         R                  " S5      [         R                  " S5      -  nUS:  a  SXVU-
  US-
  -  S-  -   -  nO?US	:  a  SXgU-
  US-
  -  S
-  -   -  nO%US:  a  SXxU-
  US	-
  -  S-  -   -  nOSUS-
  S-  -   nU[         R                  " SUS-
  U5      R                  [         R                  5         n	Uc  Sn
OUn
[        XR                  5       S9nU$ )z

    r   i  2      d         g     b@i   g     @i  g     @g      @g?      )x)	npsortshapeloglinspaceastypeint32r   copy)r   s_arg_xnnknotsa1a2a3a4knotsorderss               jC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/sandbox/gam.pydefault_smootherr%   <   sk    
B	
A 	3wVVBZ"&&)#VVC[266!9$VVC[266!9$VVC[266!9$s7Bw1r624778FWBw1s73D889FXBw1s73E99:FAI++Fr{{1ac6*11"((;<E }Uffh'A H    c                        \ rS rSrS rS rSrg)Offsetc   c                     Xl         X l        g Nfnoffset)selfr-   r.   s      r$   __init__Offset.__init__e   s    r&   c                 @    U R                   " U0 UD6U R                  -   $ r+   r,   )r/   argskws      r$   __call__Offset.__call__i   s    ww##dkk11r&   r,   N)__name__
__module____qualname____firstlineno__r0   r5   __static_attributes__ r&   r$   r(   r(   c   s    2r&   r(   c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
Resultsl   c                     UR                   u  U l        U l        Xl        X l        X@l        X`l        XPl        X0l        X`l        U R                  U5      U l
        g r+   )r   nobsk_varsYalpha	smoothersr.   familyexoglinkinversepredictmu)r/   rC   rD   rG   rE   rF   r.   s          r$   r0   Results.__init__n   sK    !%	4; 
"	))$/r&   c                 $    U R                  U5      $ )zLexpected value ? check new GLM, same as mu for given exog
maybe remove this
)rH   r/   rG   s     r$   r5   Results.__call__}   s     &&t,,r&   c                 j    U R                   R                  R                  U R                  U5      5      $ )zBexpected value ? check new GLM, same as mu for given exog
        )rF   linkinversepredictrL   s     r$   rH   Results.linkinversepredict   s(     {{''T(:;;r&   c                    U R                  U5      nUR                  S   U R                  :X  aL  SSKnUR                  " S[
        5        [        R                  " U R                  U5      SS9U R                  -   $ UR                  S   U R                  :X  a"  [        R                  " USS9U R                  -   $ [        S5      e)zkpredict response, sum of smoothed components
TODO: What's this in the case of GLM, corresponds to X*beta ?
r   Nz&old orientation, colvars, will go away)axisr   zshape mismatch in predict)
smoothedr   rB   warningswarnFutureWarningr   sumrD   
ValueError)r/   rG   exog_smoothedrV   s       r$   rQ   Results.predict   s     d+q!T[[0MMB')66$---A6CCq!T[[066-a04::==899r&   c           
          [         R                  " [        UR                  S   5       Vs/ s H8  nU R                  U   R                  USS2U4   5      U R                  U   -   PM:     sn5      R                  $ s  snf )z4get smoothed prediction for each component

        r   N)r   arrayranger   rE   rQ   r.   T)r/   rG   is      r$   rU   Results.smoothed   sx     xx #(

1"6	8 #7Q	 *224!9=AN #7	8 9 :;		; 8s   ?A;c                     U R                  U5      nUR                  S5      nUR                  5       U R                  -   nX#-
  nXT4$ )Nr   )rU   meanrY   rD   )r/   rG   
componentsmeansconstantcomponents_demeaneds         r$   smoothed_demeanedResults.smoothed_demeaned   sD    ]]4(
"99;+(0",,r&   )	rC   rD   rG   rF   rB   rI   rA   r.   rE   N)r7   r8   r9   r:   r0   r5   rH   rQ   rU   ri   r;   r<   r&   r$   r>   r>   l   s     0-<
:.;-r&   r>   c                   J    \ rS rSrSrSS jrS rS rS rS r	S	 r
SS
 jrSrg)AdditiveModel   a]  additive model with non-parametric, smoothed components

Parameters
----------
exog : ndarray
smoothers : None or list of smoother instances
    smoother instances not yet checked
weights : None or ndarray
family : None or family instance
    I think only used because of shared results with GAM and subclassing.
    If None, then Gaussian is used.
Nc                    Xl         Ub  X0l        O2[        R                  " U R                   R                  S   5      U l        U=(       d9    [        UR                  S   5       Vs/ s H  n[        US S 2U4   5      PM     snU l        [        UR                  S   5       H  nSU R                  U   l        M     Uc  [        R                  " 5       U l        g X@l        g s  snf )Nr   r   
   )rG   weightsr   onesr   r_   r%   rE   dfr   GaussianrF   )r/   rG   rE   rp   rF   ra   s         r$   r0   AdditiveModel.__init__   s    	"L77499??1#56DL"aERVR\R\]^R_L`&aL`q'7QqS	'BL`&a tzz!}%A#%DNN1  & >"++-DK K 'bs   &C"c                 >    SU l         [        R                  U l        U $ )z3initialize iteration ?, should be removed

        r   )iterr   infdevr/   s    r$   _iter__AdditiveModel._iter__   s     	66r&   c                    U R                   nU R                   R                  nUR                  U R                  5      n[        R
                  " U R                  R                  S   [        R                  5      nX R                  -  R                  5       U R                  R                  5       -  n[        U R                  R                  S   5       GH7  nU R                  U   R                  5       n[        R                  " X%-
  U-
  U-   5      R                  5       nU(       a  [        X%X75        [        S5      eU R                  U   R!                  X#-
  U-   U R                  S9  U R                  U   R                  5       n	XR                  -  R                  5       * U R                  R                  5       -  U R                   R"                  U'   [$        (       a"  [        U R                  U   R&                  5        X9U-
  -  nGM:     U R                   R"                  n[)        X%U R                  U R                  U R*                  U5      $ )zinternal calculation for one fit iteration

BUG: I think this does not improve, what is supposed to improve
    offset does not seem to be used, neither an old alpha
    The smoothers keep coef/params from previous iteration
r   znan encounteredrp   )resultsrC   rQ   rG   r   zerosr   float64rp   rY   r_   rE   isnananyprintrZ   smoothr.   DEBUGparamsr>   rF   )
r/   _resultsrC   rI   r.   rD   ra   tmpbadtmp2s
             r$   nextAdditiveModel.next   s    <<LLNNdii($))//!,bjj9\\!&&(4<<+;+;+==tyyq)*A..#++-C ((19r>C/0446Ca( !233NN1$$QVc\-1\\ % ;>>!$,,.D'+LL'8&=&=&?%?$,,BRBRBT%TDLL"udnnQ'../*B# +( $$ qDNNDKKPPr&   c                    U =R                   S-  sl         [        (       a  [        U R                   U R                  R                  R
                  5        [        U R                  R                  U R                  5      R
                  U R                  R
                  5        U R                  R                  U R                  R                  U R                  5      -
  S-  U R                  -  R                  5       nU R                   U R                  :  a  g[        R                  " U R                  U-
  U-  5      U R                  :  a  Xl        gXl        g)zcondition to continue iteration loop

Parameters
----------
tol

Returns
-------
cont : bool
    If true, then iteration should be continued.

r   r
   FT)rv   r   r   r~   rC   r   rQ   rG   rp   rY   maxiterr   fabsrx   rtol)r/   curdevs     r$   contAdditiveModel.cont  s     			Q	5$))T\\^^112$,,&&tyy1779K9KLLLNNT\\%9%9$))%DDqHDLLX]]_99t||#77DHHv%/0499<H r&   c                 2   U R                   R                  R                  S   [        R                  " [        U R                  R                  S   5       Vs/ s H  oR                  U   R                  5       PM!     sn5      R                  5       -
  $ s  snf )zIdegrees of freedom of residuals, ddof is sum of all smoothers df
        r   r   )
r~   rC   r   r   r^   r_   rG   rE   df_fitrY   )r/   ra   s     r$   df_residAdditiveModel.df_resid  sr     ||~~##A&W\]a]f]f]l]lmn]oWp2qWpRS>>!3D3K3K3MWp2q)r)v)v)xxx2qs   &B
c                     U R                   R                  U R                  U R                  5      -
  S-  R                  5       U R	                  5       -  $ )z1estimate standard deviation of residuals
        r
   )r~   rC   rG   rY   r   ry   s    r$   estimate_scaleAdditiveModel.estimate_scale$  s=     $,,tyy"99A=BBDt}}VVr&   c                    X l         X0l        U R                  5         SnXR                  -  R	                  5       U R                  R	                  5       -  n[
        R                  " U R                  R                  S   [
        R                  5      n[        U R                  R                  S   5       H  nU R                  U   R                  X-
  U-
  U R                  S9  U R                  U   R                  5       nXR                  -  R	                  5       U R                  R	                  5       -  Xg'   XR	                  5       -  nXH-  nM     [        XU R                  U R                  U R                  U5      U l        U R#                  5       (       a,  U R%                  5       U l        U R#                  5       (       a  M,  U R&                  U R                  :  a  [(        R*                  " [,        [.        5        U R                   $ )zgfit the model to a given endogenous variable Y

This needs to change for consistency with statsmodels

r   r   r}   )r   r   rz   rp   rY   r   r   rG   r   r   r_   rE   r   rQ   r>   rF   r~   r   r   rv   rV   rW   r   r   )	r/   rC   r   r   rI   rD   r.   ra   r   s	            r$   fitAdditiveModel.fit*  st    	\\!&&(4<<+;+;+==$))//!,bjj9tyyq)*ANN1$$QY^-1\\ % ;..#++-C||+002T\\5E5E5GGFI779CIB + qDNNDKKQWXiikk99;DL iikk 99$MM-/DE||r&   )	rx   rG   rF   rv   r   r~   r   rE   rp   )NNNgư>   )r7   r8   r9   r:   __doc__r0   rz   r   r   r   r   r   r;   r<   r&   r$   rl   rl      s.    !&$QL:y
Wr&   rl   c                   Z    \ rS rSrS\R
                  " 5       4S jrS rSS jrS	S jr	Sr
g)
ModeliK  Nc                 x    [         R                  XX4S9  [        R                  " XX$S9  U R                  UL d   eg )NrE   rF   )rF   )rl   r0   r   rF   )r/   endogrG   rE   rF   s        r$   r0   Model.__init__[  s7     	tYNT$6{{f$$$r&   c                 >   U R                   nUR                  n[        R                  " U R                  5      R                  5       (       a  [        S5        U R                  R                  R                  UR                  U R                  5      5      Ul        U R                  R	                  UR                  5      n[        R                  " U5      R                  5       (       a  X0l        [        S5        X0l        [        (       a[  [        S[        R                  " U R                  R                  R                  UR                  5      5      R                  5       5        UR                  U R                  5      U R                  R                  R                  UR                  5      X!R                  -
  -  -   n[!        U R                  U R"                  U R                  U R                  S9nUR%                  U5      nU R&                  R)                  XAR                  U R                  5      /5        X!l        U R                  R                  R                  UR                  U R                  5      5      Ul        U =R*                  S-  sl        Xl         U$ )Nnanweights1nanweights2zderiv isnan)rE   rp   rF   r   )r~   rC   r   r   rp   allr   rF   rO   rP   rQ   rG   rI   r   derivr   rl   rE   r   historyappendrv   )r/   r   rC   rp   Zms         r$   r   
Model.nextb  s   <<JJ88DLL!%%''- kk&&..x/?/?		/JK ++%%hkk288G  """L- 5-$++*:*:*@*@*M!N!R!R!TU TYY'{{%%hkk2a++oFG $))t~~"&,,t{{D 558Q 0 0 ;<=
kk&&..x/?/?		/JK		Q	r&   c                    Uc  U R                   nXR                  R                  -
  n[        R                  " US5      U R
                  R                  U R                  R                  5      -  R                  5       U R                  -  $ )z)
Return Pearson's X^2 estimate of scale.
r
   )	rC   r~   rI   r   powerrF   variancerY   r   )r/   rC   resids      r$   r   Model.estimate_scale  sf    
 9ALLOO#"T[[%9%9$,,//%JJOOQmm$ 	$r&   c                 6   X l         X0l        [        R                  " U[        R                  5      U l        / U l        U R                  5         U R
                  R                  5       nU R                  R                  U5      nU R                  R                  U5      U R                  R                  R                  U5      X-
  -  -   n[        U R                  U R                  U R                  S9nUR!                  U5      U l        U R                  R                  R%                  U R"                  R'                  U R                  5      5      U R"                  l        XR"                  l        U R+                  5       (       aR  U R-                  5       U l        U R/                  5       =U l        U R"                  l        U R+                  5       (       a  MR  U R2                  U R                  :  a  SS KnUR6                  " [8        [:        5        U R"                  $ )Nr   r   )r   r   r   asarrayr   rC   r   rz   rd   rF   starting_murO   r   rl   rG   rE   r   r~   rP   rQ   rI   r   r   r   scalerv   rV   rW   r   r   )	r/   rC   r   r   rD   mu0r   r   rV   s	            r$   r   	Model.fit  sc   	Arzz* 	 kk%%a(KKU#dkk&6&6&<&<U&Cqw&OO$))t~~dkkRuuQx++**224<<3G3G		3RSiikk99;DL.2.A.A.CCDJ+ iikk 99$MM-/DE||r&   )rC   r   r   r~   r   r   rp   r+   r   )r7   r8   r9   r:   r   rs   r0   r   r   r   r;   r<   r&   r$   r   r   K  s'      /38;L;L;N %"H	$r&   r   r+   )r   numpyr   statsmodels.genmodr   +statsmodels.sandbox.nonparametric.smoothersr   +statsmodels.genmod.generalized_linear_modelr   statsmodels.tools.sm_exceptionsr   r   rV   r   r%   r(   r>   rl   r   r<   r&   r$   <module>r      s^   "`  ' D ; V %N2 2E- E-NV VpfC fr&   