Skip to content

Commit

Permalink
intro tutorial: Explain how to plot reporter of multiple agents
Browse files Browse the repository at this point in the history
This is a section taken from #1717.

Co-authored-by: Ewout ter Hoeven <[email protected]>
  • Loading branch information
2 people authored and tpike3 committed Oct 16, 2023
1 parent 44ab8cd commit d40bc5b
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions docs/tutorials/intro_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,60 @@
"g.set(title=\"Wealth of agent 14 over time\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also plot a reporter of multiple agents over time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"agent_list = [3, 14, 25]\n",
"\n",
"# Get the wealth of multiple agents over time\n",
"multiple_agents_wealth = agent_wealth[\n",
" agent_wealth.index.get_level_values(\"AgentID\").isin(agent_list)\n",
"]\n",
"# Plot the wealth of multiple agents over time\n",
"g = sns.lineplot(data=multiple_agents_wealth, x=\"Step\", y=\"Wealth\", hue=\"AgentID\")\n",
"g.set(title=\"Wealth of agents 3, 14 and 25 over time\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also plot the average of all agents, with a 95% confidence interval for that average."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Transform the data to a long format\n",
"agent_wealth_long = agent_wealth.T.unstack().reset_index()\n",
"agent_wealth_long.columns = [\"Step\", \"AgentID\", \"Variable\", \"Value\"]\n",
"agent_wealth_long.head(3)\n",
"\n",
"# Plot the average wealth over time\n",
"g = sns.lineplot(data=agent_wealth_long, x=\"Step\", y=\"Value\", errorbar=(\"ci\", 95))\n",
"g.set(title=\"Average wealth over time\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which is exactly 1, as expected in this model, since each agent starts with one wealth unit, and each agent gives one wealth unit to another agent at each step."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1369,7 +1423,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -1383,7 +1437,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.10.12"
},
"widgets": {
"state": {},
Expand Down

0 comments on commit d40bc5b

Please sign in to comment.