Assign Weights/Prepare

Currently this section only applies to FRTBDataSet. In the future python users will be able to define their own, custom prepare functions too.

Prepare is a fixed calculation which happens with every request for scans/db DataSources. For InMemory it happens only when you call .prepare().

from_config() call .prepare() depending on source_type.

A good usecase for that is to assign weights column to your data, as per FRTB regulation for example. Make sure you assign weights before doing any computations:

# TODO scan here to demonstrate prepare

import polars as pl
import ultibi as ul

# Note: sometimes Polars needs abit of help to determine the correct datatype for your
# columns. This is because Polars scans first N rows to determine correct dtype
exposures = pl.read_csv("./data/frtb/Delta.csv", dtypes={"SensitivitySpot": pl.Float64})
# Feel free to join other attributes such as Hierarchy here ...
ds = ul.FRTBDataSet.from_frame(exposures)

original = ds.frame()  # keep the old value for comparison


prepared = ds.frame()

# Let's see what happened
def diff(df1: pl.DataFrame, df2: pl.DataFrame) -> pl.DataFrame:
    """Columns in df1 which are not in df2

        df1 (pl.DataFrame): First to compare
        df2 (pl.DataFrame): Second to compare

        pl.DataFrame: _description_
    return[c for c in df1 if not in df2])

diff_frame = diff(prepared, original)

You will get an error if you try to assign twice. Now, let's see what happened. We will need a little helper function:

At the time of writing this returns 6 new columns(names of the columns might change slightly but the meaning will always be the same):

Column NameExplanation
SensWeightsList of weights, one for each tenor. Eg [1,1,1,1,1] for any Vega risk.
CurvatureRiskWeightSimply max of SensWeights if PnL Up or Down was provided, otherwise NULL. This is how we identify Curvature eligible trades.
SensWeightsCRR2Same as SensWeights but under CRR2 rules.
CurvatureRiskWeightCRR2Same logic as BCBS
ScaleFactorDRC scailing: yearfrac between COB and MaturityDate
SeniorityRankMapping used internaly for DRC offsetting