Skip to content

Commit

Permalink
Fix milk pooling and forex arbitrage notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrozocca committed Oct 27, 2023
1 parent 8bff3b2 commit 7c3d79b
Show file tree
Hide file tree
Showing 5 changed files with 1,225 additions and 807 deletions.
280 changes: 140 additions & 140 deletions notebooks/04/forex-arbitrage.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion notebooks/04/graph-coloring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"```{index} networkx\n",
"```\n",
"\n",
"# Scheduling as a graph coloring problem"
"# Exam room scheduling"
]
},
{
Expand Down
691 changes: 370 additions & 321 deletions notebooks/04/tsp.ipynb

Large diffs are not rendered by default.

76 changes: 63 additions & 13 deletions notebooks/05/markowitz_portfolio.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"import sys, os\n",
"\n",
"if 'google.colab' in sys.modules:\n",
" !pip install idaes-pse --pre >/dev/null 2>/dev/null\n",
Expand Down Expand Up @@ -165,7 +164,7 @@
}
],
"source": [
"# Specify the initial capital, the risk threshold, and the guaranteed return rate. \n",
"# Specify the initial capital, the risk threshold, and the guaranteed return rate.\n",
"C = 1\n",
"gamma = 1\n",
"R = 1.01\n",
Expand All @@ -179,14 +178,14 @@
"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",
"# Sigma = A.T @ A\n",
"\n",
"\n",
"def markowitz(gamma, mu, Sigma):\n",
" \n",
" model = pyo.ConcreteModel(\"Markowitz portfolio optimization\")\n",
"\n",
" model.xtilde = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand All @@ -205,13 +204,22 @@
" return sum(m.x[i] for i in range(n)) + m.xtilde == C\n",
"\n",
" result = pyo.SolverFactory(SOLVER).solve(model)\n",
" \n",
"\n",
" return result, model\n",
"\n",
"\n",
"result, model = markowitz(gamma, 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}$\"))"
]
},
Expand Down Expand Up @@ -241,17 +249,59 @@
}
],
"source": [
"gamma_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, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 4.75, 5, 5.25, 5.5]\n",
"gamma_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",
" 0.6,\n",
" 0.7,\n",
" 0.8,\n",
" 0.9,\n",
" 1,\n",
" 1.5,\n",
" 2,\n",
" 2.5,\n",
" 3,\n",
" 3.5,\n",
" 4,\n",
" 4.5,\n",
" 4.75,\n",
" 5,\n",
" 5.25,\n",
" 5.5,\n",
"]\n",
"objective = []\n",
"\n",
"plt.rcParams.update({'font.size': 14})\n",
"plt.rcParams.update({\"font.size\": 14})\n",
"for gamma in gamma_values:\n",
" _, model = markowitz(gamma, mu, Sigma)\n",
" objective.append(round(model.objective(),3))\n",
" objective.append(round(model.objective(), 3))\n",
"\n",
"plt.plot(gamma_values, objective, color=plt.cm.tab20c(0))\n",
"plt.xlabel(r'Risk threshold $\\gamma$')\n",
"plt.ylabel('Optimal objective value')\n",
"plt.xlabel(r\"Risk threshold $\\gamma$\")\n",
"plt.ylabel(\"Optimal objective value\")\n",
"plt.tight_layout()\n",
"plt.show()"
]
Expand Down
Loading

0 comments on commit 7c3d79b

Please sign in to comment.