
    >h%                        S r SSKrSSKJr  SSKJr   " S S5      r " S S\5      r " S	 S
\5      r	\
S:X  Ga  S rS rS rS r\R                  " SSS5      r\R"                  " / SQ5      r\" \\5      r\S\R(                  R+                  \" \5      S9-  -   r\R0                  " \\\\4SS9r\	" \\5      r\R7                  SS9r\R:                  " \\\5      u  rr\R@                  " \RB                  " \5      5      r"\#" \S   5        \#" \5        \#" \R$                  5        \#" \"5        \#" \RH                  5        gg)zVNon-linear least squares



Author: Josef Perktold based on scipy.optimize.curve_fit

    N)optimize)Modelc                       \ rS rSrSrSrg)Results   zVjust a dummy placeholder for now
most results from RegressionResults can be used here
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       rC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/miscmodels/nonlinls.pyr   r      s     	r   r   c                   l    \ rS rSrSr  SS jrSS jrS rS rSS jr	S	 r
SS
 jrS rSS jrS rSrg)NonlinearLS6   a  Base class for estimation of a non-linear model with least squares

This class is supposed to be subclassed, and the subclass has to provide a method
`_predict` that defines the non-linear function `f(params) that is predicting the endogenous
variable. The model is assumed to be

:math: y = f(params) + error

and the estimator minimizes the sum of squares of the estimated error.

:math: min_parmas \sum (y - f(params))**2

f has to return the prediction for each observation. Exogenous or explanatory variables
should be accessed as attributes of the class instance, and can be given as arguments
when the instance is created.

Warning:
Weights are not correctly handled yet in the results statistics,
but included when estimating the parameters.

similar to scipy.optimize.curve_fit
API difference: params are array_like not split up, need n_params information

includes now weights similar to curve_fit
no general sigma yet (OLS and WLS, but no GLS)

This is currently holding on to intermediate results that are not necessary
but useful for testing.

Fit returns and instance of RegressionResult, in contrast to the linear
model, results in this case are based on a local approximation, essentially
y = f(X, params) is replaced by y = grad * params where grad is the Gradient
or Jacobian with the shape (nobs, nparams). See for example Greene

Examples
--------

class Myfunc(NonlinearLS):

    def _predict(self, params):
        x = self.exog
        a, b, c = params
        return a*np.exp(-b*x) + c

Ff we have data (y, x), we can create an instance and fit it with

mymod = Myfunc(y, x)
myres = mymod.fit(nparams=3)

and use the non-linear regression results, for example

myres.params
myres.bse
myres.tvalues


Nc                     Xl         X l        UbB  [        R                  " U5      nUR                  S:  a  X@l        SU-  U l        g [        S5      eS U l        g )N   g      ?z%correlated errors are not handled yet)endogexognpasarrayndimsigmaweights
ValueError)selfr   r   r   r   missings         r   __init__NonlinearLS.__init__q   sN    
	JJu%EzzA~"
!%x !HIIDLr   c                 $    U R                  U5      $ N)_predict)r   r   paramss      r   predictNonlinearLS.predict   s    }}V$$r   c                     g r#   r   r   r%   s     r   r$   NonlinearLS._predict   s    r   c                     g r#   r   )r   s    r   start_valueNonlinearLS.start_value   s    r   c                     Uc7  U R                   c  U R                  U R                  U5      -
  $ U R                   nX R                  U R                  U5      -
  -  $ r#   )r   r   r$   )r   r%   r   s      r   	geterrorsNonlinearLS.geterrors   sM    ?||#zzDMM&$999,,**t}}V'<<==r   c                 F    U R                  U5      S-  R                  5       $ Nr   )r/   sumr)   s     r   errorsumsquaresNonlinearLS.errorsumsquares   s    v&)..00r   c                    Ub  UnO<U R                  5       nUb  O(Ub  S[        R                  " U5      -  nO[        S5      eU R                  n[
        R                  " XT4SS0UD6nUu  pxpnUS;  a  SU
-   n[        U5      eU	S   nU R                  n[        U5      [        U5      :  a3  Ub0  US-  R                  5       [        U5      [        U5      -
  -  nX-  nOS n[        U5      [        U5      -
  U l        [        U5      U l        [        5       nUUl        UUl        UUl        U R                  U l        U R%                  U5      U l        [        R(                  R+                  U R&                  5      n[        R,                  " U[        R.                  " U5      5      U l        S	S
KJn  UnUnU" U UU R0                  S9nUUl        UU l        U$ )Ng?z3need information about start values foroptimizationfull_output   )r8   r         zOptimal parameters not found: fvecr   r   )RegressionResults)normalized_cov_params)r,   r   onesr   r/   r   leastsqRuntimeErrorr   lenr3   df_residdf_modelr   r%   pcovrawreswendogjac_predictwexoglinalgpinvdot	transposer=   statsmodels.regressionr<   fitres_results)r   r,   nparamskwp0funcrespoptrD   infodicterrmsgiermsgerrydatas_sqrN   
pinv_wexogr<   resultsbetalfits                         r   fitNonlinearLS.fit   s   
 "B !!#B~$2777++  ", - - ~~t=Q="=.1+Xsi2V;Cs##v

JR d&6 F<<>3u:c"g#56D;DDE
3r7*Bjj%%d+
YY^^DJJ/
%'VVJ)+j)A&C" 	=# t-1-G-GI r   c                 P    U R                   n[        R                  " X14SS0UD6nU$ )z*minimal fitting with no extra calculationsr7   r   )r/   r   r?   )r   r,   kwargsrS   rT   s        r   fit_minimalNonlinearLS.fit_minimal   s*    ~~tJaJ6J
r   c           	         Uc  U R                   nUc!  [        R                  R                  SSX4S9nOU" X4S9n[        R                  " U Vs/ s H'  n[        R
                  U R                  U5      U4   PM)     sn5      nU$ s  snf )zNfit with random starting values

this could be replaced with a global fitter

i
   )lowhighsizerk   )rP   r   randomuniformarrayr_re   )r   ntriesrvs_generatorrP   rvsrvr^   s          r   
fit_randomNonlinearLS.fit_random   s     ?llG ))##"F;L#MCf%67C((sKsBEE$"2"22"6";<sKL  Ls   .Bc                 6    SSK Jn  U" XR                  5      nU$ )zjacobian of prediction function using complex step derivative

This assumes that the predict function does not use complex variable
but is designed to do so.

r   )approx_fprime_cs)statsmodels.tools.numdiffrx   r$   )r   r%   rx   	jaccs_errs       r   rG   NonlinearLS.jac_predict   s     	?$V]];	r   )
rO   rC   rB   r   r   r=   r   r   rF   rH   )NNNNnoner#   )NN)rh   NN)r	   r
   r   r   r   r    r&   r$   r,   r/   r4   ra   re   ru   rG   r   r   r   r   r   r   6   sF    8t CG %
>1AF&
r   r   c                       \ rS rSrS rSrg)Myfunc   c                 d    U R                   nUu  p4nU[        R                  " U* U-  5      -  U-   $ r#   )r   r   exp)r   r%   xabcs         r   r$   Myfunc._predict
  s1    IIa1~!!r   r   N)r	   r
   r   r   r$   r   r   r   r   r~   r~      s    "r   r~   __main__c                 B    U[         R                  " U* U -  5      -  U-   $ r#   r   r   )r   r   r   r   s       r   func0r     s    1~!!r   c                 L    U u  p#nU[         R                  " U* U-  5      -  U-   $ r#   r   )r%   r   r   r   r   s        r   rS   rS     s(    a1~!!r   c                     U[        X5      -
  $ r#   rS   r%   r   ys      r   errorr     s    4?""r   c                 $    U[        X5      -
  S-  $ r2   r   r   s      r   error2r     s    DO#a''r   r:   2   )g      @g?g      ?g?rl   T)argsr7   r9   )rP   )%r   numpyr   scipyr   statsmodels.base.modelr   r   r   r~   r	   r   rS   r   r   linspacer   ro   r%   y0rm   normalrA   r   r?   rT   modra   resmy	curve_fit	cf_paramscf_pcovsqrtdiagcf_bseprintbser   r   r   <module>r      sJ     (	 	PF% FR"[ "( z""#( 	AaAXXo&F	faB
S!!s1v!...A


5&1v4
HC A,CGGAGE!++E1a8IwWWRWWW%&F	#a&M	)	%,,	&M	%))E r   