Replies: 1 comment 11 replies
-
Hi, There isnt' a function in the atoti API which will allow you to do this directly but I think I have a possible implementation for you. However this only works if the PL vectors are sorted as they are in your example, and if discarded scenarios always have higher values than those remaining after being discarded. The general idea of my solution is to say that the scenario we want is the first one with the lowest value where all the PL vectors across all scenarios have different values (which guarantees that no more scenarios need eliminating). To build these measures, we will start by creating a parameter hierarchy to index our PL vectors: cube.create_parameter_hierarchy_from_members("PnLIndex", [i for i in range(4)], index_measure_name="PnLIndex")
# We want to aggregate on this hierarchy so we make it not slicing
h["PnLIndex"].slicing = False
# This allows us to access each member of the PnL vectors
m["PnLValue"] = m["PnL at date"][m["PnLIndex"]] Then we need to find the first index for which all scenarios have different values, using the m["1"] = 1
# This measure will give us the number of scenarios
m["ScenarioCount"] = tt.agg.single_value(
tt.parent_value(
tt.agg.sum(m["1"], scope=tt.OriginScope(l["Scenario"])),
degrees={h["Scenario"]: 1},
),
scope=tt.OriginScope(l["Scenario"]),
)
# This measure will give us the number of distinct values at each index
m["DistinctPnL"] = tt.agg.count_distinct(m["PnLValue"], scope=tt.OriginScope(l["Scenario"])) With these two measures, we will then create the measures returning the values for scenarios where the minimum is reached: m["MinScenario"] = tt.where(m["ScenarioCount"] == m["DistinctPnL"], tt.agg.min_member(m["PnLValue"], l["Scenario"]), None)
m["MinPnL"] = tt.where(m["ScenarioCount"] == m["DistinctPnL"], tt.agg.min(m["PnLValue"], scope=tt.OriginScope(l["Scenario"])), None) Now that we have the scenario and PnL values where the minimum is reached, we want to get the first index for which we find the minimum value. To do that, we can use the from atoti.function._first import first We will use this function to find the first scenario and PnL values where the tie breaker is resolved: m["TieBreakerScenario"] = first(m["MinScenario"], l["PnLIndex"])
m["TieBreakerPnLValue"] = first(m["PnLValue"], l["PnLIndex"]) Using these measures, I am able to get the following pivot table: Hopefully this fits your requirements or at least provides a starting point to implement your usecase. |
Beta Was this translation helpful? Give feedback.
-
The user we asked us to implement a the method to tiebreaker the worst stress windows.
we are looking for measures methods to do it but, i dont found it
we need somebody tell us the correct way to implement that.
To put in context , the our use case is based on https://github.com/atoti/notebooks/blob/main/notebooks/stressed-var-calibration/main.ipynb
in this use case we find the worst strees windows but usually we have a scenarys intervals with the same value of VAR and the aim is to tiebreaker the intervals scenarys to obtain a unique scenary.
We explain the method to obtain the tiebreaker
Supose we have one measure which get the components of PNL at Date for each scenary of worst strees windows
Scenary index PLN at date
2022-05-01 1 {-99500,-80000, -70000,-50000}
2022-05-02 2 {-99500,-80000, -60000,-40000}
2022-05-03 3 {-99500,-70000, -30000,-10000}
To obtain the Scenary in which the tiebreaker occurs, we need one form to compare the vector components in the three Scenarios with the minimun value of each i component of vectors at Scenary level
in this case, we need three steps to obtain it:
The first step is to compare the first element of the three vectors , in this case the minimun value is -99500 and if the result is they have the same value then, in this situation, cannot discard any scenario,
the second step is to compare second Element of three vectors, in this situation the minimun value is -80000 and we have two Scenary with this value, but the third one is diferent so that we can discard the scenary to the next interaction.
And third step
finally, we compare the third component of the vector PLN but now beetwen two scenarios (2022-05-01 and 20022-05-03 ) in this case the min value is -70000 and this case only have one scenary at this point the tiebreaker occurs ,
And the process is finish obtain finally scenario value = 2022-05-01 and value of the PNL Vector is -70000.
That is the logic we want to implement to obtain the PNL At DAte values and Scenary in which the tiebreaker is done
Could you help us what is the way to implement that?
thanks a lot
Beta Was this translation helpful? Give feedback.
All reactions