Skip to content
All library documents

Vectorized Black–Scholes Greeks with the fOptions R Package

Article Quant Q&A · Author: RockScience

Summary

The document addresses a performance question about calculating option prices or Greeks across many inputs in R. Its answer explains that the fOptions package's GBSGreeks function already accepts vector inputs, so a separate compiled-loop package is not required for the described calculation. The example applies the function to a set of puts with corresponding vectors of underlying prices, strikes, times, rates, cost-of-carry values, and volatilities.

The function call selects one Greek and one option type for the vectorized calculation; the response notes that calls and puts or different Greeks should not be mixed in a single call. It compares the vectorized result with an mapply calculation as a validation check. The excerpt covers a specific package interface and Black–Scholes-style Greeks, not a general performance benchmark or a claim that every option-pricing task is vectorized in the same way.

Key ideas

  • The fOptions GBSGreeks function can process vector inputs for the described calculation.
  • A vectorized call selects one Greek and one option type at a time.
  • Input vectors provide the corresponding values for each option contract.
  • The example checks the vectorized output against results from mapply.
  • The discussion addresses a particular R package interface rather than general performance across pricing libraries.

Tags

Full text
# good R package for vectorized option pricing


# good R package for vectorized option pricing












I am using for now the package `fOptions` but it doesn't allow for vectorized computation of black76 prices and delta. Which package can be used to do that?

As noted by @Richard, I could use `lapply`, but it is actually looping in R, which is slow (at least too slow for me). I am looking for a package that has a compiled loop, ie that provides a native vectorized function.

## Answer by RockScience (score 4, accepted)

https://quant.stackexchange.com/a/18611

Answering my own question as it could be useful for others. Actually package `fOptions` is vectorized. The only constraint (and that make sense) is that you can't compute at the same time 2 different greeks, or mix up calls and puts.

So assuming that you want to compute the delta of a set of puts, the code will be the following:

```
fOptions::GBSGreeks(Selection = "Delta",
                    TypeFlag="p",
                    S=c(100,100)+1:2, 
                    X=c(100,100), 
                    Time=c(0.1,0.2),
                    r=c(0,0), 
                    b=c(0,0), 
                    sigma=c(0.2,0.2))
```

which returns the same (that's the validation) as the output of `mapply`:

```
mapply(FUN       = fOptions::GBSGreeks,
       Selection = c("Delta","Delta"),
       TypeFlag=c("p","p"),
       S=c(100,100)+1:2, 
       X=c(100,100),
       Time=c(0.1,0.2),
       r=c(0,0), 
       b=c(0,0), 
       sigma=c(0.2,0.2))
```

nice...

## Answer by Richi Wa (score 0)

https://quant.stackexchange.com/a/18610

if you put all your option objects into a list then you can use `lapply`. Read the documentation or just thist post for details.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.