Metadata-Version: 2.4
Name: simulatingrisk
Version: 1.1.0
Summary: Agent-based modeling for simulations related to risk and rationality
License: Apache-2
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mesa==2.1.5
Requires-Dist: altair>=6.0
Requires-Dist: marimo>=0.23.11
Dynamic: license-file

# Simulating Risk

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.18361156.svg)](https://doi.org/10.5281/zenodo.18361156)
[![DH community code review: June 2024](https://img.shields.io/badge/DHCodeReview-June_2024-blue)](https://github.com/DHCodeReview/simulating-risk/pull/1) [![unit tests](https://github.com/Princeton-CDH/simulating-risk/actions/workflows/unit_tests.yml/badge.svg)](https://github.com/Princeton-CDH/simulating-risk/actions/workflows/unit_tests.yml)

The code in this repository is associated with the CDH project [Simulating risk, risking simulations](https://cdh.princeton.edu/projects/simulating-risk/).

Simulations are implemented with [Mesa](https://mesa.readthedocs.io/en/stable/), using Agent Based Modeling to explore risk attitudes within populations.

---

## Simulations with risk attitudes and agent interaction

- [Hawk/Dove with risk attitudes](simulatingrisk/hawkdove)
- [Hawk/Dove with multiple risk attitudes and adjustment](simulatingrisk/hawkdovemulti)

> [!TIP]  
> Run an interactive version of the simulation online: [Hawk/Dove with multiple risk attitudes](https://py.cafe/rlskoeser/simulatingrisk-hawk-dove-multirisk)

> [!NOTE]
> Altenately, run the simulation in a Jupyter notebook: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/Princeton-CDH/simulating-risk.git/HEAD?urlpath=%2Fdoc%2Ftree%2Fsimulatingrisk%2Fhawkdovemulti%2Frun_simulation.ipynb)

The code for **Hawk/Dove with risk attitudes** and **Hawk/Dove with multiple risk attitudes** in this codebase was [reviewed](https://github.com/DHCodeReview/simulating-risk/pull/1) in June 2024 by [Scott Foster](https://github.com/sgfost) and [Malte Vogl](https://github.com/maltevogl) (Senior Research Fellow, Max Planck Institute of Geoanthropology) via [DHTech Community Code Review](https://dhcodereview.github.io/); review was facilitated by [Cole Crawford](https://github.com/ColeDCrawford) (Senior Software Engineer, Harvard Arts and Humanities Research Computing).

## Simulations with risky choices (environment)

- [Risky Food](simulatingrisk/risky_food)
- [Risky Bet](simulatingrisk/risky_bet)

## 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.

```mermaid
---
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`](https://docs.astral.sh/uv/) to create a Python 3.12 virtualenv and install dependencies, including development dependencies:

```sh
uv sync
```

### Install pre-commit hooks

Install pre-commit hooks (currently [black](https://github.com/psf/black) and [ruff](https://beta.ruff.rs/docs/)):

```sh
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:

```sh
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:

```sh
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](https://github.com/casey/just) version 1.52 or newer.

With `just` installed, run the following to build the documentation and serve it locally:

````sh
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:

```sh
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).

```sh
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:

```sh
uv run marimo export html notebooks/evolv-risk-attitudes/convergence.py -o docs/analysis/evolve/index.html
```
