Types & registry¶
Core types¶
types
¶
Shared data structures for jaxfolio.
These are lightweight, framework-agnostic containers. Optimizers return a
:class:PortfolioResult; the backtester and plotting utilities consume it.
OptimizerConfig
dataclass
¶
OptimizerConfig(
risk_free_rate: float = 0.0,
long_only: bool = True,
weight_bounds: tuple[float, float] | None = None,
max_iter: int = 2000,
solver: str = "spg",
learning_rate: float | None = None,
tol: float = 1e-07,
l2_reg: float = 0.0,
)
Configuration shared by the projected-gradient classical optimizers.
Attributes:
| Name | Type | Description |
|---|---|---|
risk_free_rate |
float
|
Per-period risk-free rate used by Sharpe-style objectives. |
long_only |
bool
|
If |
weight_bounds |
tuple[float, float] | None
|
Explicit |
max_iter |
int
|
Maximum projected-gradient iterations. |
solver |
str
|
Which projected-gradient solver to use. |
learning_rate |
float | None
|
Step size for the solver. |
tol |
float
|
Convergence tolerance. For |
l2_reg |
float
|
Optional L2 penalty on weights (encourages diversification). |
bounds
¶
Resolve the effective per-asset weight bounds.
Honors an explicit weight_bounds; otherwise defaults to (0, 1)
for a long-only portfolio and (-1, 1) when shorting is allowed.
Source code in src/jaxfolio/types.py
PortfolioResult
dataclass
¶
PortfolioResult(
weights: ndarray,
assets: list[str],
method: str,
expected_return: float | None = None,
volatility: float | None = None,
sharpe: float | None = None,
metadata: dict[str, Any] = dict(),
)
The output of an optimizer: weights plus diagnostics.
Attributes:
| Name | Type | Description |
|---|---|---|
weights |
ndarray
|
Portfolio weights as a 1-D numpy array (sums to 1). |
assets |
list[str]
|
Asset names aligned with |
method |
str
|
Human-readable optimizer name. |
expected_return |
float | None
|
Annualized (or per-period, if unscaled) expected portfolio return. |
volatility |
float | None
|
Portfolio volatility on the same scale as |
sharpe |
float | None
|
Sharpe ratio implied by |
metadata |
dict[str, Any]
|
Free-form diagnostics (iterations, converged flag, cluster labels, ...). |
as_dict
¶
Return {asset: weight} sorted by descending absolute weight.
Registry¶
registry
¶
A lightweight registry for portfolio strategies.
Any callable with the optimizer shape method(returns, ...) -> PortfolioResult
can be registered by name and then looked up, listed, and mixed with the
built-ins in the backtester. Built-in optimizers register themselves at import
time (see :func:_register_builtins), so :func:list_strategies always reflects
the full catalog — including the user's own additions.
Usage
from jaxfolio import register_strategy, get_strategy, list_strategies @register_strategy("my_momentum", description="12-1 momentum tilt") ... def my_momentum(returns): ... ... get_strategy("my_momentum") is my_momentum True "my_momentum" in list_strategies() True
StrategyInfo
dataclass
¶
A registered strategy: its callable, canonical name, and description.
register_strategy
¶
register_strategy(
name: str | None = None,
*,
description: str = "",
builtin: bool = False,
overwrite: bool = False,
) -> Callable[[Strategy], Strategy]
Register a strategy under name (defaults to the function's name).
Usable as a decorator (@register_strategy("x")) or directly
(register_strategy("x")(fn)). The wrapped function is returned unchanged,
so decorating never alters behavior. Re-registering an existing name raises
unless overwrite=True.
Source code in src/jaxfolio/registry.py
get_strategy
¶
Return the registered strategy callable for name.
strategy_info
¶
strategy_info(name: str) -> StrategyInfo
Return the full :class:StrategyInfo record for name.
list_strategies
¶
List registered strategy names (optionally filtered by origin).
Source code in src/jaxfolio/registry.py
unregister
¶
registry
¶
registry() -> dict[str, StrategyInfo]