class_docstr: First-order k-regime Markov switching regression model
  
  Parameters
  ----------
  endog : array_like
      The endogenous variable.
  k_regimes : int
      The number of regimes.
  trend : {'n', 'c', 't', 'ct'}
      Whether or not to include a trend. To include an intercept, time trend,
      or both, set `trend='c'`, `trend='t'`, or `trend='ct'`. For no trend,
      set `trend='n'`. Default is an intercept.
  exog : array_like, optional
      Array of exogenous regressors, shaped nobs x k.
  order : int, optional
      The order of the model describes the dependence of the likelihood on
      previous regimes. This depends on the model in question and should be
      set appropriately by subclasses.
  exog_tvtp : array_like, optional
      Array of exogenous or lagged variables to use in calculating
      time-varying transition probabilities (TVTP). TVTP is only used if this
      variable is provided. If an intercept is desired, a column of ones must
      be explicitly included in this array.
  switching_trend : bool or iterable, optional
      If a boolean, sets whether or not all trend coefficients are
      switching across regimes. If an iterable, should be of length equal
      to the number of trend variables, where each element is
      a boolean describing whether the corresponding coefficient is
      switching. Default is True.
  switching_exog : bool or iterable, optional
      If a boolean, sets whether or not all regression coefficients are
      switching across regimes. If an iterable, should be of length equal
      to the number of exogenous variables, where each element is
      a boolean describing whether the corresponding coefficient is
      switching. Default is True.
  switching_variance : bool, optional
      Whether or not there is regime-specific heteroskedasticity, i.e.
      whether or not the error term has a switching variance. Default is
      False.
  
  Notes
  -----
  This model is new and API stability is not guaranteed, although changes
  will be made in a backwards compatible way if possible.
  
  The model can be written as:
  
  .. math::
  
      y_t = a_{S_t} + x_t' \beta_{S_t} + \varepsilon_t \\
      \varepsilon_t \sim N(0, \sigma_{S_t}^2)
  
  i.e. the model is a dynamic linear regression where the coefficients and
  the variance of the error term may be switching across regimes.
  
  The `trend` is accommodated by prepending columns to the `exog` array. Thus
  if `trend='c'`, the passed `exog` array should not already have a column of
  ones.
  
  See the notebook `Markov switching dynamic regression
  <../examples/notebooks/generated/markov_regression.html>`__ for an
  overview.
  
  References
  ----------
  Kim, Chang-Jin, and Charles R. Nelson. 1999.
  "State-Space Models with Regime Switching:
  Classical and Gibbs-Sampling Approaches with Applications".
  MIT Press Books. The MIT Press.
class_docstr_tok: ["First-order","k-regime","Markov","switching","regression","model","Parameters","--","--","--","--","--","endog",":","array_like","The","endogenous","variable",".","k_regimes",":","int","The","number","of","regimes",".","trend",":","{","'n","'",",","'","c","'",",","'t","'",",","'ct","'","}","Whether","or","not","to","include","a","trend",".","To","include","an","intercept",",","time","trend",",","or","both",",","set","`","trend=","'","c","'","`",",","`","trend='t","'","`",",","or","`","trend='ct","'","`",".","For","no","trend",",","set","`","trend='n","'","`",".","Default","is","an","intercept",".","exog",":","array_like",",","optional","Array","of","exogenous","regressors",",","shaped","nobs","x","k.","order",":","int",",","optional","The","order","of","the","model","describes","the","dependence","of","the","likelihood","on","previous","regimes",".","This","depends","on","the","model","in","question","and","should","be","set","appropriately","by","subclasses",".","exog_tvtp",":","array_like",",","optional","Array","of","exogenous","or","lagged","variables","to","use","in","calculating","time-varying","transition","probabilities","(","TVTP",")",".","TVTP","is","only","used","if","this","variable","is","provided",".","If","an","intercept","is","desired",",","a","column","of","ones","must","be","explicitly","included","in","this","array",".","switching_trend",":","bool","or","iterable",",","optional","If","a","boolean",",","sets","whether","or","not","all","trend","coefficients","are","switching","across","regimes",".","If","an","iterable",",","should","be","of","length","equal","to","the","number","of","trend","variables",",","where","each","element","is","a","boolean","describing","whether","the","corresponding","coefficient","is","switching",".","Default","is","True",".","switching_exog",":","bool","or","iterable",",","optional","If","a","boolean",",","sets","whether","or","not","all","regression","coefficients","are","switching","across","regimes",".","If","an","iterable",",","should","be","of","length","equal","to","the","number","of","exogenous","variables",",","where","each","element","is","a","boolean","describing","whether","the","corresponding","coefficient","is","switching",".","Default","is","True",".","switching_variance",":","bool",",","optional","Whether","or","not","there","is","regime-specific","heteroskedasticity",",","i.e",".","whether","or","not","the","error","term","has","a","switching","variance",".","Default","is","False",".","Notes","--","--","-","This","model","is","new","and","API","stability","is","not","guaranteed",",","although","changes","will","be","made","in","a","backwards","compatible","way","if","possible",".","The","model","can","be","written","as",":","..","math",":",":","y_t","=","a_","{","S_t","}","+","x_t","'","\\beta_","{","S_t","}","+","\\varepsilon_t","\\\\","\\varepsilon_t","\\sim","N","(","0",",","\\sigma_","{","S_t","}","^2",")","i.e",".","the","model","is","a","dynamic","linear","regression","where","the","coefficients","and","the","variance","of","the","error","term","may","be","switching","across","regimes",".","The","`","trend","`","is","accommodated","by","prepending","columns","to","the","`","exog","`","array",".","Thus","if","`","trend=","'","c","'","`",",","the","passed","`","exog","`","array","should","not","already","have","a","column","of","ones",".","See","the","notebook","`","Markov","switching","dynamic","regression","<","..","\/examples\/notebooks\/generated\/markov_regression.html",">","`","__","for","an","overview",".","References","--","--","--","--","--","Kim",",","Chang-Jin",",","and","Charles","R.","Nelson",".","1999",".","``","State-Space","Models","with","Regime","Switching",":","Classical","and","Gibbs-Sampling","Approaches","with","Applications","''",".","MIT","Press","Books",".","The","MIT","Press","."]
docstr: EM step for variances
docstr_tok: ["EM","step","for","variances"]
code: def _em_variance(self, result, endog, exog, betas, tmp=None):
      """
      EM step for variances
      """
      k_exog = 0 if exog is None else exog.shape[1]
  
      if self.switching_variance:
          variance = np.zeros(self.k_regimes)
          for i in range(self.k_regimes):
              if k_exog > 0:
                  resid = endog - np.dot(exog, betas[i])
              else:
                  resid = endog
              variance[i] = np.sum(
                  resid**2 * result.smoothed_marginal_probabilities[i]
              ) / np.sum(result.smoothed_marginal_probabilities[i])
      else:
          variance = 0
          if tmp is None:
              tmp = np.sqrt(result.smoothed_marginal_probabilities)
          for i in range(self.k_regimes):
              tmp_endog = tmp[i] * endog
              if k_exog > 0:
                  tmp_exog = tmp[i][:, np.newaxis] * exog
                  resid = tmp_endog - np.dot(tmp_exog, betas[i])
              else:
                  resid = tmp_endog
              variance += np.sum(resid**2)
          variance /= self.nobs
      return variance
  
imports: import numpy
  import statsmodels.base.wrapper
  from statsmodels.tsa.regime_switching import markov_switching
label_desc: Use image node_id 8 for calling the MarkovRegression obj's underlying member method code with example usage: obj._em_variance(result, endog, exog, betas, tmp) and returns: variance