Q:

Tools/R-code to create gain/loss-asymmetry plots

The gain/loss asymmetry is a well known stylized fact: It basically states that real financial time series take longer for going up than going down.
To detect it a heavy statistical machinery is needed: Detrending the time series, calculating the inverse statistics, normalizing the distribution, fitting a Generalized Gamma distribution... to name but a few.
The result are plots like these (from http://papers.ssrn.com/sol3/papers.cfm?abstract_id=844364):

My question
I want to reproduce those plots. Do you know any software, tools and/or preferably R code/packages with which this can be done? Every little hint may be helpful - Thank you.

A:

I wrote an R function to create those plots:
library(quantmod)

getSymbols("^GSPC", from = "1950-01-01")
## [1] "GSPC"

inv_stat <- function(symbol, name, target = 0.05) {
  p <- coredata(Cl(symbol))
  end <- length(p)
  days_n <- days_p <- integer(end)

  # go through all days and look when target is reached the first time from there
  for (d in 1:end) {
    ret <- cumsum(as.numeric(na.omit(ROC(p[d:end]))))
    cond_n <- ret < -target
    cond_p <- ret > target
    suppressWarnings(days_n[d] <- min(which(cond_n)))
    suppressWarnings(days_p[d] <- min(which(cond_p)))
  }

  days_n_norm <- prop.table(as.integer(table(days_n, exclude = "Inf")))
  days_p_norm <- prop.table(as.integer(table(days_p, exclude = "Inf")))

  plot(days_n_norm, log = "x", xlim = c(1, 1000), main = paste0(name, " gain-/loss-asymmetry with target ", target), xlab = "days", ylab = "density", col = "red")
  points(days_p_norm, col = "blue")

  c(which.max(days_n_norm), which.max(days_p_norm))
}

inv_stat(GSPC, name = "S&P 500")
 ## [1] 10 24

The following plot is being produced (will take some time):

Two things are missing:

Detrending of time series
Fitted probability distribution

If you want to add them or if you have ideas how to improve the code, please let me know!

