{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# T4 - Characteristics\n", "\n", "Characteristics can be conceptually difficult to define, but are fairly simple in practice. They are essentially special parameters, that are specific to working with compartments and groups of compartments. We will motivate their design with a worked example that builds on the multi-population framework from T3, and then conclude by discussing other aspects of their design that differ from parameters.\n", "\n", "The key functionality provided by characteristics is this - the example in T3 had users initialize the compartment sizes by directly entering values for the number of people in the 'Susceptible' and 'Infected' compartments, both of which appeared on the 'Stocks' sheet in the databook.\n", "\n", "![t4-framework1](assets/T4/t4_framework_1.png)\n", "\n", "However, typically country data does not correspond directly to the compartments in the databook. For example, suppose we know \n", "\n", "- The total number of people alive\n", "- The number of people who have ever been infected\n", "- The proportion of infections that have now been resolved\n", "\n", "We could use this data to work out what the corresponding compartment sizes should be. For example, if we know that there are 1000 people in total, of whom 400 have ever been infected, and of which 75% of infections have been resolved, then the corresponding initial compartment sizes would be\n", "\n", "- `sus = 600`\n", "- `inf = 100`\n", "- `rec = 300`\n", "\n", "which satisfies that `sus+inf+rec=1000`, and `inf+rec=400`, and `rec/(inf+rec)=300`. The motivation for characteristics is that we want the databook to contain data entry for the total number of people, the number ever infected, and the proportion resolved, because those are the values corresponding to the available data. We would like Atomica to work out the corresponding compartment sizes, rather than having to do the calculation manually. \n", "\n", "To do this, we need to store the information in the framework that we have quantities\n", "\n", "- `alive = sus+inf+rec`\n", "- `ever_inf = inf+rec`\n", "- `prop_resolved = rec/ever_inf` \n", "\n", "and have these quantities appear in the databook instead of the compartments themselves. We could achieve the required data entry using parameters. However, we can't use the parameters to initialize compartments. This is why there is a separate system, 'characteristics', that allows expressions of groups of compartments to be used for initialization. \n", "\n", "We can set up the three characteristics defined above in a fairly straightforward way on the 'Characteristics' sheet. Rather than writing the formulas above with '+' and '/' operations, we instead provide a comma separated list of compartments (or other characteristics) to sum (in the 'components' column) and we provide the denominator separately in the 'denominator' column. So the corresponding characteristics sheet is\n", "\n", "![t4-framework2](assets/T4/t4_framework_2.png)\n", "\n", "We will also remove the 'Databook page' for the compartments on the the compartments sheet, since we want to initialize the model using characteristics only. \n", "\n", "If we create a databook from the framework as usual, we will have updated data entry tables on the 'Stocks' sheet. We can then go ahead and fill them out with the initialization described above:\n", "\n", "![t4-databook1](assets/T4/t4_databook_1.png)\n", "\n", "The framework and databook are available in the Atomica repository under `atomica/docs/tutorial/assets/t4_framework_1.xlsx` and `atomica/docs/tutorial/assets/t4_databook_1.xlsx`, respectively. We can now load these files in and run a simulation:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import atomica as at\n", "P = at.Project(framework='assets/T4/t4_framework_1.xlsx',databook='assets/T4/t4_databook_1.xlsx')\n", "result = P.results[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now want to check that the initialization has been performed correctly. In the `result` we can retrieve the variables for the compartment sizes and inspect their values at the first timestep" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('sus = %.2f' % (result.get_variable('sus')[0].vals[0]))\n", "print('inf = %.2f' % (result.get_variable('inf')[0].vals[0]))\n", "print('rec = %.2f' % (result.get_variable('rec')[0].vals[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we have successfully used characteristics to have Atomica automatically convert from the aggregated data values to the underlying compartment values. \n", "\n", "Under the hood, we are solving a system of simultaneous equations. What happens if there are more unknowns than there are equations? This corresponds to the system being 'underdetermined'. An example would be, suppose we know that there are 1000 people in total, of whom 400 have ever been infected, but we don't know the proportion of people whose infections have been resolved. How do we then decide whether we have 100 infected and 300 recovered, or 300 infected and 100 recovered? Atomica uses the 'minimum norm' solution which means that the inputs are distributed equally across groups of compartments that are nonzero, and is zero if no information is available. We will see this with two examples. First, consider the case above where we only know the total population size and number ever infected. This corresponds to the framework and databook containing\n", "\n", "![t4-framework-3](assets/T4/t4_framework_3.png)\n", "![t4-databook-2](assets/T4/t4_databook_2.png)\n", "\n", "The minimum norm solution would see the 400 people uniformly distributed across `inf` and `rec`, so there will be 200 people in each compartment. If we run the model with these spreadsheets, we obtain" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import atomica as at\n", "P = at.Project(framework='assets/T4/t4_framework_2.xlsx',databook='assets/T4/t4_databook_2.xlsx')\n", "result = P.results[0]\n", "print('sus = %.2f' % (result.get_variable('sus')[0].vals[0]))\n", "print('inf = %.2f' % (result.get_variable('inf')[0].vals[0]))\n", "print('rec = %.2f' % (result.get_variable('rec')[0].vals[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also now recieve a warning that 'Initialization characteristics are underdetermined' which reflects the fact that we had to rely on the minimum norm solution to infer the value of some of the compartments. For compartments that are missing entirely, we can remove the 'alive' characteristic entirely, leaving us with:\n", "\n", "![t4-framework-4](assets/T4/t4_framework_4.png)\n", "![t4-databook-3](assets/T4/t4_databook_3.png)\n", "\n", "Now, we expect that the 400 people will be assigned to `inf` and `rec` in equal proportions, but since we have no information at all about `sus`, it will be initialized with a value of zero:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import atomica as at\n", "P = at.Project(framework='assets/T4/t4_framework_3.xlsx',databook='assets/T4/t4_databook_3.xlsx')\n", "result = P.results[0]\n", "print('sus = %.2f' % (result.get_variable('sus')[0].vals[0]))\n", "print('inf = %.2f' % (result.get_variable('inf')[0].vals[0]))\n", "print('rec = %.2f' % (result.get_variable('rec')[0].vals[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's possible to freely mix compartments and characteristics for initialization. For example, we could set a databook page for 'susceptible' on the compartments sheet, and have the databook explicitly contain data entry for the 'susceptible' compartment as well as the number ever infected.\n", "\n", "What happens if you enter conflicting information? For example, if the number ever infected is greater than the total number of people? In that case, a negative compartment size would occur, resulting in an error. In that case, the simulation cannot be run unless you find and fix the error. Atomica will print out diagnosic output to help identify where the negative compartment size originates from. Unfortunately, it is still challenging and one of the most difficult parts of framework design in Atomica.\n", "\n", "
\n", "Errors relating to negative compartment sizes mean that inconsistent information about the system has been entered\n", "
\n", "\n", "It's also possible for a system to be overdetermined. For example, if you specify data values for `inf`, `rec`, and `inf+rec`. If you specify inconsistent initial values, then a warning will be displayed and 'best fit' values will be used.\n", "\n", "
\n", "Further reading: Compartment initialization is described in more detail in the Atomica code documentation\n", "
\n", "\n", "Finally, the last main difference between characteristics and parameters is that characteristics take up no storage space, because they get dynamically evaluated in the results. Thus, using characteristics instead of parameters leads to smaller file sizes. " ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:atomica37]", "language": "python", "name": "conda-env-atomica37-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }