Skip to main content

This is a Python library for solving Hamilton-Jacobi-Bellman (HJB) equations using JAX. It provides a framework for numerical solutions to dynamic optimization problems in finance and economics.

Project description

FinHJB

简体中文 README

FinHJB is a Python library for solving one-dimensional Hamilton-Jacobi-Bellman (HJB) equations with JAX.

It provides typed interfaces for model construction and a high-level solver API for:

  • policy iteration,
  • boundary update/search,
  • parameter continuation (sensitivity analysis),
  • result serialization and reloading.

Installation

  • Python: >=3.10
  • Recommended: uv
uv sync

For editable installation with pip:

pip install -e .

Quick Start

from dataclasses import dataclass

import jax.numpy as jnp
from jaxtyping import Array

import finhjb as fjb


class Parameter(fjb.AbstractParameter):
    r: float = 0.03
    sigma: float = 0.15


class PolicyDict(fjb.AbstractPolicyDict):
    investment: Array


@dataclass
class Boundary(fjb.AbstractBoundary[Parameter]):
    @staticmethod
    def compute_v_left(p: Parameter) -> float:
        return 0.0

    @staticmethod
    def compute_v_right(p: Parameter, s_max: float) -> float:
        return 1.0 + 0.1 * s_max


@dataclass
class Policy(fjb.AbstractPolicy[Parameter, PolicyDict]):
    @staticmethod
    def initialize(grid: fjb.Grid, p: Parameter) -> PolicyDict:
        return PolicyDict(investment=jnp.full_like(grid.s, 0.1))

    @staticmethod
    @fjb.explicit_policy(order=1)
    def update_investment(grid: fjb.Grid) -> fjb.Grid:
        grid.policy["investment"] = jnp.maximum(1e-6, 0.5 * grid.v / grid.dv)
        return grid


@dataclass
class Model(fjb.AbstractModel[Parameter, PolicyDict]):
    @staticmethod
    def hjb_residual(v, dv, d2v, s, policy, jump, boundary, p):
        inv = policy["investment"]
        return -p.r * v + (s - inv) * dv + 0.5 * p.sigma**2 * d2v


solver = fjb.Solver(
    boundary=Boundary(p=Parameter(), s_min=0.0, s_max=0.2),
    model=Model(policy=Policy()),
    policy_guess=True,
    number=400,
    config=fjb.Config(pi_method="scan", derivative_method="central"),
)

state, history = solver.solve()
print(state.converged, state.best_error)

Main APIs

Top-level exports include:

  • Config
  • Solver
  • Grid, Grids, ImmutableBoundary
  • AbstractBoundary, BoundaryConditionTarget
  • AbstractModel, AbstractParameter
  • AbstractPolicy, AbstractPolicyDict
  • AbstractValueGuess, LinearInitialValue, QuadraticInitialValue
  • explicit_policy, implicit_policy
  • load_grid, load_grids, load_sensitivity_result

Solver Workflows

  • Solve: state, history = solver.solve()
  • Boundary update (model must implement update_boundary(grid)): state, history = solver.boundary_update()
  • Boundary search: state = solver.boundary_search(method="hybr")
  • Sensitivity analysis: result = solver.sensitivity_analysis(method="hybr", param_name="sigma", param_values=...)

Save / Load

state.grid.save("solution_grid")
loaded_grid = fjb.load_grid("solution_grid")

Similarly:

  • grids.save(path) + fjb.load_grids(path)
  • result.save(path) + fjb.load_sensitivity_result(path)

Configuration Highlights

Config controls derivative rules and convergence behavior:

  • derivative_method: central | forward | backward
  • pi_method: scan | anderson
  • pe_*, pi_*, bs_* tolerances and iteration limits
  • aa_* settings for Anderson acceleration

Testing

uv run pytest

Coverage gate (configured in project settings):

uv run pytest --cov=src/finhjb --cov-fail-under=85

Documentation

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

finhjb-0.1.5.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

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

finhjb-0.1.5-py3-none-any.whl (40.6 kB view details)

Uploaded Python 3

File details

Details for the file finhjb-0.1.5.tar.gz.

File metadata

  • Download URL: finhjb-0.1.5.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for finhjb-0.1.5.tar.gz
Algorithm Hash digest
SHA256 6eb8276987110ad545a553b75ed6a6e690cbae5555739c7b6ebfa4c127a2a266
MD5 30adcc89efc165c8144dba8f8f7fda8b
BLAKE2b-256 2980853ca918788b92b9f400ec49a7e1651eccf238bfb35949cd7b8fbac1be05

See more details on using hashes here.

Provenance

The following attestation bundles were made for finhjb-0.1.5.tar.gz:

Publisher: python-publish.yml on Su-luoya/FinHJB

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file finhjb-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: finhjb-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 40.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for finhjb-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e8e75de581128b9347bf6ead9ae46e6f2a6fad4753e165d5570ce76661fd92fa
MD5 adb7050b689b29df96c15ec4bbc24bb0
BLAKE2b-256 b8e269313fbcc59a2dd3ccc8996006ace45ae09cf84b63d4686112baf3f5e1b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for finhjb-0.1.5-py3-none-any.whl:

Publisher: python-publish.yml on Su-luoya/FinHJB

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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