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
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:
ConfigSolverGrid,Grids,ImmutableBoundaryAbstractBoundary,BoundaryConditionTargetAbstractModel,AbstractParameterAbstractPolicy,AbstractPolicyDictAbstractValueGuess,LinearInitialValue,QuadraticInitialValueexplicit_policy,implicit_policyload_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 | backwardpi_method:scan | andersonpe_*,pi_*,bs_*tolerances and iteration limitsaa_*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
- English docs: docs/en/index.md
- Chinese docs: docs/zh/index.md
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6eb8276987110ad545a553b75ed6a6e690cbae5555739c7b6ebfa4c127a2a266
|
|
| MD5 |
30adcc89efc165c8144dba8f8f7fda8b
|
|
| BLAKE2b-256 |
2980853ca918788b92b9f400ec49a7e1651eccf238bfb35949cd7b8fbac1be05
|
Provenance
The following attestation bundles were made for finhjb-0.1.5.tar.gz:
Publisher:
python-publish.yml on Su-luoya/FinHJB
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
finhjb-0.1.5.tar.gz -
Subject digest:
6eb8276987110ad545a553b75ed6a6e690cbae5555739c7b6ebfa4c127a2a266 - Sigstore transparency entry: 1154440912
- Sigstore integration time:
-
Permalink:
Su-luoya/FinHJB@8e59b04ffd880b66905e92b8b6f22dd831e6448f -
Branch / Tag:
refs/tags/v0.15 - Owner: https://github.com/Su-luoya
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8e59b04ffd880b66905e92b8b6f22dd831e6448f -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8e75de581128b9347bf6ead9ae46e6f2a6fad4753e165d5570ce76661fd92fa
|
|
| MD5 |
adb7050b689b29df96c15ec4bbc24bb0
|
|
| BLAKE2b-256 |
b8e269313fbcc59a2dd3ccc8996006ace45ae09cf84b63d4686112baf3f5e1b3
|
Provenance
The following attestation bundles were made for finhjb-0.1.5-py3-none-any.whl:
Publisher:
python-publish.yml on Su-luoya/FinHJB
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
finhjb-0.1.5-py3-none-any.whl -
Subject digest:
e8e75de581128b9347bf6ead9ae46e6f2a6fad4753e165d5570ce76661fd92fa - Sigstore transparency entry: 1154440913
- Sigstore integration time:
-
Permalink:
Su-luoya/FinHJB@8e59b04ffd880b66905e92b8b6f22dd831e6448f -
Branch / Tag:
refs/tags/v0.15 - Owner: https://github.com/Su-luoya
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8e59b04ffd880b66905e92b8b6f22dd831e6448f -
Trigger Event:
release
-
Statement type: