Categories
Starting guides

Two easy ways to perform simulations in atoti

One of the key strengths of atoti is that it was built to natively propose effortless ways to perform simulations on your data. In this article we will detail two ways of building simulations in atoti.

#1 Data Simulations

This type of simulation is particularly powerful in data models where multiple tables are joined together.

It simply consists in creating a new scenario where the content of a data table (or part of it) has been replaced by new data. This is done with a one-liner in atoti:

name_of_table_to_replace.scenarios["Name of new scenario"].load_pandas(new_table_data)

And that is it. A new scenario has been created in which all the previously defined metrics will be re-computed using the new data for this particular table. No need to duplicate datasets, redefine measures or restart the cube, every metric will be re-computed on the fly in the new scenario when needed. The “Simulation” hierarchy natively lets you compare the metrics across scenarios, be it with a query or from atoti’s UI.

Note that the new table data can be loaded from a pandas dataframe as in the example above, but also from a csv with load_csv, a spark dataframe with load_spark or parquet files (see available data sources in the documentation).

Example:

In this example we have a csv file containing sales forecasts and the corresponding  amounts in a specific currency:

We load the csv file into a sales_forecast_store:

sales_forecast_store = session.read_csv("sales_forecast.csv", sep=";")

We also have a csv file with conversion rates:

We load the rates into a conversion_rates_store:

conversion_rates_store = session.read_csv("conversion_rates.csv", keys=["currency"])

We can join these two tables, create a cube and a “Total amount” measure that will compute the total amount in EUR.

sales_store.join(conversion_rates_store, mapping={"currency":"currency"}))
cube = session.create_cube(sales_store)
m["Conversion Rate"] = conversion_rates_store["conversion_rate_to_eur"]
m["Total amount in EUR"] = atoti.agg.sum(
    m["amount.SUM"] * m["Conversion Rate"],
    scope=atoti.scope.origin("currency")
)

A quick cube.visualize() enables us to visualize the result:

Then if we have a new sales forecast, for example if we want to test the impact of a new forecast method:

sale_idamountcurrency
1400EUR
2330EUR
3190EUR
4100USD
5520USD
6150USD
78000JPY
815000JPY

We can simply create a new scenario where everything is identical except this new sales forecast:

sales_forecast_store.scenarios["New Forecast"].load_csv("sales_with_new_forecast.csv")

A ”cube.visualize()” enables us to immediately compare the total amount across the different scenarios using the standard “source simulation” hierarchy:

#2 Measure simulations

Another way to create simulations in atoti is to create a scenario where one or more measures are calculated differently.

In its most simple form, this type of simulation enables you to create a new scenario where some of the measures are either:

  • Replaced: The value of certain measures are replaced by a value you specify
  • Multiplied: Some measures are multiplied by a factor you specify
  • Incremented: A value you specify is added to certain measures

Additionally from that you can specify a cube level at which you want to perform the changes using the per parameter.

First you need to setup the simulation:

simulation = cube.setup_simulation(
    "Simulation hierarchy name",
    replace=array_of_measures_you_want_to_replace,
    multiply=array_of_measures_you_want_to_multiply,
    add=array_of_measures_you_want_to_increment,
    levels=levels_at_which_measure_changes_are_performed,
)

Then you can create as many new scenarios as you want where measures are replaced/multiplied/incremented by the values you want in one line for each scenario:

simulation.scenarios[“Scenario 1”] += (replace_value,multiply_factor,increment)    

simulation.scenarios[“Scenario 2”] += (replace_value2,multiply_factor2,increment2)

That is all you need to do. Similar to data simulations, new scenarios have been created and all metrics will be re-computed in real time when you query or view them in the UI. A new simulation hierarchy, with the name provided, has been created to let you compare scenarios easily.

This type of simulation can be used for many forecasting scenarios where you modify the forecasts and see the impact on all other metrics.

It is particularly powerful when the value of some measures depend on those you replace: the downstream measures also get cascaded and updated real-time.

In our soccer premier league notebook we use it to simulate various scoring systems using points for match wins/ties/losses and compare the impact on the standings.

Example:

Continuing with the previous sales and currencies example, we will setup a simulation on conversion rates to measure the impact on the total amount under different currency conversion assumptions:

convserion_rates_simulation = cube.setup_simulation(
    "Conversion rates simulations",
    per=[lvl["currency"]],
    replace=[m["Conversion Rate"]],
)

Then we can easily simulate a scenario where the euro drops 10% in value compared to the dollar (rate from 0.92 to 1.02)  and compared to the Japanese yen (rate from 0.0086 to 0.0096):

euro_drops_10_per = convserion_rates_simulation.scenarios["Euro drops 10%"]
euro_drops_10_per += ("USD", 1.02)
euro_drops_10_per += ("JPY", 0.0096)

Scenario comparisons can be performed in the “Conversion rate simulations” hierarchy that we created on simulation setup. We will, as usual, use a “cube.visualize()” for that:

#3 Bonus: creating simulations from atoti’s UI

Do you want to explore your data interactively and create simulations at the same time? This is actually possible in atoti’s UI.

Once you have set up a simulation as in the previous section, instead of creating the new scenarios with Python code, you can directly do it in atoti’s UI with the measures simulation widget. Here is an example , starting from the simulation of the previous section:

You can get a link to atoti’s UI of your notebook using session.url

If you want to perform more advanced atoti simulations or find additional details have a look at the corresponding documentation page.