classical_finance
This module contains several auxiliary functions used in quantitative classical finances
Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro Costas
- QQuantLib.finance.classical_finance.bs_call_price(s_0: float, risk_free_rate: float, volatility: float, maturity: float, strike: float, **kwargs)
Computes the price for a European call option.
Notes
The formula is:
\[C(S, T) = S\Phi(d_1)-Ke^{-rT}\Phi(d_2)\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
strike (float) – the strike
- Returns:
price – price of the European call option
- Return type:
float
- QQuantLib.finance.classical_finance.bs_density(s_t: float, s_0: float, risk_free_rate: float, volatility: float, maturity: float, **kwargs)
Evaluates the Black-Scholes density function at s_t for a given set of parameters. The formula is:
Notes
\[\dfrac{1}{S_T\sigma\sqrt{2\pi T}}\exp\left(-\dfrac{ \ \left(\log(S_T)-\mu\right)}{2\sigma^2T}\right)\]where \(\mu = (risk\_free\_rate-0.5\sigma)T+\log(S_0)\).
- Parameters:
s_t (float) – point where we do the evaluation
s_0 (float) – current price
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
- Returns:
density – value of the Black-Scholes density function in s_t
- Return type:
float
- QQuantLib.finance.classical_finance.bs_digital_call_price(s_0: float, risk_free_rate: float, volatility: float, maturity: float, strike: float, coupon: float, **kwargs)
Computes the price for a digital(binary) call option.
Notes
The formula is:
\[DC(S, T) = e^{-rT}Coupon N(d_2)\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
strike (float) – the strike
coupon (float) – the amount received in case that the underlying pring exceeds the strike
- Returns:
price – price of the digital call option
- Return type:
float
- QQuantLib.finance.classical_finance.bs_digital_put_price(s_0: float, risk_free_rate: float, volatility: float, maturity: float, strike: float, coupon: float, **kwargs)
Computes the price for a digital (binary) put option.
Notes
The formula is:
\[DC(S, T) = e^{-rT}Coupon N(-d_2)\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
strike (float) – the strike
coupon (float) – the amount received in case that the underlying pring exceeds the strike
- Returns:
price – price of the digital call option
- Return type:
float
- QQuantLib.finance.classical_finance.bs_em_samples(s_0: float, risk_free_rate: float, volatility: float, maturity: float, number_samples: int, time_steps: int, **kwargs)
Computes samples from the approximated solution of the Black-Scholes SDE using the Euler-Maruyama discrimination.
Notes
The formula is:
\[S_{t+\Delta t} = S_t+rS_tdt+\sigma S_t N(0, 1)\sqrt{dt}\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
strike (float) – the strike
number_samples (int) – number of samples
steps (time) – number of time steps
- Returns:
s_t – array of samples from the SDE.
- Return type:
numpy array of floats
- QQuantLib.finance.classical_finance.bs_exact_samples(s_0: float, risk_free_rate: float, volatility: float, maturity: float, number_samples: int, **kwargs)
Computes samples from the exact solution of the Black-Scholes SDE.
Notes
The formula is:
\[S_T = S_0e^{\sigma*W_t+(risk_free_rate-\sigma^2/2)t}\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
strike (float) – the strike
number_samples (int) – number of samples
- Returns:
s_t – array of samples from the SDE.
- Return type:
numpy array of floats
- QQuantLib.finance.classical_finance.bs_forward_price(s_0: float, risk_free_rate: float, maturity: float, strike: float, **kwargs)
Computes the price for a forward contract. Note that it doesn’t assume that we are at the begining of the lifetime of the contract
Notes
The formula is:
\[V(S, T) = S-Ke^{-r(T-t)}\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
maturity (float) – time to expiration of the contract
strike (float) – the strike
- Returns:
price – price of the forward
- Return type:
float
- QQuantLib.finance.classical_finance.bs_probability(s_t: numpy.array, s_0: float, risk_free_rate: float, volatility: float, maturity: float, **kwargs)
Computes a discrete probability distribution from the Black-Scholes density function for a given set of parameters. This is done by evaluating the Black-Scholes density function in s_t and the normlising this result.
- Parameters:
s_t (numpy array) – points where we define the discrete probability distribution
s_0 (float) – current price
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
- Returns:
distribution – discrete probability distribution from Black-Scholes density
- Return type:
numpy array
- QQuantLib.finance.classical_finance.bs_put_price(s_0: float, risk_free_rate: float, volatility: float, maturity: float, strike: float, **kwargs)
Computes the price for a European put option.
Notes
The formula is:
\[C(S, T) = Ke^{-rT}\Phi(-d_2)-S\Phi(-d_1)\]- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
strike (float) – the strike
- Returns:
price – price of the European put option
- Return type:
float
- QQuantLib.finance.classical_finance.bs_sde_solution(x_: numpy.array, s_0: float, risk_free_rate: float, volatility: float, maturity: float, **kwargs)
For a certain parametrization \(x\) it returns a value of the underlying \(S_T(x)\) and the probability density of that value of the underlying.
Notes
The formulas are:
\[S_T = S_0e^{\sigma x+(risk_free_rate-\sigma^2/2)t} p(S_T(x)) = N(x;mean = 0, variance = T)\]- Parameters:
x (numpy array) – parametrization
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
maturity (float) – the maturity
- Returns:
s_t (numpy array) – value of the underlying corresponding to parameter x
probability_density (numpy array) – probability density corresponding to s_t
- QQuantLib.finance.classical_finance.bs_tree(s_0: float, risk_free_rate: float, volatility: float, times: numpy.ndarray, discretization: int, bounds: float, **kwargs)
Computes the probabilities of all possible pahts from the approximated solution of the Black-Scholes SDE using exact simulation for a given discretization of the Brownian motion.
- Parameters:
s_0 (float) – current price of the underlying
risk_free_rate (float) – risk free rate
volatility (float) – the volatility
times (np.ndarray) – the times of the tree
discretization (float) – number of points to build the discrete version of the gaussian density
bounds (float) – bounds of the Gaussian density
- Returns:
s_t (list of numpy arrays) – Each element of the list is an array with all the posible asset values discretization
p_t (numpy array of floats) – array with the probabilities for all the paths
- QQuantLib.finance.classical_finance.call_payoff(s_t: float, strike: float, **kwargs)
Computes the payoff of a European call option.
Notes
\[C(S_T, K) = \left(S_T-K, 0\right)^+\]- Parameters:
s_t (float) – price
strike (float) – the strike
- Returns:
payoff – the payoff
- Return type:
float
- QQuantLib.finance.classical_finance.cliquet_cashflows(local_cap, local_floor, global_cap, global_floor, paths)
Calculate the cashflows for the Cliquet option.
- Parameters:
local_cap (float) – local cap for cliquet options
local_floor (float) – local floor for cliqet options
global_cap (float) – global cap for cliquet options
global_floor (float) – global floor for cliqet options
paths (list) – input paths for the cliquet option
- Returns:
final_return – The cashflows of the option for the input paths
- Return type:
numpy array
- QQuantLib.finance.classical_finance.digital_call_payoff(s_t: float, strike: float, coupon: float = 1.0, **kwargs)
Computes the payoff for a digital(binary) call option. The formula is:
Notes
\[DC(S_T, K, Coupon) = Coupon\mathbb{1}_{\{S_T-K\}}\]- Parameters:
s_t (float) – current price of the underlying
strike (float) – the strike
coupon (float) – the amount received in case that the underlying pring exceeds the strike
- Returns:
payoff – payoff of the european digital call option
- Return type:
float
- QQuantLib.finance.classical_finance.digital_put_payoff(s_t: float, strike: float, coupon: float = 1.0, **kwargs)
Computes the payoff for a digital(binary) put option.
Notes
The formula is:
\[DC(S_T, K, Coupon) = Coupon\mathbb{1}_{\{K-S_T\}}\]- Parameters:
s_t (float) – current price of the underlying
strike (float) – the strike
coupon (float) – the amount received in case that the underlying pring exceeds the strike
- Returns:
payoff – payoff of the european digital call option
- Return type:
float
- QQuantLib.finance.classical_finance.futures_payoff(s_t: float, strike: float, **kwargs)
Computes the payoff of a futures contract.
Notes
\[F(S_T, K) = \left(S_T-K, 0\right)\]- Parameters:
s_t (float) – price
strike (float) – the strike
- Returns:
payoff – the payoff
- Return type:
float
- QQuantLib.finance.classical_finance.geometric_sum(base: float, exponent: int, coeficient: float = 1.0, **kwargs)
Computes a geometric sum
Notes
\[\begin{split}s = a+a*base+a*base^2+a*base^3+...+a*base^{exponent} = \\ = \sum_{k=0}^{exponent}a*base^k = \ a\big(\frac{1-base^{exponent+1}}{1-base}\big)\end{split}\]- Parameters:
base (float) – base of the geometric sum
exponent (int) – maximum exponent for the geometric sum
coeficient (float) – coefficient for the geometric sum
- Returns:
return – result of the geometric sum
- Return type:
float
- QQuantLib.finance.classical_finance.put_payoff(s_t: float, strike: float, **kwargs)
Computes the payoff of a European put option.
Notes
\[P(S_T, K) = \left(K-S_T, 0\right)^+\]- Parameters:
s_t (float) – price
strike (float) – the strike
- Returns:
payoff – the payoff
- Return type:
float
- QQuantLib.finance.classical_finance.tree_to_paths(tree)
Convert a tree structure from bs_tree to a path format (table form)
- Parameters:
tree (list) – list of lists with the tree structure from bs_tree
- Returns:
paths – table conversions of the tree structure input
- Return type:
list of lists