I completed a preliminary function to calculate Aumann-Serrano riskiness in R

?Download asRisk.r
1
2
3
4
5
6
7
8
9
10
11
12
13
asRisk <- function(x){
  if (mean(x)<0|min(x)>=0){
    return(0) #If expected value is  < 0 or there are no negatives, return 0
  } else {
      asNumber <- 0.00001
      total <- 2
      while (total > 1){
        total <- sum((1/length(x))*exp(-x/asNumber))
        asNumber <- asNumber + .00001
      }
      return(sprintf("%.5f",asNumber))
  }
}

To use this function, input a vector of returns. If AS risk cannot be calculated, the function will return "0". If the gambles can be used, it will calculate AS riskiness to 5 decimal points. If more or less are desired, you can change

Generally, to use this, I would recommend using one of the functions in quantmod such as weeklyReturn() or dailyReturn(). An example of this would be

?View Code RSPLUS
1
asRisk(dailyReturn(AAPL['2010']))

This example will return the AS risk of AAPL stock in 2010. Quantmod uses the TTR package which allows a lot of quick and powerful date selection.

In the future I will add errors/warnings, and maybe make precision adjustable or switch to a solver package. I also need to revise the function to meet a few more of my design parameters. This seems to be a good start and perfect for my research project next semester though!