code: def myarima(
      x,
      order=(0, 0, 0),
      seasonal={"order": (0, 0, 0), "period": 1},
      constant=True,
      ic="aic",
      trace=False,
      approximation=False,
      offset=0,
      xreg=None,
      method=None,
      **kwargs,
  ):
      missing = np.isnan(x)
      missing_idxs = np.where(~missing)[0]
      firstnonmiss = missing_idxs.min()
      lastnonmiss = missing_idxs.max() + 1
      n = np.sum(~missing[firstnonmiss:lastnonmiss])
      m = seasonal["period"]
      seas_order = seasonal["order"]
      use_season = np.sum(seas_order) > 0 and m > 0
      diffs = order[1] + seas_order[1]
      if method is None:
          if approximation:
              method = "CSS"
          else:
              method = "CSS-ML"
      try:
          if diffs == 1 and constant:
              drift = np.arange(
                  1, x.size + 1, dtype=np.float64
              ).reshape(
                  -1, 1
              )  # drift
              if xreg is not None:
                  xreg = np.concatenate([drift, xreg], axis=1)
              else:
                  xreg = drift
              if use_season:
                  fit = arima(x, order, seasonal, xreg, method=method)
              else:
                  fit = arima(x, order, xreg=xreg, method=method)
              fit["coef"] = change_drift_name(fit["coef"])
          else:
              if use_season:
                  fit = arima(
                      x,
                      order,
                      seasonal,
                      include_mean=constant,
                      method=method,
                      xreg=xreg,
                  )
              else:
                  fit = arima(
                      x,
                      order,
                      include_mean=constant,
                      method=method,
                      xreg=xreg,
                  )
          # nxreg = 0 if xreg is None else xreg.shape[1]
          nstar = n - order[1] - seas_order[1] * m
          if diffs == 1 and constant:
              fit["xreg"] = xreg
          npar = fit["mask"].sum() + 1
          if method == "CSS":
              fit["aic"] = (
                  offset + nstar * math.log(fit["sigma2"]) + 2 * npar
              )
          if not math.isnan(fit["aic"]):
              fit["bic"] = fit["aic"] + npar * (math.log(nstar) - 2)
              fit["aicc"] = fit["aic"] + 2 * npar * (npar + 1) / (
                  nstar - npar - 1
              )
              fit["ic"] = fit[ic]
          else:
              fit["ic"] = fit["aic"] = fit["bic"] = fit[
                  "aicc"
              ] = math.inf
          fit["sigma2"] = np.nansum(fit["residuals"] ** 2) / (
              nstar - npar + 1
          )
          minroot = 2
          if order[0] + seas_order[0] > 0:
              testvec = fit["model"]["phi"]
              k = abs(testvec) > 1e-8
              if k.sum() > 0:
                  last_nonzero = np.max(np.where(k)[0])
              else:
                  last_nonzero = 0
              if last_nonzero > 0:
                  testvec = testvec[: (last_nonzero + 1)]
                  proots = np.polynomial.polynomial.polyroots(
                      np.append(1, -testvec)
                  )
                  if proots.size > 0:
                      minroot = min(minroot, *abs(proots))
          if order[2] + seas_order[2] > 0 and fit["ic"] < math.inf:
              testvec = fit["model"]["theta"]
              k = abs(testvec) > 1e-8
              if np.sum(k) > 0:
                  last_nonzero = np.max(np.where(k)[0])
              else:
                  last_nonzero = 0
              if last_nonzero > 0:
                  testvec = testvec[: (last_nonzero + 1)]
                  proots = np.polynomial.polynomial.polyroots(
                      np.append(1, testvec)
                  )
                  if proots.size > 0:
                      minroot = min(minroot, *abs(proots))
          if minroot < 1 + 0.01 or checkarima(fit):
              fit["ic"] = math.inf
          fit["xreg"] = xreg
          if trace:
              print(f"\n{arima_string(fit, padding=True)}:{fit['ic']}")
          return fit
      except ValueError as e:
          raise e
          return {"ic": math.inf}
  
imports: import math
  import warnings
  from collections import namedtuple
  from functools import partial
  from typing import Optional, Dict, Union, Tuple
  import numpy
  import pandas
  import statsmodels.api
  from numba import njit
  from scipy.optimize import minimize
  from scipy.stats import norm
  from .mstl import mstl
  from .utils import CACHE, NOGIL
label_desc: Use image node_id 24 for calling a global function with example usage: myarima(x, order, seasonal, constant, ic, trace, approximation, offset, xreg, method) and returns: fit, dict