Cash flow model output

In this post, we will discuss the output of a cash flow model using the cashflower package in Python. In particular, we will check how to create grouped output, create an output only with a subset of columns and save a custom output.


List of content:

  1. Grouping
  2. Subset of variables
  3. User-defined output

Grouping

The output of a cash flow model is a table where rows represent periods and columns represent variables. Data are the values of the variables for the given period.

The structure of the output depends on the following settings:

  • GROUP_BY - the grouping variable,
  • OUTPUT_VARIABLES - list of output variables (by default - all).

Let's see some examples.

Grouped by all

By default, the GROUP_BY setting is set to None. This configuration creates an aggregated output.

# settings.py
settings = {
    # ...
    GROUP_BY = None,
    # ...
}

Here's an example of the output.

Cash flow model output with results aggregated across all model points

The results of the aggregated output are summed across all model points.

There is only one set of projections, so the number of rows amounts to T_MAX_OUTPUT+1. The value is incremented by 1 because the projection starts at 0.

Grouped by attribute

You can also group results byt a specific attribute, such as product.

# settings.py
settings = {
    # ...
    GROUP_BY = "product",
    # ...
}

Individual output

You can create an individual output by setting GROUP_BY to a variable that differs for each row. For example, if there's a column called id containing consecutive integers, this approach will ensure distinct groupings.

# settings.py
settings = {
    # ...
    GROUP_BY = "id",
    # ...
}

Below, you can see an example of the output's structure.

Cash flow model output showing a set of results for each individual model point

Each model point has its own set of results.

Subset of variables

If you don't need all model variables in your output, you can select a subset using the OUTPUT_VARIABLES setting.

By default, the OUTPUT_VARIABLES setting is set to None which creates output for all variables.

# settings.py
settings = {
    # ...
    OUTPUT_VARIABLES = None,
    # ...
}

You can choose a subset of columns by providing the list of variables' names.

# settings.py
settings = {
    # ...
    OUTPUT_VARIABLES = ["bel"],
    # ...
}

Here only one variables has been chosen to be saved in the output.

Cash flow model output showing only one selected column: bel

The output contains only the column bel.

Default vs custom output

Default output

By default, the results of the model are saved to files with comma-separated values. Files are saved in the output folder in the same directory as the model. The filename has the form: <timestamp>_output.csv (for example: 20231125_173512_output.csv).

Timestamp contains the moment when the model has finished its work. Timestamp is of the format YYYYMMDD_hhmmss, where:

  • YYYY - year,
  • MM - month,
  • DD - day,
  • hh - hours,
  • mm - minutes,
  • ss - seconds.

The model creates one output file per version.

Custom output

You can change the default output creation and adjust the model to save the results in your way. For example, you may want to save the results to other files or upload them to a database.

To use the custom output, follow these two steps.

Firstly, if you want to stop the model from saving the output in the default way, set the SAVE_OUTPUT setting to False.

# settings.py
settings = {
    # ...
    SAVE_OUTPUT = False,
    # ...
}

Now, the model will not save the output file on its own.

Secondly, adjust the code in the run.py script. In the script, you can find the following code:

# run.py
if __name__ == "__main__":
    output = run(settings)

The output contains a data frame with reuslts.

Let's say, we don't want to have timestamps in the filenames and want to save the results as text files.

We can do it by adding the following line of code:

# run.py
output.to_string("my-awesome-results.txt")

Now, instead of output/<timestamp>_output.csv in the output folder, we will create the my-awesome-results.txt file in the main folder.


Thank you for reading the blog post and I hope you found it useful. Do you have any questions or topics to discuss? If so, please use the comment section below the post or the github repository.

Read also:

Log in to add your comment.