Skip to main content

A library providing implementations of various risk metrics for risk-aware trajectory planning.

Project description

CI Coverage PyPI Python License

RisKit: Risk Metrics for Risk-Aware Planning

RisKit provides implementations of common risk metrics used in risk-aware trajectory planning, intended for researchers who need reliable risk quantification under uncertainty.

The library provides the following metrics:

  • Expected Value,
  • Mean-Variance,
  • Value at Risk (VaR),
  • Conditional Value at Risk (CVaR), and
  • Entropic Risk metrics,

with built-in support for both NumPy and JAX backends (can easily be extended to other backends, e.g. PyTorch). The API is type-safe, but offers automatic backend inference based on the type annotations of the cost function (uncertain variable transform).

RisKit is being actively developed. Some features may be missing and some of the API might change. You can help by reporting issues or contributing fixes and features.

Installation

Python 3.13+ is required.

pip install riskit

Quick Start

import numpy as np
from numtypes import array, Dim2
from riskit import (
    risk, distribution,
    NumPyInputAndState, NumPyUncertaintySamples, NumPyCosts,
)

# A cost function takes typed trajectories and uncertainty samples,
# and returns a costs array of shape (T, M, N). You're free to use
# any type you want though, as long as it supports the necessary operations.
def distance_cost(
    *,
    trajectories: NumPyInputAndState,
    uncertainties: NumPyUncertaintySamples[Dim2],
) -> NumPyCosts:
    x = trajectories.x          # (T, D_x, M)
    xi = uncertainties           # (V, D, N)
    T, _, M = x.shape
    _, _, N = xi.shape
    return np.tile((x[:, 0, :, None] - xi[0, 0, None, :]) ** 2, (1, 1, 1)).reshape(T, M, N)

# The factory infers the NumPy backend from the cost function's return type.
metric = risk.cvar_of(distance_cost, alpha=0.1)

# Create an uncertainty distribution.
uncertainties = distribution.numpy.gaussian(
    mean=array([[0.0]], shape=(1, 1)),
    covariance=array([[[1.0]]], shape=(1, 1, 1)),
    seed=42,
)

# Compute risk over trajectories and uncertainty samples.
results = metric.compute(trajectories=..., uncertainties=uncertainties)

If the cost function in your case works with JAX and returns a JAX array, you can just use the appropriate type annotation and the JAX backend will be automatically inferred.

Risk Metrics

Metric Factory Description
Expected Value risk.expected_value_of(f) Mean cost across samples
Mean-Variance risk.mean_variance_of(f, gamma=...) Mean + γ · Variance tradeoff
Value at Risk risk.var_of(f, alpha=...) α-quantile of the cost distribution
Conditional VaR risk.cvar_of(f, alpha=...) Expected cost in the worst α-fraction
Entropic Risk risk.entropic_risk_of(f, theta=...) Exponential utility-based risk measure

Features

  • Multiple risk metrics with a consistent, protocol-based API
  • NumPy and JAX backends with automatic inference from type annotations
  • Monte Carlo sampling with built-in distributions, or custom sampling strategies

Documentation

To build the Typst-based documentation, install the custom Typst packages locally:

typi --project-directory=documents

typi is available after you've set up the project environment with uv sync.

Contributing

See CONTRIBUTING.md.

License

MIT, see LICENSE.

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

riskit-1.0.1.tar.gz (172.9 kB view details)

Uploaded Source

Built Distribution

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

riskit-1.0.1-py3-none-any.whl (39.9 kB view details)

Uploaded Python 3

File details

Details for the file riskit-1.0.1.tar.gz.

File metadata

  • Download URL: riskit-1.0.1.tar.gz
  • Upload date:
  • Size: 172.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for riskit-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ae98371d14bf1e013bb6b8793db3c784a1b8d588da202ddf8bd215c7c88e454f
MD5 3633cdd6dbfc01132bb9272c963002e0
BLAKE2b-256 e11efca4c05a18fac263682a5b3d007003b917df4df0cb56baee2990ffd75a84

See more details on using hashes here.

File details

Details for the file riskit-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: riskit-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 39.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for riskit-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52c14eb9148e81e97382d8c7760256a8f1b36bbbdac40c00740b0e10fcf166ae
MD5 70984cf14d9973019d0681d7e4429fe6
BLAKE2b-256 c7143d2932b64b91862fd3966545d1f5160879961d1ee504a8a4e9f73b17f012

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