Data
The functions below are used to load and add data. This is usually the first step where either parameters or P&L simulations are loaded and additional information is added, for example, P&L simulations for derivate instruments as well as information about market prices relative to exposures and instrument costs.
- add_instrument_info(instruments, instrument_name, cost_hold, cost_buy, cost_sell, relative_market_value)
Function for adding instrument information to an existing pd.DataFrame with shape (I, 4).
- Parameters:
instruments (
DataFrame) – pd.DataFrame containing instrument information.instrument_name (
str) – Instrument name.cost_hold (
float) – Holding cost.cost_buy (
float) – Buy transaction cost.cost_sell (
float) – Sell transaction cost.relative_market_value (
float) – Relative market value.
- Return type:
DataFrame- Returns:
Updated pd.DataFrame with the new instrument information added and shape (I+1, 4).
- Raises:
ValueError – If instruments is not a pd.DataFrame with 4 columns.
TypeError – If instrument_name is not a string.
TypeError – If cost_hold, cost_buy, cost_sell, or relative_market_value are not integers or floats.
- add_simulation_column(pnl, simulation_name, simulation)
Function for adding a new column of simulated data to an existing pd.DataFrame of P&L.
- Parameters:
pnl (
DataFrame) – Existing simulation of P&L / risk factors with shape (S, I).simulation_name (
str) – Name of the new instrument / risk factor.simulation (
ndarray) – Simulation for the new instrument / risk factor with shape (S,).
- Return type:
DataFrame- Returns:
Updated pd.DataFrame with the new instrument / risk factor added and shape (S, I+1).
- Raises:
TypeError – If pnl is not a pd.DataFrame.
TypeError – If simulation_name is not a string.
ValueError – If there is a mismatch between the number of rows for pnl and simulation.
- convert_csv(file_path, sep, decimal)
Function for converting a csv file to having sep=’,’ and decimal=’.’ by overwriting the current file.
- Parameters:
file_path (
str) – Path to the csv file.sep (
str) – Separator of the current file.decimal (
str) – Decimal of the current file.
- load_parameters(instruments_path, parameters_path, sep=',', decimal='.')
Function for loading P&L / risk factor parameters.
- Parameters:
instruments_path (
str) – Path to the file (csv or xlsx) containing instrument information and having shape (I, 4). Instrument information consists of holding, buy, and sell costs as well as a market value relative to exposure.parameters_path (
str) – Path to the file (csv or xlsx) containing the parameters with shape (I+2, I). First row for mean values, second row for volatility values, and the last I rows for the correlation matrix.sep (
str) – Separator for csv files. Default: ‘,’decimal (
str) – Decimal separator for csv files. Default: ‘.’
- Return type:
Tuple[DataFrame,DataFrame,DataFrame]- Returns:
Returns instrument information as well as P&L / risk factor means and covariance matrix.
- Raises:
TypeError – If instruments_path or parameters_path is not a string.
ValueError – If file extension is not .csv or .xlsx.
TypeError – If parameters are not integers or floats.
ValueError – If there is a mismatch between parameter and instrument names.
ValueError – If any volatility value is not positive.
ValueError – If the diagonal elements of the correlation matrix are not 1.
ValueError – If the correlation matrix is not symmetric and positive definite.
- load_pnl(instruments_path, pnl_path, sep=',', decimal='.')
Function for loading a P&L / risk factor simulation.
- Parameters:
instruments_path (
str) – Path to the file (csv or xlsx) containing instrument information and having shape (I, 4). Instrument information consists of holding, buy and sell costs as well as a market value relative to exposure.pnl_path (
str) – Path to the file (csv or xlsx) containing the P&L / risk factor simulation having shape (S, I) or (S, I+1) if the P&L / risk factor simulation contains a probability vector as the last column.sep (
str) – Separator for csv files. Default: ‘,’decimal (
str) – Decimal separator for csv files. Default: ‘.’
- Return type:
Tuple[DataFrame,DataFrame,ndarray]- Returns:
Instrument information, P&L / risk factor simulation, and scenario probability vector.
- Raises:
TypeError – If instruments_path or pnl_path is not a string.
ValueError – If file extension is not .csv or .xlsx.
TypeError – If P&L values are not integers or floats.
ValueError – If there is a mismatch between P&L and instruments names.
ValueError – If the optional probability vector is invalid.