quantlab.runs.backtest_attribution.attribution_metrics#

quantlab.runs.backtest_attribution.attribution_metrics(account, *, fill, valuation, scores, groups=10, gross_value=None, benchmark=None, delisted=None)[source]#

Return an account’s attribution block and its curves.

The universe of a rebalance bar (a decided bar of account.rebalances) is every symbol with a finite score and a fill price on the next bar. rebalanced_group_values gives the equal-weighted universe curve and the curves of groups score groups; excess_decomposition splits the annualised log growth over the benchmark (over the universe without one). A year is account.year_freq over account.bar_interval, and the window counts one bar per return, as backtest_stats.return_stats annualises.

Parameters:
accountAccountRecord

What the account did; its bars are the panels’ bars.

fill, valuationxr.DataArray

Fill and valuation prices on (timestamp, symbol); fill’s symbols are the universe’s, the other panels are aligned to them and to the account’s bars.

scoresxr.DataArray

The score the rule ranks by on (timestamp, symbol), named by its label; a symbol without one is never in the universe.

groupsint, default ATTRIBUTION_GROUPS

Score groups.

gross_valuexr.DataArray, optional

The same weights simulated without costs, on the account’s bars; without it costs is left out.

benchmarkBenchmarkRecord, optional

The benchmark on the account’s bars; its value, or its returns compounded when it has none.

delistedxr.DataArray, optional

Booleans on (timestamp, symbol) marking each delisted symbol’s last priced bar: the universe settles it as the engine does.

Returns:
blockdict

score_label, groups, decomposition, annualized_log_return (strategy, gross, universe, benchmark; None for a curve not given), group_annualized_log_return (lowest scores first) and relative_to_universe (backtest_stats.relative_stats of the account over the universe: what the scores and the rule add).

curvesxr.Dataset

universe_value and group_value (on group, 1 the lowest) at the account’s first value, and gross_value when given.

Examples

>>> block, curves = attribution_metrics(
...     account, fill=prices["open"], valuation=prices["close"], scores=predictions["fwd_ret_1"]
... )
>>> sorted(block["decomposition"])
['costs', 'selection', 'total']