class_docstr: Implements a simple HMM fitted with Viterbi algorithm.
  
  The HMM annotation estimator uses the
  the Viterbi algorithm to fit a sequence of 'hidden state' class
  annotations (represented by an array of integers the same size
  as the observation) to a sequence of observations.
  
  This is done by finding the most likely path given the emission
  probabilities - (ie the probability that a particular observation
  would be generated by a given hidden state), the transition prob
  (ie the probability of transitioning from one state to another or
  staying in the same state) and the initial probabilities - ie the
  belief of the probability distribution of hidden states at the
  start of the observation sequence).
  
  Current assumptions/limitations of this implementation:
     - the spacing of time series points is assumed to be equivalent.
     - it only works on univariate data.
     - the emission parameters and transition probabilities are
         assumed to be known.
     - if no initial probs are passed, uniform probabilities are
         assigned (ie rather than the stationary distribution.)
     - requires and returns np.ndarrays.
  
  _fit is currently empty as the parameters of the probability
  distribution are required to be passed to the algorithm.
  
  _predict - first the transition_probability and transition_id matrices are
  calculated - these are both nxm matrices, where n is the number of
  hidden states and m is the number of observations. The transition
  probability matrices record the probability of the most likely
  sequence which has observation `m` being assigned to hidden state n.
  The transition_id matrix records the step before hidden state n that
  proceeds it in the most likely path.  This logic is mostly carried
  out by helper function _calculate_trans_mats.
  Next, these matrices are used to calculate the most likely
  path (by backtracing from the final mostly likely state and the
  id's that proceeded it.)  This logic is done via a helper func
  hmm_viterbi_label.
  
  Parameters
  ----------
  emission_funcs : list, shape = [num hidden states]
      List should be of length n (the number of hidden states)
      Either a list of callables [fx_1, fx_2] with signature fx_1(X) -> float
      or a list of callables and matched keyword arguments for those
      callables [(fx_1, kwarg_1), (fx_2, kwarg_2)] with signature
      fx_1(X, **kwargs) -> float (or a list with some mixture of the two).
      The callables should take a value and return a probability when passed
      a single observation. All functions should be properly normalized PDFs
      over the same space as the observed data.
  transition_prob_mat: 2D np.ndarry, shape = [num_states, num_states]
      Each row should sum to 1 in order to be properly normalized
      (ie the j'th column in the i'th row represents the
      probability of transitioning from state i to state j.)
  initial_probs: 1D np.ndarray, shape = [num hidden states], optional
      A array of probabilities that the sequence of hidden states starts in each
      of the hidden states. If passed, should be of length `n` the number of
      hidden states and  should match the length of both the emission funcs
      list and the transition_prob_mat. The initial probs should be reflective
      of prior beliefs.  If none is passed will each hidden state will be
      assigned an equal initial prob.
  
  Attributes
  ----------
  emission_funcs : list, shape = [num_hidden_states]
      The functions to use in calculating the emission probabilities. Taken
      from the __init__ param of same name.
  transition_prob_mat: 2D np.ndarry, shape = [num_states, num_states]
      Matrix of transition probabilities from hidden state to hidden state.
      Taken from the __init__ param of same name.
  initial_probs : 1D np.ndarray, shape = [num_hidden_states]
      Probability over the hidden state identity of the first state. If the
      __init__ param of same name was passed it will take on that value.
      Otherwise it is set to be uniform over all hidden states.
  num_states : int
      The number of hidden states.  Set to be the length of the emission_funcs
      parameter which was passed.
  states : list
      A list of integers from 0 to num_states-1.  Integer labels for the hidden
      states.
  num_obs : int
      The length of the observations data.  Extracted from data.
  trans_prob : 2D np.ndarray, shape = [num_observations, num_hidden_states]
      Shape [num observations, num hidden states]. The max probability that that
      observation is assigned to that hidden state.
      Calculated in _calculate_trans_mat and assigned in _predict.
  trans_id : 2D np.ndarray, shape = [num_observations, num_hidden_states]
      Shape [num observations, num hidden states]. The state id of the state
      proceeding the observation is assigned to that hidden state in the most
      likely path where that occurs. Calculated in _calculate_trans_mat and
      assigned in _predict.
  
  Examples
  --------
  >>> from sktime.annotation.hmm import HMM
  >>> from scipy.stats import norm
  >>> from numpy import asarray
  >>> # define the emission probs for our HMM model:
  >>> centers = [3.5,-5]
  >>> sd = [.25 for i in centers]
  >>> emi_funcs = [(norm.pdf, {'loc': mean,
  ...  'scale': sd[ind]}) for ind, mean in enumerate(centers)]
  >>> hmm_est = HMM(emi_funcs, asarray([[0.25,0.75], [0.666, 0.333]]))
  >>> # generate synthetic data (or of course use your own!)
  >>> obs = asarray([3.7,3.2,3.4,3.6,-5.1,-5.2,-4.9])
  >>> hmm_est = hmm_est.fit(obs)
  >>> labels = hmm_est.predict(obs)
class_docstr_tok: ["Implements","a","simple","HMM","fitted","with","Viterbi","algorithm",".","The","HMM","annotation","estimator","uses","the","the","Viterbi","algorithm","to","fit","a","sequence","of","'hidden","state","'","class","annotations","(","represented","by","an","array","of","integers","the","same","size","as","the","observation",")","to","a","sequence","of","observations",".","This","is","done","by","finding","the","most","likely","path","given","the","emission","probabilities","-","(","ie","the","probability","that","a","particular","observation","would","be","generated","by","a","given","hidden","state",")",",","the","transition","prob","(","ie","the","probability","of","transitioning","from","one","state","to","another","or","staying","in","the","same","state",")","and","the","initial","probabilities","-","ie","the","belief","of","the","probability","distribution","of","hidden","states","at","the","start","of","the","observation","sequence",")",".","Current","assumptions\/limitations","of","this","implementation",":","-","the","spacing","of","time","series","points","is","assumed","to","be","equivalent",".","-","it","only","works","on","univariate","data",".","-","the","emission","parameters","and","transition","probabilities","are","assumed","to","be","known",".","-","if","no","initial","probs","are","passed",",","uniform","probabilities","are","assigned","(","ie","rather","than","the","stationary","distribution",".",")","-","requires","and","returns","np.ndarrays",".","_fit","is","currently","empty","as","the","parameters","of","the","probability","distribution","are","required","to","be","passed","to","the","algorithm",".","_predict","-","first","the","transition_probability","and","transition_id","matrices","are","calculated","-","these","are","both","nxm","matrices",",","where","n","is","the","number","of","hidden","states","and","m","is","the","number","of","observations",".","The","transition","probability","matrices","record","the","probability","of","the","most","likely","sequence","which","has","observation","`","m","`","being","assigned","to","hidden","state","n.","The","transition_id","matrix","records","the","step","before","hidden","state","n","that","proceeds","it","in","the","most","likely","path",".","This","logic","is","mostly","carried","out","by","helper","function","_calculate_trans_mats",".","Next",",","these","matrices","are","used","to","calculate","the","most","likely","path","(","by","backtracing","from","the","final","mostly","likely","state","and","the","id","'s","that","proceeded","it",".",")","This","logic","is","done","via","a","helper","func","hmm_viterbi_label",".","Parameters","--","--","--","--","--","emission_funcs",":","list",",","shape","=","[","num","hidden","states","]","List","should","be","of","length","n","(","the","number","of","hidden","states",")","Either","a","list","of","callables","[","fx_1",",","fx_2","]","with","signature","fx_1","(","X",")","-",">","float","or","a","list","of","callables","and","matched","keyword","arguments","for","those","callables","[","(","fx_1",",","kwarg_1",")",",","(","fx_2",",","kwarg_2",")","]","with","signature","fx_1","(","X",",","*","*","kwargs",")","-",">","float","(","or","a","list","with","some","mixture","of","the","two",")",".","The","callables","should","take","a","value","and","return","a","probability","when","passed","a","single","observation",".","All","functions","should","be","properly","normalized","PDFs","over","the","same","space","as","the","observed","data",".","transition_prob_mat",":","2D","np.ndarry",",","shape","=","[","num_states",",","num_states","]","Each","row","should","sum","to","1","in","order","to","be","properly","normalized","(","ie","the","j'th","column","in","the","i'th","row","represents","the","probability","of","transitioning","from","state","i","to","state","j",".",")","initial_probs",":","1D","np.ndarray",",","shape","=","[","num","hidden","states","]",",","optional","A","array","of","probabilities","that","the","sequence","of","hidden","states","starts","in","each","of","the","hidden","states",".","If","passed",",","should","be","of","length","`","n","`","the","number","of","hidden","states","and","should","match","the","length","of","both","the","emission","funcs","list","and","the","transition_prob_mat",".","The","initial","probs","should","be","reflective","of","prior","beliefs",".","If","none","is","passed","will","each","hidden","state","will","be","assigned","an","equal","initial","prob",".","Attributes","--","--","--","--","--","emission_funcs",":","list",",","shape","=","[","num_hidden_states","]","The","functions","to","use","in","calculating","the","emission","probabilities",".","Taken","from","the","__init__","param","of","same","name",".","transition_prob_mat",":","2D","np.ndarry",",","shape","=","[","num_states",",","num_states","]","Matrix","of","transition","probabilities","from","hidden","state","to","hidden","state",".","Taken","from","the","__init__","param","of","same","name",".","initial_probs",":","1D","np.ndarray",",","shape","=","[","num_hidden_states","]","Probability","over","the","hidden","state","identity","of","the","first","state",".","If","the","__init__","param","of","same","name","was","passed","it","will","take","on","that","value",".","Otherwise","it","is","set","to","be","uniform","over","all","hidden","states",".","num_states",":","int","The","number","of","hidden","states",".","Set","to","be","the","length","of","the","emission_funcs","parameter","which","was","passed",".","states",":","list","A","list","of","integers","from","0","to","num_states-1",".","Integer","labels","for","the","hidden","states",".","num_obs",":","int","The","length","of","the","observations","data",".","Extracted","from","data",".","trans_prob",":","2D","np.ndarray",",","shape","=","[","num_observations",",","num_hidden_states","]","Shape","[","num","observations",",","num","hidden","states","]",".","The","max","probability","that","that","observation","is","assigned","to","that","hidden","state",".","Calculated","in","_calculate_trans_mat","and","assigned","in","_predict",".","trans_id",":","2D","np.ndarray",",","shape","=","[","num_observations",",","num_hidden_states","]","Shape","[","num","observations",",","num","hidden","states","]",".","The","state","id","of","the","state","proceeding","the","observation","is","assigned","to","that","hidden","state","in","the","most","likely","path","where","that","occurs",".","Calculated","in","_calculate_trans_mat","and","assigned","in","_predict",".","Examples","--","--","--","--",">",">",">","from","sktime.annotation.hmm","import","HMM",">",">",">","from","scipy.stats","import","norm",">",">",">","from","numpy","import","asarray",">",">",">","#","define","the","emission","probs","for","our","HMM","model",":",">",">",">","centers","=","[","3.5",",","-5","]",">",">",">","sd","=","[",".25","for","i","in","centers","]",">",">",">","emi_funcs","=","[","(","norm.pdf",",","{","'loc","'",":","mean",",","...","'scale","'",":","sd","[","ind","]","}",")","for","ind",",","mean","in","enumerate","(","centers",")","]",">",">",">","hmm_est","=","HMM","(","emi_funcs",",","asarray","(","[","[","0.25,0.75","]",",","[","0.666",",","0.333","]","]",")",")",">",">",">","#","generate","synthetic","data","(","or","of","course","use","your","own","!",")",">",">",">","obs","=","asarray","(","[","3.7,3.2,3.4,3.6",",","-5.1",",","-5.2",",","-4.9","]",")",">",">",">","hmm_est","=","hmm_est.fit","(","obs",")",">",">",">","labels","=","hmm_est.predict","(","obs",")"]
docstr: Determine the most likely seq of hidden states by Viterbi algorithm.
  
  Parameters
  ----------
  X : 1D np.array, shape = [num_observations]
      Observations to apply labels to.
  
  Returns
  -------
  annotated_x : array-like, shape = [num_observations]
      Array of predicted class labels, same size as input.
docstr_tok: ["Determine","the","most","likely","seq","of","hidden","states","by","Viterbi","algorithm",".","Parameters","--","--","--","--","--","X",":","1D","np.array",",","shape","=","[","num_observations","]","Observations","to","apply","labels","to",".","Returns","--","--","--","-","annotated_x",":","array-like",",","shape","=","[","num_observations","]","Array","of","predicted","class","labels",",","same","size","as","input","."]
code: def _predict(self, X):
      """Determine the most likely seq of hidden states by Viterbi algorithm.
  
      Parameters
      ----------
      X : 1D np.array, shape = [num_observations]
          Observations to apply labels to.
  
      Returns
      -------
      annotated_x : array-like, shape = [num_observations]
          Array of predicted class labels, same size as input.
      """
      self.num_states = len(self.emission_funcs)
      self.states = list(range(self.num_states))
      self.num_obs = len(X)
      emi_probs = self._make_emission_probs(self.emission_funcs, X)
      init_probs = self.initial_probs
      if self.initial_probs is None:
          init_probs = 1.0 / self.num_states * np.ones(self.num_states)
      trans_prob, trans_id = self._calculate_trans_mats(
          init_probs,
          emi_probs,
          self.transition_prob_mat,
          self.num_obs,
          self.num_states,
      )
  
      self.trans_prob = trans_prob
      self.trans_id = trans_id
      return self._hmm_viterbi_label(
          self.num_obs, self.states, self.trans_prob, self.trans_id
      )
  
file_path: sktime/sktime/annotation/hmm.py
imports: import warnings
  from typing import Tuple
  import numpy
  from scipy.stats import norm
  from sktime.annotation.base._base import BaseSeriesAnnotator
label_desc: Use image node_id 7 for calling the HMM obj's underlying member method code with example usage: obj._predict(X) and returns: self