-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate_c.py
78 lines (65 loc) · 2.28 KB
/
generate_c.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
# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python
# https://udemy.com/unsupervised-machine-learning-hidden-markov-models-in-python
# http://lazyprogrammer.me
# Generate continuous data from an HMM.
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
def simple_init():
M = 1
K = 1
D = 1
pi = np.array([1])
A = np.array([[1]])
R = np.array([[1]])
mu = np.array([[[0]]])
sigma = np.array([[[[1]]]])
return M, K, D, pi, A, R, mu, sigma
def big_init():
M = 5
K = 3
D = 2
pi = np.array([1, 0, 0, 0, 0]) # initial state distribution
A = np.array([
[0.9, 0.025, 0.025, 0.025, 0.025],
[0.025, 0.9, 0.025, 0.025, 0.025],
[0.025, 0.025, 0.9, 0.025, 0.025],
[0.025, 0.025, 0.025, 0.9, 0.025],
[0.025, 0.025, 0.025, 0.025, 0.9],
]) # state transition matrix - likes to stay where it is
R = np.ones((M, K)) / K # mixture proportions
mu = np.array([
[[0, 0], [1, 1], [2, 2]],
[[5, 5], [6, 6], [7, 7]],
[[10, 10], [11, 11], [12, 12]],
[[15, 15], [16, 16], [17, 17]],
[[20, 20], [21, 21], [22, 22]],
]) # M x K x D
sigma = np.zeros((M, K, D, D))
for m in range(M):
for k in range(K):
sigma[m,k] = np.eye(D)
return M, K, D, pi, A, R, mu, sigma
def get_signals(N=20, T=100, init=big_init):
M, K, D, pi, A, R, mu, sigma = init()
X = []
for n in range(N):
x = np.zeros((T, D))
s = 0 # initial state is 0 since pi[0] = 1
r = np.random.choice(K, p=R[s]) # choose mixture
x[0] = np.random.multivariate_normal(mu[s][r], sigma[s][r])
for t in range(1, T):
s = np.random.choice(M, p=A[s]) # choose state
r = np.random.choice(K, p=R[s]) # choose mixture
x[t] = np.random.multivariate_normal(mu[s][r], sigma[s][r])
X.append(x)
return X
if __name__ == '__main__':
T = 500
x = get_signals(1, T)[0]
axis = range(T)
plt.plot(axis, x[:, 0], axis, x[:, 1])
plt.show()