mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-30 13:28:39 +01:00
Refactor code for improved readability and consistency across notebooks
- Standardized spacing around operators and function arguments in TP7_Kmeans.ipynb and neural_network.ipynb. - Enhanced the formatting of model building and training code in neural_network.ipynb for better clarity. - Updated the pyproject.toml to remove a specific TensorFlow version and added linting configuration for Ruff. - Improved comments and organization in the code to facilitate easier understanding and maintenance.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "81049114d821d00e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Project - Portfolio Management\n",
|
||||
"\n",
|
||||
@@ -11,52 +12,36 @@
|
||||
"### Time period studied from 2017-01-01 to 2018-01-01\n",
|
||||
"\n",
|
||||
"### Risk-free rate: 2%"
|
||||
],
|
||||
"id": "81049114d821d00e"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:46.298758Z",
|
||||
"start_time": "2024-11-25T13:43:46.293696Z"
|
||||
}
|
||||
},
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import yfinance as yf\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 51
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"id": "9f9fc36832c97e0",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:47.318911Z",
|
||||
"start_time": "2024-11-25T13:43:47.198820Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Data Extraction\n",
|
||||
"Tickers = [\"^RUT\", \"^IXIC\", \"^GSPC\", \"XWD.TO\"]\n",
|
||||
"start_input = \"2017-01-01\"\n",
|
||||
"end_input = \"2018-01-01\"\n",
|
||||
"S = pd.DataFrame()\n",
|
||||
"for t in Tickers:\n",
|
||||
" S[t] = yf.Tickers(t).history(start=start_input, end=end_input)[\"Close\"]\n",
|
||||
"\n",
|
||||
"S = S.interpolate(method=\"pad\")\n",
|
||||
"\n",
|
||||
"# Show the first five and last five values extracted\n",
|
||||
"display(S.head())\n",
|
||||
"display(S.tail())\n",
|
||||
"print(S.shape)"
|
||||
],
|
||||
"id": "9f9fc36832c97e0",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
@@ -72,15 +57,6 @@
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
" ^RUT ^IXIC ^GSPC XWD.TO\n",
|
||||
"Date \n",
|
||||
"2017-01-03 00:00:00+00:00 1365.489990 5429.080078 2257.830078 38.499630\n",
|
||||
"2017-01-04 00:00:00+00:00 1387.949951 5477.000000 2270.750000 38.553375\n",
|
||||
"2017-01-05 00:00:00+00:00 1371.939941 5487.939941 2269.000000 38.481716\n",
|
||||
"2017-01-06 00:00:00+00:00 1367.280029 5521.060059 2276.979980 38.517544\n",
|
||||
"2017-01-09 00:00:00+00:00 1357.489990 5531.819824 2268.899902 38.383186"
|
||||
],
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
@@ -152,6 +128,15 @@
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" ^RUT ^IXIC ^GSPC XWD.TO\n",
|
||||
"Date \n",
|
||||
"2017-01-03 00:00:00+00:00 1365.489990 5429.080078 2257.830078 38.499630\n",
|
||||
"2017-01-04 00:00:00+00:00 1387.949951 5477.000000 2270.750000 38.553375\n",
|
||||
"2017-01-05 00:00:00+00:00 1371.939941 5487.939941 2269.000000 38.481716\n",
|
||||
"2017-01-06 00:00:00+00:00 1367.280029 5521.060059 2276.979980 38.517544\n",
|
||||
"2017-01-09 00:00:00+00:00 1357.489990 5531.819824 2268.899902 38.383186"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
@@ -159,15 +144,6 @@
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
" ^RUT ^IXIC ^GSPC XWD.TO\n",
|
||||
"Date \n",
|
||||
"2017-12-22 00:00:00+00:00 1542.930054 6959.959961 2683.340088 44.323349\n",
|
||||
"2017-12-26 00:00:00+00:00 1544.229980 6936.250000 2680.500000 44.323349\n",
|
||||
"2017-12-27 00:00:00+00:00 1543.939941 6939.339844 2682.620117 44.052303\n",
|
||||
"2017-12-28 00:00:00+00:00 1548.930054 6950.160156 2687.540039 43.857414\n",
|
||||
"2017-12-29 00:00:00+00:00 1535.510010 6903.390137 2673.610107 43.784576"
|
||||
],
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
@@ -239,6 +215,15 @@
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" ^RUT ^IXIC ^GSPC XWD.TO\n",
|
||||
"Date \n",
|
||||
"2017-12-22 00:00:00+00:00 1542.930054 6959.959961 2683.340088 44.323349\n",
|
||||
"2017-12-26 00:00:00+00:00 1544.229980 6936.250000 2680.500000 44.323349\n",
|
||||
"2017-12-27 00:00:00+00:00 1543.939941 6939.339844 2682.620117 44.052303\n",
|
||||
"2017-12-28 00:00:00+00:00 1548.930054 6950.160156 2687.540039 43.857414\n",
|
||||
"2017-12-29 00:00:00+00:00 1535.510010 6903.390137 2673.610107 43.784576"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
@@ -252,63 +237,69 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 52
|
||||
"source": [
|
||||
"# Data Extraction\n",
|
||||
"Tickers = [\"^RUT\", \"^IXIC\", \"^GSPC\", \"XWD.TO\"]\n",
|
||||
"start_input = \"2017-01-01\"\n",
|
||||
"end_input = \"2018-01-01\"\n",
|
||||
"S = pd.DataFrame()\n",
|
||||
"for t in Tickers:\n",
|
||||
" S[t] = yf.Tickers(t).history(start=start_input, end=end_input)[\"Close\"]\n",
|
||||
"\n",
|
||||
"S = S.interpolate(method=\"pad\")\n",
|
||||
"\n",
|
||||
"# Show the first five and last five values extracted\n",
|
||||
"display(S.head())\n",
|
||||
"display(S.tail())\n",
|
||||
"print(S.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 53,
|
||||
"id": "53483cf3a925a4db",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:50.080380Z",
|
||||
"start_time": "2024-11-25T13:43:50.073119Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"R = S / S.shift() - 1\n",
|
||||
"R = R[1:]\n",
|
||||
"mean_d = R.mean()\n",
|
||||
"covar_d = R.cov()\n",
|
||||
"corr = R.corr()"
|
||||
],
|
||||
"id": "53483cf3a925a4db",
|
||||
"outputs": [],
|
||||
"execution_count": 53
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 54,
|
||||
"id": "c327ed5967b1f442",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:50.965092Z",
|
||||
"start_time": "2024-11-25T13:43:50.961969Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"mean = mean_d * 252\n",
|
||||
"covar = covar_d * 252\n",
|
||||
"std = np.sqrt(np.diag(covar))"
|
||||
],
|
||||
"id": "c327ed5967b1f442",
|
||||
"outputs": [],
|
||||
"execution_count": 54
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"id": "6bc6a850bf06cc9d",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:51.701725Z",
|
||||
"start_time": "2024-11-25T13:43:51.695020Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"print(\"Mean:\\n\")\n",
|
||||
"print(mean)\n",
|
||||
"print(\"\\nCovariance:\\n\")\n",
|
||||
"print(covar)\n",
|
||||
"print(\"\\nStandard Deviation:\\n\")\n",
|
||||
"print(std)\n",
|
||||
"print(\"\\nCorrelation:\\n\")\n",
|
||||
"print(corr)"
|
||||
],
|
||||
"id": "6bc6a850bf06cc9d",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -344,22 +335,34 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 55
|
||||
"source": [
|
||||
"print(\"Mean:\\n\")\n",
|
||||
"print(mean)\n",
|
||||
"print(\"\\nCovariance:\\n\")\n",
|
||||
"print(covar)\n",
|
||||
"print(\"\\nStandard Deviation:\\n\")\n",
|
||||
"print(std)\n",
|
||||
"print(\"\\nCorrelation:\\n\")\n",
|
||||
"print(corr)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"source": "# Question 1",
|
||||
"id": "fc4bec874f710f7c"
|
||||
"id": "fc4bec874f710f7c",
|
||||
"metadata": {},
|
||||
"source": "# Question 1"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"id": "780c9cca6e0ed2d3",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:53.113423Z",
|
||||
"start_time": "2024-11-25T13:43:53.109514Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"r = 0.02\n",
|
||||
"d = len(Tickers)\n",
|
||||
@@ -369,32 +372,18 @@
|
||||
"\n",
|
||||
"a = vec1.T.dot(inv_sigma).dot(vec1)\n",
|
||||
"b = mean.T.dot(inv_sigma).dot(vec1)"
|
||||
],
|
||||
"id": "780c9cca6e0ed2d3",
|
||||
"outputs": [],
|
||||
"execution_count": 56
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"id": "81c956f147c68070",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:54.545400Z",
|
||||
"start_time": "2024-11-25T13:43:54.541579Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Tangent portfolio\n",
|
||||
"pi_T = inv_sigma.dot(mean - r * vec1) / (b - r * a)\n",
|
||||
"sd_T = np.sqrt(pi_T.T.dot(sigma).dot(pi_T)) # Variance\n",
|
||||
"m_T = pi_T.T.dot(mean) # expected return\n",
|
||||
"\n",
|
||||
"print(f\"Expected return m_T: {m_T}\")\n",
|
||||
"print(f\"Standard deviation sd_T: {sd_T}\")\n",
|
||||
"print(f\"Allocation pi_T: {pi_T}\")\n",
|
||||
"print(\n",
|
||||
" f\"We can verify that the allocation is possible as the sum of the allocations for the different indices is {sum(pi_T)}, that is very close to 1\")"
|
||||
],
|
||||
"id": "81c956f147c68070",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -407,32 +396,36 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 57
|
||||
"source": [
|
||||
"# Tangent portfolio\n",
|
||||
"pi_T = inv_sigma.dot(mean - r * vec1) / (b - r * a)\n",
|
||||
"sd_T = np.sqrt(pi_T.T.dot(sigma).dot(pi_T)) # Variance\n",
|
||||
"m_T = pi_T.T.dot(mean) # expected return\n",
|
||||
"\n",
|
||||
"print(f\"Expected return m_T: {m_T}\")\n",
|
||||
"print(f\"Standard deviation sd_T: {sd_T}\")\n",
|
||||
"print(f\"Allocation pi_T: {pi_T}\")\n",
|
||||
"print(\n",
|
||||
" f\"We can verify that the allocation is possible as the sum of the allocations for the different indices is {sum(pi_T)}, that is very close to 1\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"source": "# Question 2",
|
||||
"id": "2e121c2dfb946f3c"
|
||||
"id": "2e121c2dfb946f3c",
|
||||
"metadata": {},
|
||||
"source": "# Question 2"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"id": "c169808384ca1112",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:43:59.797115Z",
|
||||
"start_time": "2024-11-25T13:43:59.792462Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"for i in range(len(std)):\n",
|
||||
" print(f\"The annualized volatilities of the index {Tickers[i]} is {std[i]}\")\n",
|
||||
" print(f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\")\n",
|
||||
" print(\"\")\n",
|
||||
"\n",
|
||||
"print(f\"The annualized volatility of the Tangent Portfolio is {sd_T * np.sqrt(252)}\")\n",
|
||||
"print(f\"The annualized expected return of the Tangent Portfolio is {m_T * 252}\")"
|
||||
],
|
||||
"id": "c169808384ca1112",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -455,29 +448,34 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 58
|
||||
"source": [
|
||||
"for i in range(len(std)):\n",
|
||||
" print(f\"The annualized volatilities of the index {Tickers[i]} is {std[i]}\")\n",
|
||||
" print(\n",
|
||||
" f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\"\n",
|
||||
" )\n",
|
||||
" print(\"\")\n",
|
||||
"\n",
|
||||
"print(f\"The annualized volatility of the Tangent Portfolio is {sd_T * np.sqrt(252)}\")\n",
|
||||
"print(f\"The annualized expected return of the Tangent Portfolio is {m_T * 252}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"source": "# Question 3",
|
||||
"id": "af8d29ecdbf2ae1"
|
||||
"id": "af8d29ecdbf2ae1",
|
||||
"metadata": {},
|
||||
"source": "# Question 3"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"id": "2e0215ab7904906a",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-11-25T13:44:01.393591Z",
|
||||
"start_time": "2024-11-25T13:44:01.388830Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"print(\"sharpe ratio of the Tangent portfolio :\", (m_T - r) / sd_T)\n",
|
||||
"\n",
|
||||
"for i in range(4):\n",
|
||||
" print(f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\")"
|
||||
],
|
||||
"id": "2e0215ab7904906a",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -491,7 +489,14 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 59
|
||||
"source": [
|
||||
"print(\"sharpe ratio of the Tangent portfolio :\", (m_T - r) / sd_T)\n",
|
||||
"\n",
|
||||
"for i in range(4):\n",
|
||||
" print(\n",
|
||||
" f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\"\n",
|
||||
" )"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user