Skip to content

Kernels

fundcloud.kernels is the low-level numeric surface that every user-facing metric dispatches through. Most users will never import it directly — returns.fc.sharpe() and pf.max_drawdown() already go through the same kernels — but the symbols below are the supported public API for code that wants to call the accelerated primitives explicitly. HAS_RUST and kernel_version() report which backend is active; see the Rust kernels guide for the parity methodology and published benchmarks.

fundcloud.kernels

Accelerated numerical kernels.

Prefers the Rust-backed fundcloud._core extension module when available (built by maturin from crates/fundcloud-py); falls back to pure-Python implementations in :mod:fundcloud.kernels._pyfallback when the extension isn't present.

Call sites are deliberately keep argument lists identical between the two backends so the parity tests in tests/unit/test_kernels_parity.py can swap implementations without adapters.

HAS_RUST module-attribute

HAS_RUST = True

kernel_version

kernel_version() -> str

Version string of the active kernel backend.

Source code in python/fundcloud/kernels/__init__.py
def kernel_version() -> str:
    """Version string of the active kernel backend."""
    if _core is not None:
        return str(_core.kernel_version())
    return _pyfallback.kernel_version()

returns_from_prices

returns_from_prices(prices: ndarray) -> np.ndarray

Simple period-over-period returns. First element is NaN.

Source code in python/fundcloud/kernels/__init__.py
def returns_from_prices(prices: np.ndarray) -> np.ndarray:
    """Simple period-over-period returns. First element is NaN."""
    arr = _as_1d(prices)
    if _core is not None:
        return np.asarray(_core.returns_from_prices(arr))
    return _pyfallback.returns_from_prices(arr)

rolling_mean

rolling_mean(x: ndarray, window: int) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def rolling_mean(x: np.ndarray, window: int) -> np.ndarray:
    arr = _as_1d(x)
    if _core is not None:
        return np.asarray(_core.rolling_mean(arr, int(window)))
    return _pyfallback.rolling_mean(arr, int(window))

rolling_std

rolling_std(
    x: ndarray, window: int, ddof: int = 1
) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def rolling_std(x: np.ndarray, window: int, ddof: int = 1) -> np.ndarray:
    arr = _as_1d(x)
    if _core is not None:
        return np.asarray(_core.rolling_std(arr, int(window), int(ddof)))
    return _pyfallback.rolling_std(arr, int(window), int(ddof))

rolling_mean_batch

rolling_mean_batch(x: ndarray, window: int) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def rolling_mean_batch(x: np.ndarray, window: int) -> np.ndarray:
    arr = _as_2d(x)
    if _core is not None:
        return np.asarray(_core.rolling_mean_batch(arr, int(window)))
    return _pyfallback.rolling_mean_batch(arr, int(window))

rolling_std_batch

rolling_std_batch(
    x: ndarray, window: int, ddof: int = 1
) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def rolling_std_batch(x: np.ndarray, window: int, ddof: int = 1) -> np.ndarray:
    arr = _as_2d(x)
    if _core is not None:
        return np.asarray(_core.rolling_std_batch(arr, int(window), int(ddof)))
    return _pyfallback.rolling_std_batch(arr, int(window), int(ddof))

drawdown_series

drawdown_series(returns: ndarray) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def drawdown_series(returns: np.ndarray) -> np.ndarray:
    arr = _as_1d(returns)
    if _core is not None:
        return np.asarray(_core.drawdown_series(arr))
    return _pyfallback.drawdown_series(arr)

max_drawdown_batch

max_drawdown_batch(returns: ndarray) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def max_drawdown_batch(returns: np.ndarray) -> np.ndarray:
    arr = _as_2d(returns)
    if _core is not None:
        return np.asarray(_core.max_drawdown_batch(arr))
    return _pyfallback.max_drawdown_batch(arr)

sharpe_batch

sharpe_batch(
    returns: ndarray,
    rf_per_period: float = 0.0,
    periods_per_year: float = 252.0,
) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def sharpe_batch(
    returns: np.ndarray, rf_per_period: float = 0.0, periods_per_year: float = 252.0
) -> np.ndarray:
    arr = _as_2d(returns)
    if _core is not None:
        return np.asarray(_core.sharpe_batch(arr, float(rf_per_period), float(periods_per_year)))
    return _pyfallback.sharpe_batch(arr, float(rf_per_period), float(periods_per_year))

sortino_batch

sortino_batch(
    returns: ndarray,
    target: float = 0.0,
    periods_per_year: float = 252.0,
) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def sortino_batch(
    returns: np.ndarray, target: float = 0.0, periods_per_year: float = 252.0
) -> np.ndarray:
    arr = _as_2d(returns)
    if _core is not None:
        return np.asarray(_core.sortino_batch(arr, float(target), float(periods_per_year)))
    return _pyfallback.sortino_batch(arr, float(target), float(periods_per_year))

var_batch

var_batch(
    returns: ndarray, alpha: float = 0.95
) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def var_batch(returns: np.ndarray, alpha: float = 0.95) -> np.ndarray:
    arr = _as_2d(returns)
    if _core is not None:
        return np.asarray(_core.var_batch(arr, float(alpha)))
    return _pyfallback.var_batch(arr, float(alpha))

cvar_batch

cvar_batch(
    returns: ndarray, alpha: float = 0.95
) -> np.ndarray
Source code in python/fundcloud/kernels/__init__.py
def cvar_batch(returns: np.ndarray, alpha: float = 0.95) -> np.ndarray:
    arr = _as_2d(returns)
    if _core is not None:
        return np.asarray(_core.cvar_batch(arr, float(alpha)))
    return _pyfallback.cvar_batch(arr, float(alpha))