Ë
    £�Dj8=  ã                   óª  — d Z ddlZddlmZmZ  ej                  e«      j                  Z	dZ
d„ Zddi dfd„Zddi dfd	„Zddi fd
„Zddi fd„Zddi fd„Z eddddd¬«       ee
«      ddi dfd„«       «       Z eddddd¬«       ee
«      ddi dfd„«       «       Z eddddd¬«       ee
«      ddi fd„«       «       ZeZexj                   dz  c_         y)ay  numerical differentiation function, gradient, Jacobian, and Hessian

Author : josef-pkt
License : BSD

Notes
-----
These are simple forward differentiation, so that we have them available
without dependencies.

* Jacobian should be faster than numdifftools because it does not use loop over
  observations.
* numerical precision will vary and depend on the choice of stepsizes
é    N)ÚAppenderÚSubstitutiona  
    Calculate Hessian with finite difference derivative approximation

    Parameters
    ----------
    x : array_like
       value at which function derivative is evaluated
    f : function
       function of one array f(x, `*args`, `**kwargs`)
    epsilon : float or array_like, optional
       Stepsize used, if None, then stepsize is automatically chosen
       according to EPS**(1/%(scale)s)*x.
    args : tuple
        Arguments for function `f`.
    kwargs : dict
        Keyword arguments for function `f`.
    %(extra_params)s

    Returns
    -------
    hess : ndarray
       array of partial second derivatives, Hessian
    %(extra_returns)s

    Notes
    -----
    Equation (%(equation_number)s) in Ridout. Computes the Hessian as::

      %(equation)s

    where e[j] is a vector with element j == 1 and the rest are zero and
    d[i] is epsilon[i].

    References
    ----------:

    Ridout, M.S. (2009) Statistical applications of the complex-step method
        of numerical differentiation. The American Statistician, 63, 66-74
c                 ó®  — |€Jt         d|z  z  t        j                  t        j                  t        j                  | «      «      d«      z  }nut        j
                  |«      r't        j                  |«      }|j                  |«       n9t        j                  |«      }|j                  | j                  k7  rt        d«      ‚t        j                  |«      S )Ng      ð?gš™™™™™¹?z6If h is not a scalar it must have the same shape as x.)
ÚEPSÚnpÚmaximumÚabsÚasarrayÚisscalarÚemptyÚfillÚshapeÚ
ValueError)ÚxÚsÚepsilonÚnÚhs        ú]C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels\tools\numdiff.pyÚ_get_epsilonr   ^   s—   € Ø€Ü�"�q‘&‰MœBŸJ™J¤r§v¡v¬b¯j©j¸«mÓ'<¸cÓBÑB‰ä�;‰;�wÔÜ—‘˜“ˆAØ�F‰F�7�Oä—
‘
˜7Ó#ˆAØ�w‰w˜!Ÿ'™'Ò!Ü ð "0ó 1ð 1ä�:‰:�a‹=Ðó    © Fc                 óÆ  — t        | «      } || f|z   i |¤Ž}t        j                  |«      j                  }t        j                  |f|z   t        j
                  t        | j                  «      «      }	t        j                  |ft        «      }
|sKt        | d||«      }t        |«      D ].  }||   |
|<    || |
z   f|z   i |¤Ž|z
  ||   z  |	|dd…f<   d|
|<   Œ0 n]t        | d||«      dz  }t        |«      D ]>  }||   |
|<    || |
z   f|z   i |¤Ž || |
z
  f|z   i |¤Žz
  d||   z  z  |	|dd…f<   d|
|<   Œ@ |dk(  r|	j                  S |	j                  «       j                  S )aR  
    Gradient of function, or Jacobian if function f returns 1d array

    Parameters
    ----------
    x : ndarray
        parameters at which the derivative is evaluated
    f : function
        `f(*((x,)+args), **kwargs)` returning either one value or 1d array
    epsilon : float, optional
        Stepsize, if None, optimal stepsize is used. This is EPS**(1/2)*x for
        `centered` == False and EPS**(1/3)*x for `centered` == True.
    args : tuple
        Tuple of additional arguments for function `f`.
    kwargs : dict
        Dictionary of additional keyword arguments for function `f`.
    centered : bool
        Whether central difference should be returned. If not, does forward
        differencing.

    Returns
    -------
    grad : ndarray
        gradient or Jacobian

    Notes
    -----
    If f returns a 1d array, it returns a Jacobian. If a 2d array is returned
    by f (e.g., with a value for each observation), it returns a 3d array
    with the Jacobian of each observation with shape xk x nobs x xk. I.e.,
    the Jacobian of the first observation would be [:, 0, :]
    é   Ng        é   ç       @é   )Úlenr   Ú
atleast_1dr   ÚzerosÚpromote_typesÚfloatÚdtyper   ÚrangeÚTÚsqueeze)r   Úfr   ÚargsÚkwargsÚcenteredr   Úf0ÚdimÚgradÚeiÚks               r   Úapprox_fprimer0   m   s�  € ôB 	ˆA‹€AÙ	
ˆaˆT�$‰YÐ	"˜6Ñ	"€BÜ
�-‰-˜Ó
×
!Ñ
!€CÜ�8‰8�Q�D˜3‘J¤× 0Ñ 0´¸¿¹Ó @ÓA€DÜ	�‰�1�$œÓ	€BÙÜ˜q ! W¨aÓ0ˆÜ�q“ò 	ˆAØ˜A‘JˆBˆq‰EÙ˜q ™t˜g¨™nÐ8°Ñ8¸2Ñ=¸wÀq¹zÑIˆD�’A�‰JØˆBˆqŠEñ	ô
 ˜q ! W¨aÓ0°2Ñ5ˆÜ�q“ò 	ˆAØ˜A‘JˆBˆq‰EÙ˜q ™t˜g d™lÐ6¨vÑ6Ù˜q ™t˜g d™lÐ6¨vÑ6ñ7Ø9:¸WÀQ¹Z¹ñIˆD�’A�‰JàˆBˆqŠEð		ð 	ˆA‚vØ�v‰vˆà�|‰|‹~×ÑÐr   c                 ó  — t        j                  | «      } d} || f|z   i |¤Ž}|s%t        | d||«      } || |z   f|z   i |¤Ž|z
  |z  }	|	S t        | d||«      dz  } || |z   f|z   i |¤Ž || |z
  f|z   i |¤Žz
  d|z  z  }	|	S )a'  
    Gradient of function vectorized for scalar parameter.

    This assumes that the function ``f`` is vectorized for a scalar parameter.
    The function value ``f(x)`` has then the same shape as the input ``x``.
    The derivative returned by this function also has the same shape as ``x``.

    Parameters
    ----------
    x : ndarray
        Parameters at which the derivative is evaluated.
    f : function
        `f(*((x,)+args), **kwargs)` returning either one value or 1d array
    epsilon : float, optional
        Stepsize, if None, optimal stepsize is used. This is EPS**(1/2)*x for
        `centered` == False and EPS**(1/3)*x for `centered` == True.
    args : tuple
        Tuple of additional arguments for function `f`.
    kwargs : dict
        Dictionary of additional keyword arguments for function `f`.
    centered : bool
        Whether central difference should be returned. If not, does forward
        differencing.

    Returns
    -------
    grad : ndarray
        Array of derivatives, gradient evaluated at parameters ``x``.
    r   r   r   r   )r   r
   r   )
r   r'   r   r(   r)   r*   r   r+   Úepsr-   s
             r   Ú_approx_fprime_scalarr3   §   sÇ   € ô> 	�
‰
�1‹€AØ	€Aá	
ˆaˆT�$‰YÐ	"˜6Ñ	"€BÙÜ˜1˜a ¨!Ó,ˆÙ�Q�s‘U�H˜t‘OÐ/¨Ñ/°"Ñ4¸Ñ;ˆð €Kô	 ˜1˜a ¨!Ó,¨rÑ1ˆÙ�Q�s‘U�H˜T‘MÐ- fÑ-Ù�Q�s‘U�H˜T‘MÐ- fÑ-ñ.Ø23°c±'ñ;ˆð €Kr   c           	      ó(  — t        | «      }t        | d||«      }t        j                  |«      dz  |z  }t	        |«      D ��cg c]$  \  }} || |z   g|¢­i |¤Žj
                  ||   z  ‘Œ& }	}}t        j                  |	«      j                  S c c}}w )aÈ  
    Calculate gradient or Jacobian with complex step derivative approximation

    Parameters
    ----------
    x : ndarray
        parameters at which the derivative is evaluated
    f : function
        `f(*((x,)+args), **kwargs)` returning either one value or 1d array
    epsilon : float, optional
        Stepsize, if None, optimal stepsize is used. Optimal step-size is
        EPS*x. See note.
    args : tuple
        Tuple of additional arguments for function `f`.
    kwargs : dict
        Dictionary of additional keyword arguments for function `f`.

    Returns
    -------
    partials : ndarray
       array of partial derivatives, Gradient or Jacobian

    Notes
    -----
    The complex-step derivative has truncation error O(epsilon**2), so
    truncation error can be eliminated by choosing epsilon to be very small.
    The complex-step derivative avoids the problem of round-off error with
    small epsilon because there is no subtraction.
    r   ù              ð?)r   r   r   ÚidentityÚ	enumerateÚimagÚarrayr%   )
r   r'   r   r(   r)   r   Ú
incrementsÚiÚihÚpartialss
             r   Úapprox_fprime_csr>   Õ   s›   € ôB 	ˆA‹€Aä˜1˜a ¨!Ó,€GÜ—‘˜Q“ "Ñ$ wÑ.€Jô ' zÓ2÷4Ù�A�rñ �!�B‘$Ð(˜Ò( Ñ(×-Ñ-°¸±
Ó:ð 4€Hñ 4ô �8‰8�HÓ×ÑÐùó4s   Á)Bc                 óÒ   — t        j                  | «      } | j                  d   }t        | d||«      }d|z  } || |z   g|¢­i |¤Žj                  |z  }t        j
                  |«      S )a¾  
    Calculate gradient for scalar parameter with complex step derivatives.

    This assumes that the function ``f`` is vectorized for a scalar parameter.
    The function value ``f(x)`` has then the same shape as the input ``x``.
    The derivative returned by this function also has the same shape as ``x``.

    Parameters
    ----------
    x : ndarray
        Parameters at which the derivative is evaluated.
    f : function
        `f(*((x,)+args), **kwargs)` returning either one value or 1d array.
    epsilon : float, optional
        Stepsize, if None, optimal stepsize is used. Optimal step-size is
        EPS*x. See note.
    args : tuple
        Tuple of additional arguments for function `f`.
    kwargs : dict
        Dictionary of additional keyword arguments for function `f`.

    Returns
    -------
    partials : ndarray
       Array of derivatives, gradient evaluated for parameters ``x``.

    Notes
    -----
    The complex-step derivative has truncation error O(epsilon**2), so
    truncation error can be eliminated by choosing epsilon to be very small.
    The complex-step derivative avoids the problem of round-off error with
    small epsilon because there is no subtraction.
    éÿÿÿÿr   r5   )r   r
   r   r   r8   r9   )r   r'   r   r(   r)   r   r2   r=   s           r   Ú_approx_fprime_cs_scalarrA     sj   € ôJ 	�
‰
�1‹€AØ	�‰�‰€Aä˜1˜a ¨!Ó,€GØ
ˆw‰,€CÙ��S‘Ð*˜4Ò* 6Ñ*×/Ñ/°'Ñ9€Hä�8‰8�HÓÐr   c                 óæ  — t        | «      }t        | d||«      }t        j                  |«      }t        j                  ||«      }t        | «      }t        |«      D ]“  }	t        |	|«      D ]‚  }
t        j                   || d||	dd…f   z  z   ||
dd…f   z   f|z   i |¤Ž || d||	dd…f   z  z   ||
dd…f   z
  f|z   i |¤Žz
  j                  dz  ||	|
f   z  «      ||	|
f<   ||	|
f   ||
|	f<   Œ„ Œ• |S )a£  Calculate Hessian with complex-step derivative approximation

    Parameters
    ----------
    x : array_like
       value at which function derivative is evaluated
    f : function
       function of one array f(x)
    epsilon : float
       stepsize, if None, then stepsize is automatically chosen

    Returns
    -------
    hess : ndarray
       array of partial second derivatives, Hessian

    Notes
    -----
    based on equation 10 in
    M. S. RIDOUT: Statistical Applications of the Complex-step Method
    of Numerical Differentiation, University of Kent, Canterbury, Kent, U.K.

    The stepsize is the same for the complex and the finite difference part.
    r   r5   Nr   )r   r   r   ÚdiagÚouterr$   r&   r8   ©r   r'   r   r(   r)   r   r   ÚeeÚhessr;   Újs              r   Úapprox_hess_csrI   0  s(  € ô4 	ˆA‹€AÜ�Q˜˜7 AÓ&€AÜ	�‰�‹€BÜ�8‰8�A�q‹>€DäˆA‹€Aä�1‹Xò $ˆÜ�q˜!“ò 	$ˆAÜŸ™Ù�a˜"˜R ¢1 ™X™+‘o¨¨1ªa¨4©Ñ0Ð2°TÑ9ÐE¸fÑEÙ  R¨¨1ªa¨4©¡[¡°2°aº°d±8Ñ!;Ð =¸dÑ Bð (Ø &ñ(ñ(ç)-©¨bñ1à15°a¸°d±ñ<óˆD��A�‰Jð
 ˜a ˜d™ˆD��A�ŠJñ	$ð$ð €Kr   Ú3zFreturn_grad : bool
        Whether or not to also return the gradient
z7grad : nparray
        Gradient if return_grad == True
Ú7zB1/(d_j*d_k) * ((f(x + d[j]*e[j] + d[k]*e[k]) - f(x + d[j]*e[j])))
)ÚscaleÚextra_paramsÚextra_returnsÚequation_numberÚequationc           	      ó  — t        | «      }t        | d||«      }t        j                  |«      } || f|z   i |¤Ž}	t        j                  |«      }
t        |«      D ]  } || ||d d …f   z   f|z   i |¤Ž|
|<   Œ t        j                  ||«      }t        |«      D ][  }t        ||«      D ]J  } || ||d d …f   z   ||d d …f   z   f|z   i |¤Ž|
|   z
  |
|   z
  |	z   |||f   z  |||f<   |||f   |||f<   ŒL Œ] |r|
|	z
  |z  }||fS |S )Nr   ©r   r   r   rC   r    r$   rD   )r   r'   r   r(   r)   Úreturn_gradr   r   rF   r+   Úgr;   rG   rH   r-   s                  r   Úapprox_hess1rU   ]  s\  € ô 	ˆA‹€AÜ�Q˜˜7 AÓ&€AÜ	�‰�‹€Bá	
ˆaˆT�$‰YÐ	"˜6Ñ	"€Bä
�‰�‹€AÜ�1‹Xò 2ˆÙ�A�b˜šA˜‘h‘J�= Ñ%Ð1¨&Ñ1ˆˆ!Šð2ô �8‰8�A�q‹>€Dä�1‹Xò $ˆÜ�q˜!“ò 	$ˆAÙ˜q 2 aª d¡8™|¨b°²A°©hÑ6Ð8¸4Ñ?ÐKÀFÑKØ˜A™$ñØ!" 1¡ñ&Ø(*ñ+Ø,0°°A°©Jñ7ˆD��A�‰Jà˜a ˜d™ˆD��A�ŠJñ	$ð$ñ
 Ø�B‘˜‰zˆØ�TˆzÐàˆr   z7grad : ndarray
        Gradient if return_grad == True
Ú8zã1/(2*d_j*d_k) * ((f(x + d[j]*e[j] + d[k]*e[k]) - f(x + d[j]*e[j])) -
                 (f(x + d[k]*e[k]) - f(x)) +
                 (f(x - d[j]*e[j] - d[k]*e[k]) - f(x + d[j]*e[j])) -
                 (f(x - d[k]*e[k]) - f(x)))
c           
      óÒ  — t        | «      }t        | d||«      }t        j                  |«      } || f|z   i |¤Ž}	t        j                  |«      }
t        j                  |«      }t        |«      D ]4  } || ||d d …f   z   f|z   i |¤Ž|
|<    || ||d d …f   z
  f|z   i |¤Ž||<   Œ6 t        j                  ||«      }t        |«      D ]Ž  }t        ||«      D ]}  } || ||d d …f   z   ||d d …f   z   f|z   i |¤Ž|
|   z
  |
|   z
  |	z    || ||d d …f   z
  ||d d …f   z
  f|z   i |¤Žz   ||   z
  ||   z
  |	z   d|||f   z  z  |||f<   |||f   |||f<   Œ Œ� |r|
|	z
  |z  }||fS |S )Nr   r   rR   )r   r'   r   r(   r)   rS   r   r   rF   r+   rT   Úggr;   rG   rH   r-   s                   r   Úapprox_hess2rY   ƒ  sñ  € ô$ 	ˆA‹€Aä�Q˜˜7 AÓ&€AÜ	�‰�‹€BÙ	
ˆaˆT�$‰YÐ	"˜6Ñ	"€Bä
�‰�‹€AÜ	�‰�!‹€BÜ�1‹Xò 3ˆÙ�A�b˜šA˜‘h‘J�= Ñ%Ð1¨&Ñ1ˆˆ!‰Ù�Q�r˜!šQ˜$‘x‘Z�M $Ñ&Ð2¨6Ñ2ˆˆ1Šð3ô �8‰8�A�q‹>€Dä�1‹Xò $ˆÜ�q˜!“ò 	$ˆAÙ˜q 2 aª d¡8™|¨b°²A°©hÑ6Ð8¸4Ñ?ÐKÀFÑKØ˜A™$ñØ!" 1¡ñ&Ø(*ñ+á˜q 2 aª d¡8™|¨b°²A°©hÑ6Ð8¸4Ñ?ÐKÀFÑKñLð ˜Q™%ñ ð #% Q¡%ñ(ð +-ñ-ð 01°4¸¸1¸±:©~ñ?ˆD��A�‰Jð ˜a ˜d™ˆD��A�ŠJñ	$ð$ñ Ø�B‘˜‰zˆØ�TˆzÐàˆr   Ú4Ú Ú9a	  1/(4*d_j*d_k) * ((f(x + d[j]*e[j] + d[k]*e[k]) - f(x + d[j]*e[j]
                                                     - d[k]*e[k])) -
                 (f(x - d[j]*e[j] + d[k]*e[k]) - f(x - d[j]*e[j]
                                                     - d[k]*e[k]))c                 ó4  — t        | «      }t        | d||«      }t        j                  |«      }t        j                  ||«      }t        |«      D ]Å  }	t        |	|«      D ]´  }
t        j                   || ||	d d …f   z   ||
d d …f   z   f|z   i |¤Ž || ||	d d …f   z   ||
d d …f   z
  f|z   i |¤Žz
   || ||	d d …f   z
  ||
d d …f   z   f|z   i |¤Ž || ||	d d …f   z
  ||
d d …f   z
  f|z   i |¤Žz
  z
  d||	|
f   z  z  «      ||	|
f<   ||	|
f   ||
|	f<   Œ¶ ŒÇ |S )Né   g      @)r   r   r   rC   rD   r$   r&   rE   s              r   Úapprox_hess3r_   ±  su  € ô 	ˆA‹€AÜ�Q˜˜7 AÓ&€AÜ	�‰�‹€BÜ�8‰8�A�q‹>€Dä�1‹Xò 	$ˆÜ�q˜!“ò 	$ˆAÜŸ™Ù�a˜"˜Q¢˜T™(‘l R¨ª1¨¡XÑ-Ð/°$Ñ6ÐB¸6ÑBÙ˜˜B˜q¢!˜t™H™ r¨!ªQ¨$¡xÑ/Ð1°DÑ8ÐD¸VÑDñEá˜˜R ¢1 ™X™¨¨1ªa¨4©Ñ0Ð2°TÑ9ÐE¸fÑEÙ˜1˜r !¢Q $™x™<¨"¨Q²¨T©(Ñ2Ð4°tÑ;ÐGÀÑGñHñIð �t˜A˜q˜D‘z‘Mñ	#óˆD��A�‰Jð ˜a ˜d™ˆD��A�ŠJñ	$ð	$ð €Kr   z&
    This is an alias for approx_hess3)Ú__doc__Únumpyr   Ústatsmodels.compat.pandasr   r   Úfinfor"   r2   r   Ú_hessian_docsr   r0   r3   r>   rA   rI   rU   rY   r_   Úapprox_hessr   r   r   ú<module>rf      sq  ðñóZ ç <ð €b‡h�hˆuƒo×Ñ€ð&€òRð !%¨2°bÀ5ó 7 ðt )-°2¸bØ#(ó+ð\ $(¨b¸ó ) ðX ,0°bÀó ,ð^ "&¨B°ró *ñZ Ø
ððð ðôñ 
ˆ-ÓØ#¨"°RÀUò ó óðñ2 Ø
ððð ðôñ 
ˆ-ÓØ#¨"°RÀUò ó óð ñ< Ø
ØØØðFô	ñ 
ˆ-ÓØ#¨"°Rò ó ó	ðð& €Ø × Ò Ð@Ñ @Ö r   