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.
You can install the development version from GitHub:
> devtools::install_github("mirca/spectralGraphTopology")
> devtools::install_github("mirca/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
<- readRDS("examples/crypto/crypto-prices.rds")
crypto_prices colnames(crypto_prices)
#> [1] "BTC" "ETH" "XRP" "USDT" "LTC" "BCH" "BNB" "XLM" "EOS"
#> [10] "XMR" "XEM" "NEO" "DASH" "WAVES" "MIOTA" "ZEC" "ETC" "GNO"
#> [19] "OMG" "DOGE" "BAT" "DCR" "DGB" "QTUM" "REP" "LSK" "SC"
#> [28] "NANO" "ANT" "SNT" "ZEN" "MAID" "GNT" "XVG" "BNT" "MONA"
#> [37] "RLC" "STORJ" "STEEM" "KMD" "BTS"
# compute log-returns
<- diff(log(crypto_prices), na.pad = FALSE)
log_returns
# estimate a weighted, undirected graph (markov random field)
<- learn_kcomp_heavytail_graph(scale(log_returns),
graph_mrf k = 7,
heavy_type = "student",
nu = fit_mvt(scale(log_returns),
nu="MLE-diag-resample")$nu,
verbose = FALSE)
# plot network
<- graph_from_adjacency_matrix(graph_mrf$adjacency,
net mode = "undirected",
weighted = TRUE)
<- cluster_fast_greedy(as.undirected(net))
cfg <- layout_nicely(net)
la_kcomp 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
<- read_csv("examples/stocks/SP500-sectors.csv")
SP500
# load stock prices into an xts table
<- readRDS("examples/stocks/stock-data-2014-2018.rds")
stock_prices colnames(stock_prices)
#> [1] "AEE" "AEP" "AES" "AIV" "AMT" "ARE" "ATO" "ATVI" "AVB"
#> [10] "AWK" "BXP" "CBRE" "CCI" "CHTR" "CMCSA" "CMS" "CNP" "CTL"
#> [19] "D" "DIS" "DISCA" "DISCK" "DISH" "DLR" "DRE" "DTE" "DUK"
#> [28] "EA" "ED" "EIX" "EQIX" "EQR" "ES" "ESS" "ETR" "EVRG"
#> [37] "EXC" "EXR" "FB" "FE" "FRT" "GOOG" "GOOGL" "HST" "IPG"
#> [46] "IRM" "KIM" "LNT" "LYV" "MAA" "NEE" "NFLX" "NI" "NRG"
#> [55] "NWS" "NWSA" "O" "OMC" "PEAK" "PEG" "PLD" "PNW" "PPL"
#> [64] "PSA" "REG" "SBAC" "SLG" "SO" "SPG" "SRE" "T" "TMUS"
#> [73] "TTWO" "TWTR" "UDR" "VNO" "VTR" "VZ" "WEC" "WELL" "WY"
#> [82] "XEL"
# compute log-returns
<- diff(log(stock_prices), na.pad = FALSE)
log_returns
# estimate a weighted, undirected graph (markov random field)
<- learn_kcomp_heavytail_graph(scale(log_returns),
graph_mrf rho = 10,
k = 3,
heavy_type = "student",
nu = fit_mvt(scale(log_returns),
nu="MLE-diag-resample")$nu,
verbose = FALSE)
#> Warning in tclass.xts(x): index does not have a 'tclass' attribute
#> Warning in tclass.xts(x): index does not have a 'tclass' attribute
# map stock names and sectors
<- c(SP500$GICS.Sector[SP500$Symbol %in% colnames(stock_prices)])
stock_sectors <- as.numeric(as.factor(stock_sectors))
stock_sectors_index
# plot network
<- graph_from_adjacency_matrix(graph_mrf$adjacency,
net mode = "undirected",
weighted = TRUE)
<- layout_nicely(net)
la_kcomp V(net)$label.cex = 1
<- c("#FD7272", "#55E6C1", "#25CCF7")
colors 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]],
V(net)$cluster[x[1]]], 'grey'))
colors[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: