forked from cvxgrp/GGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloworld.py
58 lines (47 loc) · 1.55 KB
/
helloworld.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
from ggs import *
import numpy as np
import matplotlib.pyplot as plt
import math
np.random.seed(0)
# Generate synthetic 1D data
# First 1000 samples: mean = 1, SD = 1
# Second 1000 samples: mean = 0, SD = 0.1
# Third 1000 samples: mean = 0, SD = 1
d1 = np.random.normal(1,1,1000)
d2 = np.random.normal(0,0.5,1000)
d3 = np.random.normal(-1,1,1000)
data = np.concatenate((d1,d2,d3))
data = np.reshape(data, (3000,1))
data = data.T #Convert to an n-by-T matrix
# Find up to 10 breakpoints at lambda = 1e-1
bps, objectives = GGS(data, Kmax = 10, lamb = 1e-1)
print bps
print objectives
# Plot objective vs. number of breakpoints. Note that the objective essentially
# stops increasing after K = 2, since there are only 2 "true" breakpoints
plotVals = range(len(objectives))
plt.plot(plotVals, objectives, 'or-')
plt.xlabel('Number of Breakpoints')
plt.ylabel(r'$\phi(b)$')
plt.show()
#Plot predicted Mean/Covs
breaks = bps[2]
mcs = GGSMeanCov(data, breaks, 1e-1)
predicted = []
varPlus = []
varMinus = []
for i in range(len(mcs)):
for j in range(breaks[i+1]-breaks[i]):
predicted.append(mcs[i][0]) # Estimate the mean
varPlus.append(mcs[i][0] + math.sqrt(mcs[i][1][0])) # One standard deviation above the mean
varMinus.append(mcs[i][0] - math.sqrt(mcs[i][1][0])) # One s.d. below
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(data)
axarr[0].set_ylim([-4,4])
axarr[0].set_ylabel('Actual Data')
axarr[1].plot(predicted)
axarr[1].plot(varPlus, 'r--')
axarr[1].plot(varMinus, 'r--')
axarr[1].set_ylim([-4,4])
axarr[1].set_ylabel('Predicted mean (+/- 1 S.D.)')
plt.show()