Kategorien
Finance

Financial markets in Switzerland: A network analysis

The term Network is often associated with Social Networks, the Internet or the human brain. However, more recently Network Analysis has become an interdisciplinary field of research and can also be applied to model the interdependence of financial markets. In this short blog, I show you how you can implement your own Network in R.

As always you need to get some data to work with first. I provide here some data of Eikon of all listed stocks in Switzerland (you find the data here: SIX-Total Return Prices). The data contains total return prices of some listed stocks in Switzerland.

Step 1: Load the data and calculate returns – this can be easily done in R.

# clean global environment
rm(list=ls())
# packages
require(networkD3)
require(igraph)
library(visNetwork)
# set working directory
setwd("C:/Users/lliebi/Dropbox/Daten Luca/PhD/Research/Network Analysis/Data")
prices <- read.csv("SwitzerlandStocks.csv",sep=";")
# clean the data
prices$Name <- as.Date(as.character(prices$Name),format="%d.%m.%Y")
# calculate returns function
return.calculation <- function(price){
returns <- rep(NA,length(price))
for(i in 2:length(price)){
returns[i] <- (price[i]-price[i-1])/price[i-1]
}
return(returns)
}
# create a new dataframe with all the returns
returns<-as.data.frame(apply(prices[,2:ncol(prices)],2,return.calculation))
returns <- cbind(prices$Name,returns)
colnames(returns)[1] <- "Date"

# only stocks with complete observations
returns <- returns[-1,]

# delete col that contain missing values
final<-returns[colSums(!is.na(returns))==nrow(returns)]

Step 2: Now you can already start with your Network analysis

There are several methodologies to model the interdependence of stocks that can be used (e.g. Granger Causality,  Spillover tables, Correlations, …).
I use a very easy and intuitive measure introduced by Diebold and Yilmaz. Furthermore, a helpful package is provided in R that you don’t have to calculate the Spillover table yourself.

require(frequencyConnectedness)
number.stocks <- 50
library(stringr)
colnames(final)[2:number.stocks] <- word(colnames(final)[2:number.stocks],1,sep = "\\.")
# Step 1: Find the correct var model
VARselect(final[,2:number.stocks], lag.max = 2, type = "const")
# you can see that the lowest AIC information criterion is found within lag 2
# therefore specify a VAR(2) model
# Step 2: Implement a VAR model for all the stocks in the sample
var.lag2 <- VAR(final[,2:number.stocks], p = 2, type = "const") # the [,-1] is due to the date in the first column
# With this model you can also predict 10 days ahead returns
var.f10 <- predict(var.lag2, n.ahead = 10, ci = 0.95)
# Step 3: Calculate the spillovers
# here use the function in the frequencyConnectedness package
spillover <- spilloverDY09(var.lag2, n.ahead = 10, no.corr = F)
# get the spillover table
solution <- as.data.frame(spillover$tables)*100

Step 3: get the Net Spillover and visualize the Network


# get Net spillovers
net.spillovers <- matrix(NA,nrow=number.stocks-1,ncol=number.stocks-1)
colnames(net.spillovers) <- colnames(solution)
rownames(net.spillovers) <- rownames(solution)

net.spillovers[lower.tri(net.spillovers)] <-solution[lower.tri(solution)]-solution[upper.tri(solution)]
net.spillovers[upper.tri(net.spillovers)] <-solution[upper.tri(solution)]-solution[lower.tri(solution)]
net.spillovers<-ifelse(net.spillovers>0,net.spillovers,0)
# Step 4: Create your own network
m <- t((net.spillovers))
net=graph.adjacency(m,mode="directed",weighted=TRUE,diag=F)
set.seed(1)
plot.igraph(net,vertex.label.color="black",
edge.color="darkgrey",
edge.arrow.size=0.2,
layout=layout.fruchterman.reingold,
edge.curved=F,
edge.lty=1,
frame=F,
vertex.size=5,
vertex.color=rainbow(number.stocks),
vertex.label.dist=0.0)
degree(net,mode = "in")
degree(net,mode = "out")

 

If you wish to do another visualization you can use the „network“ package. 

links <- as.data.frame(get.edgelist(net))
net = network(links, directed = TRUE)
# network plot
require(network)
ggnet2(net, alpha = 0.75, size = 4, edge.alpha = 0.5,color = "black",
label=T,label.size = 1.5,label.color = "darkgrey")

 

A 3D visualization can be here: Network Analysis.