risk management

Exploring Extreme Asset Returns

rcobo

27/05/2016

1

Tail (or extreme assets) returns have been extensively studied. In his amazing paper: “Empirical properties of assets returns: stylized facts and statistical issues”, Rama Cont provides a framework on statistical analysis of price variations in various types of financial markets. He presents “Heavy tails” in asset returns as a stylised fact, i.e., statistical properties common across a wide range of instruments, markets and time periods.

Today, I want to share the “Exploring Extreme Asset Returns” – Shiny web application, as an easy tool for empirical exploration. It allows us to analyse asset returns (above a given threshold) over all Yahoo Finance! tickers, with a customisable time period.

This is the code of ui.R:

library(shiny)
library(PerformanceAnalytics)

shinyUI(fluidPage(
 titlePanel("Exploring Extreme Asset Returns"),
 
 sidebarPanel(
 helpText("Leaving aside returns smaller than a threshold we examine the frequency and magnitude of returns."),
 helpText(" "),
 helpText("We consider that we are in a bull trend (bear) if the price is above (below) the 200 day moving average. Then, we analyze returns bigger than the selected threshold in these two separate regimes."),
 helpText(" "),
 helpText("Select an equity index to examine. For example:"),
 helpText("^GSPC for S&P 500, ^GDAXI for DAX, ^N225 for Nikkei 225 or ^STOXX50e for EUROSTOXX 50."),
 helpText(""),
 helpText("On main equity indexes extreme returns are more frequent during bear markets. Moreover, the biggest positive returns occur during bear markets."),
 helpText("What if we dig into...?"),
 helpText("- Individual stocks: JNJ, AAPL, JPM... "),
 helpText("- Mutual Funds: FSPCX, TRBCX, PTCIX... "),
 helpText("- Fixed income ETFs: TLT, AGG, HYG... "),
 helpText("- Commodities: GLD, SLV, DBC... "),
 helpText("- Inverse ETFs: SH, TBT, SDS... "),
 helpText("..."),
 
 textInput("symb", "Symbol (Yahoo Finance!)", "^GSPC"),
 
 dateRangeInput("dates", "Date Range",
 label = 'Date Range:',
 start = "1990-01-01", end = Sys.Date()),
 
 sliderInput("threshold", "Daily Returns Threshold:", 
 min = 0, 
 max = 0.1, 
 value = 0.03,
 step = 0.01),
 
 helpText("Source: Yahoo Finance!")
 ),
 
 mainPanel(
 plotOutput("plot", height = 1200, width = 1600)#, 
 )
))

This is the code of server.R:

library(quantmod)

shinyServer(function(input, output) {
 
 output$plot <- renderPlot({
 library(quantmod)
 dataInput <- getSymbols(input$symb, src = "yahoo", from = input$dates[1], to = input$dates[2], auto.assign = FALSE)
 
 tags$style(type="text/css",
 ".shiny-output-error { visibility: hidden; }",
 ".shiny-output-error:before { visibility: hidden; }")
 
 p <- dataInput
 p$ma <- SMA(p[,6], n = 200)
 p$check <- p[,6] > p[,7]
 
 ReturnsDuringBullMarket <- ROC(p[,6])
 ReturnsDuringBearMarket <- ReturnsDuringBullMarket
 
 ReturnsDuringBullMarket[p$check == 0] <- 0
 ReturnsDuringBearMarket[p$check == 1] <- 0
 ReturnsDuringBullMarket[1:200] <- 0
 ReturnsDuringBearMarket[1:200] <- 0
 
 ReturnsDuringBullMarket[abs(ReturnsDuringBullMarket) < input$threshold] <- 0
 ReturnsDuringBearMarket[abs(ReturnsDuringBearMarket) < input$threshold] <- 0
 
 mylim <- round(max(rbind(abs(ReturnsDuringBullMarket), abs(ReturnsDuringBearMarket))) + 0.02, 2)
 
 Bull <- ReturnsDuringBullMarket[ReturnsDuringBullMarket != 0]
 Bear <- ReturnsDuringBearMarket[ReturnsDuringBearMarket != 0]
 
 if ((length(Bull) * length(Bear)) > 0 ) {
 
 histylim <- round(max(dim(Bull)[1], dim(Bear)[1]) / 3, 0) + 4
 
 ReturnsDuringBullMarket[ReturnsDuringBullMarket == 0] <- NA
 ReturnsDuringBearMarket[ReturnsDuringBearMarket == 0] <- NA
 
 Price <- dataInput[,6]
 
 layout(matrix(c(1, 1, 2, 2, 3, 3, 4, 5), 4, 2, byrow = TRUE))
 par(mai = c(0.5, 0.5, 0, 0), mar = c(3, 3, 2, 2), cex = 1.4) # mai = c(0.1, 0.1, 0.1, 0.1),
 # PLOT 1
 plot(Price, main = "", ylim = range(na.omit(c(Price, p[,7]))))
 par(new = TRUE)
 plot(p[, 7], main = "", ylim = range(na.omit(c(Price, p[,7]))))
 title("Price", line = -1)
 # PLOT 2
 plot(ReturnsDuringBullMarket, main = "", type = "p", pch = 24, bg = "deepskyblue", ylim = c(-mylim, mylim))
 abline(h = 0, col = "gray60")
 title("Extreme Returns During Bull Markets", line = -1)
 # PLOT 3
 plot(ReturnsDuringBearMarket, main = "", type = "p", pch = 25, bg = "orangered1", ylim = c(-mylim, mylim))
 abline(h = 0, col = "gray60")
 title("Extreme Returns During Bear Markets", line = -1)
 # PLOT 4 
 hist(Bull, col="deepskyblue", main= "", seq(from = -mylim, to = mylim, by = 0.01), xlab = "", ylim = c(0, histylim)) 
 title("Extreme Returns During Bull Markets Histogram", line = -1)
 grid()
 # PLOT 5
 hist(Bear, col="orangered1", main= "", seq(from = -mylim, to = mylim, by = 0.01), xlab = "", ylim = c(0, histylim))
 title("Extreme Returns During Bear Markets Histogram", line = -1)
 grid()
 }
 })
})

</pre>
<pre>

All done! Shiny is amazing in the way it allows you to convert your script into an interactive web application with just two simple steps.

Take a look and play around with the Exploring Extreme Asset Returns.

Sin título

Related Quantdare posts (in Spanish):

http://quantdare.com/teoria-valores-extremos/

http://quantdare.com/teoria-valores-extremos-2/

 

add a comment

[…] Exploring Extreme Asset Returns [Quant Dare] Tail or extreme assets returns have been extensively studied. In his amazing paper: Empirical properties of assets returns: stylized facts and statistical issues, Rama Cont provides a framework on statistical analysis of price variations in various types of financial markets. He presents Heavy tails in asset returns as a stylized fact, i.e., statistical properties common across a wide […]

wpDiscuz