Return Contributions

Calculate how much each asset added to the portfolio’s return in every period.

The periods are weeks, months, quarters or years, and the contributions of the assets add up to the portfolio’s return of that period. Every day an asset contributes its weight at the start of the day times its return, \(c_{i,t} = w_{i,t} - 1 r_{i,t}\), which add up to the portfolio’s daily return \(R_{t} = \sum c_{i,t}\). Over a longer period the daily contributions are carried by the growth of the portfolio since the period began: \(C_{i} = \sum_{t} c_{i,t} \Pi_{s < t} (1 + R_{s})\), the contribution in money divided by the value at the start of the period, so the contributions add up to the compounded return \(\Pi (1 + R_{t}) - 1\) (Bacon, 2008).

The weights are the latest weights of the Portfolio the Toolkit came from, as they moved from day to day, or, when weights are given (or there is no Portfolio), those weights restored every day.

Also known as: contribution to return, return attribution by asset, performance contribution.

Example · AAPL, MSFT, JNJ, XOM · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_return_contributions:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT", "JNJ", "XOM"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.portfolio.get_return_contributions(
    weights={"AAPL": 0.4, "MSFT": 0.3, "JNJ": 0.2, "XOM": 0.1}, period="yearly"
)

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

How much did each stock add to the yearly return of a portfolio of 40% Apple, 30% Microsoft, 20% Johnson & Johnson and 10% ExxonMobil from 2021 to 2025?

Which returns:

Parameters

get_return_contributions accepts the following parameters.

ParameterTypeDescriptionExample
weightsdict[str, float] | NoneThe weight per asset. Defaults to None, which uses the weights of the Portfolio the Toolkit came from over time, or equal weights.None, which uses the weights of the Portfolio the Toolkit came from over time, or equal weights
periodstrThe period to add the contributions up over. Options are: “daily” (daily returns), “weekly” (weekly returns), “monthly” (monthly returns), “quarterly” (quarterly returns), “yearly” (yearly returns). Defaults to “monthly”."weekly"
include_benchmarkboolWhether to include the benchmark as an asset. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the Toolkit’s rounding.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Bacon, C. R. (2008). “Practical Portfolio Performance Measurement and Attribution.” 2nd edition, Wiley, chapter 5.
  • Library Shares similar logic as PerformanceAnalytics (R)’s to.period.contributions
Share