Correlation Examples

Correlation Examples#

Open In Colab

import pandas as pd
import yfinance as yf
from mlfinlab.codependence import distance_correlation, angular_distance, absolute_angular_distance, squared_angular_distance

def get_prices(stocks, start_date="2020-03-01", end_date="2020-05-30"):
    stocks_string = " ".join(stocks)
    data = yf.download(stocks_string, start=start_date, end=end_date,
                      group_by="ticker")
    data = data.fillna(method='ffill')
    # Drop columns with no entries
    data = data.dropna(axis='columns', how='all')

    prices_df = pd.concat([data[ticker]["Close"] for ticker in stocks], axis=1)
    prices_df.columns = stocks
    return prices_df
  
asset_returns = get_prices(['IP.CN', 'NTAR.CN'])
print(asset_returns)
# Calculate distance correlation between chosen assets
distance_corr = distance_correlation(asset_returns['IP.CN'], asset_returns['NTAR.CN'])
print(distance_corr)
# Calculate angular distance between chosen assets
angular_dist = angular_distance(asset_returns['IP.CN'], asset_returns['NTAR.CN'])
print(angular_dist)
# Calculate absolute angular distance between chosen assets
angular_dist = absolute_angular_distance(asset_returns['IP.CN'], asset_returns['NTAR.CN'])
print(angular_dist)
# Calculate squared angular distance between chosen assets
angular_dist = squared_angular_distance(asset_returns['IP.CN'], asset_returns['NTAR.CN'])
print(angular_dist)
[*********************100%***********************]  2 of 2 completed
            IP.CN  NTAR.CN
Date                      
2020-03-02  0.055     1.62
2020-03-03  0.050     1.59
2020-03-04  0.040     1.57
2020-03-05  0.045     1.48
2020-03-06  0.040     1.32
...           ...      ...
2020-05-25  0.065     2.07
2020-05-26  0.065     2.20
2020-05-27  0.060     2.18
2020-05-28  0.065     2.07
2020-05-29  0.065     2.11

[63 rows x 2 columns]
0.8200543899391678
0.2742084582009295
0.2742084582009295
0.3729254632246639
!pip3 install mlfinlab yfinance
Collecting mlfinlab
?25l  Downloading https://files.pythonhosted.org/packages/48/76/00bf9715ca24b772ea0f3233403719390a8cf49e7564adae4713d68a025a/mlfinlab-0.12.3-py3-none-any.whl (851kB)

     |▍                               | 10kB 16.7MB/s eta 0:00:01
     |▊                               | 20kB 1.7MB/s eta 0:00:01
     |█▏                              | 30kB 2.2MB/s eta 0:00:01
     |█▌                              | 40kB 2.5MB/s eta 0:00:01
     |██                              | 51kB 2.0MB/s eta 0:00:01
     |██▎                             | 61kB 2.2MB/s eta 0:00:01
     |██▊                             | 71kB 2.5MB/s eta 0:00:01
     |███                             | 81kB 2.7MB/s eta 0:00:01
     |███▌                            | 92kB 2.9MB/s eta 0:00:01
     |███▉                            | 102kB 2.8MB/s eta 0:00:01
     |████▎                           | 112kB 2.8MB/s eta 0:00:01
     |████▋                           | 122kB 2.8MB/s eta 0:00:01
     |█████                           | 133kB 2.8MB/s eta 0:00:01
     |█████▍                          | 143kB 2.8MB/s eta 0:00:01
     |█████▊                          | 153kB 2.8MB/s eta 0:00:01
     |██████▏                         | 163kB 2.8MB/s eta 0:00:01
     |██████▌                         | 174kB 2.8MB/s eta 0:00:01
     |███████                         | 184kB 2.8MB/s eta 0:00:01
     |███████▎                        | 194kB 2.8MB/s eta 0:00:01
     |███████▊                        | 204kB 2.8MB/s eta 0:00:01
     |████████                        | 215kB 2.8MB/s eta 0:00:01
     |████████▌                       | 225kB 2.8MB/s eta 0:00:01
     |████████▉                       | 235kB 2.8MB/s eta 0:00:01
     |█████████▎                      | 245kB 2.8MB/s eta 0:00:01
     |█████████▋                      | 256kB 2.8MB/s eta 0:00:01
     |██████████                      | 266kB 2.8MB/s eta 0:00:01
     |██████████▍                     | 276kB 2.8MB/s eta 0:00:01
     |██████████▊                     | 286kB 2.8MB/s eta 0:00:01
     |███████████▏                    | 296kB 2.8MB/s eta 0:00:01
     |███████████▌                    | 307kB 2.8MB/s eta 0:00:01
     |████████████                    | 317kB 2.8MB/s eta 0:00:01
     |████████████▎                   | 327kB 2.8MB/s eta 0:00:01
     |████████████▊                   | 337kB 2.8MB/s eta 0:00:01
     |█████████████                   | 348kB 2.8MB/s eta 0:00:01
     |█████████████▌                  | 358kB 2.8MB/s eta 0:00:01
     |█████████████▉                  | 368kB 2.8MB/s eta 0:00:01
     |██████████████▎                 | 378kB 2.8MB/s eta 0:00:01
     |██████████████▋                 | 389kB 2.8MB/s eta 0:00:01
     |███████████████                 | 399kB 2.8MB/s eta 0:00:01
     |███████████████▍                | 409kB 2.8MB/s eta 0:00:01
     |███████████████▊                | 419kB 2.8MB/s eta 0:00:01
     |████████████████▏               | 430kB 2.8MB/s eta 0:00:01
     |████████████████▌               | 440kB 2.8MB/s eta 0:00:01
     |█████████████████               | 450kB 2.8MB/s eta 0:00:01
     |█████████████████▎              | 460kB 2.8MB/s eta 0:00:01
     |█████████████████▊              | 471kB 2.8MB/s eta 0:00:01
     |██████████████████              | 481kB 2.8MB/s eta 0:00:01
     |██████████████████▌             | 491kB 2.8MB/s eta 0:00:01
     |██████████████████▉             | 501kB 2.8MB/s eta 0:00:01
     |███████████████████▎            | 512kB 2.8MB/s eta 0:00:01
     |███████████████████▋            | 522kB 2.8MB/s eta 0:00:01
     |████████████████████            | 532kB 2.8MB/s eta 0:00:01
     |████████████████████▍           | 542kB 2.8MB/s eta 0:00:01
     |████████████████████▊           | 552kB 2.8MB/s eta 0:00:01
     |█████████████████████▏          | 563kB 2.8MB/s eta 0:00:01
     |█████████████████████▌          | 573kB 2.8MB/s eta 0:00:01
     |██████████████████████          | 583kB 2.8MB/s eta 0:00:01
     |██████████████████████▎         | 593kB 2.8MB/s eta 0:00:01
     |██████████████████████▊         | 604kB 2.8MB/s eta 0:00:01
     |███████████████████████         | 614kB 2.8MB/s eta 0:00:01
     |███████████████████████▌        | 624kB 2.8MB/s eta 0:00:01
     |███████████████████████▉        | 634kB 2.8MB/s eta 0:00:01
     |████████████████████████▎       | 645kB 2.8MB/s eta 0:00:01
     |████████████████████████▋       | 655kB 2.8MB/s eta 0:00:01
     |█████████████████████████       | 665kB 2.8MB/s eta 0:00:01
     |█████████████████████████▍      | 675kB 2.8MB/s eta 0:00:01
     |█████████████████████████▊      | 686kB 2.8MB/s eta 0:00:01
     |██████████████████████████▏     | 696kB 2.8MB/s eta 0:00:01
     |██████████████████████████▌     | 706kB 2.8MB/s eta 0:00:01
     |███████████████████████████     | 716kB 2.8MB/s eta 0:00:01
     |███████████████████████████▎    | 727kB 2.8MB/s eta 0:00:01
     |███████████████████████████▊    | 737kB 2.8MB/s eta 0:00:01
     |████████████████████████████    | 747kB 2.8MB/s eta 0:00:01
     |████████████████████████████▌   | 757kB 2.8MB/s eta 0:00:01
     |████████████████████████████▉   | 768kB 2.8MB/s eta 0:00:01
     |█████████████████████████████▎  | 778kB 2.8MB/s eta 0:00:01
     |█████████████████████████████▋  | 788kB 2.8MB/s eta 0:00:01
     |██████████████████████████████  | 798kB 2.8MB/s eta 0:00:01
     |██████████████████████████████▍ | 808kB 2.8MB/s eta 0:00:01
     |██████████████████████████████▊ | 819kB 2.8MB/s eta 0:00:01
     |███████████████████████████████▏| 829kB 2.8MB/s eta 0:00:01
     |███████████████████████████████▌| 839kB 2.8MB/s eta 0:00:01
     |████████████████████████████████| 849kB 2.8MB/s eta 0:00:01
     |████████████████████████████████| 860kB 2.8MB/s 
?25hCollecting yfinance
  Downloading https://files.pythonhosted.org/packages/c2/31/8b374a12b90def92a4e27d0fc595fc43635f395984e36a075244d98bd265/yfinance-0.1.54.tar.gz
Requirement already satisfied: numpy==1.18.5 in /usr/local/lib/python3.6/dist-packages (from mlfinlab) (1.18.5)
Collecting matplotlib==3.2.1
?25l  Downloading https://files.pythonhosted.org/packages/93/4b/52da6b1523d5139d04e02d9e26ceda6146b48f2a4e5d2abfdf1c7bac8c40/matplotlib-3.2.1-cp36-cp36m-manylinux1_x86_64.whl (12.4MB)
     |████████████████████████████████| 12.4MB 309kB/s 
?25hCollecting pandas==1.0.4
?25l  Downloading https://files.pythonhosted.org/packages/8e/86/c14387d6813ebadb7bf61b9ad270ffff111c8b587e4d266e07de774e385e/pandas-1.0.4-cp36-cp36m-manylinux1_x86_64.whl (10.1MB)
     |████████████████████████████████| 10.1MB 47.7MB/s 
?25hRequirement already satisfied: scipy==1.4.1 in /usr/local/lib/python3.6/dist-packages (from mlfinlab) (1.4.1)
Collecting statsmodels==0.11.1
?25l  Downloading https://files.pythonhosted.org/packages/cb/83/540fd83238a18abe6c2d280fa8e489ac5fcefa1f370f0ca1acd16ae1b860/statsmodels-0.11.1-cp36-cp36m-manylinux1_x86_64.whl (8.7MB)
     |████████████████████████████████| 8.7MB 42.7MB/s 
?25hCollecting cvxpy==1.1.1
?25l  Downloading https://files.pythonhosted.org/packages/fa/89/6e4f99b36ce2d002f2792529b130fd8ed5d7004c92ce8ae7d56496f51426/cvxpy-1.1.1.tar.gz (990kB)
     |████████████████████████████████| 993kB 42.0MB/s 
?25h  Installing build dependencies ... ?25l?25hdone
  Getting requirements to build wheel ... ?25l?25hdone
    Preparing wheel metadata ... ?25l?25hdone
Collecting scikit-learn==0.23.1
?25l  Downloading https://files.pythonhosted.org/packages/d9/3a/eb8d7bbe28f4787d140bb9df685b7d5bf6115c0e2a969def4027144e98b6/scikit_learn-0.23.1-cp36-cp36m-manylinux1_x86_64.whl (6.8MB)
     |████████████████████████████████| 6.9MB 32.7MB/s 
?25hCollecting numba==0.49.1
?25l  Downloading https://files.pythonhosted.org/packages/56/dc/0e3d3732fd62c73fbb3317fc7bba22574832ab7a8e075620557bd4311641/numba-0.49.1-cp36-cp36m-manylinux2014_x86_64.whl (3.6MB)
     |████████████████████████████████| 3.6MB 33.9MB/s 
?25hRequirement already satisfied: requests>=2.20 in /usr/local/lib/python3.6/dist-packages (from yfinance) (2.23.0)
Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.6/dist-packages (from yfinance) (0.0.9)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.1->mlfinlab) (2.8.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.1->mlfinlab) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.1->mlfinlab) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.2.1->mlfinlab) (0.10.0)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas==1.0.4->mlfinlab) (2018.9)
Requirement already satisfied: patsy>=0.5 in /usr/local/lib/python3.6/dist-packages (from statsmodels==0.11.1->mlfinlab) (0.5.1)
Requirement already satisfied: osqp>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.1.1->mlfinlab) (0.6.1)
Requirement already satisfied: ecos>=2 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.1.1->mlfinlab) (2.0.7.post1)
Requirement already satisfied: scs>=1.1.3 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.1.1->mlfinlab) (2.1.2)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn==0.23.1->mlfinlab) (0.16.0)
Collecting threadpoolctl>=2.0.0
  Downloading https://files.pythonhosted.org/packages/f7/12/ec3f2e203afa394a149911729357aa48affc59c20e2c1c8297a60f33f133/threadpoolctl-2.1.0-py3-none-any.whl
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from numba==0.49.1->mlfinlab) (49.2.0)
Requirement already satisfied: llvmlite<=0.33.0.dev0,>=0.31.0.dev0 in /usr/local/lib/python3.6/dist-packages (from numba==0.49.1->mlfinlab) (0.31.0)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2020.6.20)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2.10)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (1.24.3)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.1->matplotlib==3.2.1->mlfinlab) (1.15.0)
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from osqp>=0.4.1->cvxpy==1.1.1->mlfinlab) (0.16.0)
Building wheels for collected packages: cvxpy
  Building wheel for cvxpy (PEP 517) ... ?25l?25hdone
  Created wheel for cvxpy: filename=cvxpy-1.1.1-cp36-cp36m-linux_x86_64.whl size=2654188 sha256=a26a6bc4f5d1fd6c94fb0f8e6d7dc682cb1a0b40165cb1ce6e92e89c9d4f1d9c
  Stored in directory: /root/.cache/pip/wheels/06/db/59/b5af93d86703e0903b9b94ccc300ac70daf9d273f13e6c0350
Successfully built cvxpy
Building wheels for collected packages: yfinance
  Building wheel for yfinance (setup.py) ... ?25l?25hdone
  Created wheel for yfinance: filename=yfinance-0.1.54-py2.py3-none-any.whl size=22409 sha256=c03517340ca865ab65213b423dde5625b362067eb62c54f5dcca90c92c2dbeae
  Stored in directory: /root/.cache/pip/wheels/f9/e3/5b/ec24dd2984b12d61e0abf26289746c2436a0e7844f26f2515c
Successfully built yfinance
ERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.
Installing collected packages: matplotlib, pandas, statsmodels, cvxpy, threadpoolctl, scikit-learn, numba, mlfinlab, yfinance
  Found existing installation: matplotlib 3.2.2
    Uninstalling matplotlib-3.2.2:
      Successfully uninstalled matplotlib-3.2.2
  Found existing installation: pandas 1.0.5
    Uninstalling pandas-1.0.5:
      Successfully uninstalled pandas-1.0.5
  Found existing installation: statsmodels 0.10.2
    Uninstalling statsmodels-0.10.2:
      Successfully uninstalled statsmodels-0.10.2
  Found existing installation: cvxpy 1.0.31
    Uninstalling cvxpy-1.0.31:
      Successfully uninstalled cvxpy-1.0.31
  Found existing installation: scikit-learn 0.22.2.post1
    Uninstalling scikit-learn-0.22.2.post1:
      Successfully uninstalled scikit-learn-0.22.2.post1
  Found existing installation: numba 0.48.0
    Uninstalling numba-0.48.0:
      Successfully uninstalled numba-0.48.0
Successfully installed cvxpy-1.1.1 matplotlib-3.2.1 mlfinlab-0.12.3 numba-0.49.1 pandas-1.0.4 scikit-learn-0.23.1 statsmodels-0.11.1 threadpoolctl-2.1.0 yfinance-0.1.54