Advertisement
Guest User

Aaron's Part 2 Code for debugging

a guest
Apr 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.57 KB | None | 0 0
  1. clc; close all; clear all;
  2.  
  3. data = csvread('returns.dat',1,1); % reads in the monthly returns data file for all 19 stock indices
  4.  
  5. indices = [1, 3, 4 , 6, 8, 9, 10, 14, 16, 18];  % The indicies allocated to me
  6.  
  7. subset=data(:,indices);  % your subset of the data
  8. training=subset(1:25,:); % take first 25 months as training data to optimise portfolio weights
  9. testing=subset(26:49,:); % last 24 months are test data to evaluate performance
  10.  
  11. N=10;     % number of stock indices considered in portfolio
  12. T=25;     % months in training data
  13. Ttest=24; % months in testing data
  14. C=1000;   % dollar budget constraint
  15. R=0.005;    % return threshold, to be varied
  16. u = .50;  % maximum percentage an index can hold in each portfolio
  17.  
  18. %% %%%%%%%%%%%%%%%%%%%%%%%%%%%% QP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  19.  
  20. % Now write some code that defines the matrices and vectors needed to solve
  21. % the quadratic programming problem to minimise variance. You need to
  22. % define H, f, A, b, Aeq, beq, LB, UB in the quadprog function
  23.  
  24. [leng,wid] = size(training); %get the dimensions of the training set to use in the loop
  25.  
  26. H = cov(training);                            %H matrix
  27. f = zeros(1,N)';                              %f vector
  28. A = -1 .* sum(training)/leng;                 %inequality constraints
  29. b = [-R*C];                                   %inequality constraints
  30. Aeq = ones(1,N);                              %equality constraints
  31. beq = C;                                      %equailty constraints
  32. LB = zeros(1,N);                              %lower bound is zero
  33. UB = repmat(u*C,1,N);                         %upper bound is mu x C
  34.  
  35. options = optimset( 'Algorithm', 'interior-point-convex','Display','off');
  36. [xQP,FVAL,EXITFLAG]=quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
  37.  
  38. if EXITFLAG == 1;
  39.     FVAL=round(FVAL/0.01)*0.01;
  40. else
  41.     FVAL=0;
  42. end %report to two decimal places
  43.  
  44. %Now experiment with R to find the range of values (from -5% to 5% in steps of 0.1) that gives a feasible solution
  45. % with minimised variance. Lock in the largest value of R that minimises variance on the
  46. %training data. Calculate x (the allocations) with that R value.
  47.  
  48. %initialising variables for data collection in the for loop
  49. Rval = -0.05:0.001:0.05;
  50. x = zeros(10,length(Rval));
  51. FVAL = zeros(length(Rval),1);
  52. i=1;
  53.  
  54. for R = -0.05:0.001:0.05
  55.     b = -R*C;
  56.    
  57.     [x(:,i),f_value,EXITFLAG]=quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
  58.    
  59.     if EXITFLAG == 1;
  60.         FVAL(i,1)=round(f_value/0.01)*0.01; %report to two decimal places
  61.     else
  62.         FVAL(i)=0;
  63.     end
  64.     disp(R)
  65.     i=i+1;
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement