-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_utilities.py
89 lines (70 loc) · 2.18 KB
/
test_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
def mistake_rate(sts, var_q):
incor = 0
for i in xrange(len(sts)):
pred = np.argmax(var_q[i,:])
if pred != sts[i]:
incor += 1
return incor / float(len(sts))
def plot_MAP(var_q, obs):
x = [i for i in range(len(obs))]
plt.plot(x, obs)
colors = ['r', 'b', 'g']
p = np.argmax(var_q[0])
s = 0
for i in xrange(1, len(obs)):
n = np.argmax(var_q[i])
if n != p:
plt.axvspan(s, i - 1, facecolor=colors[p], alpha=0.5)
s = i - 1
p = n
plt.axvspan(s, len(obs) - 1, facecolor=colors[p], alpha=0.5)
plt.xlabel('Time')
plt.ylabel('Feature Value')
plt.show()
def plot_bar(var_q, obs):
colors = ['r', 'b', 'g']
colormap = []
bounds = [0]
p = np.argmax(var_q[0])
for i in xrange(1, len(obs)):
n = np.argmax(var_q[i])
if n != p:
colormap.append(colors[p])
bounds.append(i)
p = n
#cmap = mpl.colors.ListedColormap(colormap)
#norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
def plot_var_q(var_q, sts):
t = np.arange(len(sts))
var_q_true = []
for i in xrange(len(sts)):
var_q_true.append(var_q[i][sts[i]])
plt.fill_between(t, var_q_true)
plt.xlabel('Time')
plt.ylabel('Probability of True State')
plt.show()
# These just generate random parameters for dirichlet {
def generate_rand_init(N):
# Generate Random Parameters
init = np.random.randint(1, 10, N)
return init
def generate_rand_tran(N):
tran = np.random.randint(1, 10, N)
for i in xrange(N - 1):
tmp = np.random.randint(1, 10, N)
tran = np.vstack((tran, tmp))
return tran
# }
def generate_rand_mu(lo, hi, dim, n):
return [np.random.randint(lo, hi, dim) for i in xrange(n)]
def generate_rand_sig(lmbda, dof, n):
# lmbda : The scale matrix, if we're generating an N x N covariance matrix
# this must be an N x N matrix.
#
# dof : degrees of freedom
#
# n : The number of covariance matrices to generate
return [stats.sample_invwishart(lmbda, dof) for i in xrange(n)]