forked from hildensia/bayesian_changepoint_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
43 lines (36 loc) · 1.49 KB
/
example.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
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import seaborn
import cProfile
import bayesian_changepoint_detection.offline_changepoint_detection as offcd
import bayesian_changepoint_detection.generate_data as gd
from functools import partial
if __name__ == '__main__':
show_plot = True
dim = 4
if dim == 1:
partition, data = gd.generate_normal_time_series(7, 50, 200)
else:
partition, data = gd.generate_multinormal_time_series(7, dim, 50, 200)
changes = np.cumsum(partition)
if show_plot:
fig, ax = plt.subplots(figsize=[16,12])
for p in changes:
ax.plot([p,p],[np.min(data),np.max(data)],'r')
for d in range(dim):
ax.plot(data[:,d])
plt.show()
#Q, P, Pcp = offcd.offline_changepoint_detection(data,partial(offcd.const_prior, l=(len(data)+1)),offcd.gaussian_obs_log_likelihood, truncate=-20)
#Q_ifm, P_ifm, Pcp_ifm = offcd.offline_changepoint_detection(data,partial(offcd.const_prior, l=(len(data)+1)),offcd.ifm_obs_log_likelihood,truncate=-20)
Q_full, P_full, Pcp_full = offcd.offline_changepoint_detection(data,partial(offcd.const_prior, l=(len(data)+1)),offcd.fullcov_obs_log_likelihood, truncate=-50)
if show_plot:
fig, ax = plt.subplots(figsize=[18, 16])
ax = fig.add_subplot(2, 1, 1)
for p in changes:
ax.plot([p,p],[np.min(data),np.max(data)],'r')
for d in range(dim):
ax.plot(data[:,d])
ax = fig.add_subplot(2, 1, 2, sharex=ax)
ax.plot(np.exp(Pcp_full).sum(0))
plt.show()