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.
No programming experience? With the Finance Toolkit MCP server, AI assistants such as Claude and ChatGPT can calculate the Correlation Matrix for you. Just ask in plain English.
Calculate the Correlation Matrix in Python
The Correlation Matrix is available in the Performance module of the open-source Finance Toolkit. Install it with:
pip install financetoolkit -U
Then call get_correlation_matrix as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(["AMZN", "TSLA"], api_key="FINANCIAL_MODELING_PREP_KEY")
toolkit.performance.get_correlation_matrix()
Which returns:
| AMZN | TSLA | Benchmark | |
|---|---|---|---|
| AMZN | 1 | 0.935 | 0.7751 |
| TSLA | 0.935 | 1 | 0.8982 |
| Benchmark | 0.7751 | 0.8982 | 1 |
Parameters
get_correlation_matrix accepts the following parameters:
- period (str, optional): The data frequency for returns (weekly, monthly, quarterly, or yearly). Defaults to “quarterly” if the Toolkit is initialised with quarterly=True, otherwise “yearly”.
- rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
Related Performance Metrics
The Performance module page introduces the module, and the sidebar lists all of its functions.