Refactor code for improved readability and consistency across multiple Jupyter notebooks

- Added missing commas in various print statements and function calls for better syntax.
- Reformatted code to enhance clarity, including breaking long lines and aligning parameters.
- Updated function signatures to use float type for sigma parameters instead of int for better precision.
- Cleaned up comments and documentation strings for clarity and consistency.
- Ensured consistent formatting in plotting functions and data handling.
This commit is contained in:
2025-12-13 23:38:17 +01:00
parent f89ff4a016
commit d5a6bfd339
50 changed files with 779 additions and 449 deletions

View File

@@ -27,9 +27,10 @@
},
"outputs": [],
"source": [
"import yfinance as yf\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import yfinance as yf"
"import pandas as pd"
]
},
{
@@ -406,7 +407,7 @@
"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",
" 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",
")"
]
},
@@ -452,9 +453,9 @@
"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",
" f\"The annualized expected returns of the index {Tickers[i]} is {mean[Tickers[i]]}\",\n",
" )\n",
" print(\"\")\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}\")"
@@ -494,7 +495,7 @@
"\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",
" f\"the sharpe ratio of the index {Tickers[i]} is {(mean[Tickers[i]] - r) / std[i]}\",\n",
" )"
]
}

View File

@@ -13,9 +13,10 @@
},
"outputs": [],
"source": [
"import yfinance as yf\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import yfinance as yf"
"import pandas as pd"
]
},
{
@@ -530,7 +531,7 @@
"\n",
"# Self financing portfolio\n",
"m_w = np.sqrt(\n",
" (mean - b / a * vec1).T.dot(inv_sigma).dot(mean - b / a * vec1)\n",
" (mean - b / a * vec1).T.dot(inv_sigma).dot(mean - b / a * vec1),\n",
") # Expected return\n",
"\n",
"# Tangent portfolio\n",
@@ -580,7 +581,7 @@
"range_sup = np.max(mean) + 1\n",
"y = np.linspace(range_inf, range_sup, 50)\n",
"x_1 = np.array(\n",
" [np.sqrt(((y - m_a) / m_w) ** 2 + sd_a**2)]\n",
" [np.sqrt(((y - m_a) / m_w) ** 2 + sd_a**2)],\n",
") # Sigma values for the frontier\n",
"x_2 = np.array([(y - r) / (m_T - r) * sd_T]) # Sigma values for the Capital Market Line\n",
"\n",