From 85b83e174583101506713ee5045ee8df632fd407 Mon Sep 17 00:00:00 2001 From: Michael Krabbe Borregaard Date: Tue, 30 Oct 2018 11:40:25 +0100 Subject: [PATCH 1/3] add portfoliocomposition --- src/examples.jl | 18 ++++++++++++++++++ src/recipes.jl | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/examples.jl b/src/examples.jl index 7050b1e3..acb6688e 100644 --- a/src/examples.jl +++ b/src/examples.jl @@ -435,6 +435,22 @@ each line segment or marker in the plot. end)] ), +PlotExample("Portfolio Composition maps", +""" +see: http://stackoverflow.com/a/37732384/5075246 +""", + [:(begin + tickers = ["IBM", "Google", "Apple", "Intel"] + N = 10 + D = length(tickers) + weights = rand(N,D) + weights ./= sum(weights, 2) + returns = sort!((1:N) + D*randn(N)) + + portfoliocomposition(weights, returns, labels = permutedims(tickers)) + end)] +), + ] # Some constants for PlotDocs and PlotReferenceImages @@ -446,6 +462,8 @@ _backend_skips = Dict( :pgfplots => [2, 5, 6, 10, 16, 20, 22, 23, 25, 28, 30], ) + + # --------------------------------------------------------------------------------- # make and display one plot diff --git a/src/recipes.jl b/src/recipes.jl index 6ac0d11d..4b513a4b 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -1131,3 +1131,25 @@ end title := string(grad.args[1]) z end + + +# Moved in from PlotRecipes - see: http://stackoverflow.com/a/37732384/5075246 +@userplot PortfolioComposition + +# this shows the shifting composition of a basket of something over a variable +# - "returns" are the dependent variable +# - "weights" are a matrix where the ith column is the composition for returns[i] +# - since each polygon is its own series, you can assign labels easily +@recipe function f(pc::PortfolioComposition) + weights, returns = pc.args + n = length(returns) + weights = cumsum(weights,2) + seriestype := :shape + + # create a filled polygon for each item + for c=1:size(weights,2) + sx = vcat(weights[:,c], c==1 ? zeros(n) : reverse(weights[:,c-1])) + sy = vcat(returns, reverse(returns)) + @series Plots.isvertical(plotattributes) ? (sx, sy) : (sy, sx) + end +end From 5e06e0d2d2f8ce70c3ae69e0c135ddedf4ac89c2 Mon Sep 17 00:00:00 2001 From: Michael Krabbe Borregaard Date: Tue, 30 Oct 2018 12:15:07 +0100 Subject: [PATCH 2/3] fix example --- src/examples.jl | 2 +- src/recipes.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/examples.jl b/src/examples.jl index acb6688e..8e05dec6 100644 --- a/src/examples.jl +++ b/src/examples.jl @@ -444,7 +444,7 @@ see: http://stackoverflow.com/a/37732384/5075246 N = 10 D = length(tickers) weights = rand(N,D) - weights ./= sum(weights, 2) + weights ./= sum(weights, dims = 2) returns = sort!((1:N) + D*randn(N)) portfoliocomposition(weights, returns, labels = permutedims(tickers)) diff --git a/src/recipes.jl b/src/recipes.jl index 4b513a4b..5ae3cebf 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -1143,7 +1143,7 @@ end @recipe function f(pc::PortfolioComposition) weights, returns = pc.args n = length(returns) - weights = cumsum(weights,2) + weights = cumsum(weights, dims = 2) seriestype := :shape # create a filled polygon for each item From 759cd7b3f2369c8d39bdb61f2cfe2240d5bca02d Mon Sep 17 00:00:00 2001 From: Michael Krabbe Borregaard Date: Tue, 30 Oct 2018 14:05:18 +0100 Subject: [PATCH 3/3] add seed! --- src/examples.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/examples.jl b/src/examples.jl index 8e05dec6..65737326 100644 --- a/src/examples.jl +++ b/src/examples.jl @@ -440,6 +440,8 @@ PlotExample("Portfolio Composition maps", see: http://stackoverflow.com/a/37732384/5075246 """, [:(begin + using Random + Random.seed!(111) tickers = ["IBM", "Google", "Apple", "Intel"] N = 10 D = length(tickers)