-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathetf_graphical_lasso.py
More file actions
executable file
·67 lines (57 loc) · 2.56 KB
/
etf_graphical_lasso.py
File metadata and controls
executable file
·67 lines (57 loc) · 2.56 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
# Import necessary libraries
import pandas as pd
import numpy as np
import datetime as dt
from pandas_datareader import data as pdr
import yfinance as yf
from sklearn.covariance import GraphicalLassoCV
import seaborn as sns
import matplotlib.pyplot as plt
import networkx as nx
from pylab import rcParams
# Override pandas_datareader with yfinance
yf.pdr_override()
# Set parameters for data retrieval
num_years = 10
start_date = dt.datetime.now() - dt.timedelta(days=num_years * 365.25)
end_date = dt.datetime.now()
# ETF symbols and their respective countries
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"}
# Retrieve adjusted close prices for ETFs
symbols = list(etfs.keys())
etf_data = pdr.get_data_yahoo(symbols, start=start_date, end=end_date)['Adj Close']
# Convert prices to log returns
log_returns = np.log1p(etf_data.pct_change()).dropna()
# Normalize and fit Graphical Lasso model
log_returns_normalized = log_returns / log_returns.std(axis=0)
edge_model = GraphicalLassoCV(cv=10)
edge_model.fit(log_returns_normalized)
# Plot precision matrix as heatmap
rcParams['figure.figsize'] = 15, 10
sns.heatmap(edge_model.precision_, xticklabels=etfs.values(), yticklabels=etfs.values())
plt.title('Precision Matrix Heatmap')
plt.show()
# Prepare data for network graph
precision_df = pd.DataFrame(edge_model.precision_, index=etfs.keys(), columns=etfs.keys())
links = precision_df.stack().reset_index()
links.columns = ['ETF1', 'ETF2', 'Value']
links_filtered = links[(abs(links['Value']) > 0.17) & (links['ETF1'] != links['ETF2'])]
# Build and display the network graph
G = nx.from_pandas_edgelist(links_filtered, 'ETF1', 'ETF2')
pos = nx.spring_layout(G, k=0.2 * 1 / np.sqrt(len(G.nodes())), iterations=20)
plt.figure(figsize=(15, 15))
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', edge_color='grey')
plt.title('ETF Relationships Network Graph')
plt.show()
# Save network graph to file
nx.write_gexf(G, 'etf_network_graph.gexf')