Factor Reference¶
Alpha formulas and factor analysis tools.
Factor analysis facade.
FactorAnalyzer
dataclass
¶
Analyze cross-sectional factor values and forward returns.
__post_init__ ¶
__post_init__() -> None
Promote single-index data to MultiIndex to support single-asset analysis.
from_clean_factor_data
classmethod
¶
from_clean_factor_data(
clean: DataFrame,
*,
periods: tuple[int, ...] | None = None,
annualization_periods: int = 252,
quantiles: int = 5,
) -> MultiPeriodFactorAnalyzer | MultiFactorAnalyzer
Return an analysis object built from clean factor data.
clean is the frame produced by clean_factor_and_forward_returns.
A factor_name column produces one multi-period analyzer per factor.
multi_period_summary
classmethod
¶
multi_period_summary(
clean: DataFrame,
*,
periods: tuple[int, ...] | None = None,
annualization_periods: int = 252,
quantiles: int = 5,
) -> pd.DataFrame
Return summary metrics indexed by prediction horizon.
ic ¶
ic(by_group: bool = False) -> pd.Series | pd.DataFrame
Return per-date Pearson information coefficient.
factor_information_coefficient ¶
factor_information_coefficient(
by_group: bool = False,
) -> pd.Series | pd.DataFrame
Return per-date Spearman rank information coefficient.
mean_information_coefficient ¶
mean_information_coefficient(
by_time: str | None = None,
) -> pd.Series | pd.DataFrame
Return mean Spearman IC, optionally grouped by time rule.
mean_return_by_quantile ¶
mean_return_by_quantile(
group_neutral: bool = False,
) -> pd.DataFrame
Return mean forward returns by factor quantile.
mean_return_by_quantile_std_error ¶
mean_return_by_quantile_std_error() -> pd.DataFrame
Return standard error of forward returns by date and factor quantile.
quantile_counts ¶
quantile_counts() -> pd.DataFrame
Return per-date asset counts by factor quantile.
quantile_forward_returns ¶
quantile_forward_returns() -> pd.DataFrame
Return raw forward return observations with assigned factor quantiles.
quantile_decay ¶
quantile_decay(window: int = 5) -> pd.DataFrame
Return rolling mean returns by factor quantile.
quantile_cumulative_returns ¶
quantile_cumulative_returns() -> pd.DataFrame
Return compounded returns by factor quantile.
compute_mean_returns_spread ¶
compute_mean_returns_spread(
reverse: bool = False,
) -> tuple[pd.Series, pd.Series]
Return top-minus-bottom quantile spread and joint standard error.
long_short_returns ¶
long_short_returns() -> pd.DataFrame
Return long, short, and spread factor returns.
long_short_cumulative_returns ¶
long_short_cumulative_returns() -> pd.DataFrame
Return compounded long, short, and spread factor returns.
factor_returns ¶
factor_returns(
demeaned: bool = True, equal_weight: bool = False
) -> pd.DataFrame
Return Alphalens-style factor-weighted returns.
factor_cumulative_returns ¶
factor_cumulative_returns(
demeaned: bool = True, equal_weight: bool = False
) -> pd.Series
Return compounded Alphalens-style factor returns with starting value 1.
average_cumulative_return_by_quantile ¶
average_cumulative_return_by_quantile(
returns: DataFrame,
periods_before: int = 10,
periods_after: int = 15,
demeaned: bool = True,
) -> pd.DataFrame
Return average event-window cumulative returns by factor quantile.
monthly_ic_heatmap ¶
monthly_ic_heatmap(by_group: bool = False) -> pd.DataFrame
Return mean IC by year and month.
event_returns
staticmethod
¶
event_returns(
prices: Series,
events: MultiIndex,
before: int = 5,
after: int = 5,
) -> pd.DataFrame
Return average event-window returns around (date, symbol) events.
quantile_turnover ¶
quantile_turnover(
quantile: int | None = None, period: int = 1
) -> pd.Series | pd.DataFrame
Return Alphalens-style turnover for one factor quantile or all quantiles.
factor_rank_autocorrelation ¶
factor_rank_autocorrelation() -> pd.Series
Return factor rank autocorrelation.
events_distribution ¶
events_distribution() -> pd.DataFrame
Return factor observation events for event-count distribution charts.
monotonicity ¶
monotonicity() -> dict[str, float]
Return monotonicity diagnostics for the factor quantile returns.
Checks whether mean returns increase monotonically from Q1 to Q(n). Returns Spearman rank correlation between quantile rank and mean return, and a boolean flag indicating perfect monotonicity.
to_analysis ¶
to_analysis(*, include_figures: bool = False) -> dict
Compute existing diagnostics for the common report renderer.
report ¶
report(path: str, format: str | None = None) -> Path
Render completed diagnostics through the shared Reporter.
MultiFactorAnalyzer
dataclass
¶
Thin facade around one multi-period analyzer per factor.
factor_information_coefficient ¶
factor_information_coefficient(
by_group: bool = False,
) -> pd.DataFrame
Return factor information coefficient with (factor, period) columns.
MultiPeriodFactorAnalyzer
dataclass
¶
Thin facade around one FactorAnalyzer per prediction horizon.
ic ¶
ic(by_group: bool = False) -> pd.Series | pd.DataFrame
Return IC series for every prediction horizon.
factor_information_coefficient ¶
factor_information_coefficient(
by_group: bool = False,
) -> pd.Series | pd.DataFrame
Return factor information coefficient for every prediction horizon.
mean_return_by_quantile ¶
mean_return_by_quantile(
period: int | None = None,
) -> pd.DataFrame | dict[int, pd.DataFrame]
Return mean returns by quantile for one or all prediction horizons.
plot ¶
plot(period: int | None = None)
Return factor diagnostic charts for a selected prediction horizon.
FactorRiskModel
dataclass
¶
Static or dated multi-factor risk model.
Parameters¶
exposures
Asset factor exposure matrix. Use a symbol index for a static model,
or a (date, symbol) MultiIndex for dated exposures.
factor_cov
Factor covariance matrix with factor names as index and columns.
specific_var
Asset-specific variance by symbol. Missing symbols default to zero.
portfolio_exposure ¶
portfolio_exposure(
weights: Series | dict[str, float], date=None
) -> pd.Series
Return weighted factor exposure for a portfolio.
portfolio_variance ¶
portfolio_variance(
weights: Series | dict[str, float], date=None
) -> float
Return total portfolio variance from factor and specific risk.
portfolio_risk ¶
portfolio_risk(
weights: Series | dict[str, float], date=None
) -> float
Return portfolio volatility.
risk_contribution ¶
risk_contribution(
weights: Series | dict[str, float], date=None
) -> pd.DataFrame
Return variance contribution by factor plus specific risk.
active_risk ¶
active_risk(
portfolio_weights: Series | dict[str, float],
benchmark_weights: Series | dict[str, float],
date=None,
) -> float
Return active risk against a benchmark weight vector.
PerformanceAttribution
dataclass
¶
Attribute strategy returns to factor and specific returns.
The data contract follows the same shape as pyfolio: positions are
wide by asset, factor_returns are wide by factor, and dated
factor_loadings use a (date, symbol) MultiIndex.
attribution ¶
attribution() -> pd.DataFrame
Return factor, common, specific, and total return attribution.
summary ¶
summary() -> tuple[dict[str, float], pd.DataFrame]
Return scalar return attribution and factor exposure summaries.
plot ¶
plot()
Return a Bokeh column of attribution charts.
Mirrors FactorAnalyzer.plot(): every chart is built from the already computed frames, and an empty input yields an empty layout instead of a placeholder series.
to_analysis ¶
to_analysis(*, include_figures: bool = False) -> dict
Compute the attribution tables for the common report renderer.
report ¶
report(path: str, format: str | None = None) -> Path
Render the attribution through the shared Reporter.
clean_factor_and_forward_returns ¶
clean_factor_and_forward_returns(
factors: DataFrame,
*,
factor: str | Sequence[str],
prices: Series | DataFrame,
periods: tuple[int, ...] = (1,),
quantiles: int = 5,
groupby: str | Series | None = None,
nan_policy: NanPolicy = "drop",
) -> pd.DataFrame
Return an Alphalens-style clean factor frame.
factors is a factor table indexed by (date, symbol). factor
names the column or columns to analyze, and prices supplies aligned close prices.
Output is indexed by (date, symbol) and contains factor,
forward_return_N columns, factor_quantile, and optional group.