
    M/Ph                     >    d Z ddlZddlmZ ddlmZ dZ	 	 	 	 dd
ZdS )a  

Created on Mon Mar 18 15:48:23 2013
Author: Josef Perktold

TODO:
  - test behavior if nans or infs are encountered during the evaluation.
    now partially robust to nans, if increasing can be determined or is given.
  - rewrite core loop to use for...except instead of while.

    N)optimize)HolderF h㈵>d   
   c                    ||}}||}n||dk     rt          d          |}nd}||}n-||dk    rt          d          |}nt          d|dz
            }|t          ||dz             }|||||k     sJ  | |g|R  } | |g|R  }t          j        t          j        ||z
                      dk     r|dk    r|d	k    rd
} | |g|R  }||k     }||z
  }t          j        |          r;dD ])}|||z  z   } | |g|R  }t          j        |          s n*t          d          t          j        |          r;dD ])}|||z  z   } | |g|R  }t          j        |          s n*t          d          ||k     }|s||}}||}}d}|5|dk    r/|} | |g|R  dk    r |}||
z  }||k    rn|d	z  } | |g|R  dk     |5|dk    r/|} | |g|R  dk     r |}||
z  }||k    rn|d	z  } | |g|R  dk      ||k    rI | |g|R  } | |g|R  }t          j        |          r#t          j        |          rt          d          t          j        | |||||	|          }|r_|d         }t          |d	         j	        |d	         j
        |d	         j        |d	         j        |d	         j        |||f||f|	  	        }||fS |S )a9
  find the root of a function in one variable by expanding and brentq

    Assumes function ``func`` is monotonic.

    Parameters
    ----------
    func : callable
        function for which we find the root ``x`` such that ``func(x) = 0``
    low : float or None
        lower bound for brentq
    upp : float or None
        upper bound for brentq
    args : tuple
        optional additional arguments for ``func``
    xtol : float
        parameter x tolerance given to brentq
    start_low : float (positive) or None
        starting bound for expansion with increasing ``x``. It needs to be
        positive. If None, then it is set to 1.
    start_upp : float (negative) or None
        starting bound for expansion with decreasing ``x``. It needs to be
        negative. If None, then it is set to -1.
    increasing : bool or None
        If None, then the function is evaluated at the initial bounds to
        determine wether the function is increasing or not. If increasing is
        True (False), then it is assumed that the function is monotonically
        increasing (decreasing).
    max_it : int
        maximum number of expansion steps.
    maxiter_bq : int
        maximum number of iterations of brentq.
    factor : float
        expansion factor for step of shifting the bounds interval, default is
        10.
    full_output : bool, optional
        If full_output is False, the root is returned. If full_output is True,
        the return value is (x, r), where x is the root, and r is a
        RootResults object.


    Returns
    -------
    x : float
        root of the function, value at which ``func(x) = 0``.
    info : RootResult (optional)
        returned if ``full_output`` is True.
        attributes:

         - start_bounds : starting bounds for expansion stage
         - brentq_bounds : bounds used with ``brentq``
         - iterations_expand : number of iterations in expansion stage
         - converged : True if brentq converged.
         - flag : return status, 'converged' if brentq converged
         - function_calls : number of function calls by ``brentq``
         - iterations : number of iterations in ``brentq``


    Notes
    -----
    If increasing is None, then whether the function is monotonically
    increasing or decreasing is inferred from evaluating the function at the
    initial bounds. This can fail if there is numerically no variation in the
    data in this range. In this case, using different starting bounds or
    directly specifying ``increasing`` can make it possible to move the
    expansion in the right direction.

    If

    Nr   zstart_upp needs to be positiveg      ?zstart_low needs to be negativeg      gV瞯<   g:0yE>)g      ?g      ?g      ?z{could not determine whether function is increasing based on starting interval.
specify increasing or change starting boundszzcould not determine whether function isincreasing based on starting interval.
specify increasing or change starting boundszsmax_it reached
the function values at boths bounds are NaN
change the starting bounds, set boundsor increase max_it)argsxtolmaxiterfull_output)	root
iterationsfunction_calls	convergedflagiterations_expandstart_boundsbrentq_bounds
increasing)
ValueErrorminmaxnpabsisnanr   brentqr   r   r   r   r   r   )funclowuppr   r   	start_low	start_uppr   max_it
maxiter_bqfactorr   leftrightsuslf_lowf_uppdeltafractionsl_su_n_itresvalinfos                             ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tools/rootfinding.pybrentq_expandingr7      sW   T s%D 		q===>>>
		q===>>>b2g {R"W 
#+J,>BwwwwR$R$ 6"&''((500R2XX"''BDOdOOOE%-J R8E?? 	+ . 	+ 	+8e++S(4(((x E ! "* + + + 8E?? 
	+- 	+ 	+8e++S(4(((x E ! "* + + +
 em
 "RBTeD|ad4$!##EFNDv~~AID d4$!## }qd5 4   1$$DVOEv~~AID d5 4   1$$ v~~ R$R$8E?? 	3rx 	3 2 3 3 3
 /$e$#Z&13 3 3C  !fQ1v(q60!f&Q"b-!   Dy
    )NNr   r   NNNr   r   r   F)	__doc__numpyr   scipyr   statsmodels.tools.testingr   DEBUGr7   r   r8   r6   <module>r>      sy   
 
           , , , , , , >B@D8:!&H H H H H Hr8   