Agent-based modeling for simulations related to risk and rationality
Project description
Simulating Risk
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:
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: use
uvto create a Python 3.12 virtualenv and install dependencies, including development dependencies:
uv sync
Install pre-commit hooks
Install pre-commit hooks (currently black and ruff):
pre-commit install
Interactive interface to the simulations
We use a marimo notebook as the interface for running the Hawk/Dove simulation interactively. To run locally for development, using the local development environment, run with sandbox disabled:
uv run marimo edit simulatingrisk/app.py --no-sandbox
For publication via static site, this notebook should be saved as html + web assembly. This requires a version of the simulatingrisk package that can be installed in pyodide, either by a published version on pypi or a local wheel for testing.
To export as html+wasm in edit mode, to debug any wasm-specific problems:
uv run marimo export html-wasm simulatingrisk/app.py -o docs/sim/ --mode edit
For convenience, a .justfile is included for building documentation and serving locally. Requires just version 1.52 or newer.
With just installed, run the following to build the documentation and serve it locally:
just docs
just serve-docs
```
That is equivalent to running the following commands. To export manually in run mode:
```sh
uv run marimo export html-wasm simulatingrisk/app.py -o docs/sim/ --mode run --no-sandbox -f
To view locally, start a python webserver:
python -m http.server --directory docs/
The interacvite simulation will be available at http://localhost:8000/sim/
For testing the html+wasm application notebook with a local version of the simrisk code (notebook must be updated to install simulatingrisk from the wheel).
uv build --wheel -o docs/sim/
uv run marimo export html-wasm simulatingrisk/app.py --mode edit -o docs/sim/ --no-sandbox -f
To export static html copies of analysis notebooks:
uv run marimo export html notebooks/evolv-risk-attitudes/convergence.py -o docs/analysis/evolve/index.html
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simulatingrisk-1.1.0.tar.gz.
File metadata
- Download URL: simulatingrisk-1.1.0.tar.gz
- Upload date:
- Size: 6.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fae811828934f2b1f67772ef7985567b4bf4a19448c95b521d76ec62718d1c8
|
|
| MD5 |
7018944af2f198894d6dbb4283b3c173
|
|
| BLAKE2b-256 |
22c09f42839a53f2c30d1da302c7d512fa0dd565374ad8248093c8fa033cccf4
|
File details
Details for the file simulatingrisk-1.1.0-py3-none-any.whl.
File metadata
- Download URL: simulatingrisk-1.1.0-py3-none-any.whl
- Upload date:
- Size: 64.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
482a31574da6382b2d3675465638353297a98bab0f2fc55ec7c300a6cd7cd731
|
|
| MD5 |
51f7b68f014876e5c56f946bb3e5836a
|
|
| BLAKE2b-256 |
212a8a967d25b745cd18be8dc4710ac7f862ea9b19675e717fa94f366b9a6905
|