code: def _fit_lbfgs(
      f,
      score,
      start_params,
      fargs,
      kwargs,
      disp=True,
      maxiter=100,
      callback=None,
      retall=False,
      full_output=True,
      hess=None,
  ):
      """
      Fit using Limited-memory Broyden-Fletcher-Goldfarb-Shannon algorithm.
  
      Parameters
      ----------
      f : function
          Returns negative log likelihood given parameters.
      score : function
          Returns gradient of negative log likelihood with respect to params.
      start_params : array_like, optional
          Initial guess of the solution for the loglikelihood maximization.
          The default is an array of zeros.
      fargs : tuple
          Extra arguments passed to the objective function, i.e.
          objective(x,*args)
      kwargs : dict[str, Any]
          Extra keyword arguments passed to the objective function, i.e.
          objective(x,**kwargs)
      disp : bool
          Set to True to print convergence messages.
      maxiter : int
          The maximum number of iterations to perform.
      callback : callable callback(xk)
          Called after each iteration, as callback(xk), where xk is the
          current parameter vector.
      retall : bool
          Set to True to return list of solutions at each iteration.
          Available in Results object's mle_retvals attribute.
      full_output : bool
          Set to True to have all available output in the Results object's
          mle_retvals attribute. The output is dependent on the solver.
          See LikelihoodModelResults notes section for more information.
      hess : str, optional
          Method for computing the Hessian matrix, if applicable.
  
      Returns
      -------
      xopt : ndarray
          The solution to the objective function
      retvals : dict, None
          If `full_output` is True then this is a dictionary which holds
          information returned from the solver used. If it is False, this is
          None.
  
      Notes
      -----
      Within the mle part of statsmodels, the log likelihood function and
      its gradient with respect to the parameters do not have notationally
      consistent sign.
      """
      check_kwargs(
          kwargs,
          (
              "m",
              "pgtol",
              "factr",
              "maxfun",
              "epsilon",
              "approx_grad",
              "bounds",
              "loglike_and_score",
              "iprint",
          ),
          "lbfgs",
      )
      # Use unconstrained optimization by default.
      bounds = kwargs.setdefault(
          "bounds", [(None, None)] * len(start_params)
      )
      kwargs.setdefault("iprint", 0)
  
      # Pass the following keyword argument names through to fmin_l_bfgs_b
      # if they are present in kwargs, otherwise use the fmin_l_bfgs_b
      # default values.
      names = (
          "m",
          "pgtol",
          "factr",
          "maxfun",
          "epsilon",
          "approx_grad",
      )
      extra_kwargs = {x: kwargs[x] for x in names if x in kwargs}
  
      # Extract values for the options related to the gradient.
      approx_grad = kwargs.get("approx_grad", False)
      loglike_and_score = kwargs.get("loglike_and_score", None)
      epsilon = kwargs.get("epsilon", None)
  
      # The approx_grad flag has superpowers nullifying the score function arg.
      if approx_grad:
          score = None
  
      # Choose among three options for dealing with the gradient (the gradient
      # of a log likelihood function with respect to its parameters
      # is more specifically called the score in statistics terminology).
      # The first option is to use the finite-differences
      # approximation that is built into the fmin_l_bfgs_b optimizer.
      # The second option is to use the provided score function.
      # The third option is to use the score component of a provided
      # function that simultaneously evaluates the log likelihood and score.
      if epsilon and not approx_grad:
          raise ValueError(
              "a finite-differences epsilon was provided "
              "even though we are not using approx_grad"
          )
      if approx_grad and loglike_and_score:
          raise ValueError(
              "gradient approximation was requested "
              "even though an analytic loglike_and_score function "
              "was given"
          )
      if loglike_and_score:
          func = lambda p, *a: tuple(
              -x for x in loglike_and_score(p, *a)
          )
      elif score:
          func = f
          extra_kwargs["fprime"] = score
      elif approx_grad:
          func = f
  
      retvals = optimize.fmin_l_bfgs_b(
          func,
          start_params,
          maxiter=maxiter,
          callback=callback,
          args=fargs,
          bounds=bounds,
          disp=disp,
          **extra_kwargs,
      )
  
      if full_output:
          xopt, fopt, d = retvals
          # The warnflag is
          # 0 if converged
          # 1 if too many function evaluations or too many iterations
          # 2 if stopped for another reason, given in d['task']
          warnflag = d["warnflag"]
          converged = warnflag == 0
          gopt = d["grad"]
          fcalls = d["funcalls"]
          iterations = d["nit"]
          retvals = {
              "fopt": fopt,
              "gopt": gopt,
              "fcalls": fcalls,
              "warnflag": warnflag,
              "converged": converged,
              "iterations": iterations,
          }
      else:
          xopt = retvals[0]
          retvals = None
  
      return xopt, retvals
  
imports: from __future__ import annotations
  from statsmodels.compat.scipy import SP_LT_15, SP_LT_17
  from typing import Any
  from collections.abc import Sequence
  import numpy
  from scipy import optimize
label_desc: Use image node_id 6 for calling a global function with example usage: _fit_lbfgs(f, score, start_params, fargs, kwargs, disp, maxiter, callback, retall, full_output, hess) and returns: xopt,  retvals