Implementation:AI4Finance Foundation FinGPT Pyfolio Perf Attrib
| Knowledge Sources | |
|---|---|
| Domains | Performance_Attribution, Portfolio_Analytics, Trading |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for attributing portfolio returns to risk factors and computing specific (alpha) vs. common returns provided by the vendored pyfolio package within the FinGPT Trading subproject.
Description
The perf_attrib module decomposes a strategy's daily returns into returns explained by a set of risk factors (common returns) and the residual specific returns (alpha). It preprocesses positions and returns, validates turnover, and delegates the core computation to empyrical's performance attribution routine.
Usage
Import this module when you need to measure how much of a trading strategy's performance is driven by known risk factors (momentum, sector, etc.) versus idiosyncratic alpha.
Code Reference
Source Location
- Repository: AI4Finance_Foundation_FinGPT
- File: fingpt/FinGPT_Others/FinGPT_Trading/chatgpt-trading-v2/pyfolio/perf_attrib.py
- Lines: 1-653
Signature
PERF_ATTRIB_TURNOVER_THRESHOLD = 0.25
def perf_attrib(returns, positions, factor_returns, factor_loadings,
transactions=None, pos_in_dollars=True):
"""
Attributes the performance of a returns stream to a set of risk factors.
Parameters
----------
returns : pd.Series
Daily returns of the strategy.
positions : pd.DataFrame
Daily holdings (dollars or percentages), indexed by date.
factor_returns : pd.DataFrame
Returns by factor, date-indexed.
factor_loadings : pd.DataFrame
Factor loadings, date+ticker indexed.
transactions : pd.DataFrame, optional
Executed trade volumes and fill prices.
pos_in_dollars : bool
Whether positions are in dollars (True) or percentages (False).
Returns
-------
tuple of (pd.DataFrame, pd.DataFrame)
(common_returns, specific_returns) indexed by date.
"""
def create_perf_attrib_tear_sheet(...): ...
Import
from pyfolio.perf_attrib import perf_attrib, create_perf_attrib_tear_sheet
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| returns | pd.Series | Yes | Daily noncumulative returns |
| positions | pd.DataFrame | Yes | Daily holdings indexed by date |
| factor_returns | pd.DataFrame | Yes | Factor returns by date |
| factor_loadings | pd.DataFrame | Yes | Factor loadings by date+ticker |
| transactions | pd.DataFrame | No | Trade volumes for turnover check |
| pos_in_dollars | bool | No | Whether positions are in dollars (default True) |
Outputs
| Name | Type | Description |
|---|---|---|
| common_returns | pd.DataFrame | Returns attributable to risk factors |
| specific_returns | pd.DataFrame | Residual alpha returns |
Usage Examples
Factor Attribution of a Trading Strategy
import pandas as pd
from pyfolio.perf_attrib import perf_attrib
# 1. Load strategy returns and positions
returns = pd.read_csv("strategy_returns.csv", index_col=0, parse_dates=True)["returns"]
positions = pd.read_csv("positions.csv", index_col=0, parse_dates=True)
# 2. Load factor data
factor_returns = pd.read_csv("factor_returns.csv", index_col=0, parse_dates=True)
factor_loadings = pd.read_csv("factor_loadings.csv", index_col=[0,1], parse_dates=True)
# 3. Compute performance attribution
common, specific = perf_attrib(
returns=returns,
positions=positions,
factor_returns=factor_returns,
factor_loadings=factor_loadings,
pos_in_dollars=True,
)
print("Common (factor) returns:\n", common.sum())
print("Specific (alpha) returns:\n", specific.sum())