code: def optimize_ets_target_fn(
      x0,
      par,
      y,
      nstate,
      errortype,
      trendtype,
      seasontype,
      damped,
      par_noopt,
      lowerb,
      upperb,
      opt_crit,
      nmse,
      bounds,
      m,
      pnames,
      pnames2,
  ):
      alpha = (
          par_noopt["alpha"] if np.isnan(par["alpha"]) else par["alpha"]
      )
      if np.isnan(alpha):
          raise ValueError("alpha problem!")
      if trendtype != "N":
          beta = (
              par_noopt["beta"]
              if np.isnan(par["beta"])
              else par["beta"]
          )
          if np.isnan(beta):
              raise ValueError("beta problem!")
      else:
          beta = np.nan
      if seasontype != "N":
          gamma = (
              par_noopt["gamma"]
              if np.isnan(par["gamma"])
              else par["gamma"]
          )
          if np.isnan(gamma):
              raise ValueError("gamma problem!")
      else:
          m = 1
          gamma = np.nan
      if damped:
          phi = par_noopt["phi"] if np.isnan(par["phi"]) else par["phi"]
          if np.isnan(phi):
              raise ValueError("phi problem!")
      else:
          phi = np.nan
  
      optAlpha = not np.isnan(alpha)
      optBeta = not np.isnan(beta)
      optGamma = not np.isnan(gamma)
      optPhi = not np.isnan(phi)
  
      givenAlpha = False
      givenBeta = False
      givenGamma = False
      givenPhi = False
  
      if not np.isnan(par_noopt["alpha"]):
          optAlpha = False
          givenAlpha = True
      if not np.isnan(par_noopt["beta"]):
          optBeta = False
          givenBeta = True
      if not np.isnan(par_noopt["gamma"]):
          optGamma = False
          givenGamma = True
      if not np.isnan(par_noopt["phi"]):
          optPhi = False
          givenPhi = True
  
      if not damped:
          phi = 1.0
      if trendtype == "N":
          beta = 0.0
      if seasontype == "N":
          gamma = 0.0
      res = nelder_mead_ets(
          x0,
          args=(
              y,
              nstate,
              switch(errortype),
              switch(trendtype),
              switch(seasontype),
              damped,
              lowerb,
              upperb,
              opt_crit,
              nmse,
              bounds,
              m,
              optAlpha,
              optBeta,
              optGamma,
              optPhi,
              givenAlpha,
              givenBeta,
              givenGamma,
              givenPhi,
              alpha,
              beta,
              gamma,
              phi,
          ),
          lower=lowerb,
          upper=upperb,
          tol_std=1e-4,
          max_iter=1_000,
          adaptive=True,
      )
      return res
  
imports: import math
  from collections import namedtuple
  from typing import Tuple
  import numpy
  from numba import njit
  from numba.typed import List
  from statsmodels.tsa.seasonal import seasonal_decompose
  from .utils import _calculate_intervals, CACHE, NOGIL
label_desc: Use image node_id 18 for calling a global function with example usage: optimize_ets_target_fn(x0, par, y, nstate, errortype, trendtype, seasontype, damped, par_noopt, lowerb, upperb, opt_crit, nmse, bounds, m, pnames, pnames2) and returns: res