Beispiel #1
0
def make_multi_Gaussian_HMM(t_1, t_2, m_1, m_2, s_1, s_2):
  K = 2
  A = np.array([[t_1, 1. - t_1], [1. - t_2, t_2]])
  pi = np.array([0.5, 0.5])
  D = [mnorm([m_1, s_1]), mnorm([m_2, s_2])]
  
  return HMM(K, A, pi, D)
Beispiel #2
0
def easy_case(eps):
  """
    When K = 2, the transition matrix is 0.5 everywhere,
    and state 0 emits only 0 and state 1 emits only 1s,
    forward backward should guess 0's (p = 1) whenever it sees
    a 0 and a 1 (p = 1) whenever it sees a 1.
  """
  #set hyperparams
  K = 2
  T = 10

  #initialize estimates
  x = [[0 for j in range(0,T)] + [1 for j in range(0,T)]]
  B = [mn(np.array([.999, 0.001])), mn(np.array([0.001, .999]))]
  A = np.array([[0.5,0.5],[0.5,0.5]])
  pi = [0.5,0.5]
  M = HMM(2, A, pi, B)
  S = States(M, x)
  S.e_step()
  gamma = np.exp(np.array(S.gamma[0]))
  xi = np.exp(np.array(S.xi[0]))
  gamma_correct = np.array([[1.0,0.0] for j in range(0,T)]
    + [[0.0,1.0] for j in range(0,T)])
  xi_correct = ([np.array([[1,0], [0,0]]) for j
                in range(0, T - 1)] + 
                [np.array([[0,1], [0,0]])] +
                [np.array([[0,0], [0,1]]) for j in range(0, T - 1)])

  gamma_score = check_gamma(gamma, gamma_correct, eps)
  xi_score = True
  for j in range(0, len(xi)):
    xi_score = xi_score and check_gamma(xi[j], xi_correct[j], eps)

  return gamma_score and xi_score
Beispiel #3
0
def make_HMM():
  t_1 = 0.9
  t_2 = 0.1
  m_1 = np.array([0., 0.])
  m_2 = np.array([10., 10.])
  s_1 = np.eye(2)
  s_2 = np.eye(2)
  K = 2
  A = np.array([[t_1, 1. - t_1], [1. - t_2, t_2]])
  pi = np.array([0.5, 0.5])
  D = [mnorm([m_1, s_1]), mnorm([m_2, s_2])]
  
  return HMM(K, A, pi, D)
Beispiel #4
0
def biased_coins_HMM(p, q, t_1, t_2):
    """
  This method returns a 2 state HMM, where state 1 is a p-biased coin,
  state 2 is a q-biased coin, the probability of staying in state
  1 is t_1, and the probability of staying in state 2 is t_2.
  The initial distribution pi is uniform.
  """
    K = 2
    A = np.array([[t_1, 1. - t_1], [1. - t_2, t_2]])
    eigval, eigvec = np.linalg.eig(A)
    maxind = max((l, i) for i, l in enumerate(eigval))[1]
    pi = eigvec[:, maxind]
    if np.min(pi) < 0:
        pi = -pi
        pi = (1. / np.sum(pi)) * pi
    D = [mn([p, 1. - p]), mn([q, 1. - q])]

    return HMM(K, A, pi, D)
Beispiel #5
0
def umbrella_example(eps):
  """
  Umbrella example from wikipedia
  """

  x = [[0, 0, 1, 0, 0]]
  A = np.array([[0.7,0.3], [0.3,0.7]])
  D = [mn([0.9, 0.1]), mn([0.2, 0.8])]
  pi = np.array([0.5,0.5])
  M = HMM(2, A, pi, D)
  S = States(M, x)
  S.e_step()
  gamma = np.exp(np.array(S.gamma[0]))
  xi = np.exp(np.array(S.xi[0]))
  gamma_correct = np.array([[0.8673,0.1327],[0.82,.18],
    [0.31,0.70],[0.82,0.18],[0.87,0.13]])
  xi_correct = np.array([[.75,.12],[.07,.06]])
  gamma_score = check_gamma(gamma, gamma_correct, eps)
  xi_score = check_gamma(xi[0], xi_correct, eps)
  #if not gamma_score:
  #  print("    gamma computation failed")
  #if not xi_score:
  #  print("    xi computation failed")
  return gamma_score and xi_score