Correlation Matrix
Calculate the full pairwise Correlation Matrix across all assets (and the benchmark) in the Toolkit instance, based on the returns at the frequency given by period.
Unlike get_beta, which relates a single asset to the benchmark, this computes the correlation between every pair of assets at once. This is a prerequisite for portfolio variance calculations and any mean-variance optimization work.
The period sets the return frequency, and with it the number of observations: yearly returns over five years give only a handful of points per pair, which makes the correlations close to meaningless, so monthly or weekly returns over the same span are the usual choice for diversification and portfolio work. Correlations between risky assets tend to rise in sell-offs, so a matrix estimated over a calm stretch understates how much assets fall together when it matters.
The formula is as follows:
\[\text{Correlation} (A,\; B) = \text{Covariance} (\text{Return}\; A,\; \text{Return}\; B) / (\text{Standard Deviation}\; A \cdot \text{Standard Deviation}\; B),\;\; \text{the Pearson correlation of the period returns (e.g. monthly) within the Toolkit's date range},\;\; \text{using the overlapping (pairwise complete) observations of each pair}\]Correlation Matrix in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_correlation_matrix as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["TLT", "GLD", "EFA", "VNQ"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.performance.get_correlation_matrix(period="monthly")
Or, after setting up the Finance Toolkit MCP, ask:
How did the monthly returns of TLT, GLD, EFA, VNQ and the S&P 500 correlate with each other from 2021 to 2025?
Which returns:
| TLT | GLD | EFA | VNQ | Benchmark | |
|---|---|---|---|---|---|
| TLT | 1 | 0.3747 | 0.6185 | 0.6626 | 0.5646 |
| GLD | 0.3747 | 1 | 0.3918 | 0.2266 | 0.1283 |
| EFA | 0.6185 | 0.3918 | 1 | 0.781 | 0.7999 |
| VNQ | 0.6626 | 0.2266 | 0.781 | 1 | 0.8282 |
| Benchmark | 0.5646 | 0.1283 | 0.7999 | 0.8282 | 1 |
Parameters
get_correlation_matrix accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency for returns (weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”. | "quarterly" |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 2 |