%This case study investigates the optimal pipeline hedging strategy with %five different deviation measures: Standard Deviation, %Mean Absolute Deviation, CVaR Deviation, two-tailed VaR75, and %two-tailed VaR90. It tests out-of-sample performance with five %deviation measures and three downside risk measures. clc clear; load input_data.mat %data_bench datafull %Generate list of risk functions: risk_functions ={'cvar_dev';'meanabs_dev';'st_dev';'TwoTailedVaR75';'TwoTailedVaR90'}; %Generate list of fragments of risk functions with parameters used in %problem description: str_risk_functions_one ={'cvar_dev(0.9, ';'meanabs_dev( ';'st_dev( ';}; str_risk_functions_two ={'var_risk_g_75(0.75, ';'var_risk_75(0.75, ';... 'var_risk_g_90(0.9, ';'var_risk_90(0.9,'}; jj=1; point_variables={'x1','x2','x3'}; %Pack data to PSG Matrix: toolboxstruc_arr(1) = tbpsg_matrix_pack('matrix_scenarios', datafull,[],data_bench); loss_arr=[];loss_arr_in_sample_1=[];risk_values_on_optimal_points_11=[];optimal_point_1=[]; %Begin loop over risk measures: for j=1:size(str_risk_functions_one,1)+size(str_risk_functions_two,1)/2 if j<=size(str_risk_functions_one,1) %Create problem statement: problem_statement = sprintf('%s\n',... 'for {matrix_fact_in; matrix_fact_out; num#} = crossvalidation(10, matrix_scenarios)',... ' ',... 'minimize',... [str_risk_functions_one{j}, 'matrix_fact_in)'],... ' ',... 'Value:',... ' L_out_num#(matrix_fact_out)',... ' L_in_num#(matrix_fact_in)',... ' ',... ' cvar_dev(0.9, matrix_fact_in)',... ' meanabs_dev(matrix_fact_in)',... ' st_dev(matrix_fact_in)',... ' var_risk(0.75, matrix_fact_in)',... ' var_risk_g(0.75, matrix_fact_in)',... ' var_risk(0.9, matrix_fact_in)',... ' var_risk_g(0.9, matrix_fact_in)',... ' ',... 'end for',... ''); else problem_statement = sprintf('%s\n',... 'for {matrix_fact_in; matrix_fact_out; num#} = crossvalidation(10, matrix_scenarios)',... ' ',... 'minimize',... [str_risk_functions_two{jj}, 'matrix_fact_in)'],... [str_risk_functions_two{jj+1}, 'matrix_fact_in)'],... ' ',... 'Value:',... ' L_out_num#(matrix_fact_out)',... ' L_in_num#(matrix_fact_in)',... ' ',... ' cvar_dev(0.9, matrix_fact_in)',... ' meanabs_dev(matrix_fact_in)',... ' st_dev(matrix_fact_in)',... ' var_risk(0.75, matrix_fact_in)',... ' var_risk_g(0.75, matrix_fact_in)',... ' var_risk(0.9, matrix_fact_in)',... ' var_risk_g(0.9, matrix_fact_in)',... ' ',... 'end for',... ''); jj=jj+2; end %Uncomment the following line to open the problem in Toolbox Window: %tbpsg_toolbox(problem_statement, toolboxstruc_arr) %Optimize problem [solution_str, outargstruc_arr] = tbpsg_run(problem_statement, toolboxstruc_arr); %Extract optimal solution to structure: [output_structure] = tbpsg_solution_struct(solution_str, outargstruc_arr); %Extract vectors of loss for all out-of-sample: loss_arr_j=[]; for i=1:size(fields(output_structure.vector_data),1)/2 loc = sprintf('l_out_%g',i); loss_arr_j = [loss_arr_j;output_structure.vector_data.(loc)]; end loss_arr=[loss_arr,loss_arr_j]; %Extract vectors of loss for all in-sample: loss_arr_j=[]; for i=1:size(fields(output_structure.vector_data),1)/2 loc = sprintf('l_in_%g',i); loss_arr_j = [loss_arr_j;output_structure.vector_data.(loc)]; end loss_arr_in_sample_1 = [loss_arr_in_sample_1,loss_arr_j(:,end)]; %Extract values of risks for last in-sample: if j<=size(str_risk_functions_one,1) func_val_10 = output_structure.function_value(end-6:end); risk_values_on_optimal_points_11 = [risk_values_on_optimal_points_11,[func_val_10(1:3);func_val_10(4)+func_val_10(6);func_val_10(5)+func_val_10(7)]]; else func_val_10 = output_structure.function_value(end-8:end); risk_values_on_optimal_points_11 = [risk_values_on_optimal_points_11,[func_val_10(1:3);func_val_10(4)+func_val_10(7);func_val_10(5)+func_val_10(8)]]; end %Extract optimal points for last in-sample: point_loc = []; for k=1:size(output_structure.optimal_point_data,2) point_loc = [point_loc;output_structure.optimal_point_data.problem_10]; end optimal_point_1=[optimal_point_1,point_loc(end,:)']; end %Calculate risk functions on aggregated out-of-sample losses: clear str_risk_functions; clear deviation_risk_functions; str_risk_functions =['cvar_dev ' 'meanabs_dev ' 'st_dev ' 'var_risk_g ' ... 'var_risk ' 'var_risk_g ' 'var_risk']; parameters = [0.9; 0; 0; 0.75; 0.75; 0.9; 0.9]; deviation_risk_functions = strread(str_risk_functions, '%s'); clear str_risk_functions; str_risk_functions =['max_risk ' 'cvar_risk ' 'var_risk ']; downside_risk_functions = strread(str_risk_functions, '%s'); clear str_risk_functions; deviation_risk_out_of_sample = []; downside_risk_out_of_sample = []; for k=1:1:size(loss_arr,2) deviation_risk_out_of_sample1 = []; downside_risk_out_of_sample1 = []; for i=1:1:length(parameters) if parameters(i) == 0 [a] = functionvalue(deviation_risk_functions{i}, [], diag((-1)*ones(size(loss_arr,1),1)), [], [], loss_arr(:,k)); else [a] = functionvalue(deviation_risk_functions{i}, parameters(i), diag((-1)*ones(size(loss_arr,1),1)), [], [], loss_arr(:,k)); end deviation_risk_out_of_sample1 = [deviation_risk_out_of_sample1; a]; if i < 4 if i == 1 [a] = functionvalue(downside_risk_functions{i}, [], diag((-1)*ones(size(loss_arr,1),1)), [], [], loss_arr(:,k)); else [a] = functionvalue(downside_risk_functions{i}, 0.9, diag((-1)*ones(size(loss_arr,1),1)), [], [], loss_arr(:,k)); end downside_risk_out_of_sample1 = [downside_risk_out_of_sample1; a]; end end deviation_risk_out_of_sample = [deviation_risk_out_of_sample deviation_risk_out_of_sample1]; clear deviation_risk_out_of_sample1; downside_risk_out_of_sample = [downside_risk_out_of_sample downside_risk_out_of_sample1]; end deviation_risk_out_of_sample(4,:) = deviation_risk_out_of_sample(4,:) + deviation_risk_out_of_sample(5,:); deviation_risk_out_of_sample(5,:) = deviation_risk_out_of_sample(6,:) + deviation_risk_out_of_sample(7,:); %outfilename = 'CS_Mortgage_Pipeline_Hedging_Output.txt'; %fid = fopen(outfilename, 'w'); fprintf('\n\n Table 1. Optimal Hedging Strategies.'); %fprintf(fid,'\n\n Table 1. Optimal Hedging Strategies.'); fprintf('\n\n Strategy Name Deviation Measure Used as the Objective Function'); %fprintf(fid,'\n\n Strategy Name Deviation Measure Used as the Objective Function'); for i=1:1:size(risk_functions,1) fprintf('\n%11s%.0f\t\t\t\t\t\t%s', 'Strategy_', i, risk_functions{i}); %fprintf(fid,'\n%11s%.0f\t\t\t\t\t\t%s', 'Strategy_', i, risk_functions{i}); end fprintf('\n\n Table 2. Out-of-Sample Performance: Deviation Risk Measures.\n'); %fprintf(fid,'\n\n Table 2. Out-of-Sample Performance: Deviation Risk Measures.\n'); fprintf('%13s\t', ' '); %fprintf(fid,'%13s\t', ' '); for i=1:1:size(risk_functions,1) fprintf('%11s%.0f', 'Strategy_', i); % fprintf(fid,'%11s%.0f', 'Strategy_', i); end for i=1:1:size(risk_functions,1) fprintf('\n%-13s\t', risk_functions{i}); % fprintf(fid,'\n%-13s\t', risk_functions{i}); for j=1:1:size(deviation_risk_out_of_sample,2) fprintf('%12f', deviation_risk_out_of_sample(i,j)); % fprintf(fid,'%12f', deviation_risk_out_of_sample(i,j)); end end fprintf('\n\n'); %fprintf(fid,'\n\n'); fprintf('\n\n Table 3. Out-of-Sample Performance: Downside Risk Measures.\n'); %fprintf(fid,'\n\n Table 3. Out-of-Sample Performance: Downside Risk Measures.\n'); fprintf('%13s\t', ' '); %fprintf(fid,'%13s\t', ' '); for i=1:1:size(risk_functions,1) fprintf('%11s%.0f', 'Strategy_', i); % fprintf(fid,'%11s%.0f', 'Strategy_', i); end for i=1:1:size(downside_risk_functions,1) fprintf('\n%-13s\t', downside_risk_functions{i}); % fprintf(fid,'\n%-13s\t', downside_risk_functions{i}); for j=1:1:size(downside_risk_out_of_sample,2) fprintf('%12f', downside_risk_out_of_sample(i,j)); % fprintf(fid,'%12f', downside_risk_out_of_sample(i,j)); end end fprintf('\n\n'); %fprintf(fid,'\n\n'); % generate histograms of losses for aggregated out-of-sample datasets str_color = ['c ' 'y ' 'm ' 'b ' 'g ']; array_color = strread(str_color, '%s'); leg = strrep(risk_functions, '_', ' '); for i=1:1:size(risk_functions,1) tit =sprintf('%s%.0f%s%s','Histogram of Losses for Aggregated Out-of-Sample Datasets: Strategy ', i,' - ',leg{i}); figure [bins,xout] = hist(loss_arr(:,i),65); bins = bins/sum(bins); bar(xout,bins,array_color{i}); xlabel('Losses'); title(tit); end % generate graph comparing Out-of-Sample Performance of optimal hedging % strategies measured by CVaR deviation, mean absolute deviation, % standard deviation, two-tailed VaR75, and two-tailed VaR90 str_Strategies = ''; for i=1:1:size(risk_functions,1) str_Strategies = sprintf('%s%s%.0f%s',str_Strategies,'Strategy_', i,' '); end array_Strategies = strread(str_Strategies, '%s'); array_Strategies = strrep(array_Strategies, '_', ' '); figure plot(deviation_risk_out_of_sample(1:size(risk_functions,1),:)','.', 'MarkerSize',20) set(gca,'XTick',1:1:size(array_Strategies,1)); hAxes = gca; set(hAxes, 'XTickLabel', array_Strategies); grid on; legend(leg, 'Location', 'NorthWest'); xlabel('Optimal Hedging Strategies'); ylabel('Values of Deviation Risk Functions'); title('Out-of-Sample Performance: Deviation Risk Measures'); % generate graph comparing Out-of-Sample Performance of optimal hedging % strategies measured by Downside Risk Measures leg1 = strrep(downside_risk_functions, '_', ' '); figure plot(downside_risk_out_of_sample(1:size(downside_risk_functions,1),:)','.', 'MarkerSize',20) set(gca,'XTick',1:1:size(array_Strategies,1)); hAxes = gca; set(hAxes, 'XTickLabel', array_Strategies); grid on; legend(leg1, 'Location', 'NorthWest'); xlabel('Optimal Hedging Strategies'); ylabel('Values of Downside Risk Functions'); title('Out-of-Sample Performance: Downside Risk Measures'); % generate graph showing CVaR deviation, mean absolute deviation, standard % deviation, two-tailed VaR75, and two-tailed VaR90, on five optimal points in run 1 figure plot(risk_values_on_optimal_points_11','.', 'MarkerSize',20) set(gca,'XTick',1:1:size(array_Strategies,1)); hAxes = gca; set(hAxes, 'XTickLabel', array_Strategies); grid on; legend(leg, 'Location', 'NorthWest'); xlabel('Optimal Hedging Strategies'); ylabel('Values of Deviation Risk Functions'); title('In-Sample Performance: Values of Deviation Risk Measures on Five Optimal Points in Run 1'); % generate graph showing optimal hedging strategies for five % deviation measures in run 1. figure plot(optimal_point_1,'.', 'MarkerSize',20) set(gca,'XTick',1:1:size(point_variables,1)); hAxes = gca; set(hAxes, 'XTickLabel', point_variables); grid on; legend(leg, 'Location', 'NorthEast'); xlabel('Components (Hedging Positions)'); ylabel('Values of Hedging Positions '); title('Optimal Hedging Positions for Five Strategies (Deviation Risk Measures)'); % generate histograms of losses for the first in-sample dataset str_color = ['c ' 'y ' 'm ' 'b ' 'g ']; array_color = strread(str_color, '%s'); for i=1:1:size(risk_functions,1) tit =sprintf('%s%.0f%s%s','Histogram of Losses for First In-Sample Dataset: Strategy ', i,' - ',leg{i}); figure [bins,xout] = hist(loss_arr_in_sample_1(:,i),65); bins = bins/sum(bins); bar(xout,bins,array_color{i}); xlabel('Losses'); title(tit); end %=======================================================================| %American Optimal Decisions, Inc. Copyright | %Copyright ©American Optimal Decisions, Inc. 2007-2016. | %American Optimal Decisions (AOD) retains copyrights to this material. | % | %Permission to reproduce this document and to prepare derivative works | %from this document for internal use is granted, provided the copyright | %and “No Warranty” statements are included with all reproductions | %and derivative works. | % | %For information regarding external or commercial use of copyrighted | %materials owned by AOD, contact AOD at support@aorda.com. | %=======================================================================|