xpectral.quant

Financial metrics and quantitative analysis accessor registered on Polars Expressions as expr.quant.

class xpectral.quant.polars_accessors.QuantAccessor(expr)[source]

Bases: object

Parameters:

expr (Expr)

returns(periods=1, over=None)[source]

Compute simple percentage return for the expression.

Return type:

Expr

Parameters:
  • periods (int)

  • over (str | None)

Parameters

periodsint

Number of periods to shift (default is 1).

overstr | None

Optional grouping column (e.g. ‘ticker’). If None, compute over the full series.

compound(over=None)[source]

Compound an existing series.

Return type:

Expr

Parameters:

over (str | None)

Parameters

overstr | None

Optional grouping column (e.g. ‘ticker’). If None, compound over the full series.

rolling_vol(window_size, weights=None, *, min_samples=None, center=False, ddof=1, over=None)[source]

Compute rolling volatility (standard deviation) for the expression.

Return type:

Expr

Parameters:
  • window_size (int)

  • weights (list[float] | None)

  • min_samples (int | None)

  • center (bool)

  • ddof (int)

  • over (str | None)

Parameters

overstr | None

Optional grouping column (e.g. ‘ticker’).

window_sizeint

Rolling window size.

weightslist[float] | None

Optional weights for weighted standard deviation.

min_samplesint | None

Minimum number of samples required to compute a value.

centerbool

Whether to center the window around the current row.

ddofint

Delta degrees of freedom (default = 1 for sample std).

rolling_beta(benchmark_col, window_size, min_periods=None, ddof=1, over=None)[source]

Compute rolling beta of this expression against a benchmark expression.

Return type:

Expr

Parameters:
  • benchmark_col (Expr)

  • window_size (int)

  • min_periods (int | None)

  • ddof (int)

  • over (str | list[str] | None)

Parameters

benchmarkpl.Expr

Benchmark return column.

windowint

Rolling window size.

min_periodsint | None

Minimum number of observations required to compute a value.

ddofint

Delta degrees of freedom for variance (default 1 for sample variance).

overstr | list[str] | None

Optional grouping column(s), e.g., “ticker”.

Returns

pl.Expr

Rolling beta expression

xpectral.quant.polars_expressions.ffill_inside(expr)[source]
Return type:

Expr

Parameters:

expr (Expr)

class xpectral.quant.portfolio.Portfolio(df, portfolio, benchmark)[source]

Bases: object

Parameters:
  • df (DataFrame)

  • portfolio (Dict)

  • benchmark (str)

__init__(df, portfolio, benchmark)[source]

Portfolio analytics class for computing total, systematic, and idiosyncratic returns.

This class takes in a dataset of asset returns (including a benchmark) and a dictionary of portfolio weights, then computes the time series of portfolio-level returns and their decomposition into systematic and idiosyncratic components.

Parameters

dfpl.DataFrame

Polars DataFrame containing asset and benchmark data. Must include at least the following columns: - ‘timestamp’ : datetime - ‘ticker’ : str - ‘return’ : float - ‘beta’ : float

portfolioDict[str, float]

Dictionary mapping asset tickers to their portfolio weights. Example: {"AAPL": 0.4, "MSFT": 0.6}.

benchmarkstr

Ticker symbol identifying the benchmark asset within df

Parameters:
  • df (DataFrame)

  • portfolio (Dict)

  • benchmark (str)

compute_returns()[source]

Compute portfolio total, systematic, and idiosyncratic returns.

Return type:

DataFrame