For more background on SOC and the model, see Self-Organized Criticality and Economic Applications
Variable setup for storing income and demand
Now let's set up some variables to store the income and demand information from the model run that we are interest in analyzing, and runs the model. This is done outside the model.py file because we want to separate the core model functionality from the analysis of model results. We might want to instantiate multiple models with different parameters, run them all, and track income and demand separately for each one.
We set up a list for income data, and one for demand data, and set the number of model steps. Each list entry will be a tally of income (or demand) for one model period or step. The for loop calls the model's step() function num_steps number of times, and after each step appends demand and income data to our lists:
For running the model outside a notebook, use model_run.py (which contains code similar to the above).
Plotting The Data - Basics
Now let's look at some plots of the income and demand data. We create a figure and some lines, add axis labels and set the y-axis limit to 0, and then display the plot. The if statement gives us two sets of lines, one for short runs (< 5000 steps) and one for longer runs - this is because for longer runs, the noise in income will look messy when plotted.
This version of the plot uses matplotlib.
Here's some code and a similar plot, this time using Plotly instead of matplotlib. Dash and Plotly make is easy to build beautiful web-friendly applications using interactive data visualization. Play around with the plot below - you can zoom, select an area to view in detail, turn lines on and off by clicking in the legend, and more. This plot is visually more appealing, and it's more useful. What's really cool is that whenever you create plots in your code, Plotly automatically saves them to your account for later use. This was originally generated in a Jupyter notebook, but now is available in my saved plots at the plotly site. All I had to do was include a small code snippet (see below).
Here's the embed code: