Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
373 changes: 373 additions & 0 deletions EDA.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,373 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "8c27d092",
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "594e37d2",
"metadata": {},
"outputs": [],
"source": [
"df_train = pd.read_csv('data/returns_train.csv', index_col='month_end')\n",
"df_train.sort_index()\n",
"df_test = pd.read_csv('data/returns_test.csv', index_col='month_end')\n",
"df_test.sort_index();"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab9a7f05",
"metadata": {},
"outputs": [],
"source": [
"df_all = pd.concat([df_train, df_test])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23429d59",
"metadata": {},
"outputs": [],
"source": [
"df_test"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f59d07e2",
"metadata": {},
"outputs": [],
"source": [
"df_all"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f45c8e11",
"metadata": {},
"outputs": [],
"source": [
"stocks = df_train.columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c19773d",
"metadata": {},
"outputs": [],
"source": [
"(1 + df_train.Stock1).cumprod().plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e173c714",
"metadata": {},
"outputs": [],
"source": [
"len(df_all.columns)\n",
"1/54"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adeead11",
"metadata": {},
"outputs": [],
"source": [
"for stock in stocks:\n",
" ax = (1 + df_all[stock]).cumprod().plot()\n",
"ax.axvline(len(df_train), c='r')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "876417ed",
"metadata": {},
"outputs": [],
"source": [
"(1 + df_all.Stock66).cumprod().plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2604ffcd",
"metadata": {},
"outputs": [],
"source": [
"(1 + df_all.Stock54).cumprod().plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a5291f5",
"metadata": {},
"outputs": [],
"source": [
"df_test"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1e14611",
"metadata": {},
"outputs": [],
"source": [
"(1 + df_all).cumprod().loc[['2017-09-30']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a079265d",
"metadata": {},
"outputs": [],
"source": [
"df_all.Stock54.iloc[-13:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "602b26ea",
"metadata": {},
"outputs": [],
"source": [
"from pypfopt.expected_returns import mean_historical_return\n",
"from pypfopt.risk_models import CovarianceShrinkage\n",
"df_train_cum = (df_train + 1).cumprod()\n",
"mu = mean_historical_return(df_train_cum)\n",
"S = CovarianceShrinkage(df_train_cum).ledoit_wolf()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "893784c5",
"metadata": {},
"outputs": [],
"source": [
"df_train"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22396233",
"metadata": {},
"outputs": [],
"source": [
"from pypfopt.efficient_frontier import EfficientFrontier\n",
"ef = EfficientFrontier(mu, S, weight_bounds=(0,0.1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e2ba7e90",
"metadata": {},
"outputs": [],
"source": [
"weights = ef.max_sharpe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9524682f",
"metadata": {},
"outputs": [],
"source": [
"list(weights.keys)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc685705",
"metadata": {},
"outputs": [],
"source": [
"def dict_to_df(dic):\n",
" new_dic = {}\n",
" for key in dic:\n",
" new_dic[key] = [dic[key]]\n",
"\n",
" return pd.DataFrame(new_dic)\n",
"dict_to_df(weights)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4a2cf47",
"metadata": {},
"outputs": [],
"source": [
"(1.5)**(1/(4*12))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d147a818",
"metadata": {},
"outputs": [],
"source": [
"(1.07)**(1/12) - 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2e99242",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import normaltest\n",
"\n",
"for i in range(len(df_all)):\n",
" print(normaltest(df_all[stocks[i]])[1])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f64a8ba",
"metadata": {},
"outputs": [],
"source": [
"df_all[stocks[7]].hist(bins=30)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc03e60d",
"metadata": {},
"outputs": [],
"source": [
"len(df_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e13378b",
"metadata": {},
"outputs": [],
"source": [
"scatter_data = []\n",
"for stock in stocks:\n",
" series = df_all[stock]\n",
" for i in range(len(series) - 1):\n",
" scatter_data.append([series[i], series[i + 1]])\n",
"x = [i[0] for i in scatter_data]\n",
"y = [i[1] for i in scatter_data]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "636e23ca",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import linregress\n",
"linregress(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe3791a9",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import pearsonr\n",
"pearsonr(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "874962a9",
"metadata": {},
"outputs": [],
"source": [
"len({'a': 1, 'b': 5})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4fd8af38",
"metadata": {},
"outputs": [],
"source": [
"plt.scatter([i[0] for i in scatter_data], [i[1] for i in scatter_data])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78714014",
"metadata": {},
"outputs": [],
"source": [
"from pypfopt.risk_models import CovarianceShrinkage, semicovariance, risk_matrix\n",
"risk_df = risk_matrix((1 + df_train).cumprod(), method='sample_cov')\n",
"\n",
"for i in range(len(risk_df)):\n",
" for j in range(len(risk_df)):\n",
" risk_df.iloc[i, j] /= (risk_df.iloc[i, i]*risk_df.iloc[j, j])**0.5\n",
" \n",
"risk_df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading