-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from Ayush-iitkgp/pull-request/d6caa548
Added solution Unconstrained Markowitz Efficient Frontier example
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions
97
examples/portfolio_optimization3_unconstrained_markowitz_efficient_frontier.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Portfolio Optimization - Unconstrained Markowitz Efficient Frontier \n", | ||
"\n", | ||
"In this problem, we will find the unconstrained portfolio allocation where we introduce the weighting parameter $\\lambda(0 \\leq \\lambda \\leq$ 1)and minimize the $\\lambda * risk - (1-\\lambda)* return$. By varying the values of $\\lambda$, we trace out the efficient frontier. \n", | ||
"\n", | ||
"Suppose that we know the mean returns $R \\in \\mathbf{R}^n$ of each asset and the covariance $Q \\in \\mathbf{R}^{n \\times n}$ between the assets. Our objective is to find a portfolio allocation that minimizes the *risk* (which we measure as the variance $w^T Q w$) and maximizes the *return* ($w^T R$) of the portfolio of the simulataneously. We suppose further that our portfolio allocation must comply with some lower and upper bounds on the allocation, $w_\\mbox{lower} \\leq w \\leq w_\\mbox{upper}$ and also $w \\in \\mathbf{R}^n$ $\\sum_i w_i = 1$.\n", | ||
"\n", | ||
"This problem can be written as\n", | ||
"\n", | ||
"\\begin{array}{ll}\n", | ||
" \\mbox{minimize} & \\lambda*w^T Q w - (1-\\lambda)*w^T R \\\\\n", | ||
" \\mbox{subject to} & \\sum_i w_i = 1 \\\\\n", | ||
" & w_\\mbox{lower} \\leq w \\leq w_\\mbox{upper}\n", | ||
"\\end{array}\n", | ||
"\n", | ||
"where $w \\in \\mathbf{R}^n$ is the vector containing weights allocated to each asset in the efficient frontier.\n", | ||
"\n", | ||
"We can solve this problem as follows." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"using Convex, ECOS #We are using ECOS solver. Install using Pkg.add(\"ECOS\")\n", | ||
"\n", | ||
"# generate problem data\n", | ||
"srand(0); #Set the seed\n", | ||
"n = 5; # Assume that we have portfolio of 5 assets.\n", | ||
"R = 5 * randn(n);\n", | ||
"A = randn(n, 5);\n", | ||
"Q = A * A' + diagm(rand(n));\n", | ||
"w_lower = 0;\n", | ||
"w_upper = 1;\n", | ||
"\n", | ||
"\n", | ||
"risk = zeros(2000); # Initialized the risk and the return vectors. \n", | ||
"ret = zeros(2000); # lambda varies in the interval(0,1) in the steps of 1/2000.\n", | ||
"\n", | ||
"w = Variable(length(R));\n", | ||
"\n", | ||
"#Defining constraints\n", | ||
"c1 = sum(w) == 1;\n", | ||
"c2 = w_lower <= x; \n", | ||
"c3 = w <= w_upper;\n", | ||
"for i in 1:2000\n", | ||
" λ = i/2000;\n", | ||
"\n", | ||
" #Defining Objective function\n", | ||
" objective = λ * quadform(w,Q) - (1-λ)* w' *R;\n", | ||
" p = minimize(objective, c1,c2,c3);\n", | ||
" solve!(p, ECOSSolver(verbose = false));\n", | ||
" risk[i] = (w.value' * Q * w.value)[1];\n", | ||
" ret[i] = (w.value'R)[1];\n", | ||
" #println(\"$i \",\"$(λ*risk[i] - (1-λ)*ret[i]) \",\"$p.optval\");\n", | ||
" end\n", | ||
"\n", | ||
"using PyPlot #Install PyPlot if you don't have it installed. Pkg.add(\"PyPlot\")\n", | ||
"plot(risk,ret)\n", | ||
"title(\"Markowitz Efficient Frontier\");\n", | ||
"xlabel(\"Expected Risk-Variance\");\n", | ||
"ylabel(\"Expected Return\");" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<img src=\"efficient_frontier.png\">" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Julia 0.5.0-dev", | ||
"language": "julia", | ||
"name": "julia-0.5" | ||
}, | ||
"language_info": { | ||
"file_extension": ".jl", | ||
"mimetype": "application/julia", | ||
"name": "julia", | ||
"version": "0.5.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |