-
Notifications
You must be signed in to change notification settings - Fork 34
/
financeExample.py
31 lines (23 loc) · 976 Bytes
/
financeExample.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
from ggs import *
import numpy as np
import matplotlib.pyplot as plt
# Read in daily log returns from 1997-2015 of 10 indices:
# DM stocks, EM stocks, Real estate, Oil, Gold, HY bonds, EM HY bonds, GVT bonds, CORP bonds, IFL bonds
filename = "Data/Returns.txt"
data = np.genfromtxt(filename,delimiter=' ')
# Select DM stocks, Oil, and GVT bonds
feats = [0,3,7]
data = data.T #Convert to an n-by-T matrix
# Find 10 breakpoints at lambda = 1e-4
bps, objectives = GGS(data, Kmax = 10, lamb = 1e-4, features = feats)
# Find means and covariances of the segments, given the selected breakpoints
bp10 = bps[10] # Get breakpoints for K = 10
meancovs = GGSMeanCov(data, breakpoints = bp10, lamb = 1e-4, features = feats)
print "Breakpoints are at", bps
print "Objectives are", objectives
# Plot objective vs. number of breakpoints
plotVals = range(len(objectives))
plt.plot(plotVals, objectives, 'or-')
plt.xlabel('Number of Breakpoints')
plt.ylabel(r'$\phi(b)$')
plt.show()