Network Analysis of Financial Markets
(Karthick Venkatesan)
1. Abstract
In this project we have analysed the dynamics of the Financial Markets through Network Analysis.We have built networks of the equities that are part of the S and P 500 index over a range Time Periods between 2007 and 2017 based on Winner Take All and the Minimum Spanning Tree method .Both these methods utilise the correlation coefficient computed between the attributes of these stocks such as Price,Volume,Returns etc . Community detection techniques were then applied to the constructed networks. The resulting communities were compared for consistency with the identified market sections using Standard Industrial Classification code.We also studied the evolution of the network and the communities over the study period and found interesting behaviors. We have compared our results from both the methods for each of our analysis.We created a GEXF file for this dynamic network and visuvalised the same in Gephi a open source network visuvalisation software.The visualization results offer a very intuitive
way to look at the overall correlation structure of different the equities in the S and P 500 and evolution of these networks over a period of time .
2. Introduction
Network analysis of Equities is a extensively researched topic and in section 3 we have detailed current literature which was utilised as part of this project.In all the current studies the focus has been on studying the properties of the market in a stationary view for a fixed time period.By leveraging the techniques noted in these current literature we have as part of this study built multiple networks of the stocks in the S and P 500 index for mutiple non overlapping windows of Time Period (T) between 2007 and 2017 and studied how the network evolves and how the communities in the network behave with the changing dynamics of the market.
Below are key Objectives of the project.For each of these items we have compared the results we got for for the networks built based on both these methods.
1. Build network for the stocks in S and P 500 index based on the correlations between Prices/Volume for Multiple Time Periods using Winner Take All method and Minimum Spanning Tree Method
2. Analyse the topology of the networks in multiple time periods.Does the network of stocks exhibit scale free properties at each of the time period?
3. Detect communities in these networks and find out if the stocks actually trade in groups based on the SIC(Standard Industry Classification) Code.
4. Studied the evolution of these communities
5. Find important stocks and sectors they belong to based on the Network Properties at Different time periods.
6. Visuvalize the network and also the dynamic evolution of network by building dynamic graphs using Gephi
3. Literature Review
Current Studies about network analysis for stock market can be classified
into below categories :
(1) Applying network analysis techniques for different markets and analyze the topological characteristics of each market
Statistical Analysis of Financial Markets,
Hierarchical structure in financial markets
(2) Propose different correlation metric analysis among various stock markets to suggest different definitions of edges between stocks and study the impact on the network using different edge definitions
Network analysis of a financial market based on genuine correlation and threshold method,
Network of Equities in Financial Markets,
A network perspective of the stock market
3.1. Edge Definition
Approach to construct the edges of stock market network is
not unique. In the current literature, multiple measures were
investigated to construct the edges between nodes namely Zero-lag correlation,Detrended covariance,Time-lag correlations of prices changes over a certain period of time
3.2. Network Properties
Studies have covered both emerging and mature markets. Authors
claim that understanding the topological properties can
help to understand correlation patterns among stocks, thus providing
guidance for risk management. Topological properties
often of interest include degree distribution, clustering and component
structure. In this subcategory study, usually only one
correlation measure is proposed to establish the connections
between nodes. In the introduction session of Statistical Analysis of Financial Markets, the author
covered a wide range of previous studies in this category
4. Build Network
4.1 Data Collection
We collected the prices for the stocks that trade in both NASDAQ and the NYSE stock exchange from Eod Data . The data consisted of Opening , Closing prices , Volume information for each trading day for the period of 2007 to 2017.From this data we filtered and selected only the prices that are a part of S and P 500 . We chose the S and P 500 since the index had a well balanced portfolio of stocks from different industry segments .
## Read S and P 500 list
import pandas as pd
import numpy as np
dfsp500 = pd.read_csv('data/SANDP500.csv')
companies=dfsp500['Symbol'].tolist()
companies=np.random.choice(companies, size=500, replace=False)
import glob
import os
path = r'data/NASDAQ'
all_files = glob.glob(os.path.join(path, "*.txt"))
df_from_each_file = (pd.read_csv(f) for f in all_files)
concatenated_df_NAS = pd.concat(df_from_each_file, ignore_index=True)
concatenated_df_NAS=concatenated_df_NAS[concatenated_df_NAS['<ticker>'].isin(companies)]
path = r'data/NYSE'
all_files = glob.glob(os.path.join(path, "*.txt"))
df_from_each_file = (pd.read_csv(f) for f in all_files)
concatenated_df_NYS = pd.concat(df_from_each_file, ignore_index=True)
concatenated_df_NYS=concatenated_df_NYS[concatenated_df_NYS['<ticker>'].isin(companies)]
concatenated_df = pd.concat([concatenated_df_NAS,concatenated_df_NYS])
col_p = 'close'
concatenated_df.columns = ['ticker','date','open','high','low','close','vol']
concatenated_df=concatenated_df[concatenated_df['ticker'].isin(companies)]
concatenated_df=concatenated_df.merge(dfsp500,left_on='ticker',right_on='Symbol')
concatenated_df['ticker'] = concatenated_df['ticker']
df_price = concatenated_df[['ticker','date',col_p]]
df_price=df_price.drop_duplicates( keep='last')
df_price['date'] = pd.to_datetime(df_price['date'], format='%Y%m%d', errors='ignore')
df_price.set_index(['date','ticker'],inplace=True)
df_price=df_price.unstack()[col_p]
df_price.reset_index(inplace=True)
df_price.fillna(method='bfill',inplace=True)
df_price.fillna(method='ffill',inplace=True)
4.2 Detrend data - Compute log returns
Of the methods in current literature number we choose Time-lag correlations of prices changes over a certain period of time .One of the keys challenges in computing the correlation on stock prices is that the values are moving time series and have inherent trends which can lead to spurious correlations if the data is not properly normalised.
If we think about a time series of prices, you could write it out as
[P0,P1,P2,...,PN], or [P0,P0+R1,P0+R1+R2,...,P0+R1+...+RN], where Ri = Pi-P(i-1).
Written this way we can see that the first return R1, contributes to every entry in the series, whereas the last only contributes to one. This gives the early values in the correlation of prices more weight than they should have.
So for computing the correlation we take the difference between the prices for each day giving us the returns for each .We computed the log returns between two days since it has a key benefit of being additive over multiple time periods .
Though log returns can be computed over multiple time periods of 7 , 30 , 60 , 100 days for the sake of simplicity we kept the return window to 1 day.
import scipy.signal
t = 1
for key in df_price.columns:
if key not in companies:
continue
try:
df_price[key] = np.log(df_price[key]) - np.log(df_price[key].shift(t))
except:
print (key)
df_price.set_index('date',inplace=True)
## A quick visualization: detrended data
import matplotlib.pyplot as plt
%matplotlib inline
import random as rn
NUM_COLORS = len(companies)
cm = plt.get_cmap('gist_rainbow')
colors = [cm(i/NUM_COLORS) for i in range(NUM_COLORS)]
rn.seed = len(companies) # for choosing random colors
fig, ax = plt.subplots(nrows=5,ncols=2,figsize=(20, 20))
y=2007
for row in ax:
for col in row:
yfs = str(y) + '0101'
yfe = str(y) + '1231'
n = 0
col.set_ylim([0.5, -0.5])
for i in df_price.columns:
df_price.loc[yfs:yfe][i].plot(ax=col,color=colors[n])
n = n + 1
y = y + 1
plt.tight_layout()
plt.show()

4.3 Compute Correlation matrix for multiple windows
Next we computed the pearson correlation between the log returns.The data is divided into windows of width (T) in order to uncover dynamic characteristics of the networks. The window width corresponds to the number of daily returns included in the computation of the correlation between Stocks. The method of time windows division to construct asset graphs can be found in the literature Asset trees and asset graphs in financial markets
To determine the ideal length of the window we computed the mean correlation for window values of 21,42,63,84 and 105 and plotted the variations in the correlation.As seen in the plot the window of 63 captures the fluctuations of the market well.Values less than this are too noisy and higher than this we lose the sensitivity in the changes in the market .Also from a market perspective 63 days ideally falls into the Quarterly reporting cycle of these companies so we felt it would be appropriate choice
The correlation matrix is then computed based on this window length of 63 by dividing the period between 2007 and 2017 into multiple windows.
import matplotlib.pyplot as plt
%matplotlib inline
corr_dict = {}
T = 1
for w in range(21,126,21):
x = []
y = []
W = w
for i in range(t,len(df_price),W):
dkey = i
corr_dict[dkey]=df_price.iloc[i:(i+W)].corr(method='pearson')
corr_dict[dkey].fillna(0,inplace=True)
x.append(dkey)
y.append(np.mean([abs(j) for j in corr_dict[dkey].values.flatten().tolist()]))
plt.plot(x,y)
plt.xlabel('Days')
plt.ylabel('Mean Correlation')
plt.legend(list(range(21,126,21)), loc='center left', bbox_to_anchor=(1, 0.5),ncol=1)
plt.show()
W = 63
corr_dict = {}
for i in range(t,len(df_price),W):
dkey = i
corr_dict[dkey]=df_price.iloc[i:(i+W)].corr(method='pearson')
corr_dict[dkey].fillna(0,inplace=True)

4.4 Build network - (Winner take all method)
Literature A network perspective of the stock market details the winner take all method where we build a network based on the correlation matrix if the value of the Correlation index is greater than a threshold.
What is the ideal value of threshold ?.Since one the key objectives of the study is to find if the stocks behave in groups we wanted to select a threshold which maximises the modularity .However we were also consious not overfit the data in which case we might loose some of the underlying dynamics at work in the market.
In the next step we built the network based on different thresholds ranging from 0.6 to 0.99 for the windows identified .We then picked the value of the threshold from the values where the modularity is maximum.On analysis of this result we noted that most networks tended to have a high modularity for threshold value between 0.75 and 0.85 .There were certains windows where the threshold for the best modularity was seen to be greater than 0.9 however at this threshold the number edges was very less so in such cases we set the threshold to 0.8 and built the network.We treated both positive and negative correlations the same and looked at the absolute value.
import networkx as nx
import community
def get_modularity(y,threshold):
df_price_corr = corr_dict[y]
elist = []
outdict=df_price_corr.to_dict()
for i in outdict.keys():
for j in outdict[i].keys():
if abs(outdict[i][j]) > threshold :
if i == j :
continue
if i < j:
elist.append([i,j,dict(weight=abs(outdict[i][j]),start=y,end=y+W)])
#elist.append([i,j,dict(start=y,end=y+1)])
else:
None
#print (len(elist))
G=nx.Graph()
G.add_edges_from(elist)
#print (nx.info(G))
partition = community.best_partition(G)
try:
m = community.modularity(partition, G)
except:
m = 0
return m
# This will be our list of fractions to run the simulation over
fractions = np.linspace(0.6, 0.99, 20)
M_list = {}
for y in corr_dict.keys():
M_list[y] = [ get_modularity(y, frac) for frac in fractions ]
The below is the plot between the threshold value and the computed markdown in the various windows
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(20,10))
for y in corr_dict.keys():
plt.plot(fractions, M_list[y], lw=2)
plt.legend(list(M_list.keys()), loc='center left', bbox_to_anchor=(1, 0.5),ncol=2)
plt.xlabel('Threshold')
plt.ylabel('Modularity')
plt.show()

# Pick value of threshold for each Window (T)
T_val = {}
for y in M_list.keys():
val, idx = max((val, idx) for (idx, val) in enumerate(M_list[y]))
if fractions[idx] > 0.8:
T_val[y] = 0.8
else:
T_val[y] = fractions[idx]
#print (str(y) + ":" + str(T_val[y]))
# Create Edge List
elist_dict={}
for y in corr_dict.keys():
df_price_corr = corr_dict[y]
threshold = T_val[y]
elist = []
outdict=df_price_corr.to_dict()
for i in outdict.keys():
for j in outdict[i].keys():
if abs(outdict[i][j]) > threshold :
if i == j :
continue
if i < j:
elist.append([i,j,dict(weight=1,start=y,end=y+W-1)])
else:
None
elist_dict[y] = elist
# Constructing the graph different windows
import networkx as nx
import community
G_dict = {}
for y in elist_dict.keys():
G=nx.Graph()
elist = elist_dict[y]
G.add_edges_from(elist)
values = dfsp500.set_index('Symbol').to_dict(orient='dict')['Sector']
for node, value in values.items():
try:
G.node[node]['Sector'] = value
except:
#name = (value[0:3] + '-' + node)
#G.add_node(name,Sector=value)
None
partition = community.best_partition(G)
deg_cent=dict((k,float(v)) for k,v in nx.degree_centrality(G).items())
degree = dict((k,float(v)) for k,v in nx.degree(G).items())
#katz_cent=nx.katz_centrality(G)
#eigen_cent= dict((k,float(v)) for k,v in nx.eigenvector_centrality(G).items())
close_cent= dict((k,float(v)) for k,v in nx.closeness_centrality(G).items())
betw_cent= dict((k,float(v)) for k,v in nx.betweenness_centrality(G).items())
nx.set_node_attributes(G, "community", partition)
nx.set_node_attributes(G, "degreecent", deg_cent)
nx.set_node_attributes(G, "degree", degree)
#nx.set_node_attributes(G, "katz", katz_cent)
#nx.set_node_attributes(G, "eigenvector", eigen_cent)
nx.set_node_attributes(G, "closeness", close_cent)
nx.set_node_attributes(G, "betweenness", betw_cent)
nx.set_node_attributes(G, 'start',y)
nx.set_node_attributes(G, 'end',y+W-1)
#G.remove_nodes_from(nx.isolates(G))
#T = nx.minimum_spanning_tree(G)
T = G
G_dict[y] = T
# Collect the node level attributes for the nodes for all the windows
df_list = []
for k in G_dict.keys():
G = G_dict[k]
a = G.node
df_list.append(pd.DataFrame(a).T.reset_index())
attrib_df = pd.concat(df_list)
attrib_df.fillna(0,inplace=True)
attrib_df1=attrib_df.merge(dfsp500,left_on='index',right_on='Symbol')
attrib_df = attrib_df1[['index','Sector_x','betweenness','closeness','community','degree','degreecent','start','Name']]
attrib_df.columns=['ticker','Sector','Betweeness','Closeness','Community','Degree','DegreeCent','start','Name']
# Collect the graph level attributes for all the windows
from scipy.stats import linregress
G_val_dict = {}
for Y in G_dict.keys():
G_val = {}
G= G_dict[Y]
G_val['nodes'] = int(nx.number_of_nodes(G))
G_val['edges'] = int(nx.number_of_edges(G))
#G_val['AvgDegree'] = nx.average_degree(G)
G_val['AvgClustering'] = nx.average_clustering(G)
try:
G_val['AvgShortestPathLength'] = nx.average_shortest_path_length(G)
except:
G_val['AvgShortestPathLength'] = 99999
try:
G_val['Diameter'] = nx.diameter(G)
except:
G_val['Diameter'] = 99999
degs = {}
for n in G.nodes() :
deg = G.degree(n)
if deg not in degs.keys() :
degs[deg] = 0
degs[deg] += 1
items = sorted(degs.items())
x= [k for (k , v ) in items ]
y= [ v for (k ,v ) in items ]
xlog= np.array([np.log(k) for (k , v ) in items ])
ylog= np.array([np.log(v) for (k ,v ) in items ])
slope,intercept,rvalue,pvalue,stderr=linregress(xlog,ylog)
G_val['Slope'] = slope
G_val['No of Communities'] = attrib_df.groupby(by=['start'])['Community'].nunique().ix[Y]
G_val_dict[Y] = G_val
Gvaldf=pd.DataFrame(G_val_dict).T
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:32: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
The below table lists the network properties for the networks created using the Winner Take All Method Method over multiple Time Periods.
pd.options.display.max_rows = 999
Gvaldf
|
AvgClustering |
AvgShortestPathLength |
Diameter |
No of Communities |
Slope |
edges |
nodes |
| 1 |
0.437301 |
99999.000000 |
99999.0 |
17.0 |
-0.734717 |
462.0 |
139.0 |
| 64 |
0.583921 |
99999.000000 |
99999.0 |
19.0 |
-0.555489 |
897.0 |
178.0 |
| 127 |
0.538656 |
99999.000000 |
99999.0 |
16.0 |
-1.081694 |
307.0 |
112.0 |
| 190 |
0.464502 |
99999.000000 |
99999.0 |
17.0 |
-0.917059 |
222.0 |
84.0 |
| 253 |
0.343108 |
99999.000000 |
99999.0 |
21.0 |
-1.244365 |
379.0 |
149.0 |
| 316 |
0.337280 |
99999.000000 |
99999.0 |
20.0 |
-1.183432 |
253.0 |
121.0 |
| 379 |
0.472535 |
99999.000000 |
99999.0 |
22.0 |
-0.892980 |
781.0 |
171.0 |
| 442 |
0.497699 |
99999.000000 |
99999.0 |
11.0 |
-0.829045 |
3798.0 |
344.0 |
| 505 |
0.439454 |
99999.000000 |
99999.0 |
18.0 |
-1.018640 |
696.0 |
192.0 |
| 568 |
0.429914 |
99999.000000 |
99999.0 |
20.0 |
-0.967200 |
1108.0 |
215.0 |
| 631 |
0.397613 |
99999.000000 |
99999.0 |
18.0 |
-1.110171 |
303.0 |
120.0 |
| 694 |
0.489646 |
99999.000000 |
99999.0 |
20.0 |
-1.072235 |
266.0 |
110.0 |
| 757 |
0.351498 |
99999.000000 |
99999.0 |
14.0 |
-1.209373 |
142.0 |
83.0 |
| 820 |
0.378896 |
99999.000000 |
99999.0 |
22.0 |
-1.131378 |
470.0 |
173.0 |
| 883 |
0.618682 |
99999.000000 |
99999.0 |
14.0 |
-0.514376 |
6146.0 |
309.0 |
| 946 |
0.340429 |
99999.000000 |
99999.0 |
18.0 |
-1.257725 |
219.0 |
106.0 |
| 1009 |
0.111712 |
99999.000000 |
99999.0 |
11.0 |
-1.735520 |
33.0 |
37.0 |
| 1072 |
0.128993 |
99999.000000 |
99999.0 |
22.0 |
-1.509773 |
59.0 |
64.0 |
| 1135 |
0.371843 |
99999.000000 |
99999.0 |
22.0 |
-1.279903 |
381.0 |
173.0 |
| 1198 |
0.732433 |
1.884846 |
5.0 |
4.0 |
-0.251248 |
22994.0 |
409.0 |
| 1261 |
0.586980 |
99999.000000 |
99999.0 |
13.0 |
-0.751500 |
2997.0 |
271.0 |
| 1324 |
0.151453 |
99999.000000 |
99999.0 |
17.0 |
-1.969121 |
53.0 |
59.0 |
| 1387 |
0.374351 |
99999.000000 |
99999.0 |
20.0 |
-1.112039 |
464.0 |
172.0 |
| 1450 |
0.123333 |
99999.000000 |
99999.0 |
15.0 |
-2.144086 |
39.0 |
50.0 |
| 1513 |
0.235170 |
99999.000000 |
99999.0 |
22.0 |
-1.584047 |
70.0 |
70.0 |
| 1576 |
0.050292 |
99999.000000 |
99999.0 |
24.0 |
-2.596231 |
35.0 |
57.0 |
| 1639 |
0.369880 |
99999.000000 |
99999.0 |
26.0 |
-1.521337 |
301.0 |
165.0 |
| 1702 |
0.422181 |
99999.000000 |
99999.0 |
16.0 |
-1.106065 |
129.0 |
71.0 |
| 1765 |
0.203037 |
99999.000000 |
99999.0 |
20.0 |
-2.030411 |
60.0 |
69.0 |
| 1828 |
0.260765 |
99999.000000 |
99999.0 |
20.0 |
-1.401260 |
93.0 |
72.0 |
| 1891 |
0.304440 |
99999.000000 |
99999.0 |
22.0 |
-1.711807 |
95.0 |
79.0 |
| 1954 |
0.448140 |
99999.000000 |
99999.0 |
21.0 |
-0.996135 |
234.0 |
100.0 |
| 2017 |
0.344473 |
99999.000000 |
99999.0 |
36.0 |
-1.140130 |
514.0 |
206.0 |
| 2080 |
0.515361 |
99999.000000 |
99999.0 |
32.0 |
-0.840034 |
706.0 |
187.0 |
| 2143 |
0.323645 |
99999.000000 |
99999.0 |
27.0 |
-0.978314 |
244.0 |
116.0 |
| 2206 |
0.537092 |
99999.000000 |
99999.0 |
26.0 |
-0.815566 |
3269.0 |
342.0 |
| 2269 |
0.443097 |
99999.000000 |
99999.0 |
37.0 |
-1.063193 |
521.0 |
178.0 |
| 2332 |
0.301949 |
99999.000000 |
99999.0 |
43.0 |
-1.113032 |
557.0 |
223.0 |
| 2395 |
0.469023 |
99999.000000 |
99999.0 |
31.0 |
-0.826418 |
472.0 |
136.0 |
| 2458 |
0.454574 |
99999.000000 |
99999.0 |
32.0 |
-0.914775 |
1348.0 |
246.0 |
| 2521 |
0.446138 |
99999.000000 |
99999.0 |
37.0 |
-1.093756 |
1307.0 |
313.0 |
| 2584 |
0.470094 |
99999.000000 |
99999.0 |
23.0 |
-0.797446 |
306.0 |
111.0 |
| 2647 |
0.439164 |
99999.000000 |
99999.0 |
28.0 |
-0.794508 |
328.0 |
109.0 |
| 2710 |
0.352363 |
99999.000000 |
99999.0 |
40.0 |
-1.442425 |
297.0 |
190.0 |
4.5. Build network - Minimum Spanning Tree Method
One of the negatives of the Winner Take all method is that in certain period the threshold gave us highly noisy data .There were two many edges in the graph and results during this period are difficult to visuvalise .In this section we built the same network based on the Minimum Spanning tree method as noted in Network of Equities in Financial Markets .
In case of the minimum spanning tree method a metric distance dij is calculated using the cross correlation matrix.
dij = (2(1-Cij))^(0.5)
Where dij is the edge distance between stock i and stock j.
To find the ideal window size for constructing the network we computed the mean distance metric for mutiple windows ranging from 21 to 105 .The plot of the results is below.The Results largely indicate similar pattern to mean correlation we saw in the Winner take all method.Here to we can see that the window size of 63 resonable captures the fluctuations in the market.So we used the window width of 63 to compute the correlation and the corresponding distance metric and built the networks using the Minimum Spanning Tree Method.
import math
import matplotlib.pyplot as plt
%matplotlib inline
def calc_d(x):
x = round(x,3)
d = math.sqrt(2 * (1 - x))
return d
corr_dict = {}
corr_dist_dict = {}
T = 1
W = 63
x = []
y = []
for w in range(21,126,21):
x = []
y = []
W = w
for i in range(t,len(df_price),W):
dkey = i
corr_dict[dkey]=df_price.iloc[i:(i+W)].corr(method='pearson')
corr_dict[dkey].fillna(1,inplace=True)
corr_dist_dict[dkey] = corr_dict[dkey].applymap(calc_d)
x.append(dkey)
y.append(np.mean([abs(j) for j in corr_dist_dict[dkey].values.flatten().tolist()]))
plt.plot(x,y)
plt.xlabel('Days')
plt.ylabel('Mean Distance')
plt.legend(list(range(21,126,21)), loc='center left', bbox_to_anchor=(1, 0.5),ncol=1)
W = 63
corr_dict = {}
corr_dist_dict = {}
for i in range(t,len(df_price),W):
dkey = i
corr_dict[dkey]=df_price.iloc[i:(i+W)].corr(method='pearson')
corr_dict[dkey].fillna(1,inplace=True)
corr_dist_dict[dkey] = corr_dict[dkey].applymap(calc_d)

#MST Start
elistmst_dict={}
for y in corr_dist_dict.keys():
df_price_corr = corr_dist_dict[y]
elistmst = []
outdict=df_price_corr.to_dict()
for i in outdict.keys():
for j in outdict[i].keys():
if (abs(outdict[i][j]) > 0 and (i>j)):
elistmst.append([i,j,dict(weight=abs(outdict[i][j]),start=y,end=y+W-1)])
elistmst_dict[y] = elistmst
import networkx as nx
import community
GMST_dict = {}
for y in elistmst_dict.keys():
G=nx.Graph()
elist = elistmst_dict[y]
G.add_edges_from(elist)
T = nx.minimum_spanning_tree(G)
G = T
values = dfsp500.set_index('Symbol').to_dict(orient='dict')['Sector']
for node, value in values.items():
try:
G.node[node]['Sector'] = value
except:
#name = (value[0:3] + '-' + node)
#G.add_node(name,Sector=value)
None
partition = community.best_partition(G)
deg_cent=dict((k,float(v)) for k,v in nx.degree_centrality(G).items())
degree = dict((k,float(v)) for k,v in nx.degree(G).items())
#katz_cent=nx.katz_centrality(G)
#eigen_cent= dict((k,float(v)) for k,v in nx.eigenvector_centrality(G).items())
close_cent= dict((k,float(v)) for k,v in nx.closeness_centrality(G).items())
betw_cent= dict((k,float(v)) for k,v in nx.betweenness_centrality(G).items())
nx.set_node_attributes(G, "community", partition)
nx.set_node_attributes(G, "degreecent", deg_cent)
nx.set_node_attributes(G, "degree", degree)
#nx.set_node_attributes(G, "katz", katz_cent)
#nx.set_node_attributes(G, "eigenvector", eigen_cent)
nx.set_node_attributes(G, "closeness", close_cent)
nx.set_node_attributes(G, "betweenness", betw_cent)
nx.set_node_attributes(G, 'start',y)
nx.set_node_attributes(G, 'end',y+W)
T = G
GMST_dict[y] = T
df_list = []
for k in GMST_dict.keys():
G = GMST_dict[k]
a = G.node
df_list.append(pd.DataFrame(a).T.reset_index())
attribMST_df = pd.concat(df_list)
attribMST_df.fillna(0,inplace=True)
attribMST_df1=attribMST_df.merge(dfsp500,left_on='index',right_on='Symbol')
attribMST_df = attribMST_df1[['index','Sector_x','betweenness','closeness','community','degree','degreecent','start','Name']]
attribMST_df.columns=['ticker','Sector','Betweeness','Closeness','Community','Degree','DegreeCent','start','Name']
from scipy.stats import linregress
G_valMST_dict = {}
for Y in GMST_dict.keys():
G_val = {}
G= GMST_dict[Y]
G_val['nodes'] = nx.number_of_nodes(G)
G_val['edges'] = nx.number_of_edges(G)
#G_val['AvgDegree'] = nx.average_degree(G)
G_val['AvgClustering'] = nx.average_clustering(G)
try:
G_val['AvgShortestPathLength'] = nx.average_shortest_path_length(G)
except:
G_val['AvgShortestPathLength'] = 99999
try:
G_val['Diameter'] = nx.diameter(G)
except:
G_val['Diameter'] = 99999
degs = {}
for n in G.nodes() :
deg = G.degree(n)
if deg not in degs.keys() :
degs[deg] = 0
degs[deg] += 1
items = sorted(degs.items())
x= [k for (k , v ) in items ]
y= [ v for (k ,v ) in items ]
xlog= np.array([np.log(k) for (k , v ) in items ])
ylog= np.array([np.log(v) for (k ,v ) in items ])
slope,intercept,rvalue,pvalue,stderr=linregress(xlog,ylog)
G_val['Slope'] = slope
G_val['No of Communities'] = attribMST_df.groupby(by=['start'])['Community'].nunique().ix[Y]
G_valMST_dict[Y] = G_val
GMST_df=pd.DataFrame(G_valMST_dict).T
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:31: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
The below table lists the network properties for the networks created using the Minimum Spanning Tree Method over multiple Time Periods.
GMST_df
|
AvgClustering |
AvgShortestPathLength |
Diameter |
No of Communities |
Slope |
edges |
nodes |
| 1 |
0.0 |
10.006982 |
25.0 |
23.0 |
-2.302370 |
429.0 |
430.0 |
| 64 |
0.0 |
11.656817 |
30.0 |
24.0 |
-2.511209 |
431.0 |
432.0 |
| 127 |
0.0 |
11.685365 |
25.0 |
24.0 |
-2.414082 |
434.0 |
435.0 |
| 190 |
0.0 |
12.705015 |
33.0 |
23.0 |
-2.094925 |
436.0 |
437.0 |
| 253 |
0.0 |
13.100791 |
30.0 |
24.0 |
-2.414212 |
436.0 |
437.0 |
| 316 |
0.0 |
12.929396 |
34.0 |
25.0 |
-2.226890 |
440.0 |
441.0 |
| 379 |
0.0 |
11.736004 |
37.0 |
23.0 |
-2.397362 |
442.0 |
443.0 |
| 442 |
0.0 |
9.870432 |
24.0 |
23.0 |
-2.176792 |
444.0 |
445.0 |
| 505 |
0.0 |
11.700028 |
29.0 |
26.0 |
-2.423807 |
448.0 |
449.0 |
| 568 |
0.0 |
11.991151 |
28.0 |
25.0 |
-2.337993 |
448.0 |
449.0 |
| 631 |
0.0 |
12.206835 |
32.0 |
25.0 |
-2.094288 |
447.0 |
448.0 |
| 694 |
0.0 |
11.966274 |
28.0 |
23.0 |
-2.401395 |
448.0 |
449.0 |
| 757 |
0.0 |
10.225780 |
26.0 |
25.0 |
-2.400149 |
448.0 |
449.0 |
| 820 |
0.0 |
10.720473 |
25.0 |
22.0 |
-2.286885 |
450.0 |
451.0 |
| 883 |
0.0 |
9.268146 |
24.0 |
25.0 |
-2.270480 |
451.0 |
452.0 |
| 946 |
0.0 |
7.232595 |
17.0 |
24.0 |
-2.094576 |
453.0 |
454.0 |
| 1009 |
0.0 |
11.460324 |
27.0 |
23.0 |
-2.404244 |
455.0 |
456.0 |
| 1072 |
0.0 |
9.898856 |
27.0 |
23.0 |
-2.202451 |
457.0 |
458.0 |
| 1135 |
0.0 |
11.048054 |
26.0 |
24.0 |
-2.306812 |
458.0 |
459.0 |
| 1198 |
0.0 |
10.048669 |
24.0 |
25.0 |
-2.201338 |
459.0 |
460.0 |
| 1261 |
0.0 |
9.375795 |
21.0 |
26.0 |
-2.121953 |
468.0 |
469.0 |
| 1324 |
0.0 |
10.258025 |
25.0 |
22.0 |
-2.509338 |
466.0 |
467.0 |
| 1387 |
0.0 |
10.286589 |
26.0 |
26.0 |
-2.438308 |
468.0 |
469.0 |
| 1450 |
0.0 |
9.377163 |
25.0 |
22.0 |
-2.249755 |
469.0 |
470.0 |
| 1513 |
0.0 |
11.060595 |
28.0 |
24.0 |
-2.509590 |
470.0 |
471.0 |
| 1576 |
0.0 |
9.406258 |
27.0 |
23.0 |
-2.332476 |
471.0 |
472.0 |
| 1639 |
0.0 |
9.620221 |
21.0 |
23.0 |
-2.281254 |
475.0 |
476.0 |
| 1702 |
0.0 |
10.191933 |
27.0 |
27.0 |
-2.419526 |
475.0 |
476.0 |
| 1765 |
0.0 |
9.666815 |
26.0 |
25.0 |
-2.572109 |
479.0 |
480.0 |
| 1828 |
0.0 |
10.022886 |
25.0 |
23.0 |
-2.278903 |
479.0 |
480.0 |
| 1891 |
0.0 |
13.378051 |
41.0 |
27.0 |
-2.429328 |
481.0 |
482.0 |
| 1954 |
0.0 |
12.746888 |
38.0 |
26.0 |
-2.497661 |
482.0 |
483.0 |
| 2017 |
0.0 |
12.801533 |
33.0 |
25.0 |
-2.468109 |
483.0 |
484.0 |
| 2080 |
0.0 |
11.445382 |
30.0 |
26.0 |
-2.523927 |
486.0 |
487.0 |
| 2143 |
0.0 |
11.845514 |
36.0 |
25.0 |
-2.380275 |
485.0 |
486.0 |
| 2206 |
0.0 |
11.853195 |
31.0 |
26.0 |
-2.531080 |
491.0 |
492.0 |
| 2269 |
0.0 |
14.269999 |
38.0 |
25.0 |
-2.344246 |
493.0 |
494.0 |
| 2332 |
0.0 |
11.389670 |
27.0 |
24.0 |
-2.401746 |
494.0 |
495.0 |
| 2395 |
0.0 |
15.049847 |
33.0 |
25.0 |
-2.560258 |
496.0 |
497.0 |
| 2458 |
0.0 |
12.346634 |
32.0 |
26.0 |
-2.450851 |
497.0 |
498.0 |
| 2521 |
0.0 |
16.771448 |
47.0 |
25.0 |
-2.231318 |
497.0 |
498.0 |
| 2584 |
0.0 |
15.656228 |
36.0 |
25.0 |
-2.619341 |
498.0 |
499.0 |
| 2647 |
0.0 |
14.443294 |
41.0 |
25.0 |
-2.464404 |
497.0 |
498.0 |
| 2710 |
0.0 |
14.880367 |
34.0 |
26.0 |
-2.511511 |
497.0 |
498.0 |
5. Result
5.1 Degree distribution and Scale Free Properties
We plotted the degree distribution histogram and also plotted the degree distribution on a log log plotted and regression fitted a line whose slope will give as the Power law exponent.The plot show that network shows scale free properties in most of the windows .The scale free nature is more evident in the Networks generated based on the Minimum Spanning Tree Method .
Winner Take All Method
## Explore graph properties
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.stats import linregress
fig, ax = plt.subplots(nrows=10,ncols=4,figsize=(20, 30))
Y = 1
for row in ax:
for col in row:
degs = {}
for n in G_dict[Y].nodes() :
deg = G_dict[Y].degree(n)
if deg not in degs.keys() :
degs[deg] = 0
degs[deg] += 1
items = sorted(degs.items())
x= [k for (k , v ) in items ]
y= [ v for (k ,v ) in items ]
xlog= np.array([np.log(k) for (k , v ) in items ])
ylog= np.array([np.log(v) for (k ,v ) in items ])
col.scatter(xlog, ylog)
slope,intercept,rvalue,pvalue,stderr=linregress(xlog,ylog)
col.plot(xlog, (slope * xlog + intercept), color='red')
#ax.set_xscale( 'log' )
#ax.set_yscale( 'log' )
col.set_title ( " Day :" + str(Y) + " - Slope :" + str(round(slope,2) ),fontsize=8)
Y = Y + W
plt.tight_layout()
plt.show()

## Explore graph properties
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=10,ncols=4,figsize=(20, 30))
y = 1
for row in ax:
for col in row:
deg_dist = [v for k,v in nx.degree(G_dict[y]).items()]
deg_dist.sort(reverse=True)
pdf, bins, patch = col.hist(deg_dist, bins=10)
col.set_title ( " Day :" + str(y),fontsize=8 )
y = y + W
plt.tight_layout()
plt.show()

Minimum Spanning Tree Method
## Explore graph properties
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.stats import linregress
fig, ax = plt.subplots(nrows=10,ncols=4,figsize=(20, 30))
Y = 1
for row in ax:
for col in row:
degs = {}
for n in GMST_dict[Y].nodes() :
deg = GMST_dict[Y].degree(n)
if deg not in degs.keys() :
degs[deg] = 0
degs[deg] += 1
items = sorted(degs.items())
x= [k for (k , v ) in items ]
y= [ v for (k ,v ) in items ]
xlog= np.array([np.log(k) for (k , v ) in items ])
ylog= np.array([np.log(v) for (k ,v ) in items ])
col.scatter(xlog, ylog)
slope,intercept,rvalue,pvalue,stderr=linregress(xlog,ylog)
col.plot(xlog, (slope * xlog + intercept), color='red')
#ax.set_xscale( 'log' )
#ax.set_yscale( 'log' )
col.set_title ( " Day :" + str(Y) + " - Slope :" + str(round(slope,2) ),fontsize=8)
Y = Y + W
plt.tight_layout()
plt.show()

## Explore graph properties
import matplotlib.pyplot as plt
%matplotlib inline
fig, ax = plt.subplots(nrows=10,ncols=4,figsize=(20, 30))
y = 1
for row in ax:
for col in row:
deg_dist = [v for k,v in nx.degree(GMST_dict[y]).items()]
deg_dist.sort(reverse=True)
pdf, bins, patch = col.hist(deg_dist, bins=10)
col.set_title ( " Degree Dist Day :" + str(y),fontsize=8 )
y = y + W
plt.tight_layout()
plt.show()

5.2 Average degree of the network over time
We looked at the change in the average degree degree over time .For the networks from the Winner Take All method we can see that this varies widely overtime indicating the dynamic nature of the stock market.The peaks in the graph correspond well to major events in the market such as the 2008 - 2009 subprime crisis.However for the networks based on the MST method the Average degree is constant this is one of the key drawbacks of the MST method were major fluctuations in the market are not well represented in the network.We can also see in the plot that during normal time periods the Average Degree between the networks from both the methods is the same.
import matplotlib.pyplot as plt
%matplotlib inline
avgdf=attrib_df.groupby(by=['start'])['Degree'].mean()
avgdf.plot()
avgdf=attribMST_df.groupby(by=['start'])['Degree'].mean()
avgdf.plot()
plt.xlabel('Days')
plt.ylabel('Average Degree')
plt.legend(['Winner Take All','MST'], loc='center left', bbox_to_anchor=(1, 0.5),ncol=1)
plt.show()

5.3 High degree stocks in the network
We looked at the high degree stocks in the network at different windows to get out the important stocks which have high influence or which are a good indicator of how the stock market as whole is moving.The results are below.As we can see the stocks from financials sector have the highest degree in a number of windows .Is there a pattern here?
Winner Take All Method
attrib_df.sort_values(['start','Degree'],ascending=False).groupby(['start']).head(3)[['start','ticker','Name','Degree','Sector']]
|
start |
ticker |
Name |
Degree |
Sector |
| 995 |
2710 |
DVN |
Devon Energy Corp. |
14.0 |
Energy |
| 2774 |
2710 |
RIG |
Transocean |
14.0 |
Energy |
| 2026 |
2710 |
MRO |
Marathon Oil Corp. |
12.0 |
Energy |
| 2576 |
2647 |
PRU |
Prudential Financial |
24.0 |
Financials |
| 3202 |
2647 |
UNM |
Unum Group |
24.0 |
Financials |
| 2516 |
2647 |
PNC |
PNC Financial Services |
23.0 |
Financials |
| 3572 |
2584 |
AEE |
Ameren Corp |
18.0 |
Utilities |
| 4092 |
2584 |
LNT |
Alliant Energy Corp |
18.0 |
Utilities |
| 4257 |
2584 |
PNW |
Pinnacle West Capital |
18.0 |
Utilities |
| 2058 |
2521 |
MS |
Morgan Stanley |
39.0 |
Financials |
| 4819 |
2521 |
DFS |
Discover Financial Services |
38.0 |
Financials |
| 2271 |
2521 |
O |
Realty Income Corporation |
36.0 |
Real Estate |
| 172 |
2458 |
AMP |
Ameriprise Financial |
60.0 |
Financials |
| 4514 |
2458 |
ETN |
Eaton Corporation |
56.0 |
Industrials |
| 5307 |
2458 |
MCO |
Moody's Corp |
56.0 |
Financials |
| 2572 |
2395 |
PRU |
Prudential Financial |
29.0 |
Financials |
| 546 |
2395 |
C |
Citigroup Inc. |
28.0 |
Financials |
| 2056 |
2395 |
MS |
Morgan Stanley |
28.0 |
Financials |
| 2055 |
2332 |
MS |
Morgan Stanley |
35.0 |
Financials |
| 2511 |
2332 |
PNC |
PNC Financial Services |
33.0 |
Financials |
| 475 |
2332 |
BK |
The Bank of New York Mellon Corp. |
32.0 |
Financials |
| 413 |
2269 |
BBT |
BB&T Corporation |
27.0 |
Financials |
| 2510 |
2269 |
PNC |
PNC Financial Services |
26.0 |
Financials |
| 2570 |
2269 |
PRU |
Prudential Financial |
26.0 |
Financials |
| 5261 |
2206 |
HON |
Honeywell Int'l Inc. |
105.0 |
Industrials |
| 3391 |
2206 |
WFC |
Wells Fargo |
96.0 |
Financials |
| 6474 |
2206 |
BRK.B |
Berkshire Hathaway |
96.0 |
Financials |
| 509 |
2143 |
BXP |
Boston Properties |
18.0 |
Real Estate |
| 2716 |
2143 |
REG |
Regency Centers Corporation |
17.0 |
Real Estate |
| 1263 |
2143 |
FRT |
Federal Realty Investment Trust |
16.0 |
Real Estate |
| 167 |
2080 |
AMP |
Ameriprise Financial |
27.0 |
Financials |
| 3021 |
2080 |
STI |
SunTrust Banks |
25.0 |
Financials |
| 3390 |
2080 |
WFC |
Wells Fargo |
25.0 |
Financials |
| 166 |
2017 |
AMP |
Ameriprise Financial |
25.0 |
Financials |
| 1989 |
2017 |
MET |
MetLife Inc. |
25.0 |
Financials |
| 1503 |
2017 |
HES |
Hess Corporation |
21.0 |
Energy |
| 845 |
1954 |
D |
Dominion Resources |
20.0 |
Utilities |
| 3353 |
1954 |
WEC |
Wec Energy Group Inc |
18.0 |
Utilities |
| 3468 |
1954 |
XEL |
Xcel Energy Inc |
18.0 |
Utilities |
| 649 |
1891 |
CMS |
CMS Energy |
10.0 |
Utilities |
| 3467 |
1891 |
XEL |
Xcel Energy Inc |
9.0 |
Utilities |
| 3078 |
1891 |
TMK |
Torchmark Corp. |
8.0 |
Financials |
| 163 |
1828 |
AMP |
Ameriprise Financial |
11.0 |
Financials |
| 2563 |
1828 |
PRU |
Prudential Financial |
8.0 |
Financials |
| 1862 |
1828 |
LNC |
Lincoln National |
7.0 |
Financials |
| 3388 |
1765 |
WFC |
Wells Fargo |
7.0 |
Financials |
| 5812 |
1765 |
L |
Loews Corp. |
6.0 |
Financials |
| 162 |
1765 |
AMP |
Ameriprise Financial |
4.0 |
Financials |
| 2342 |
1702 |
PEG |
Public Serv. Enterprise Inc. |
12.0 |
Utilities |
| 3153 |
1702 |
UDR |
UDR Inc |
12.0 |
Real Estate |
| 3349 |
1702 |
WEC |
Wec Energy Group Inc |
12.0 |
Utilities |
| 2371 |
1639 |
PFG |
Principal Financial Group |
21.0 |
Financials |
| 3348 |
1639 |
WEC |
Wec Energy Group Inc |
17.0 |
Utilities |
| 4077 |
1639 |
LNT |
Alliant Energy Corp |
16.0 |
Utilities |
| 1858 |
1576 |
LNC |
Lincoln National |
5.0 |
Financials |
| 2560 |
1576 |
PRU |
Prudential Financial |
3.0 |
Financials |
| 130 |
1576 |
AMG |
Affiliated Managers Group Inc |
2.0 |
Financials |
| 3462 |
1513 |
XEL |
Xcel Energy Inc |
8.0 |
Utilities |
| 39 |
1513 |
AEP |
American Electric Power |
6.0 |
Utilities |
| 943 |
1513 |
DTE |
DTE Energy Co. |
6.0 |
Utilities |
| 2291 |
1450 |
OXY |
Occidental Petroleum |
5.0 |
Energy |
| 3461 |
1450 |
XEL |
Xcel Energy Inc |
4.0 |
Utilities |
| 5136 |
1450 |
IVZ |
Invesco Ltd. |
4.0 |
Financials |
| 465 |
1387 |
BK |
The Bank of New York Mellon Corp. |
20.0 |
Financials |
| 1257 |
1387 |
FRT |
Federal Realty Investment Trust |
19.0 |
Real Estate |
| 1855 |
1387 |
LNC |
Lincoln National |
19.0 |
Financials |
| 2230 |
1324 |
NTRS |
Northern Trust Corp. |
8.0 |
Financials |
| 532 |
1324 |
C |
Citigroup Inc. |
6.0 |
Financials |
| 3385 |
1324 |
WFC |
Wells Fargo |
5.0 |
Financials |
| 5808 |
1261 |
L |
Loews Corp. |
119.0 |
Financials |
| 5255 |
1261 |
HON |
Honeywell Int'l Inc. |
99.0 |
Industrials |
| 127 |
1261 |
AMG |
Affiliated Managers Group Inc |
98.0 |
Financials |
| 2905 |
1198 |
SNA |
Snap-On Inc. |
294.0 |
Consumer Discretionary |
| 5197 |
1198 |
CTAS |
Cintas Corporation |
289.0 |
Industrials |
| 5624 |
1198 |
EFX |
Equifax Inc. |
280.0 |
Industrials |
| 1852 |
1135 |
LNC |
Lincoln National |
18.0 |
Financials |
| 205 |
1135 |
APA |
Apache Corporation |
17.0 |
Energy |
| 2961 |
1135 |
SPG |
Simon Property Group Inc |
16.0 |
Real Estate |
| 3274 |
1072 |
VNO |
Vornado Realty Trust |
9.0 |
Real Estate |
| 98 |
1072 |
AIV |
Apartment Investment & Mgmt |
6.0 |
Real Estate |
| 498 |
1072 |
BXP |
Boston Properties |
6.0 |
Real Estate |
| 3181 |
1009 |
UNM |
Unum Group |
5.0 |
Financials |
| 280 |
1009 |
AVB |
AvalonBay Communities, Inc. |
4.0 |
Real Estate |
| 1099 |
1009 |
EQR |
Equity Residential |
4.0 |
Real Estate |
| 4208 |
946 |
PCAR |
PACCAR Inc. |
22.0 |
Industrials |
| 1913 |
946 |
MAA |
Mid-America Apartments |
17.0 |
Real Estate |
| 2902 |
946 |
SNA |
Snap-On Inc. |
16.0 |
Consumer Discretionary |
| 5251 |
883 |
HON |
Honeywell Int'l Inc. |
169.0 |
Industrials |
| 5194 |
883 |
CTAS |
Cintas Corporation |
152.0 |
Industrials |
| 730 |
883 |
COL |
Rockwell Collins |
149.0 |
Industrials |
| 3270 |
820 |
VNO |
Vornado Realty Trust |
22.0 |
Real Estate |
| 2590 |
820 |
PSA |
Public Storage |
21.0 |
Real Estate |
| 1096 |
820 |
EQR |
Equity Residential |
20.0 |
Real Estate |
| 276 |
757 |
AVB |
AvalonBay Communities, Inc. |
12.0 |
Real Estate |
| 493 |
757 |
BXP |
Boston Properties |
12.0 |
Real Estate |
| 3269 |
757 |
VNO |
Vornado Realty Trust |
11.0 |
Real Estate |
| 2466 |
README 内容过长,已截取前 64 KB。完整内容请前往 GitHub 仓库查看。
评论