Filtering

Often, one would want filter their data. May be you are just interested in number of one particular subset (eg a Book). Or, if you are investigating an issue, this can be very useful to narrow down the potential underlying root cause.

Note: you specify type of filter with "op" argument.

Equal (Eq)

In this example we breakdown by Country and RiskClass. We also filter on Desk == FXOptions.

import ultibi as ul
import polars as pl

pl.Config.set_tbl_rows(100)
ds = ul.FRTBDataSet.from_config_path("./data/frtb/datasource_config.toml")


request = dict(
    measures=[["SBM Charge", "scalar"]],
    groupby=["Country", "RiskClass"],
    filters=[[{"op": "Eq", "field": "Desk", "value": "FXOptions"}]],
    hide_zeros=True,
    totals=True,
    calc_params={
        "jurisdiction": "BCBS",
        "apply_fx_curv_div": "true",
        "drc_offset": "true",
    },
)
result = ds.compute(request)
print(result)

Not Equial (Neq)

In this example we breakdown by Country and RiskClass. We also filter on Desk != FXOptions.

request = dict(
    measures=[["SBM Charge", "scalar"]],
    groupby=["Country", "RiskClass"],
    filters=[[{"op": "Neq", "field": "Desk", "value": "FXOptions"}]],
    hide_zeros=True,
    totals=True,
    calc_params={
        "jurisdiction": "BCBS",
        "apply_fx_curv_div": "true",
        "drc_offset": "true",
    },
)
result = ds.compute(request)
print(result)

In (In)

In is similar to Eq, but instead of one value you can provide multiple. In this example we filter on Desk to be FXOptions or Rates.


request = dict(
    measures=[["SBM Charge", "scalar"]],
    groupby=["Country", "RiskClass"],
    filters=[[{"op": "In", "field": "Desk", "value": ["FXOptions", "Rates"]}]],
    hide_zeros=True,
    totals=True,
    calc_params={
        "jurisdiction": "BCBS",
        "apply_fx_curv_div": "true",
        "drc_offset": "true",
    },
)
result = ds.compute(request)

print(result)

Not In (NotIn)

NotIn is similar to Neq, but instead of saying that column value should not be equal to A, you can say that it should not be equal neither to A not to B. In this example we filter on Desk not being one of FXOptions or Rates.


request = dict(
    measures=[["SBM Charge", "scalar"]],
    groupby=["Country", "RiskClass"],
    filters=[[{"op": "NotIn", "field": "Desk", "value": ["FXOptions", "Rates"]}]],
    hide_zeros=True,
    totals=True,
    calc_params={
        "jurisdiction": "BCBS",
        "apply_fx_curv_div": "true",
        "drc_offset": "true",
    },
)
result = ds.compute(request)

print(result)

Combining Filters

You can combine filters as much as you want. In all the previous examples notice that filter. Is a nested list, ie a list of lists. It is important to keep in mind that inner filters of each list will be joined as OR, while outer filters will be joined as AND. For example, here we want LegalEntity to be equal to EMEA OR Country to be UK, AND we also want RiskClass either FX or Equity.


request = dict(
    measures=[["SBM Charge", "scalar"]],
    groupby=["Country", "RiskClass"],
    filters=[
        [
            {"op": "Eq", "field": "LegalEntity", "value": "EMEA"},
            {"op": "Eq", "field": "Country", "value": "UK"},
        ],
        [{"op": "In", "field": "RiskClass", "value": ["FX", "Equity"]}],
    ],
    hide_zeros=True,
    totals=True,
    calc_params={
        "jurisdiction": "BCBS",
        "apply_fx_curv_div": "true",
        "drc_offset": "true",
    },
)
result = ds.compute(request)
print(result)