class_docstr: TSM PARENT CLASS
  
  Contains general time series methods to be inherited by models.
  
  Parameters
  ----------
  model_type : str
      The type of model (e.g. 'ARIMA', 'GARCH')
class_docstr_tok: ["TSM","PARENT","CLASS","Contains","general","time","series","methods","to","be","inherited","by","models",".","Parameters","--","--","--","--","--","model_type",":","str","The","type","of","model","(","e.g",".","'ARIMA","'",",","'GARCH","'",")"]
docstr: This function fits models using Maximum Likelihood or Penalized Maximum Likelihood
docstr_tok: ["This","function","fits","models","using","Maximum","Likelihood","or","Penalized","Maximum","Likelihood"]
code: def _optimize_fit(self, obj_type=None, **kwargs):
      """
      This function fits models using Maximum Likelihood or Penalized Maximum Likelihood
      """
  
      preopt_search = kwargs.get(
          "preopt_search", True
      )  # If user supplied
  
      if obj_type == self.neg_loglik:
          method = "MLE"
      else:
          method = "PML"
  
      # Starting values - check to see if model has preoptimize method, if not, simply use default starting values
      if preopt_search is True:
          try:
              phi = self._preoptimize_model(
                  self.latent_variables.get_z_starting_values(), method
              )
              preoptimized = True
          except:
              phi = self.latent_variables.get_z_starting_values()
              preoptimized = False
      else:
          preoptimized = False
          phi = self.latent_variables.get_z_starting_values()
  
      phi = kwargs.get("start", phi).copy()  # If user supplied
  
      # Optimize using L-BFGS-B
      p = optimize.minimize(
          obj_type, phi, method="L-BFGS-B", options={"gtol": 1e-8}
      )
      if preoptimized is True:
          p2 = optimize.minimize(
              obj_type,
              self.latent_variables.get_z_starting_values(),
              method="L-BFGS-B",
              options={"gtol": 1e-8},
          )
          if self.neg_loglik(p2.x) < self.neg_loglik(p.x):
              p = p2
  
      (
          theta,
          Y,
          scores,
          states,
          states_var,
          X_names,
      ) = self._categorize_model_output(p.x)
  
      # Check that matrix is non-singular; act accordingly
      try:
          ihessian = np.linalg.inv(nd.Hessian(obj_type)(p.x))
          ses = np.power(np.abs(np.diag(ihessian)), 0.5)
          self.latent_variables.set_z_values(p.x, method, ses, None)
  
      except:
          ihessian = None
          ses = None
          self.latent_variables.set_z_values(p.x, method, None, None)
  
      self.latent_variables.estimation_method = method
  
      # Change this in future
      try:
          latent_variables_store = self.latent_variables.copy()
      except:
          latent_variables_store = self.latent_variables
  
      return MLEResults(
          data_name=self.data_name,
          X_names=X_names,
          model_name=self.model_name,
          model_type=self.model_type,
          latent_variables=latent_variables_store,
          results=p,
          data=Y,
          index=self.index,
          multivariate_model=self.multivariate_model,
          objective_object=obj_type,
          method=method,
          ihessian=ihessian,
          signal=theta,
          scores=scores,
          z_hide=self._z_hide,
          max_lag=self.max_lag,
          states=states,
          states_var=states_var,
      )
  
imports: from math import exp, sqrt, log, tanh
  import copy
  import sys
  import warnings
  import numpy
  from scipy import optimize
  import numdifftools
  import pandas
  from .covariances import acf
  from .families import Normal
  from .inference import BBVI, BBVIM, MetropolisHastings, norm_post_sim
  from .output import TablePrinter
  from .tests import find_p_value
  from .latent_variables import LatentVariable, LatentVariables
  from .results import BBVIResults, MLEResults, LaplaceResults, MCMCResults
label_desc: Use image node_id 7 for calling the TSM obj's underlying member method code with example usage: obj._optimize_fit(obj_type) and returns: MLEResults