rformulavarperformanceanalyticsvolatility

Is it possible to flip a formula in R?


I was working with a project and I used the VaR() function from the PerformanceAnalytics package to calculate Value-at-risk. I wanted to find out the probability of a stock generating making a loss of 1% or more. I found a solution to the problem by plugging numbers in to the probability variable, and controlling to see if it was approaching -1%. However, I was curious if it was possible to flip the formula so that I can just plug in the output and then the function will produce what would have been the input.

Produced the loss at 97.5% probability:

VaR(DNOlog, p = 0.975)

Produced a loss of -1% by changing the probability until it fit:

VaR(DNOlog, p = 0.6512184)

Solution

  • Let's get a reproducible example to demonstrate how you would go about this:

    library(PerformanceAnalytics)
    
    set.seed(2)
    returns <- rnorm(1000, sd = 0.01)
    

    This gives us a sensible result from VaR

    VaR(returns, p = 0.975)
    #>            [,1]
    #> VaR -0.01893631
    

    To reverse this, we can use uniroot. This is a function which uses an iterative approach to finding the input value that makes a function return 0:

    inverse_VaR <- function(x, target) {
      f <- function(p) VaR(x, p)[1, 1] - target
      uniroot(f, c(0.6, 0.99999), tol = .Machine$double.eps)$root
    }
    

    In our example, if we want to find the p value that makes VaR give an output of -0.01 with our vector returns, we can do:

    inverse_VaR(returns, -0.01)
    #> [1] 0.848303
    

    And to show this works, we can do:

    VaR(returns, 0.848303)
    #>             [,1]
    #> VaR -0.009999999
    

    Created on 2022-04-16 by the reprex package (v2.0.1)