Returns#
!pip install mlfinlab
!pip install yfinance
# See https://plotly.com/python/static-image-export/
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca
!chmod +x /usr/local/bin/orca
!apt-get install xvfb libgtk2.0-0 libgconf-2-4
Collecting mlfinlab
Using cached https://files.pythonhosted.org/packages/a9/9b/d6a2e5ec3818c9294d6e9e34a522c6ca7746efd4bd4e9640bbc4233ab2f0/mlfinlab-0.11.2-py3-none-any.whl
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 2.5MB/s
?25hCollecting matplotlib==3.1.1
Using cached https://files.pythonhosted.org/packages/57/4f/dd381ecf6c6ab9bcdaa8ea912e866dedc6e696756156d8ecc087e20817e2/matplotlib-3.1.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting pandas==0.25.3
?25l Downloading https://files.pythonhosted.org/packages/52/3f/f6a428599e0d4497e1595030965b5ba455fd8ade6e977e3c819973c4b41d/pandas-0.25.3-cp36-cp36m-manylinux1_x86_64.whl (10.4MB)
|████████████████████████████████| 10.4MB 44.8MB/s
?25hCollecting xmlrunner==1.7.7
Downloading https://files.pythonhosted.org/packages/57/c0/a19e29bc6038a56bb690549573af6ea11a9d2a5c07aff2e27ed308c2cab9/xmlrunner-1.7.7.tar.gz
Collecting cvxpy==1.0.25
?25l Downloading https://files.pythonhosted.org/packages/d9/ed/90e0a13ad7ac4e7cdc2aeaefed26cebb4922f205bb778199268863fa2fbe/cvxpy-1.0.25.tar.gz (906kB)
|████████████████████████████████| 911kB 13.4MB/s
?25hCollecting scipy==1.3.1
?25l Downloading https://files.pythonhosted.org/packages/29/50/a552a5aff252ae915f522e44642bb49a7b7b31677f9580cfd11bcc869976/scipy-1.3.1-cp36-cp36m-manylinux1_x86_64.whl (25.2MB)
|████████████████████████████████| 25.2MB 1.7MB/s
?25hCollecting numpy==1.17.3
Using cached https://files.pythonhosted.org/packages/0e/46/ae6773894f7eacf53308086287897ec568eac9768918d913d5b9d366c5db/numpy-1.17.3-cp36-cp36m-manylinux1_x86_64.whl
Collecting numba==0.49.1
Using cached https://files.pythonhosted.org/packages/56/dc/0e3d3732fd62c73fbb3317fc7bba22574832ab7a8e075620557bd4311641/numba-0.49.1-cp36-cp36m-manylinux2014_x86_64.whl
Collecting scikit-learn==0.21.3
Using cached https://files.pythonhosted.org/packages/a0/c5/d2238762d780dde84a20b8c761f563fe882b88c5a5fb03c056547c442a19/scikit_learn-0.21.3-cp36-cp36m-manylinux1_x86_64.whl
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: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (2.4.7)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (0.10.0)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (2.8.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==3.1.1->mlfinlab) (1.2.0)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas==0.25.3->mlfinlab) (2018.9)
Requirement already satisfied: osqp>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (0.6.1)
Requirement already satisfied: ecos>=2 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (2.0.7.post1)
Requirement already satisfied: scs>=1.1.3 in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (2.1.2)
Requirement already satisfied: multiprocess in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (0.70.9)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cvxpy==1.0.25->mlfinlab) (1.12.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from numba==0.49.1->mlfinlab) (47.1.1)
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: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn==0.21.3->mlfinlab) (0.15.1)
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from osqp>=0.4.1->cvxpy==1.0.25->mlfinlab) (0.16.0)
Requirement already satisfied: dill>=0.3.1 in /usr/local/lib/python3.6/dist-packages (from multiprocess->cvxpy==1.0.25->mlfinlab) (0.3.1.1)
Building wheels for collected packages: xmlrunner, cvxpy
Building wheel for xmlrunner (setup.py) ... ?25l?25hdone
Created wheel for xmlrunner: filename=xmlrunner-1.7.7-cp36-none-any.whl size=6235 sha256=75cb0b6de31da6e7e7e44dae9bfb4c4fe26332e7afbc8923ce271323d880aa99
Stored in directory: /root/.cache/pip/wheels/c8/0e/05/28e4ff5b683c7a319756b412c7f4dc508a7ffa654e12c54ef5
Building wheel for cvxpy (setup.py) ... ?25l?25hdone
Created wheel for cvxpy: filename=cvxpy-1.0.25-cp36-cp36m-linux_x86_64.whl size=2044785 sha256=af0f5f1f5c097ee4e8b1b6338df6874b34e3de574b331a13a8b2bb47a58ec38c
Stored in directory: /root/.cache/pip/wheels/f1/b0/c3/0d3e98435c5070047e033ead500eb4f93a87c4f3ab6ea6dc1e
Successfully built xmlrunner cvxpy
ERROR: tensorflow 2.2.0 has requirement scipy==1.4.1; python_version >= "3", but you'll have scipy 1.3.1 which is incompatible.
ERROR: google-colab 1.0.0 has requirement pandas~=1.0.0; python_version >= "3.0", but you'll have pandas 0.25.3 which is incompatible.
ERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.
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: numpy, scipy, pandas, statsmodels, matplotlib, xmlrunner, cvxpy, numba, scikit-learn, mlfinlab
Found existing installation: numpy 1.18.4
Can't uninstall 'numpy'. No files were found to uninstall.
Found existing installation: scipy 1.4.1
Uninstalling scipy-1.4.1:
Successfully uninstalled scipy-1.4.1
Found existing installation: pandas 1.0.4
Uninstalling pandas-1.0.4:
Successfully uninstalled pandas-1.0.4
Found existing installation: statsmodels 0.10.2
Uninstalling statsmodels-0.10.2:
Successfully uninstalled statsmodels-0.10.2
Found existing installation: matplotlib 3.2.1
Can't uninstall 'matplotlib'. No files were found to uninstall.
Found existing installation: cvxpy 1.0.31
Uninstalling cvxpy-1.0.31:
Successfully uninstalled cvxpy-1.0.31
Found existing installation: numba 0.48.0
Uninstalling numba-0.48.0:
Successfully uninstalled numba-0.48.0
Found existing installation: scikit-learn 0.22.2.post1
Can't uninstall 'scikit-learn'. No files were found to uninstall.
Successfully installed cvxpy-1.0.25 matplotlib-3.1.1 mlfinlab-0.11.2 numba-0.49.1 numpy-1.17.3 pandas-0.25.3 scikit-learn-0.21.3 scipy-1.3.1 statsmodels-0.11.1 xmlrunner-1.7.7
Collecting yfinance
Downloading https://files.pythonhosted.org/packages/c2/31/8b374a12b90def92a4e27d0fc595fc43635f395984e36a075244d98bd265/yfinance-0.1.54.tar.gz
Requirement already satisfied: pandas>=0.24 in /usr/local/lib/python3.6/dist-packages (from yfinance) (0.25.3)
Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.6/dist-packages (from yfinance) (1.17.3)
Requirement 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: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24->yfinance) (2018.9)
Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24->yfinance) (2.8.1)
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: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2.9)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2020.4.5.1)
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: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.6.1->pandas>=0.24->yfinance) (1.12.0)
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=9d64574defb8d37072a024bf94977d03f666b6efbdb17141c96a0b0ae68ef864
Stored in directory: /root/.cache/pip/wheels/f9/e3/5b/ec24dd2984b12d61e0abf26289746c2436a0e7844f26f2515c
Successfully built yfinance
Installing collected packages: yfinance
Successfully installed yfinance-0.1.54
--2020-06-07 20:34:28-- https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage
Resolving github.com (github.com)... 140.82.114.4
Connecting to github.com (github.com)|140.82.114.4|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/99037241/9dc3a580-286a-11e9-8a21-4312b7c8a512?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200607%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200607T203429Z&X-Amz-Expires=300&X-Amz-Signature=d4c2ba15a5dc256c0fba2279240d27d10bd9219c83c807c239f2fcd3661e4a0c&X-Amz-SignedHeaders=host&actor_id=0&repo_id=99037241&response-content-disposition=attachment%3B%20filename%3Dorca-1.2.1-x86_64.AppImage&response-content-type=application%2Foctet-stream [following]
--2020-06-07 20:34:29-- https://github-production-release-asset-2e65be.s3.amazonaws.com/99037241/9dc3a580-286a-11e9-8a21-4312b7c8a512?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200607%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200607T203429Z&X-Amz-Expires=300&X-Amz-Signature=d4c2ba15a5dc256c0fba2279240d27d10bd9219c83c807c239f2fcd3661e4a0c&X-Amz-SignedHeaders=host&actor_id=0&repo_id=99037241&response-content-disposition=attachment%3B%20filename%3Dorca-1.2.1-x86_64.AppImage&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.36.188
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.36.188|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 51607939 (49M) [application/octet-stream]
Saving to: ‘/usr/local/bin/orca’
/usr/local/bin/orca 100%[===================>] 49.22M 13.3MB/s in 4.2s
2020-06-07 20:34:34 (11.6 MB/s) - ‘/usr/local/bin/orca’ saved [51607939/51607939]
Reading package lists... Done
Building dependency tree
Reading state information... Done
libgtk2.0-0 is already the newest version (2.24.32-1ubuntu1).
libgtk2.0-0 set to manually installed.
The following additional packages will be installed:
gconf-service gconf-service-backend gconf2-common libdbus-glib-1-2
The following NEW packages will be installed:
gconf-service gconf-service-backend gconf2-common libdbus-glib-1-2
libgconf-2-4 xvfb
0 upgraded, 6 newly installed, 0 to remove and 32 not upgraded.
Need to get 1,687 kB of archives.
After this operation, 10.5 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libdbus-glib-1-2 amd64 0.110-2 [58.3 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf2-common all 3.2.6-4ubuntu1 [700 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libgconf-2-4 amd64 3.2.6-4ubuntu1 [84.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf-service-backend amd64 3.2.6-4ubuntu1 [58.1 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf-service amd64 3.2.6-4ubuntu1 [2,036 B]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 xvfb amd64 2:1.19.6-1ubuntu4.4 [784 kB]
Fetched 1,687 kB in 3s (599 kB/s)
Selecting previously unselected package libdbus-glib-1-2:amd64.
(Reading database ... 144467 files and directories currently installed.)
Preparing to unpack .../0-libdbus-glib-1-2_0.110-2_amd64.deb ...
Unpacking libdbus-glib-1-2:amd64 (0.110-2) ...
Selecting previously unselected package gconf2-common.
Preparing to unpack .../1-gconf2-common_3.2.6-4ubuntu1_all.deb ...
Unpacking gconf2-common (3.2.6-4ubuntu1) ...
Selecting previously unselected package libgconf-2-4:amd64.
Preparing to unpack .../2-libgconf-2-4_3.2.6-4ubuntu1_amd64.deb ...
Unpacking libgconf-2-4:amd64 (3.2.6-4ubuntu1) ...
Selecting previously unselected package gconf-service-backend.
Preparing to unpack .../3-gconf-service-backend_3.2.6-4ubuntu1_amd64.deb ...
Unpacking gconf-service-backend (3.2.6-4ubuntu1) ...
Selecting previously unselected package gconf-service.
Preparing to unpack .../4-gconf-service_3.2.6-4ubuntu1_amd64.deb ...
Unpacking gconf-service (3.2.6-4ubuntu1) ...
Selecting previously unselected package xvfb.
Preparing to unpack .../5-xvfb_2%3a1.19.6-1ubuntu4.4_amd64.deb ...
Unpacking xvfb (2:1.19.6-1ubuntu4.4) ...
Setting up gconf2-common (3.2.6-4ubuntu1) ...
Creating config file /etc/gconf/2/path with new version
Setting up libdbus-glib-1-2:amd64 (0.110-2) ...
Setting up xvfb (2:1.19.6-1ubuntu4.4) ...
Setting up libgconf-2-4:amd64 (3.2.6-4ubuntu1) ...
Setting up gconf-service-backend (3.2.6-4ubuntu1) ...
Setting up gconf-service (3.2.6-4ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
import yfinance as yf
import pandas as pd
from mlfinlab.portfolio_optimization import ReturnsEstimation
stocks = ["NEXCF", "ACB.TO", "BB.TO", "N.V", "APHA.TO", "TTT.CN", "HUT.TO", "HIVE.V", "AC.TO", "CVE.TO"]
def enable_plotly_in_cell():
import IPython
from plotly.offline import init_notebook_mode
display(IPython.core.display.HTML('''<script src="/static/components/requirejs/require.js"></script>'''))
init_notebook_mode(connected=False)
enable_plotly_in_cell()
def get_prices(stocks, start_date="2019-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_prices = get_prices(stocks)
# Class that contains needed functions
ret_est = ReturnsEstimation()
# Calculate simple returns
assets_returns = ret_est.calculate_returns(asset_prices)
print(assets_returns)
# Calculate annualised mean historical returns for daily data
assets_annual_returns = ret_est.calculate_mean_historical_returns(asset_prices, frequency=252)
print(assets_annual_returns)
# Calculate exponentially-weighted annualized mean of historical returns for daily data and span of 200
assets_exp_annual_returns = ret_est.calculate_exponential_historical_returns(asset_prices,
frequency=252,
span=200)
print(assets_exp_annual_returns)
[*********************100%***********************] 10 of 10 completed
NEXCF ACB.TO BB.TO ... HIVE.V AC.TO CVE.TO
Date ...
2019-03-04 0.006036 -0.012346 -0.001715 ... -0.111111 -0.011765 -0.056743
2019-03-05 -0.040000 0.125000 0.000000 ... 0.079545 -0.017262 0.000000
2019-03-06 -0.010417 -0.019444 0.050687 ... 0.021053 -0.007268 -0.020052
2019-03-07 0.026316 -0.036827 -0.005724 ... -0.020619 0.006711 0.010676
2019-03-08 0.152821 0.016667 0.013158 ... 0.031579 0.004848 -0.031690
... ... ... ... ... ... ... ...
2020-05-25 0.000000 0.021352 0.023511 ... 0.027778 0.002992 0.003384
2020-05-26 -0.025093 -0.060540 -0.019908 ... -0.054054 0.054296 0.015177
2020-05-27 0.016820 -0.024108 0.003125 ... 0.042857 -0.069609 0.028239
2020-05-28 -0.048434 0.007126 -0.015576 ... 0.041096 -0.026764 -0.025848
2020-05-29 0.020610 -0.091038 0.012658 ... -0.013158 -0.006250 -0.004975
[319 rows x 10 columns]
NEXCF 1.766938
ACB.TO -0.862648
BB.TO -0.303306
N.V -0.190125
APHA.TO -0.368730
TTT.CN -0.581373
HUT.TO 1.160076
HIVE.V 0.636560
AC.TO -0.409008
CVE.TO -0.184405
dtype: float64
NEXCF 1.915090
ACB.TO 0.092373
BB.TO -0.036332
N.V 0.601380
APHA.TO 0.284633
TTT.CN 0.431195
HUT.TO 2.064334
HIVE.V 2.094106
AC.TO -1.188513
CVE.TO 0.108182
Name: 2020-05-29 00:00:00, dtype: float64