Skip to main content

Agent-based modeling for simulations related to risk and rationality

Project description

# Simulating Risk

[![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 agent interaction

  • [Hawk/Dove with risk attitudes](simulatingrisk/hawkdove)

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

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 faciliated by [Cole Crawford](https://github.com/ColeDCrawford) (Senior Software Engineer, Harvard Arts and Humanities Research Computing).

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

The simulation can also be run 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)

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

## Development instructions

Initial setup and installation:

  • Recommmended: create and activate a Python 3.12 virtualenv:

`sh python3 -m venv simrisk source simrisk/bin/activate `

  • Install the package, dependencies, and development dependencies:

`sh pip install -e . pip install -e ".[dev]" `

To run the Solara app with tabs for all simulations that are available as Solara apps: `sh solara run simulatingrisk/app.py `

To run a single simulation, run Solara with the model-specific app file: `sh solara run simulatingrisk/hawkdovemulti/app.py `

An interactive version of the [Solara app with tabs for all simulations](https://py.cafe/app/rlskoeser/simulatingrisk) 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](https://github.com/psf/black) and [ruff](https://beta.ruff.rs/docs/)):

`sh pre-commit install `

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.0.0.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.0.0-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file simulatingrisk-1.0.0.tar.gz.

File metadata

  • Download URL: simulatingrisk-1.0.0.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.0.0.tar.gz
Algorithm Hash digest
SHA256 b41ac5e2f6e2cc106e66734f5f186093c3e34c6f77cddbdfa50041f709446a27
MD5 b85cdb23698c4e5160ccf9c5eb8e10be
BLAKE2b-256 eabf21eb8c25982e40e6ae8b54c1914be34cd2bb3a3e335a91fab92af7c6e183

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simulatingrisk-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for simulatingrisk-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5cfaeae2122cf4c533c8a191ae95140e6cb397060e6b5f60b4a9a798f276fcb0
MD5 2008634a3a118d8f2f379ffde64c054e
BLAKE2b-256 36071b11ee864f072600c039c97492a34024a9c87104d8f9e62c4655f2e71c2c

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