Data & moments¶
See the Data guide.
Synthetic data¶
synthetic
¶
Reproducible synthetic market data as Polars frames.
generate_prices
¶
generate_prices(
n_assets: int = 12,
n_days: int = 756,
*,
seed: int = 0,
start_price: float = 100.0,
mean_annual_return: float = 0.08,
annual_vol_range: tuple[float, float] = (0.15, 0.45),
correlation_strength: float = 0.5,
start_date: str = "2021-01-01",
tickers: list[str] | None = None,
) -> DataFrame
Generate correlated daily GBM prices with an explicit date column.
Source code in src/jaxfolio/data/synthetic.py
generate_returns
¶
Convenience wrapper returning simple daily returns.
Loaders¶
loaders
¶
Data ingestion into explicit-date Polars frames.
load_csv
¶
load_csv(
path: str | Path,
*,
date_col: str = "date",
asset_col: str | None = None,
price_col: str = "close",
**read_csv_kwargs,
) -> DataFrame
Load a wide or long/tidy CSV price panel.
Source code in src/jaxfolio/data/loaders.py
load_parquet
¶
load_parquet(
path: str | Path,
*,
date_col: str = "date",
asset_col: str | None = None,
price_col: str = "close",
) -> DataFrame
Load a wide or long/tidy Parquet price panel.
Source code in src/jaxfolio/data/loaders.py
load_yfinance
¶
load_yfinance(
tickers: list[str] | str,
*,
start: str | None = None,
end: str | None = None,
period: str | None = "2y",
interval: str = "1d",
price_field: str = "Close",
) -> DataFrame
Download a wide price panel from Yahoo Finance as Polars.
Source code in src/jaxfolio/data/loaders.py
load_option_chain
¶
Fetch and normalize a Yahoo Finance option chain as a tidy Polars frame.
Source code in src/jaxfolio/data/loaders.py
Returns & splitting¶
returns
¶
Return computation, alignment, cleaning, and train/test splitting.
Data panels are Polars frames. A temporal column (conventionally date) is
kept as an ordinary, explicit column; every other numeric column is an asset.
date_column
¶
Return the panel's temporal key, preferring the conventional name.
Source code in src/jaxfolio/data/returns.py
asset_columns
¶
Return numeric asset columns, excluding temporal metadata.
Source code in src/jaxfolio/data/returns.py
to_returns
¶
Convert a wide price panel to simple or logarithmic returns.
Source code in src/jaxfolio/data/returns.py
align
¶
Align frames on their common (inner) or combined (outer) dates.
Source code in src/jaxfolio/data/returns.py
clean_returns
¶
clean_returns(
returns: DataFrame,
*,
max_missing_frac: float = 0.1,
fill: str = "zero",
) -> DataFrame
Drop sparse asset columns and fill remaining null/NaN values.
Source code in src/jaxfolio/data/returns.py
train_test_split
¶
Chronological split into in-sample and out-of-sample frames.
Source code in src/jaxfolio/data/returns.py
annualization_factor
¶
Moment estimators¶
estimators
¶
Moment estimators (mean & covariance) implemented in JAX.
Every estimator takes a (T, N) return array and returns JAX arrays, so the
downstream optimizers can jit through the whole pipeline. Inputs may be Polars
frames or numpy arrays; :func:as_matrix normalizes them.
as_matrix
¶
Return (matrix, asset_names) from a returns object.
Accepts a Polars DataFrame (numeric columns become asset names), or a raw
array (names default to asset_0...). Date/datetime columns are metadata
and are excluded from the optimizer matrix. Nulls and NaNs become zeros.
Source code in src/jaxfolio/moments/estimators.py
mean_returns
¶
Sample mean of returns; annualized if periods_per_year is given.
Source code in src/jaxfolio/moments/estimators.py
sample_covariance
¶
Sample covariance matrix (denominator T - 1); optionally annualized.
Requires at least two observations; a single row has no defined covariance.
Source code in src/jaxfolio/moments/estimators.py
ewma_covariance
¶
ewma_covariance(
returns: Array,
*,
halflife: float = 63.0,
periods_per_year: int | None = None,
) -> Array
Exponentially-weighted covariance matrix.
Recent observations receive more weight; halflife is in periods (default
~one quarter of trading days).
Source code in src/jaxfolio/moments/estimators.py
ledoit_wolf_covariance
¶
ledoit_wolf_covariance(
returns: Array, *, periods_per_year: int | None = None
) -> tuple[Array, float]
Ledoit-Wolf shrinkage toward a scaled-identity target.
Returns (shrunk_cov, shrinkage_intensity). The shrinkage intensity is
estimated analytically (Ledoit & Wolf, 2004) and clipped to [0, 1].
Source code in src/jaxfolio/moments/estimators.py
correlation_from_covariance
¶
Convert a covariance matrix to a correlation matrix.
Constraint projections¶
projections
¶
Constraint projections used by the projected-gradient optimizers.
All projections are pure JAX and jit/vmap-safe. The two workhorses are:
- :func:
project_simplex— Euclidean projection onto the probability simplex{w : w >= 0, sum(w) = 1}(long-only, fully-invested). - :func:
project_box_budget— projection onto a box[lo, hi]intersected with the budget hyperplanesum(w) = budget(allows bounded shorting).
project_simplex
¶
Euclidean projection of v onto the scaled simplex summing to budget.
Implements the classic Duchi et al. (2008) sort-based algorithm, which is exact and differentiable almost everywhere.
Source code in src/jaxfolio/constraints/projections.py
project_box_budget
¶
project_box_budget(
v: Array,
lower: float = 0.0,
upper: float = 1.0,
budget: float = 1.0,
*,
max_iter: int = 50,
tol: float = 1e-10,
) -> Array
Project v onto {w : lower <= w <= upper, sum(w) = budget}.
Solves for the Lagrange multiplier tau on the budget constraint by
bisection: w(tau) = clip(v - tau, lower, upper) is monotone decreasing in
tau, so we bisect until sum(w(tau)) == budget. Feasible whenever
n*lower <= budget <= n*upper.
Source code in src/jaxfolio/constraints/projections.py
normalize_weights
¶
Rescale weights to sum to budget (assumes a non-zero sum).
Source code in src/jaxfolio/constraints/projections.py
softmax_weights
¶
Map unconstrained logits to long-only weights via softmax (sums to budget).