Skip to content

Commit

Permalink
Update ch9 Markovitz portfolio notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrozocca committed Nov 28, 2023
1 parent fe36f98 commit 9926d67
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions notebooks/09/02-markowitz-portfolio-with-chance-constraint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"id": "0e194e8d",
"metadata": {
"tags": []
Expand All @@ -118,49 +118,33 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Solver status: ok, optimal\n",
"Solution:"
]
},
{
"data": {
"text/markdown": [
"$\\tilde x = -0.000$, $x_1 = 0.667$, $x_2 = 0.255$, $x_3 = 0.078$"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solver status: ok, Termination condition: optimal\n",
"Solution: xtilde = 0.070, x_1 = 0.620, x_2 = 0.239, x_3 = 0.071\n",
"Maximum objective value: 1.23\n"
]
}
],
"source": [
"# set our risk threshold and risk levels (sometimes you may get an infeasible problem if the chance\n",
"# constraint becomes too tight!)\n",
"# set our risk threshold and risk levels (sometimes you may get an infeasible\n",
"# problem if the chance constraint becomes too tight!)\n",
"alpha = 0.6\n",
"beta = 0.3\n",
"\n",
"# specify the initial capital, the risk-free return the number of risky assets, their expected returns, and their covariance matrix.\n",
"# specify the initial capital, the risk-free return the number of risky assets,\n",
"# their expected returns, and their covariance matrix.\n",
"C = 1\n",
"R = 1.05\n",
"R = 1.25\n",
"n = 3\n",
"mu = np.array([1.25, 1.15, 1.35])\n",
"Sigma = np.array([[1.5, 0.5, 2], [0.5, 2, 0], [2, 0, 5]])\n",
"\n",
"# Check how dramatically the optimal solution changes if we assume i.i.d. deviations for the returns.\n",
"# Sigma = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\n",
"# Check how dramatically the optimal solution changes if we assume i.i.d.\n",
"# deviations for the returns. # Sigma = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\n",
"\n",
"# To change covariance matrix, make sure 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",
"# and then taking the matrix A^T A (which is always semi-definite positive)\n",
"# The easiest way to generate a random covariance matrix is first generating\n",
"# a random m x m matrix A and then taking the matrix A^T A (which is always\n",
"# semi-definite positive)\n",
"# m = 3\n",
"# A = np.random.rand(m, m)\n",
"# Sigma = A.T @ A\n",
Expand All @@ -174,7 +158,7 @@
"\n",
" @model.Objective(sense=pyo.maximize)\n",
" def objective(m):\n",
" return mu @ m.x\n",
" return mu @ m.x + R * m.xtilde\n",
"\n",
" @model.Constraint()\n",
" def chance_constraint(m):\n",
Expand All @@ -191,12 +175,12 @@
"model = markowitz_chanceconstraints(alpha, beta, mu, Sigma)\n",
"result = SOLVER.solve(model)\n",
"\n",
"print(f\"Solver status: {result.solver.status}, {result.solver.termination_condition}\")\n",
"print(f\"Solution:\", end=\"\")\n",
"display(\n",
" Markdown(\n",
" f\"$\\\\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",
"print(\n",
" f\"Solver status: {result.solver.status}, Termination condition: {result.solver.termination_condition}\"\n",
")\n",
"print(f\"Solution: \", end=\"\")\n",
"print(\n",
" f\"xtilde = {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",
"print(f\"Maximum objective value: {model.objective():.2f}\")"
]
Expand Down

0 comments on commit 9926d67

Please sign in to comment.