QQuantLib.finance¶
QQuantLib.finance.classical_finance¶
This module contains several auxiliary functions used in quantitative classical finances
Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro Costas
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.QQuantLib.finance.classical_finance.
bs_tree
(s_0: float, risk_free_rate: float, volatility: float, maturity: float, number_samples: int, time_steps: int, discretization: int, bounds: float, **kwargs)¶ Computes the probabilities of all possible pahts from the approximated solution of the Black-Scholes SDE using the Euler-Maruyama discretization 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
maturity (float) – the maturity
strike (float) – the strike
number_samples (int) – number of samples
steps (time) – number of time steps
discretization (float) – number of points to build the discrete version of the gaussian density
bounds (float) – bounds of the Gaussian density
- Returns
s_t – array of samples from the SDE.
- Return type
numpy array of floats
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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
-
tnbs.BTC_02_AE.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.payoff_class¶
Class definition for the PayOff
Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro
-
class
tnbs.BTC_02_AE.QQuantLib.finance.payoff_class.
PayOff
(**kwargs)¶ Bases:
object
Class for selecting derivative options and configuring them.
- Parameters
kwargs (dictionary) –
Dictionary for selecting and configuring the derivative option. ç Implemented keys:
- strike: float
strike of the option.
- coupon: float
only valid for Digital Options
- pay_offstring
type of pay_off function to load
-
get_pay_off
()¶ Select of a PayOff
QQuantLib.finance.probability_class¶
Definition for DensityProbability Class.
Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro
-
class
tnbs.BTC_02_AE.QQuantLib.finance.probability_class.
DensityProbability
(probability_type: str, **kwargs)¶ Bases:
object
Class for selecting pay off functions algorithm
- Parameters
probability_type (string) – type of probability density function to load
kwargs (dictionary) –
Dictionary for configuring the asset and the probability used for simulating its behaviour.Implemented keys:
- s_0float
initial value of the asset
- risk_free_ratefloat
risk free ratio
- maturityfloat
time where the probability wants to be calculated.
- volatiliyfloat
volatility of the asset.
-
static
get_density
(probability_type, **kwargs)¶ Create the probability function
- Parameters
probability_type (string) – type of probability density function to load
kwargs (dictionary) –
with necessary information for configuring the probability density
- s_0float
initial value of the asset
- risk_free_ratefloat
risk free ratio
- maturityfloat
time where the probability wants to be calculated
- volatiliyfloat
volatility of the asset
-
static
get_density_prob
(probability_type, **kwargs)¶ Configures a probability density
- Parameters
probability_type (string) – type of probability density function to load
kwargs (dictionary) –
with necessary information for configuring the probability density.
- s_0float
initial value of the asset
- risk_free_ratefloat
risk free ratio
- maturityfloat
time where the probability wants to be calculated
- volatiliyfloat
volatility of the asset
QQuantLib.finance.quantum_integration¶
This module contains a function for solving integrals using Amplitude Estimation techniques. The function uses the Encoding class for loading data into the quantum circuit by creating an oracle and the AE class for getting the amplitude of a selected state using AE techniques. The function deals with all the normalisations needed for transform this amplitude into the desired integral.
Authors: Alberto Pedro Manzano Herrero & Gonzalo Ferro
-
tnbs.BTC_02_AE.QQuantLib.finance.quantum_integration.
q_solve_integral
(**kwargs)¶ Function for solving an integral using quantum amplitude estimation techniques.
- Parameters
kwars (dictionary) –
Dictionary for configuring the integration to compute and the AE technique used for getting the solution.
- array_functionnumpy array
numpy array with the desired function for encoding into the Quantum Circuit.
- encodingint
Selecting the encode protocol
- array_probabilitynumpy array
numpy array with the desired probability for encoding into the Quantum Circuit. It can be None (uniform distribution will be used)
- ae_typestring
string with the desired AE algorithm: MLAE, CQPEAE, IQPEAE, IQAE, RQAE
Note
Other kwargs input dictionary keys will be related with the encoding of the integral into the quantum circuit (see QQuantLib.DL.encoding_protocols) and for the configuration of the AE algorithm used (see QQuantLib.AE.ae_class)
- Returns
ae_estimation (pandas DataFrame) – DataFrame with the desired integral computation.
solver_ae (objet based on the AE class)