# vollib.black_scholes package¶

## vollib.black_scholes.implied_volatility module¶

### vollib.black_scholes.implied_volatility¶

A library for option pricing, implied volatility, and greek calculation. vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.

The source code of LetsBeRational resides at www.jaeckel.org/LetsBeRational.7z .

```======================================================================================

Permission to use, copy, modify, and distribute this software is freely granted,
provided that this notice is preserved.

WARRANTY DISCLAIMER
The Software is provided "as is" without warranty of any kind, either express or implied,
including without limitation any implied warranties of condition, uninterrupted use,
merchantability, fitness for a particular purpose, or non-infringement.
======================================================================================
```
`vollib.black_scholes.implied_volatility.``implied_volatility`(price, S, K, t, r, flag)[source]

Calculate the Black-Scholes implied volatility.

Parameters: price (float) – the Black-Scholes option price S (float) – underlying asset price K (float) – strike price t (float) – time to expiration in years r (float) – risk-free interest rate flag (str) – ‘c’ or ‘p’ for call or put.
```>>> S = 100
>>> K = 100
>>> sigma = .2
>>> r = .01
>>> flag = 'c'
>>> t = .5
```
```>>> price = black_scholes(flag, S, K, t, r, sigma)
>>> iv = implied_volatility(price, S, K, t, r, flag)
```
```>>> print price, iv
5.87602423383 0.2
```
`vollib.black_scholes.implied_volatility.``implied_volatility_limited_iterations`(price, S, K, t, r, flag, N)[source]

Calculate the Black-Scholes implied volatility with limited iterations.

Parameters: price (float) – the Black-Scholes option price S (float) – underlying asset price K (float) – strike price t (float) – time to expiration in years r (float) – risk-free interest rate flag (str) – ‘c’ or ‘p’ for call or put. N (int) – the maximum number of iterations to perform
```>>> S = 100
>>> K = 100
>>> sigma = .232323232
>>> r = .01
>>> flag = 'c'
>>> t = .5
```
```>>> price = black_scholes(flag, S, K, t, r, sigma)
>>> iv = implied_volatility_limited_iterations(price, S, K, t, r, flag,1)
```
```>>> print price, iv
6.78242400926 0.232323232
```

## Module contents¶

### vollib.black_scholes¶

A library for option pricing, implied volatility, and greek calculation. vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.

The source code of LetsBeRational resides at www.jaeckel.org/LetsBeRational.7z .

```======================================================================================

Permission to use, copy, modify, and distribute this software is freely granted,
provided that this notice is preserved.

WARRANTY DISCLAIMER
The Software is provided "as is" without warranty of any kind, either express or implied,
including without limitation any implied warranties of condition, uninterrupted use,
merchantability, fitness for a particular purpose, or non-infringement.
======================================================================================
```
`vollib.black_scholes.``black_scholes`(flag, S, K, t, r, sigma)[source]

Return the Black-Scholes option price.

Parameters: S (float) – underlying asset price K (float) – strike price sigma (float) – annualized standard deviation, or volatility t (float) – time to expiration in years r (float) – risk-free interest rate flag (str) – ‘c’ or ‘p’ for call or put.
```>>> c = black_scholes('c',100,90,.5,.01,.2)
>>> abs(c - 12.111581435) < .000001
True
```
```>>> p = black_scholes('p',100,90,.5,.01,.2)
>>> abs(p - 1.66270456231) < .000001
True
```
```>>> flag, S, K, t, r, sigma = 'c',100,90,.5,.01,.2
>>> c = black_scholes(flag, S, K, t, r, sigma)
>>> python_c = python_black_scholes(flag, S, K, t, r, sigma)
>>> abs(c - python_c) < .000001
True
```
```>>> flag, S, K, t, r, sigma = 'p',100,90,.5,.01,.2
>>> p = black_scholes(flag, S, K, t, r, sigma)
>>> python_p = python_black_scholes(flag, S, K, t, r, sigma)
>>> abs(p - python_p) < .000001
True
```
`vollib.black_scholes.``d1`(S, K, t, r, sigma)[source]

Calculate the d1 component of the Black-Scholes PDE.

Parameters: S (float) – underlying asset price K (float) – strike price sigma (float) – annualized standard deviation, or volatility t (float) – time to expiration in years r (float) – risk-free interest rate

John C. Hull, “Options, Futures and Other Derivatives,” 7th edition, Example 13.6, page 294

```>>> S = 42
>>> K = 40
>>> r = .10
>>> sigma = .20
>>> t = 0.5
>>> calculated_d1 = d1(S,K,t,r,sigma)
>>> text_book_d1 = 0.7693
>>> abs(calculated_d1 - text_book_d1) < 0.0001
True
```
`vollib.black_scholes.``d2`(S, K, t, r, sigma)[source]

Calculate the d2 component of the Black-Scholes PDE.

Parameters: S (float) – underlying asset price K (float) – strike price sigma (float) – annualized standard deviation, or volatility t (float) – time to expiration in years r (float) – risk-free interest rate

John C. Hull, “Options, Futures and Other Derivatives,” 7th edition, Example 13.6, page 294

```>>> S = 42
>>> K = 40
>>> r = .10
>>> sigma = .20
>>> t = 0.5
>>> calculated_d2 = d2(S,K,t,r,sigma) #0.627841271869
>>> text_book_d2 = 0.6278
>>> abs(calculated_d2 - text_book_d2) < 0.0001
True
```
`vollib.black_scholes.``python_black_scholes`(flag, S, K, t, r, sigma)[source]
Return the Black-Scholes option price implemented in
python (for reference).
Parameters: S (float) – underlying asset price K (float) – strike price sigma (float) – annualized standard deviation, or volatility t (float) – time to expiration in years r (float) – risk-free interest rate flag (str) – ‘c’ or ‘p’ for call or put.
```>>> S,K,t,r,sigma = 60,65,.25,.08,.3
>>> c1 = black_scholes('c',S,K,t,r,sigma)
>>> c2 = python_black_scholes('c',S,K,t,r,sigma)
>>> abs(c1 - c2) < .00001
True
>>> abs(c1 - 2.13336844492) < .00001
True
```