## matlabtest01.m listpX0 = 1; doMatLabTest01 = 0; ## get 2014 data oneBdata2014; # funds, assets, adjclose ## funds n = rows(funds); # n = 243 tickers = funds(:, 1:4); names = funds(:, 8 : columns(funds)); ## adjusted closing prices and dates M = rows(adjclose); # M = 253 unixtime = adjclose(:, 1); adjclose(:, 1) = []; growth = adjclose(M, :) ./ adjclose(1, :); date = zeros(0, 10); for i = 1 : length(unixtime) date = [date; strftime("%Y-%m-%d", localtime(unixtime(i)))]; endfor ## normalized adjusted closing price differences R0 = diff(adjclose) * diag(1 ./ adjclose(1, :)); ## do rtndecomp and minnormy pput = 252; # market days per year [E, F, f0] = rtndecomp(R0, 0, pput); P = minnormy(F); nP = columns(P); EP = E * P; ## covariance and standard deviation of return V = f0 ^ 2 + F' * F; Sig = sqrt(diag(V))'; condition_V = cond(V); # condition_V = 1.2734e+08 ## get the efficient portfolio pX with 15% total return in 2014 eX = 0.15; k0 = nP; while EP(k0) >= eX k0--; endwhile k1 = k0 + 1; t0 = (EP(k1) - eX)/(EP(k1) - EP(k0)); t1 = 1 - t0; pX0 = P(:, k0) * t0 + P(:, k1) * t1; JX = [1 : n](pX0 > 0); ## list efficient, 2013-12-31-close portfolio pX0 if listpX0 printf("The efficient, 2013-12-31-close portfolio with return %.2f%% in 2014.\n", 100 * eX); printf("This portfolio was selected from 243 exchange traded funds/notes with\n"); printf("at least $1 billion in assets under management at the end of 2014.\n"); printf("The funds are listed in decreasing order of assets under management.\n"); for j = JX printf("%3d. %.5f %-s\n", j, pX0(j), deblank(funds(j, :))); endfor endif #### MatLab test #### # as per # http://www.mathworks.com/help/finance/estimate-efficient-portfolios_bswmb3d-1.html if doMatLabTest01 pM = Portfolio; pM = setAssetMoments(pM, E', V); pM = setDefaultConstraints(pM); pMwgt = estimateFrontierByReturn(pM, eX); endif ## The portfolio pMwgt should be (approximately) the same as pX0 above.