Unlock the Power of Quantitative Strategies: Explore Our Cutting-Edge Website Today!

R

More examples in Financial Visualisation

rcobo

12/12/2018

No Comments

In line with the previous post, Group Funds with the Sun, we continue exploring new ways to visualise and analyse financial data.

We will take annual data from the current components of Dow Jones Industrial with data going back to 2000 to play around.

Animated Risk – Return scatter

Risk-Return scatter is one of the most widely used plots in finance. The plot below shows the relationship between risk (volatility) and return every calendar year from 2000 for the 27 selected stocks. Pressing the “Play” button we see the evolution and you can move also to a specific year using the slider. It’s easy to add information about, for example, market capitalization by linking it the pointer size, but for now, we’ll keep it simple. Click on the following image to see the plot.

Risk return plot by calendar year

It is not only about time evolution and slider control, we can also benefit from all the possibilities that plotly offers us (select, zoom, pan, hover…).

Network plots

Less common are network plots to show relationships between different assets. If we take the annual returns of the stocks we can compute the correlation coefficient for each pair. Taking one minus that correlation we get a distance of how far one stock is from the other. In this example, we take the correlation during the last five years. For pairs whose distance is small (below 0.25) we draw a line between them. It shows easily how some stocks are “isolated”  (MRK, HD or NKE). DIS and WBA are only related reciprocally. In contrast, JNJ or UNH appear very close to several stocks. Click on the following image to see the plot.

3D Network Plot based on correlation distance

We can rotate the network by clicking and dragging (caution, very addictive!), and zoom with mouse wheel. More info in igraphthe network analysis package.

It’s amazing how we can use it by writing just a few lines of code:

Animated scatter. Data here

library(ggplot2)
library(plotly)

dji <- read.csv(url("https://quantdare.com/wp-content/uploads/2018/12/dji_comp_annual.csv"), sep=',', header=TRUE)
p <- ggplot(dji, aes(Volatility, Return, color = Sector)) +
     geom_point(aes(frame = Year, ids = Company), size = 3) +
     scale_x_continuous(labels = scales::percent) +
     scale_y_continuous(labels = scales::percent) +
     theme(text=element_text(size = 16, family="Arial")) +
     ggtitle("Risk Return Plot By Calendar Year")

p <- ggplotly(p) %>% animation_opts(1000, easing="elastic", redraw = FALSE)
p

 

IGraph. Data here

library(threejs)
suppressMessages(library(igraph))

d <- as.matrix(read.csv(url("https://quantdare.com/wp-content/uploads/2018/12/dji_comp_distance.csv"), sep=',', header=FALSE))
mynames <- c("MMM","AXP","AAPL","BA","CAT","CVX","CSCO","KO","XOM","GS","HD","INTC","IBM","JNJ","JPM","MCD","MRK","MSFT","NKE","PFE","PG","UTX","UNH","VZ","WMT","WBA","DIS")
mycolors <- c("#CA7CFF","#00BFC7","#FF5FC8","#CA7CFF","#CA7CFF","#06BB66","#FF5FC8","#7AAC09","#06BB66","#00BFC7","#BD983E","#FF5FC8","#FF5FC8","#00A9FC","#00BFC7","#BD983E","#00A9FC","#FF5FC8","#BD983E","#00A9FC","#7AAC09","#CA7CFF","#00A9FC","#F57678","#7AAC09","#7AAC09","#F57678")

Q = d * (d<0.25)
g = graph.adjacency(Q, mode="undirected", weighted=TRUE, diag=FALSE)
g = set_vertex_attr(g, "color", value=mycolors)
graphjs(g,
        vertex.size=0.2,
        vertex.shape=mynames,
        main= "3D Similarity Plot based in one minus annual returns correlations (last 5 years)")

 

All comments are welcome. Special thanks to:

Carson Sievert (2018) plotly for R. https://plotly-book.cpsievert.me

B. W. Lewis (2017). threejs: Interactive 3D Scatter Plots, Networks and Globes. R package version 0.3.1. https://CRAN.R-project.org/package=threejs

Csardi G, Nepusz T: The igraph software package for complex network research, InterJournal, Complex Systems 1695. 2006. http://igraph.org

0 Comments
Inline Feedbacks
View all comments