Skip to main content

Agent-based modeling for simulations related to risk and rationality

Reason this release was yanked:

testing

Project description

Simulating Risk

DOI DH community code review: June 2024 unit tests

The code in this repository is associated with the CDH project Simulating risk, risking simulations.

Simulations are implemented with Mesa, using Agent Based Modeling to explore risk attitudes within populations.


Simulations with risk attitudes and agent interaction

[!TIP]
Run an interactive version of the simulation online: Hawk/Dove with multiple risk attitudes

[!NOTE] Altenately, run the simulation in a Jupyter notebook: Binder

The code for Hawk/Dove with risk attitudes and Hawk/Dove with multiple risk attitudes in this codebase was reviewed in June 2024 by Scott Foster and Malte Vogl (Senior Research Fellow, Max Planck Institute of Geoanthropology) via DHTech Community Code Review; review was facilitated by Cole Crawford (Senior Software Engineer, Harvard Arts and Humanities Research Computing).

Simulations with risky choices (environment)

Risk attitude definitions

Across simulations, we define agents with risk attitudes tracked via a numeric r or risk_level 0.0 - 1.0, where r is that agent's minimum acceptable risk level for taking the risky bet. When the probability 'p' of the risky bet paying off is greater than an agent's 'r', that agent will take the bet. An agent with r=1 will always take the safe option (no risk is acceptable); an agent with r=0 will always take the risky choice (any risk is acceptable). Notice that the agent is never indifferent; allowing indifference would require introducing a tie-breaking rule, which would be a further parameter.

When the risky bet might be better or worse than the safe bet by the same amount (for example, the risky bet yields 3 or 1 and the safe bet yields 2), an agent who maximizes expected utility will prefer the risky bet when p > 0.5 and will prefer the safe bet when 'p < 0.5'; and they will be indifferent between the risky bet and the safe bet. Thus, r = 0.5 corresponds to expected utility maximization except in the case in which the probability is exactly 0.5 (or, we might say, a point epsilon units to the left of 0.5, where epsilon is smaller than the fineness of our random number generator, corresponds to expected utility maximization). These complications make no difference in practice, so we can simply say that r = 0.5 corresponds to expected utility maximization.

---
title: risk attitude / risk level (for probabilistic choices)
---
flowchart LR
    r0["<b>0.0</b>
always takes risky choice
(any risk is acceptable)"]
    reu["<b>0.5</b>
risk neutral
(expected utility maximizer)"]
    r1["<b>1.0</b>
always takes safe choice
(no risk is acceptable)"]
    r0 ---|risk seeking|reu---|risk averse|r1

Development instructions

Initial setup and installation:

  • Recommmended: create and activate a Python 3.12 virtualenv:
python3 -m venv simrisk
source simrisk/bin/activate
  • Install the package, dependencies, and development dependencies:
pip install -e .
pip install -e ".[dev]"

To run the Solara app with tabs for all simulations that are available as Solara apps:

solara run simulatingrisk/app.py

To run a single simulation, run Solara with the model-specific app file:

solara run simulatingrisk/hawkdovemulti/app.py

An interactive version of the Solara app with tabs for all simulations is available online.

Running the simulations (non-solara versions)

  • Simulations can be run interactively with mesa runserver by specifying the path to the model, e.g. mesa runserver simulatingrisk/risky_bet/ Refer to the readme for each model for more details.
  • Simulations can be run in batches to aggregate data across multiple runs and different parameters. For example, ./simulatingrisk/batch_run.py riskyfood

Install pre-commit hooks

Install pre-commit hooks (currently black and ruff):

pre-commit install

To run on py.cafe

Build the application as a wheel, excluding dependencies:

pip wheel . --no-deps

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simulatingrisk-1.1.0.dev0.tar.gz (4.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simulatingrisk-1.1.0.dev0-py3-none-any.whl (55.7 kB view details)

Uploaded Python 3

File details

Details for the file simulatingrisk-1.1.0.dev0.tar.gz.

File metadata

  • Download URL: simulatingrisk-1.1.0.dev0.tar.gz
  • Upload date:
  • Size: 4.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for simulatingrisk-1.1.0.dev0.tar.gz
Algorithm Hash digest
SHA256 fdd71462a21e61a30a63d624e30f6a2627b3c50e507522f1ddcf7647be4002d1
MD5 1a4280d7c2045e8b44c78b6c2d392c63
BLAKE2b-256 335ec716e181a01472f3431fbf0fa06737351c586c69e52737cb992c82ee6f4f

See more details on using hashes here.

File details

Details for the file simulatingrisk-1.1.0.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for simulatingrisk-1.1.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb497669cd8e7254b68bc13e8ef6a94af6de6c131c9a65a4f949b4f47e2076cf
MD5 850726efe2d47e86638fecaeb1009e28
BLAKE2b-256 7140613437560cab0015b98d3268a1c57c48b7f56404d38dfe7284cbf142eec3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page