Search code examples
matlabplotmatlab-figurejuliagadfly

Plot portfolio composition map in Julia (or Matlab)


I am optimizing portfolio of N stocks over M levels of expected return. So after doing this I get the time series of weights (i.e. a N x M matrix where where each row is a combination of stock weights for a particular level of expected return). Weights add up to 1.

Now I want to plot something called portfolio composition map (right plot on the picture), which is a plot of these stock weights over all levels of expected return, each with a distinct color and length (at every level of return) is proportional to it's weight.

enter image description here

My questions is how to do this in Julia (or MATLAB)?


Solution

  • I came across this and the accepted solution seemed so complex. Here's how I would do it:

    using Plots
    @userplot PortfolioComposition
    
    @recipe function f(pc::PortfolioComposition)
        weights, returns = pc.args
        weights = cumsum(weights,dims=2)
        seriestype := :shape
        for c=1:size(weights,2)
            sx = vcat(weights[:,c], c==1 ? zeros(length(returns)) : reverse(weights[:,c-1]))
            sy = vcat(returns, reverse(returns))
            @series Shape(sx, sy)
        end
    end
    
    # fake data
    tickers = ["IBM", "Google", "Apple", "Intel"]
    N = 10
    D = length(tickers)
    weights = rand(N,D)
    weights ./= sum(weights, dims=2)
    returns = sort!((1:N) + D*randn(N))
    
    # plot it
    portfoliocomposition(weights, returns, labels = tickers)
    

    enter image description here