function mpt_invest(investments,lb,ub,min,max); addpath ("/usr/share/octave/site/api-v22/m/octave2.9-forge/io") #Plot point styles style = {'*','+','o','x','s'}; stylecount = 5; # Count the total number of investmentss count = rows(investments); # Set initial value x0 = (ub-lb)/2+lb; x0 = x0*1/sum(x0); # Calculate investments with smallest number of data points points = inf; for i =1:rows(investments) tmp = csvread(investments{i,:},1); if rows(tmp) < points points = rows(tmp); end end # Read in each investments, crop to min size and store column 9 (return) in = zeros(points,rows(investments)); for i =1:rows(investments) tmp = csvread(investments{i,:},1); # Column 9 has adjusted return tmp = tmp(1:points,9); in(:,i) = tmp; end; # Calculate percentage return for each months close points=rows(in); py = zeros(points-1,count); for a=1:count for b=1:points-1 py(b,a) = (in(b,a)-in(b+1,a))/in(b+1,a)*100; end; end; # Calculate mean, sd, correlation, etc. e = mean(py)'; sd = std(py)'; cc = corrcoef(py); C = (sd*sd').*cc; steps = 10; axis = 1:steps; axis = axis.*(max-min)/steps + min; figure(1); clearplot hold on y=[]; for i=1:steps rt = axis(i); [tmp, e_mix(i), sd_mix(i)] = gqp(rt,e,C,lb,ub,x0); y(i,:) = tmp'; end; plot(sd_mix,e_mix,"-@;PORTFOLIO;"); for a=1:rows(sd) tmp = ["@",style{mod(a,stylecount)+1},dec2base(mod(floor(a/stylecount)+1,7),10),";", investments{a,:}, ";"]; plot(sd(a),e(a),tmp) end; xlabel('Standard Deviation') ylabel('Return') figure(2); clearplot hold on for a=1:columns(y) # tmp = ["@",dec2base(a+7,7),";", investments{a,:}, ";"]; tmp = ["-@",style{mod(a,stylecount)+1},dec2base(mod(floor(a/stylecount)+1,7),10),";", investments{a,:}, ";"]; plot(axis,y(:,a),tmp); end; xlabel('Risk') ylabel('Portfolio Percentage') hold off for a=1:rows(sd) printf("%s %f\n",investments{a,:},y(steps/2,a)); end; sd_expected = sd_mix(steps/2) e_expected = e_mix(steps/2) end;