Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc; close all; clear all;
- data = csvread('returns.dat',1,1); % reads in the monthly returns data file for all 19 stock indices
- indices = [1, 3, 4 , 6, 8, 9, 10, 14, 16, 18]; % The indicies allocated to me
- subset=data(:,indices); % your subset of the data
- training=subset(1:25,:); % take first 25 months as training data to optimise portfolio weights
- testing=subset(26:49,:); % last 24 months are test data to evaluate performance
- N=10; % number of stock indices considered in portfolio
- T=25; % months in training data
- Ttest=24; % months in testing data
- C=1000; % dollar budget constraint
- R=0.005; % return threshold, to be varied
- u = .50; % maximum percentage an index can hold in each portfolio
- %% %%%%%%%%%%%%%%%%%%%%%%%%%%%% QP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Now write some code that defines the matrices and vectors needed to solve
- % the quadratic programming problem to minimise variance. You need to
- % define H, f, A, b, Aeq, beq, LB, UB in the quadprog function
- [leng,wid] = size(training); %get the dimensions of the training set to use in the loop
- H = cov(training); %H matrix
- f = zeros(1,N)'; %f vector
- A = -1 .* sum(training)/leng; %inequality constraints
- b = [-R*C]; %inequality constraints
- Aeq = ones(1,N); %equality constraints
- beq = C; %equailty constraints
- LB = zeros(1,N); %lower bound is zero
- UB = repmat(u*C,1,N); %upper bound is mu x C
- options = optimset( 'Algorithm', 'interior-point-convex','Display','off');
- [xQP,FVAL,EXITFLAG]=quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
- if EXITFLAG == 1;
- FVAL=round(FVAL/0.01)*0.01;
- else
- FVAL=0;
- end %report to two decimal places
- %Now experiment with R to find the range of values (from -5% to 5% in steps of 0.1) that gives a feasible solution
- % with minimised variance. Lock in the largest value of R that minimises variance on the
- %training data. Calculate x (the allocations) with that R value.
- %initialising variables for data collection in the for loop
- Rval = -0.05:0.001:0.05;
- x = zeros(10,length(Rval));
- FVAL = zeros(length(Rval),1);
- i=1;
- for R = -0.05:0.001:0.05
- b = -R*C;
- [x(:,i),f_value,EXITFLAG]=quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
- if EXITFLAG == 1;
- FVAL(i,1)=round(f_value/0.01)*0.01; %report to two decimal places
- else
- FVAL(i)=0;
- end
- disp(R)
- i=i+1;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement