Risk Indicators

Risk management
Author

Beniamino Sartini

Published

January 17, 2024

Modified

January 17, 2024

1 Risk Indicators

Show setup code
library(dplyr)
library(ggplot2)
library(readr)
library(DT)
asset <- readr::read_csv("../../../databases/temporary/asset.csv")
asset <- na.omit(asset)
head(asset) %>%
DT::datatable(rownames = NULL, 
              filter = "none",
              escape = FALSE,
              options = list(
                dom = 't',
                initComplete = DT::JS(
                  "function(settings, json) {",
                  "$(this.api().table().header()).css({'background-color':
                  '#000', 'color': '#fff'});", "}")
                )) %>%
  DT::formatRound(c("btc", "bnb", "eth"), digits = 2) %>%
  DT::formatRound(c("xrp"), digits = 4)

1.1 Sharpe Ratio

Sharpe Ratio

\[SR = \frac{r_{\Pi} - r_{\texttt{free}}}{\sigma_{\Pi}}\]

sharpe_ratio function
sharpe_ratio <- function(x, rf = 0, na.rm = TRUE){
  # log-price 
  log_x <- log(x)
  # log-returns 
  ret_x <- c(0, diff(log_x))
  # log-returns mean 
  mu_x <- mean(ret_x, na.rm = na.rm)
  # log-returns standard deviation
  sd_x <- sd(ret_x, na.rm = na.rm)
  # sharpe ratio
  sr <- (mu_x - rf)/sd_x
  return(sr)
}

1.1.1 Example

Show the code
tibble(btc = sharpe_ratio(asset$btc), 
       eth = sharpe_ratio(asset$eth),
       bnb = sharpe_ratio(asset$bnb),
       xrp = sharpe_ratio(asset$xrp)) %>%
DT::datatable(rownames = '<a target="_blank" href="https://it.wikipedia.org/wiki/Indice_di_Sharpe">Sharpe Ratio</a>', 
              filter = "none",
              escape = FALSE,
              options = list(
                dom = 't',
                initComplete = DT::JS(
                  "function(settings, json) {",
                  "$(this.api().table().header()).css({'background-color':
                  '#000', 'color': '#fff'});", "}")
                )) %>%
  DT::formatRound(c("btc", "bnb", "eth", "xrp"), digits = 4) 

1.2 Diaman Ratio

Consider the series of weekly prices \(P_1, \dots, P_i, \dots, P_{\bar{t}}\), where \(\bar{t}\) is the number of observations in the sample. Let’s define the time index as \(t_i = (0, \frac{1}{f}, \dots, \frac{\bar{t}-1}{f})\) where \(f = 52\) denote the number of weeks in a year. Then, fitting the following linear model, i.e.  \[P_i = \alpha + \beta t_i + e_t \text{,}\] and given the estimated \(\beta\) and the \(R^2\) of the regression, the Diaman Ratio is computed as: \[DR = \beta R^2\] Some considerations:

  1. The Diaman Ratio is obtained from a regression of the historical price series against time, so for the observed time period, given the same return and volatility of returns, the Diaman Ratio discriminates between different trajectories.
  2. The Diaman Ratio is not tied to the risk-free rate, although it can be easily incorporated into the calculation.
  3. Changing the frequency of data used for evaluating a series can lead to very different ranking results just by using weekly data instead of daily data.
  4. The Diaman Ratio is capable of estimating both positive and negative slopes, but it has difficulty in the presence of directional changes and non-linear historical series.
Show diaman_ratio function
diaman_ratio <- function(x, weekly = TRUE){
  # length of the time series 
  t <- length(x)
  # log-price 
  log_x <- log(x)
  # time indicator 
  f <- ifelse(weekly, 52, 365)
  t_i <- c(1:t)/f
  # regression 
  p_i <- lm(log_x ~ t_i)
  # coefficient beta 
  beta <- coef(p_i)[2]
  # R2 of the regression 
  r2 <- summary(p_i)$r.squared
  # diaman ratio
  dr <- c(diaman_ratio = beta*r2)
  attr(dr, "trend") <- cumprod(exp(predict(p_i)))
  return(dr)
}

1.2.1 Example

Code for Table
dplyr::tibble(btc = diaman_ratio(asset$btc, weekly = FALSE), 
              eth = diaman_ratio(asset$eth, weekly = FALSE),
              bnb = diaman_ratio(asset$bnb, weekly = FALSE),
              xrp = diaman_ratio(asset$xrp, weekly = FALSE)) %>%
DT::datatable(rownames = '<a target="_blank" href="https://it.wikipedia.org/wiki/Diaman_Ratio">Diaman Ratio</a>', 
              filter = "none", 
              escape = FALSE, 
              options = list(
                dom = 't',
                initComplete = DT::JS(
                  "function(settings, json) {",
                  "$(this.api().table().header()).css({'background-color':
                  '#000', 'color': '#fff'});", "}")
                )) %>%
  DT::formatRound(c("btc", "bnb", "eth", "xrp"), digits = 4) 

1.3 Sortino Ratio

The downside risk is a measure of risk similar to standard deviation, but the DSR is concentrated on the negative part of the volatility of the investment. Its reference value \(r_{\texttt{target}}\) is not the mean of the return, but the minimum acceptable return, for example the risk-free rate, fixed a priori. The downside risk measure the deviations of the returns with respect to a minimum threshold \(r_{\texttt{target}}\) and it is computed as follows: \[DSR = \sqrt{\int_{-\infty}^{T}(r_{\texttt{target}}-x)^2f(x)dx}\]

In general, an elevate Sortino Ratio denote that the variability of the returns is not centered below the acceptable threshold fixed. On the other hand a low Sortino Ratio denote that the variability is concentrated below the minimum return, therefore in general we should prefer instruments with an high Sortino’s value. \[SR = \frac{r_{\Pi} - r_{\texttt{target}}}{\text{DSR}}\]

sortino_ratio function
sortino_ratio <- function(x, r_target = 0){
  # Log-price 
  log_x <- log(x)
  # Log-returns mean
  r <- c(0, diff(log_x))
  # Mean return
  r_pi <- mean(r, na.rm = TRUE)
  # Density of log-returns 
  ker <- density(r)
  ker <- dplyr::tibble(x = ker$x, p = ker$y/sum(ker$y))
  ker <- ker[ker$x < r_target,]
  # Downside risk
  dsr <- sqrt(sum(ker$p*(r_target - ker$x)^2))
  # Sortino ratio
  sr <- (r_pi - r_target)/dsr
  return(sr)
}

1.3.1 Example

Code for Table
dplyr::tibble(btc = sortino_ratio(asset$btc, r_target = 0.065/365), 
              eth = sortino_ratio(asset$eth, r_target = 0.065/365),
              bnb = sortino_ratio(asset$bnb, r_target = 0.065/365),
              xrp = sortino_ratio(asset$xrp, r_target = 0.065/365)) %>%
DT::datatable(rownames = '<a target="_blank" href="https://it.wikipedia.org/wiki/Indice_di_Sortino">Sortino Ratio</a>',
              filter = "none", 
              escape = FALSE,
              options = list(
                dom = 't',
                initComplete = DT::JS(
                  "function(settings, json) {",
                  "$(this.api().table().header()).css({'background-color':
                  '#000', 'color': '#fff'});", "}")
                )) %>%
  DT::formatRound(c("btc", "bnb", "eth", "xrp"), digits = 4) 
Back to top

Citation

BibTeX citation:
@online{sartini2024,
  author = {Sartini, Beniamino},
  title = {Risk {Indicators}},
  date = {2024-01-17},
  url = {https://cryptoverser.org/articles/signals-risk-indicators/risk-indicators.html},
  langid = {en}
}
For attribution, please cite this work as:
Sartini, Beniamino. 2024. “Risk Indicators.” January 17, 2024. https://cryptoverser.org/articles/signals-risk-indicators/risk-indicators.html.