-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetf_graphical_lasso.py
More file actions
executable file
·97 lines (84 loc) · 2.72 KB
/
Copy pathetf_graphical_lasso.py
File metadata and controls
executable file
·97 lines (84 loc) · 2.72 KB
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
88
89
90
91
92
93
94
95
96
97
# Import dependencies
import pandas as pd
import numpy as np
from pandas_datareader import data as pdr
import yfinance
import datetime as dt
from sklearn import covariance
import seaborn as sns
import matplotlib.pyplot as plt
import networkx as nx
from pylab import rcParams
# Use yfinance as the default data source
yfinance.pdr_override()
# Set the number of years to retrieve the data
num_of_years = 10
# Calculate the start and end dates
start = dt.datetime.now() - dt.timedelta(int(365.25 * num_of_years))
end = dt.datetime.now()
# Define a mapping from ticker to country
etfs = {"EWJ": "Japan",
"EWZ": "Brazil",
"FXI": "China",
"EWY": "South Korea",
"EWT": "Taiwan",
"EWH": "Hong Kong",
"EWC": "Canada",
"EWG": "Germany",
"EWU": "United Kingdom",
"EWA": "Australia",
"EWW": "Mexico",
"EWL": "Switzerland",
"EWP": "Spain",
"EWQ": "France",
"EIDO": "Indonesia",
"ERUS": "Russia",
"EWS": "Singapore",
"EWM": "Malaysia",
"EZA": "South Africa",
"THD": "Thailand",
"ECH": "Chile",
"EWI": "Italy",
"TUR": "Turkey",
"EPOL": "Poland",
"EPHE": "Philippines",
"EWD": "Sweden",
"EWN": "Netherlands",
"EPU": "Peru",
"ENZL": "New Zealand",
"EIS": "Israel",
"EWO": "Austria",
"EIRL": "Ireland",
"EWK": "Belgium"}
# Separate the keys and values from the mapping dictionary
symbols, names = np.array(sorted(etfs.items())).T
# Load the data from input.csv into a pandas DataFrame
df = pdr.get_data_yahoo(symbols.tolist(), start, end)['Adj Close']
# Convert price series to log return series
df = np.log1p(df.pct_change()).iloc[1:]
# Fit the Glasso algorithm to the data to calculate the precision matrix
edge_model = covariance.GraphicalLassoCV(cv=10)
df /= df.std(axis=0)
df = df.dropna()
edge_model.fit(df)
p = edge_model.precision_
# Plot the heatmap of the precision matrix
rcParams['figure.figsize'] = 15, 10
sns.heatmap(p)
plt.show()
# Prepare the matrix for network illustration
p = pd.DataFrame(p)
links = p.stack().reset_index()
links.columns = ['var1', 'var2', 'value']
# Filter the links to only include those with absolute value greater than 0.17
# and exclude those with var1 equal to var2
links = links.loc[(abs(links['value']) > 0.17) & (links['var1'] != links['var2'])]
# Build the graph using networkx library
G = nx.from_pandas_edgelist(links, 'var1', 'var2', create_using=nx.Graph())
pos = nx.spring_layout(G, k=0.2 * 1 / np.sqrt(len(G.nodes())), iterations=20)
plt.figure(3, figsize=(15, 15))
nx.draw(G, pos=pos)
nx.draw_networkx_labels(G, pos=pos)
plt.show()
# Write to graph.gexf
nx.write_gexf(G, 'graph.gexf')