-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Documenter.jl
committed
Dec 4, 2023
1 parent
35e9145
commit ff7adbf
Showing
15 changed files
with
1,219 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,65 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Markowitz Efficient Frontier · ParametricOptInterface.jl</title><script data-outdated-warner src="../../assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.045/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.13.24/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="../.."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../../assets/documenter.js"></script><script src="../../siteinfo.js"></script><script src="../../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../../assets/themeswap.js"></script><link href="../../assets/favicon.ico" rel="icon" type="image/x-icon"/></head><body><div id="documenter"><nav class="docs-sidebar"><div class="docs-package-name"><span class="docs-autofit"><a href="../../">ParametricOptInterface.jl</a></span></div><form class="docs-search" action="../../search/"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li><a class="tocitem" href="../../">Home</a></li><li><a class="tocitem" href="../../manual/">Manual</a></li><li><span class="tocitem">Examples</span><ul><li><a class="tocitem" href="../example/">Basic Examples</a></li><li><a class="tocitem" href="../benders/">Benders Quantile Regression</a></li><li class="is-active"><a class="tocitem" href>Markowitz Efficient Frontier</a></li></ul></li><li><a class="tocitem" href="../../reference/">Reference</a></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li><a class="is-disabled">Examples</a></li><li class="is-active"><a href>Markowitz Efficient Frontier</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Markowitz Efficient Frontier</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com/jump-dev/ParametricOptInterface.jl/blob/master/docs/src/Examples/markowitz.md" title="Edit on GitHub"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch">Edit on GitHub</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="Markowitz-Efficient-Frontier"><a class="docs-heading-anchor" href="#Markowitz-Efficient-Frontier">Markowitz Efficient Frontier</a><a id="Markowitz-Efficient-Frontier-1"></a><a class="docs-heading-anchor-permalink" href="#Markowitz-Efficient-Frontier" title="Permalink"></a></h1><p>In this example, we solve the classical portfolio problem where we introduce the weight parameter <span>$\gamma$</span> and maximize <span>$\gamma \text{ risk} - \text{expected return}$</span>. By updating the values of <span>$\gamma$</span> we trace the efficient frontier.</p><p>Given the prices changes with mean <span>$\mu$</span> and covariance <span>$\Sigma$</span>, we can construct the classical portfolio problem:</p><p class="math-container">\[\begin{array}{ll} | ||
\text{maximize} & \gamma* x^T \mu - x^T \Sigma x \\ | ||
\text{subject to} & \| x \|_1 = 1 \\ | ||
& x \succeq 0 | ||
\end{array}\]</p><p>The problem data was gotten from the example <a href="https://jump.dev/Convex.jl/dev/examples/portfolio_optimization/portfolio_optimization2/">portfolio optimization</a></p><pre><code class="language-julia hljs">using ParametricOptInterface, MathOptInterface, JuMP, Ipopt | ||
using LinearAlgebra, Plots | ||
|
||
const POI = ParametricOptInterface | ||
const MOI = MathOptInterface | ||
|
||
# generate problem data | ||
μ = [11.5; 9.5; 6] / 100 #expected returns | ||
Σ = [ | ||
166 34 58 #covariance matrix | ||
34 64 4 | ||
58 4 100 | ||
] / 100^2 | ||
</code></pre><p>We first build the model with <span>$\gamma$</span> as parameter in POI</p><pre><code class="language-julia hljs">function first_model(μ,Σ) | ||
cached = MOI.Bridges.full_bridge_optimizer( | ||
MOIU.CachingOptimizer( | ||
MOIU.UniversalFallback(MOIU.Model{Float64}()), | ||
Ipopt.Optimizer(), | ||
), | ||
Float64, | ||
) | ||
optimizer = POI.Optimizer(cached) | ||
portfolio = direct_model(optimizer) | ||
set_silent(portfolio) | ||
|
||
N = length(μ) | ||
@variable(portfolio, x[1:N] >= 0) | ||
@variable(portfolio, γ in MOI.Parameter(0.0)) | ||
|
||
@objective(portfolio, Max, γ*dot(μ,x) - x' * Σ * x) | ||
@constraint(portfolio, sum(x) == 1) | ||
optimize!(portfolio) | ||
|
||
return portfolio | ||
end</code></pre><p>Then, we update the <span>$\gamma$</span> value in the model</p><pre><code class="language-julia hljs">function update_model!(portfolio,γ_value) | ||
γ = portfolio[:γ] | ||
MOI.set(portfolio, POI.ParameterValue(), γ, γ_value) | ||
optimize!(portfolio) | ||
return portfolio | ||
end</code></pre><p>Collecting all the return and risk resuls for each <span>$\gamma$</span></p><pre><code class="language-julia hljs">function add_to_dict(portfolios_values,portfolio,μ,Σ) | ||
γ = portfolio[:γ] | ||
γ_value = value(γ) | ||
x = portfolio[:x] | ||
x_value = value.(x) | ||
portfolio_return = dot(μ,x_value) | ||
portfolio_deviation = x_value' * Σ * x_value | ||
portfolios_values[γ_value] = (portfolio_return,portfolio_deviation) | ||
end</code></pre><p>Run the portfolio optimization for different values of <span>$\gamma$</span></p><pre><code class="language-julia hljs">portfolio = first_model(μ,Σ) | ||
portfolios_values = Dict() | ||
# Create a reference to the model to change it later | ||
portfolio_ref = [portfolio] | ||
add_to_dict(portfolios_values,portfolio,μ,Σ) | ||
|
||
for γ_value in 0.02:0.02:1.0 | ||
portfolio_ref[] = update_model!(portfolio_ref[],γ_value) | ||
add_to_dict(portfolios_values,portfolio_ref[],μ,Σ) | ||
end</code></pre><p>Plot the efficient frontier</p><pre><code class="language-julia hljs">portfolios_values = sort(portfolios_values,by=x->x[1]) | ||
portfolios_values_matrix = hcat([[v[1],v[2]] for v in values(portfolios_values)]...)' | ||
plot(portfolios_values_matrix[:,2],portfolios_values_matrix[:,1],legend=false, | ||
xlabel="Standard Deviation", ylabel = "Return", title = "Efficient Frontier")</code></pre></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../benders/">« Benders Quantile Regression</a><a class="docs-footer-nextpage" href="../../reference/">Reference »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 0.27.25 on <span class="colophon-date" title="Monday 4 December 2023 01:01">Monday 4 December 2023</span>. Using Julia version 1.6.7.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
Oops, something went wrong.