-
Notifications
You must be signed in to change notification settings - Fork 13
/
README.Rmd
160 lines (134 loc) · 5.33 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "fingraph README"
output:
html_document:
keep_md: true
---
```{r, echo = FALSE}
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)
```
# fingraph
[![codecov](https://codecov.io/gh/convexfi/fingraph/branch/main/graph/badge.svg?token=OhreF1p2Yt)](https://app.codecov.io/gh/convexfi/fingraph)
This repo contains ADMM implementations to estimate weighted undirected graphs
(Markov random fields) under Student-t assumptions with applications to financial
markets.
## Installation
**fingraph** depends on the development version of **spectralGraphTopology**,
which can be installed as:
```{r, eval = FALSE}
> devtools::install_github("convexfi/spectralGraphTopology")
```
The stable version of **fingraph** can be installed directly from CRAN:
```{r, eval = FALSE}
> install.packages("fingraph")
```
#### Microsoft Windows
On MS Windows environments, make sure to install the most recent version of ``Rtools``.
## Usage
### Learning a graph of cryptocurrencies
```{r plot_crypto_network, message=FALSE}
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)
```
### Learning a network of S&P500 stocks
```{r plot_sp500_stocks_network, message=FALSE}
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)
```
## Citation
If you made use of this software please consider citing:
- [Cardoso JVM](https://mirca.github.io), [Ying J](https://github.com/jxying),
[Palomar DP](https://www.danielppalomar.com) (2021).
[Graphical Models in Heavy-Tailed Markets](https://papers.nips.cc/paper/2021/hash/a64a034c3cb8eac64eb46ea474902797-Abstract.html).
[Advances in Neural Information Processing Systems](https://neurips.cc/Conferences/2021) (NeurIPS’21).
## Links
- [RFinance'23 Slides](https://github.com/mirca/rfinance-talk/blob/main/rfinance.pdf)
- [NeurIPS’21 Slides](https://palomar.home.ece.ust.hk/papers/2021/CardosoYingPalomar-NeurIPS2021-slides.pdf)
- [NeurIPS'21 Poster](https://palomar.home.ece.ust.hk/papers/2021/CardosoYingPalomar-NeurIPS2021-poster.png)
- [NeurIPS'21 Supplementary Material](https://palomar.home.ece.ust.hk/papers/2021/CardosoYingPalomar-NeurIPS2021-supplemental.pdf)
- [CRAN Package](https://cran.r-project.org/package=fingraph)