library(knitr) opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", fig.align = "center", fig.retina = 2, out.width = "75%", dpi = 96 ) knit_hooks$set(pngquant = hook_pngquant)
This repo contains ADMM implementations to estimate weighted undirected graphs (Markov random fields) under Student-t assumptions with applications to financial markets.
fingraph depends on the development version of spectralGraphTopology, which can be installed as:
> devtools::install_github("convexfi/spectralGraphTopology")
The stable version of fingraph can be installed directly from CRAN:
> install.packages("fingraph")
On MS Windows environments, make sure to install the most recent version of Rtools
.
library(igraph) library(fingraph) library(fitHeavyTail) library(xts) set.seed(123) # load crypto prices into an xts table crypto_prices <- readRDS("examples/crypto/crypto-prices.rds") colnames(crypto_prices) # compute log-returns log_returns <- diff(log(crypto_prices), na.pad = FALSE) # estimate a weighted, undirected graph (markov random field) graph_mrf <- learn_kcomp_heavytail_graph(scale(log_returns), k = 8, heavy_type = "student", nu = fit_mvt(scale(log_returns))$nu, verbose = FALSE) # plot network net <- graph_from_adjacency_matrix(graph_mrf$adjacency, mode = "undirected", weighted = TRUE) cfg <- cluster_fast_greedy(as.undirected(net)) la_kcomp <- layout_nicely(net) V(net)$label.cex = 1 plot(cfg, net, vertex.label = colnames(crypto_prices), layout = la_kcomp, vertex.size = 4.5, col = "black", edge.color = c("#686de0"), vertex.label.family = "Helvetica", vertex.label.color = "black", vertex.label.dist = 1.25, vertex.shape = "circle", edge.width = 20*E(net)$weight, edge.curved = 0.1)
library(xts) library(igraph) library(fingraph) library(fitHeavyTail) library(readr) set.seed(123) # load table w/ stocks and their sectors SP500 <- read_csv("examples/stocks/SP500-sectors.csv") # load stock prices into an xts table stock_prices <- readRDS("examples/stocks/stock-data-2014-2018.rds") colnames(stock_prices) # compute log-returns log_returns <- diff(log(stock_prices), na.pad = FALSE) # estimate a weighted, undirected graph (markov random field) graph_mrf <- learn_kcomp_heavytail_graph(scale(log_returns), rho = 10, k = 3, heavy_type = "student", nu = fit_mvt(scale(log_returns))$nu, verbose = FALSE) # map stock names and sectors stock_sectors <- c(SP500$GICS.Sector[SP500$Symbol %in% colnames(stock_prices)]) stock_sectors_index <- as.numeric(as.factor(stock_sectors)) # plot network net <- graph_from_adjacency_matrix(graph_mrf$adjacency, mode = "undirected", weighted = TRUE) la_kcomp <- layout_nicely(net) V(net)$label.cex = 1 colors <- c("#FD7272", "#55E6C1", "#25CCF7") V(net)$color <- colors[stock_sectors_index] V(net)$type <- stock_sectors_index V(net)$cluster <- stock_sectors_index E(net)$color <- apply(as.data.frame(get.edgelist(net)), 1, function(x) ifelse(V(net)$cluster[x[1]] == V(net)$cluster[x[2]], colors[V(net)$cluster[x[1]]], 'grey')) plot(net, vertex.label = colnames(stock_prices), layout = la_kcomp, vertex.size = 4.5, vertex.label.family = "Helvetica", vertex.label.dist = 1.25, vertex.label.color = "black", vertex.shape = "circle", edge.width = 20*E(net)$weight, edge.curved = 0.1)
If you made use of this software please consider citing:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.