From 85b83e174583101506713ee5045ee8df632fd407 Mon Sep 17 00:00:00 2001 From: Michael Krabbe Borregaard Date: Tue, 30 Oct 2018 11:40:25 +0100 Subject: [PATCH] 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