Skip to content

Commit

Permalink
Update chapter 6 notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrozocca committed Oct 23, 2023
1 parent c9b11e0 commit b39db29
Show file tree
Hide file tree
Showing 7 changed files with 1,249 additions and 951 deletions.
290 changes: 129 additions & 161 deletions notebooks/05/svm.ipynb

Large diffs are not rendered by default.

174 changes: 90 additions & 84 deletions notebooks/06/building-insulation.ipynb

Large diffs are not rendered by default.

777 changes: 538 additions & 239 deletions notebooks/06/economic-order-quantity.ipynb

Large diffs are not rendered by default.

89 changes: 45 additions & 44 deletions notebooks/06/kelly-criterion.ipynb

Large diffs are not rendered by default.

89 changes: 61 additions & 28 deletions notebooks/06/markowitz_portfolio_revisited.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,39 @@
"\n",
"This cell selects and verifies a global SOLVER for the notebook.\n",
"\n",
"If run on Google Colab, the cell installs Pyomo and ipopt, then sets SOLVER to \n",
"use the ipopt solver. If run elsewhere, it assumes Pyomo and the Mosek solver\n",
"have been previously installed and sets SOLVER to use the Mosek solver via the Pyomo \n",
"SolverFactory. It then verifies that SOLVER is available."
"If run on Google Colab, the cell installs Pyomo and ipopt, then sets SOLVER to use the ipopt solver. If run elsewhere, it assumes Pyomo and the Mosek solver\n",
"have been previously installed and sets SOLVER to use the Mosek solver via the Pyomo SolverFactory. It then verifies that SOLVER is available."
]
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 9,
"id": "5da22c67-5c34-4c3a-90a4-61222899e855",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"\n",
"if 'google.colab' in sys.modules:\n",
" !pip install idaes-pse --pre >/dev/null 2>/dev/null\n",
" !idaes get-extensions --to ./bin \n",
" os.environ['PATH'] += ':bin'\n",
" SOLVER_CONIC = \"ipopt\"\n",
" \n",
" solver = \"ipopt\"\n",
"else:\n",
" SOLVER_CONIC = \"mosek_direct\"\n",
" solver = \"mosek_direct\"\n",
"\n",
"import pyomo.environ as pyo\n",
"if not pyo.SolverFactory(SOLVER_CONIC).available():\n",
" print(f\"Solver {SOLVER_CONIC} is not available\")"
"if not pyo.SolverFactory(solver).available():\n",
" print(f\"Solver {solver} is not available\")\n",
"else:\n",
" SOLVER = pyo.SolverFactory(solver)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 10,
"id": "b13edf26",
"metadata": {
"tags": []
Expand Down Expand Up @@ -114,7 +112,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 11,
"id": "0e194e8d",
"metadata": {
"tags": []
Expand Down Expand Up @@ -158,7 +156,7 @@
}
],
"source": [
"# Specify the initial capital, the risk tolerance, and the guaranteed return rate. \n",
"# Specify the initial capital, the risk tolerance, and the guaranteed return rate.\n",
"C = 1\n",
"alpha = 0.1\n",
"R = 1.05\n",
Expand All @@ -172,7 +170,7 @@
"assert np.all(np.linalg.eigvals(Sigma) >= 0)\n",
"\n",
"# If you want to change the covariance matrix Sigma, ensure you input a semi-definite positive one.\n",
"# The easiest way to generate a random covariance matrix is first generating a random m x m matrix A \n",
"# The easiest way to generate a random covariance matrix is first generating a random m x m matrix A\n",
"# and then taking the matrix A^T A (which is always semi-definite positive)\n",
"# m = 3\n",
"# A = np.random.rand(m, m)\n",
Expand All @@ -184,8 +182,8 @@
"# y=Ax, |y|^2 <= s,\n",
"# corresponding to the mathematical formulation above.\n",
"\n",
"\n",
"def markowitz_revisited(alpha, mu, Sigma):\n",
" \n",
" model = pyo.ConcreteModel(\"Markowitz portfolio optimization revisited\")\n",
"\n",
" model.xtilde = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand All @@ -194,7 +192,7 @@
"\n",
" @model.Objective(sense=pyo.maximize)\n",
" def objective(m):\n",
" return mu @ m.x + R * m.xtilde - alpha*m.s\n",
" return mu @ m.x + R * m.xtilde - alpha * m.s\n",
"\n",
" @model.Constraint()\n",
" def bounded_variance(m):\n",
Expand All @@ -204,20 +202,29 @@
" def total_assets(m):\n",
" return sum(m.x[i] for i in range(n)) + m.xtilde == C\n",
"\n",
" result = pyo.SolverFactory(SOLVER_CONIC).solve(model)\n",
" \n",
" result = SOLVER.solve(model)\n",
"\n",
" return result, model\n",
"\n",
"\n",
"result, model = markowitz_revisited(alpha, mu, Sigma)\n",
"\n",
"display(Markdown(f\"**Solver status:** *{result.solver.status}, {result.solver.termination_condition}*\"))\n",
"display(Markdown(f\"**Solution:** $\\\\tilde x = {model.xtilde.value:.3f}$, $x_1 = {model.x[0].value:.3f}$, $x_2 = {model.x[1].value:.3f}$, $x_3 = {model.x[2].value:.3f}$\"))\n",
"display(\n",
" Markdown(\n",
" f\"**Solver status:** *{result.solver.status}, {result.solver.termination_condition}*\"\n",
" )\n",
")\n",
"display(\n",
" Markdown(\n",
" f\"**Solution:** $\\\\tilde x = {model.xtilde.value:.3f}$, $x_1 = {model.x[0].value:.3f}$, $x_2 = {model.x[1].value:.3f}$, $x_3 = {model.x[2].value:.3f}$\"\n",
" )\n",
")\n",
"display(Markdown(f\"**Maximizes objective value to:** ${model.objective():.2f}$\"))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 12,
"id": "9a2b00d7-433a-46c8-a4b6-c451244c9b0f",
"metadata": {
"tags": []
Expand All @@ -235,17 +242,43 @@
}
],
"source": [
"alpha_values = [0.005, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.25, 0.3, 0.4, 0.5]\n",
"alpha_values = [\n",
" 0.005,\n",
" 0.01,\n",
" 0.02,\n",
" 0.03,\n",
" 0.04,\n",
" 0.05,\n",
" 0.06,\n",
" 0.07,\n",
" 0.08,\n",
" 0.09,\n",
" 0.1,\n",
" 0.11,\n",
" 0.12,\n",
" 0.13,\n",
" 0.14,\n",
" 0.15,\n",
" 0.16,\n",
" 0.17,\n",
" 0.18,\n",
" 0.19,\n",
" 0.20,\n",
" 0.25,\n",
" 0.3,\n",
" 0.4,\n",
" 0.5,\n",
"]\n",
"objective = []\n",
"\n",
"plt.rcParams.update({'font.size': 14})\n",
"plt.rcParams.update({\"font.size\": 14})\n",
"for alpha in alpha_values:\n",
" _, model = markowitz_revisited(alpha, mu, Sigma)\n",
" objective.append(round(model.objective(),3))\n",
" objective.append(round(model.objective(), 3))\n",
"\n",
"plt.plot(alpha_values, objective, color=plt.cm.tab20c(0))\n",
"plt.xlabel(r'Risk tolerance $\\alpha$')\n",
"plt.ylabel('Optimal objective value')\n",
"plt.xlabel(r\"Risk tolerance $\\alpha$\")\n",
"plt.ylabel(\"Optimal objective value\")\n",
"plt.tight_layout()\n",
"plt.show()"
]
Expand All @@ -267,7 +300,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.10.10"
},
"varInspector": {
"cols": {
Expand Down
537 changes: 274 additions & 263 deletions notebooks/06/optimal-growth-portfolios.ipynb

Large diffs are not rendered by default.

244 changes: 112 additions & 132 deletions notebooks/06/svm-conic.ipynb

Large diffs are not rendered by default.

0 comments on commit b39db29

Please sign in to comment.