Skip to main content

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 in Python 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

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 uv to 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/app/ --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/app/ --mode run --no-sandbox -f

To view locally, start a python webserver:

python -m http.server --directory docs/

The interactive simulation will be available at http://localhost:8000/simulating-risk/app/

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; make sure the version string and filename match exactly).

uv build --wheel -o docs/app/public/
uv run marimo export html-wasm simulatingrisk/app.py --mode edit -o docs/app/ --no-sandbox -f
just serve-docs

Make sure any changes made to the editable html+wasm version of the app get propagated back to the python version.

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

Publishing to PyPI

This package has been published on PyPI to simplify running the interactive simulation. This is not automated with a GitHub Actions workflow; use uv to publish (requires access on pypi and credentials):

uv build --wheel
uv publish

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

simulatingrisk-1.2.0-py3-none-any.whl (72.1 kB view details)

Uploaded Python 3

File details

Details for the file simulatingrisk-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: simulatingrisk-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 72.1 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

Hashes for simulatingrisk-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8c4c85696ba05c93217feaddaf3c5341238b1dac90c845cedc920520eef30e1
MD5 28ca93984d9ded6a081367831d65ee2f
BLAKE2b-256 e36786a221d3a5e3d03d767fc6f9406ed2e7d4deae9bb5b7c53b21a9eb1a1e90

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.2.0 This release

1 file

1.1.0

2 files

1.1.0.dev0

2 files

1.0.0.post1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page