code: def initstate(y, m, trendtype, seasontype):
      n = len(y)
      if seasontype != "N":
          if n < 4:
              raise ValueError(
                  "You've got to be joking (not enough data)."
              )
          elif n < 3 * m:  # fit simple Fourier model
              fouriery = fourier(
                  y, List(x for x in [m]), List(x for x in [1])
              )
              X_fourier = np.full((n, 4), fill_value=np.nan)
              X_fourier[:, 0] = np.ones(n)
              X_fourier[:, 1] = np.arange(1, n + 1)
              X_fourier[:, 2:4] = fouriery
              coefs, *_ = np.linalg.lstsq(X_fourier, y, rcond=-1)
              if seasontype == "A":
                  y_d = dict(
                      seasonal=y - coefs[0] - coefs[1] * X_fourier[:, 1]
                  )
              else:
                  if not min(y) > 0:
                      raise Exception(
                          "Multiplicative seasonality is not appropriate for zero and negative values"
                      )
                  y_d = dict(
                      seasonal=y
                      / (coefs[0] + coefs[1] * X_fourier[:, 1])
                  )
          else:
              # n is large enough to do a decomposition
              y_d = seasonal_decompose(
                  y,
                  period=m,
                  model="additive"
                  if seasontype == "A"
                  else "multiplicative",
              )
              y_d = dict(seasonal=y_d.seasonal)
          init_seas = y_d["seasonal"][1:m][::-1]
          if seasontype == "A":
              y_sa = y - y_d["seasonal"]
          else:
              init_seas = np.clip(init_seas, a_min=1e-2, a_max=None)
              if init_seas.sum() > m:
                  init_seas = init_seas / np.sum(init_seas + 1e-2)
              y_sa = y / np.clip(
                  y_d["seasonal"], a_min=1e-2, a_max=None
              )
      else:
          m = 1
          init_seas = []
          y_sa = y
      maxn = min(max(10, 2 * m), len(y_sa))
      if trendtype == "N":
          l0 = y_sa[:maxn].mean()
          b0 = None
          return np.concatenate([[l0], init_seas])
      else:  # simple linear regression on seasonally adjusted data
          X = np.full((n, 2), fill_value=np.nan)
          X[:, 0] = np.ones(n)
          X[:, 1] = np.arange(1, n + 1)
          (l, b), *_ = np.linalg.lstsq(X[:maxn], y_sa[:maxn], rcond=-1)
          if trendtype == "A":
              l0 = l
              b0 = b
              # if error type is M then we dont want l0+b0=0
              # so perturb just in case
              if abs(l0 + b0) < 1e-8:
                  l0 = l0 * (1 + 1e-3)
                  b0 = b0 * (1 - 1e-3)
          else:
              l0 = l + b
              if abs(l0) < 1e-8:
                  l0 = 1e-7
              b0 = (l + 2 * b) / l0
              if math.isclose(b0, 0.0, abs_tol=1e-8):
                  div = 1e-8
              else:
                  div = b0
              l0 = l0 / div
              if abs(b0) > 1e10:
                  b0 = np.sign(b0) * 1e10
              if (
                  l0 < 1e-8 or b0 < 1e-8
              ):  # simple linear approximation didnt work
                  l0 = max(y_sa[0], 1e-3)
                  if math.isclose(y_sa[0], 0.0, abs_tol=1e-8):
                      div = 1e-8
                  else:
                      div = y_sa[0]
                  b0 = max(y_sa[1] / div, 1e-3)
      return np.concatenate([[l0, b0], init_seas])
  
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 12 for calling a global function with example usage: initstate(y, m, trendtype, seasontype) and returns: np, np