Documentation
Data
The P&L parameters from Vorobets [2022] and simulation from Vorobets [2021] follow with this package. The simulation assumes that returns follow a log-normal distribution, while the parameters are given by the Danish common return expectations for the 2nd half of 2021.
The parameters and P&L simulation are used in the examples and allows you to immediately start exploring the functionality of this package. You can also use it to test your understanding of the theory by replicating results.
In addition to the above, a simulated time series of an equity index, an associated implied volatility surface, a risk-free zero-coupon curve, and a credit spread curve is included. You can use this time series data to, e.g., validate your risk modeling approach. See the example that uses the time series data for more information.
- load_parameters()
Function for loading the P&L parameters from Vorobets (2022).
- Return type
Tuple
[list
,ndarray
,ndarray
]- Returns
Instrument names, means vector, and covariance matrix.
- load_pnl()
Function for loading the P&L simulation from Vorobets (2021).
- Return type
DataFrame
- Returns
P&L simulation.
- load_time_series()
Function for loading the time series simulation.
- Return type
DataFrame
- Returns
Time series simulation.
Entropy Pooling
The Entropy Pooling approach solves the problem
subject to the constraints
The approach was first introduced by Meucci [2008], while the code is implemented using notation from Vorobets [2021].
- entropy_pooling(p, A, b, G=None, h=None)
Function for computing Entropy Pooling posterior probabilities.
- Parameters
p (
ndarray
) – Prior probability vector with shape (S, 1).A (
ndarray
) – Equality constraint matrix with shape (M, S).b (
ndarray
) – Equality constraint vector with shape (M, 1).G (
Optional
[ndarray
]) – Inequality constraint matrix with shape (N, S).h (
Optional
[ndarray
]) – Inequality constraint vector with shape (N, 1).
- Return type
ndarray
- Returns
Posterior probability vector with shape (S, 1).
Functions
The functions below are useful when working with Entropy Pooling, e.g., they can be used to calculate posterior moments and correlation matrix to verify that views have been implemented correctly. The covariance matrix function is useful for mean-variance optimization with Entropy Pooling views.
- correlation_matrix(R, p=None)
Function for computing the correlation matrix.
- Parameters
R (
Union
[DataFrame
,ndarray
]) – P&L / risk factor simulation with shape (S, I).p (
Optional
[ndarray
]) – probability vector with shape (S, 1). Default np.ones((S, 1)) / S.
- Return type
DataFrame
- Returns
Correlation matrix with shape (I, I).
- covariance_matrix(R, p=None)
Function for computing the covariance matrix.
- Parameters
R (
Union
[DataFrame
,ndarray
]) – P&L / risk factor simulation with shape (S, I).p (
Optional
[ndarray
]) – probability vector with shape (S, 1). Default np.ones((S, 1)) / S.
- Return type
DataFrame
- Returns
Covariance matrix with shape (I, I).
- simulation_moments(R, p=None)
Function for computing simulation moments (mean, volatility, skewness, and kurtosis).
- Parameters
R (
Union
[DataFrame
,ndarray
]) – P&L / risk factor simulation with shape (S, I).p (
Optional
[ndarray
]) – probability vector with shape (S, 1). Default np.ones((S, 1)) / S.
- Return type
DataFrame
- Returns
DataFrame with shape (I, 4) containing simulation moments.
Option Pricing
The option pricing functionality consists of functions that use Black’s model to price European call and put options.
- call_option(F, K, sigma, r, T)
Function for computing European call option price using Black’s formula.
- Parameters
F (
float
) – Forward price for maturity T.K (
float
) – Strike value.sigma (
float
) – Implied volatility for maturity T and strike K.r (
float
) – Interest rate for maturity T.T (
float
) – Time to maturity.
- Return type
float
- Returns
European call option price.
- put_option(F, K, sigma, r, T)
Function for computing European put option price using Black’s formula.
- Parameters
F (
float
) – Forward price for maturity T.K (
float
) – Strike value.sigma (
float
) – Implied volatility for maturity T and strike K.r (
float
) – Interest rate for maturity T.T (
float
) – Time to maturity.
- Return type
float
- Returns
European put option price.
Portfolio Optimization
The MeanCVaR and MeanVariance objects solve the problem
with \(\mathcal{R}\left(e\right)\) being the CVaR or variance for exposures \(e\in\mathbb{R}^{I}\), subject to the constraints
A method for solving the CVaR problem was first introduced by Rockafellar and Uryasev [2000], while the implemented algorithm is based on Künzi-Bay and Mayer [2006]. The notation in relation to the P&L simulations \(R\) follows Vorobets [2021]. For the variance risk measure, a standard quadratic programming solver is used.
- class MeanCVaR(R, A=None, b=None, G=None, h=None, p=None, alpha=None, **kwargs)
Class for efficient mean-CVaR optimization using Benders decomposition.
- Parameters
R (
ndarray
) – Matrix with P&L simulations and shape (S, I).A (
Optional
[ndarray
]) – Equality constraints matrix with shape (M, I).b (
Optional
[ndarray
]) – Equality constraints matrix with shape (M,).G (
Optional
[ndarray
]) – Inequality constraints matrix with shape (N, I).h (
Optional
[ndarray
]) – Inequality constraints vector with shape (N,).p (
Optional
[ndarray
]) – Vector containing scenario probabilities with shape (S, 1). Default: np.ones((S, 1)) / S.alpha (
Optional
[float
]) – Alpha value for alpha-VaR and alpha-CVaR. Default: 0.95.kwargs (
dict
) – options dictionary with Benders algorithm parameters.
- Raises
ValueError – If constraints are infeasible.
- efficient_frontier(num_portfolios=None)
Method for computing the efficient frontier.
- Parameters
num_portfolios (
Optional
[int
]) – Number of portfolios used to span the efficient frontier. Default: 9.- Return type
ndarray
- Returns
Efficient frontier with shape (I, num_portfolios).
- Raises
ValueError – If expected return is unbounded.
- efficient_portfolio(return_target=None)
Method for computing a mean-CVaR efficient portfolio with return a target.
- Parameters
return_target (
Optional
[float
]) – Return target for the efficient portfolio. The minimum CVaR portfolio is computed by default.- Return type
ndarray
- Returns
Efficient portfolio exposures with shape (I, 1).
- class MeanVariance(mean, covariance_matrix, A=None, b=None, G=None, h=None)
Class for efficient mean-variance optimization.
- Parameters
mean (
ndarray
) – Mean vector with shape (I,).covariance_matrix (
ndarray
) – Covariance matrix with shape (I, I).A (
Optional
[ndarray
]) – Equality constraints matrix with shape (M, I).b (
Optional
[ndarray
]) – Equality constraints matrix with shape (M,).G (
Optional
[ndarray
]) – Inequality constraints matrix with shape (N, I).h (
Optional
[ndarray
]) – Inequality constraints vector with shape (N,).
- Raises
ValueError – If constraints are infeasible.
- efficient_frontier(num_portfolios=None)
Method for computing the efficient frontier.
- Parameters
num_portfolios (
Optional
[int
]) – Number of portfolios used to span the efficient frontier. Default: 9.- Return type
ndarray
- Returns
Efficient frontier with shape (I, num_portfolios).
- Raises
ValueError – If expected return is unbounded.
- efficient_portfolio(return_target=None)
Method for computing a mean-variance efficient portfolio with a return target.
- Parameters
return_target (
Optional
[float
]) – Return target for the efficient portfolio. The minimum variance portfolio is computed by default.- Return type
ndarray
- Returns
Efficient portfolio exposures with shape (I, 1).
Algorithm Parameters
For the variance risk measure, CVXOPT’s default values are used. These can be adjusted directly following the instructions given in the link.
For the CVaR risk measure, control parameters can be set globally using the cvar_options dictionary, e.g.,
import fortitudo.tech as ft
ft.cvar_options['demean'] = False
ft.cvar_options['R_scalar'] = 10000
or for a particular instance of the MeanCVaR class:
opt = ft.MeanCVaR(R, A, b, G, h, options={'demean': False, 'R_scalar': 10000})
The following parameters can be adjusted:
'demean'
Whether to use demeaned P&L when calculating CVaR. Default:
True
.'R_scalar'
Scaling factor for the P&L simulation. Default:
1000
.'maxiter'
Maximum number of iterations for the decomposition algorithm, i.e., maximum number of relaxed master problems the algorithm is allowed to solve. Default:
500
.'reltol'
Relative tolerance for the difference between the currently best upper and lower bounds. Default:
1e-8
.'abstol'
Absolute tolerance for the difference between the currently best upper and lower bounds if the lower bound is less than
1e-10
. Default:1e-8
.
The algorithm stops when one of the 'maxiter'
, 'reltol'
,
or 'abstol'
conditions are satisfied. The parameters have been tested
with “percentage return” P&L and work well. In most cases, the algorithm stops
due to relative convergence in less than 100 iterations. If you use P&L
simulations that are scaled differently, you might need to adjust them.