# Black-Scholes Option Pricing in R

The Black-Scholes or Black-Scholes-Merton model is a financial mathematical equation for pricing options contracts and other derivatives.

Fischer Black and Myron Scholes published the formula in their 1973 paper “The Pricing of Options and Corporate Liabilities“. Robert C. Merton published a paper expanding on the mathematical understanding of the options pricing model and devised the term “Black-Scholes options pricing model”.

This tutorial will go through how to write the Black-Scholes options pricing formula from scratch in R and how to use the formulate to price an option

You can use our free Black-Scholes option pricing calculator to estimate the fair value of a European call or put option.

## Formula

Using the Black-Scholes model, the price of a European call option (only one time you can exercise the option) is calculated using the formula:

C = S0N(d1) – Xe-rtN(d2)

Where:

• C = price of the call option
• S0 = price of the underlying stock
• X = option exercise price or strike price
• r = risk-free interest rate
• t = time until expiration (maturity)

N represents the cumulative distribution function for a normal (Gaussian) distribution, which we can understand as “the probability that a random variable is less or equal to its input for a normal distribution”.

d1 = (ln(S0/X) + (r + 𝞂2 / 2 )*T) / (𝞂√T)

d2 = (ln(S0/X) + (r – 𝞂2 / 2 )*T) / (𝞂√T)

Where 𝞂 is the volatility of the underlying asset.

We can think of the two terms in the sum as the current price in the stock weighted by the probability of exercising the option to buy the stock minus the discounted price of exercising the option weighted by the probability of exercising the option. In other words, what you will pay minus what you will receive (Introduction to the Black-Scholes formula, Khan Academy 2013).

The price of a put option is:

P = Xe-rtN(-d2)S0N(-d1)

You can use our free Black-Scholes option pricing calculator to estimate the fair value of a European call or put option.

## Black-Scholes Assumptions

The Black-Scholes model holds the following assumptions:

• The option is European and can only be exercised at expiration
• The returns of the underlying asset are normally distributed
• Markets are random, in that market movements cannot be predicted
• No dividends are paid out during the life of the option
• No transaction costs for buying the option
• Risk-free interest rate and volatility of the underlying asset are known and constant

The original model does not account for dividends paid during the option’s lifetime. Still, there are versions of the model that account for dividends by determining the ex-dividend date value of the underlying asset. The model has also been adapted for the effect of options that can be exercised before expiration.

## Black-Scholes Option Pricing using R from scratch

We can write a function to calculate the option price once we have the values for the parameters:

• S = price of the underlying stock
• X = option exercise price
• r = risk-free interest rate
• t = time until expiration (maturity)
• sig = volatility of the underlying asset
```bsm_func <- function(S, X, r, T, sig, option_type){

if (option_type=="C") {

d1 <- (log(S/X) + (r + sig^2/2)*T) / (sig*sqrt(T))

d2 <- (log(S/X) + (r - sig^2/2)*T) / (sig*sqrt(T))

val <- S*pnorm(d1) - X*exp(-r*T)*pnorm(d2)

return (val)

}

if (option_type=="P") {

d1 <- (log(S/X) + (r + sig^2/2)*T) / (sig*sqrt(T))

d2 <- (log(S/X) + (r - sig^2/2)*T) / (sig*sqrt(T))

val <-  X*exp(-r*T)*pnorm(-d2) - S*pnorm(-d1)

return (val)

}

}```

We can call the function and pass the five parameters to calculate the value of a call or put option.

```call <- bsm_func(110, 100, 0.04, 1, 0.2, "C")
call```

Let’s run the code to get the price of the call option.

` 16.96868`

The value of the option is \$16.97.

## Black-Scholes Option Pricing using R using qrmtools

We can also use the Black_Scholes function from the qrmtools library. The syntax for the Black_Scholes function is as follows:

`Black_Scholes(t, S, r, sigma, K, T, type = c("call", "put"))`

Where:

• t – Initial or current time in years
• S – Stock price at time t
• r – Risk-free annual interest rate
• sigma – Annual volatility (standard deviation)
• K – Strike price
• T – Time until expiration in years
• type`character` string indicating whether pricing a call (default) or a put option

We can install and load the `qrmtools` as follows:

```install.packages("qrmtools")
library("qrmtools")```

Let’s run an example of the `Black_Scholes` to compare with the previous from-scratch example.

```# t = 0, S = 110, r = 0.04, sigma = 0.2, K = 100, T = 1

Black_Scholes(0, 110, 0.04, 0.2, 100, 1)```
`16.96868`

The value of the option is \$16.97.

## Summary

Congratulations on reading to the end of this tutorial!

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

To learn how to use Black-Scholes in Python and C++, go to the articles:

For further reading on the Options Greeks, go to the article:

You can use our free Black-Scholes option pricing calculator to estimate the fair value of a European call or put option.

You can use our free Implied Volatility calculator to estimate the Implied Volatility of a priced option.

Have fun and happy researching!