Skip to content
GeneratedGenerated from the built extension by scripts/generate_python_api_reference.py; CI fails if it is stale.

openquant.ef3m

Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.

Functions

centered_moment

centered_moment(moments: Sequence[float], order: int) -> float

The order-th centred moment E[(x - m_1)^order] from raw moments.

Uses the binomial expansion sum_j C(order, j) (-m_1)^j m_{order - j} with m_0 = 1, so only the first order raw moments are read. Order 0 gives 1 and order 1 gives 0 (up to rounding).

Parameters

moments : list[float]

Raw moments [E[x], E[x^2], ...].

order : int

Order of the centred moment.

Returns

float

The centred moment.

Raises

ValueError

If moments has fewer than max(order, 1) entries.

fit_m2n

fit_m2n(
    moments: Sequence[float],
    epsilon: float = 1e-05,
    factor: float = 5.0,
    n_runs: int = 1,
    variant: int = 2,
    max_iter: int = 100000,
) -> list[tuple[float, float, float, float, float, float]]

Fit a mixture of two Gaussians to five raw moments with EF3M.

López de Prado and Foreman (2014); used in AFML §10.2 and §15.4.1. Each run searches a grid of starting values mu_2 = m_1 + i * epsilon * factor * sigma (about 1 / epsilon starts), each from a random starting p_1, iterating the moment equations until p_1 moves by less than epsilon, and keeps the fit with the smallest squared error on the five raw moments. variant=1 fits four moments, variant=2 all five (more accurate; fitted about the mean so a negative mu_2 can be recovered). Fits use an unseeded RNG, so results vary between calls: run several and summarise them with most_likely_parameters. The component labels can come back swapped between runs.

Parameters

moments : list[float]

Raw moments [E[x], E[x^2], E[x^3], E[x^4], E[x^5]]; at least five.

epsilon : float, default 1e-5

Convergence tolerance on p_1 and spacing of the mu_2 start grid; must be > 0 (0 never returns, a negative value yields no fits). Smaller values are slower.

factor : float, default 5.0

Width of the mu_2 start grid in standard deviations.

n_runs : int, default 1

Number of independent searches, run serially.

variant : int, default 2

1 to fit four moments or 2 to fit five (the default, and the more accurate).

max_iter : int, default 100000

Maximum iterations of one attempt from one start; 0 runs none.

Returns

list[tuple[float, float, float, float, float, float]]

Up to n_runs rows of (mu_1, mu_2, sigma_1, sigma_2, p_1, error), where the sigmas are standard deviations, p_1 is the weight of component 1 and error is the sum of squared differences between the target and implied raw moments. A run that finds no admissible fit contributes no row.

Raises

ValueError

If moments has fewer than five entries or variant is not 1 or 2.

most_likely_parameters

most_likely_parameters(
    data: Sequence[tuple[float, float, float, float, float, float]],
    res: int,
) -> Any

The most likely value of each mixture parameter over many EF3M fits.

López de Prado and Foreman (2014): for each of mu_1, mu_2, sigma_1, sigma_2 and p_1 separately, the peak of a Gaussian kernel density over the fits, rounded to five decimals (the error column is ignored). The bandwidth is std * n^(-1/5) (population standard deviation, floored at 1e-6) and the density is evaluated on max(res, 10) evenly spaced points from the column's minimum to its maximum; a constant column returns its value. The five modes may come from different runs and need not reproduce the moments; check that the runs agree on which component is which.

Parameters

data : list[tuple[float, float, float, float, float, float]]

Fits as (mu_1, mu_2, sigma_1, sigma_2, p_1, error) rows, e.g. from fit_m2n.

res : int

Number of grid points for the density search (at least 10 are used).

Returns

dict[str, float]

Keys mu_1, mu_2, sigma_1, sigma_2 and p_1; empty if data is empty.

raw_moment

raw_moment(central_moments: Sequence[float], dist_mean: float) -> list[float]

Raw moments [E[x], E[x^2], ...] from centred moments and the mean.

central_moments starts at the first centred moment, which is 0: [0, E[(x - mu)^2], E[(x - mu)^3], ...]. Entry 0 of the output is dist_mean and entry n - 1 is sum_k C(n, k) c_k mu^(n - k) with c_0 = 1. The first centred moment is used as given in that sum, so pass 0 there.

Parameters

central_moments : list[float]

Centred moments from order 1 upward, starting with 0.

dist_mean : float

Mean of the distribution.

Returns

list[float]

Raw moments, as many as central_moments (at least one).