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 individual and aggregated output, create an output only with a subset of columns and save a custom output.


List of content:

  1. Structure
  2. User-defined output

Structure

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 two settings:

  • AGGREGATE - the flag if the results should be aggregated or not,
  • OUTPUT_COLUMNS - list of output columns (by default - all).

Let's see some examples.

Individual vs aggregated output

Aggregated output

By default, the AGGREGATE setting is set to True. This configuration creates an aggregated output.

settings.py
settings = {
    ...
    AGGREGATE = True,
    ...
}

Here's an example of the output.

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.

Individual output

You can create an individual output by setting AGGREGATE to False.

settings.py
settings = {
    ...
    AGGREGATE = False,
    ...
}

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

Each model point has its own set of results.

Subset of columns

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

By default, the OUTPUT_COLUMNS setting is set to an empty list which creates output for all variables.

settings.py
settings = {
    ...
    OUTPUT_COLUMNS = [],
    ...
}

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

settings.py
settings = {
    ...
    OUTPUT_COLUMNS = ["bel"],
    ...
}

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

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.