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.
Correlation Matrix in Python
get_correlation_matrix is part of 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 initialized with quarterly=True, otherwise “yearly”.
- rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.