% Example solves a large scale hedging problem without and with cardinality constraint % bounding the number of nonzero hedges % Problem description is explicitly specified by a string in this file % Input data for a solver (matrices and points) are prepared by the % CS_CDO_Hedging_MAD_Data_Preparation function using source data extracted from the file CDO1000.mat (or CDO2000.mat). % file CDO1000.mat (CDO2000.mat)contains raw data for the problem with 1000 (2000) variables; % Two optimization problem statements: Problem 1 and Problem 2 clc clear % load primary data with 1000 variables load 'CDO1000.mat'; % for 1000 variables %Comment the previous line and uncomment the next line to solve Problems with 2000 variables %load 'CDO2000.mat'; %Define Problem 1 statement string: problem_statement = sprintf('%s\n', ... 'minimize',... ' meanabs_dev(matrix_scenarios)',... 'Constraint: <= 80, linearize = 1',... ' polynom_abs(matrix_constraint_budget)',... 'Box: >= point_lowerbounds, <= point_upperbounds',... 'Solver: TANK, precision = 2'); %Display Problem 1 statement: disp(problem_statement); %Construct matrices using raw data: [matrix_scenarios, matrix_constraint_budget]=CS_CDO_Hedging_MAD_Data_Preparation(Pi,BOCds,BOInd,DurCds,DurInd,HistCdsSpread,HistIndSpread); %Determine total number of columns in the matrix: loc_arr = size(matrix_constraint_budget); %Determine number of columns corresponding to decision variables (excluding % columns "id", "scenario_probability", and "scenario_benchmark"): numvariables = loc_arr(2)-3; %Generate headers for matrices: header_matrix = {}; header_matrix{1} = 'id'; for i=1:1:numvariables-4 header_matrix{i+1}=sprintf('h%i', i); end header_matrix{numvariables-2} = 'hbar1'; header_matrix{numvariables-1} = 'hbar2'; header_matrix{numvariables} = 'hbar3'; header_matrix{numvariables+1} = 'hbar4'; header_matrix{numvariables+2} = 'scenario_probability'; header_matrix{numvariables+3} = 'scenario_benchmark'; count = 1; %Pack matrices to the array of structures "toolboxstruc_arr": clear iargstruc_arr; toolboxstruc_arr(count) = tbpsg_matrix_pack('matrix_scenarios', matrix_scenarios,header_matrix); count = count+1; toolboxstruc_arr(count) = tbpsg_matrix_pack('matrix_constraint_budget', matrix_constraint_budget,header_matrix); count = count+1; %Generate headers for points: header_point_bound = {}; for i=1:1:numvariables-4 header_point_bound{i}=sprintf('h%i', i); end header_point_bound{numvariables-3} = 'hbar1'; header_point_bound{numvariables-2} = 'hbar2'; header_point_bound{numvariables-1} = 'hbar3'; header_point_bound{numvariables} = 'hbar4'; %Generate lower bounds point: point_lowerbound_arr = []; for i=1:1:numvariables-4 point_lowerbound_arr(i)=-5000000.0; end; point_lowerbound_arr(numvariables-3)=-10000000.0; point_lowerbound_arr(numvariables-2)=-10000000.0; point_lowerbound_arr(numvariables-1)=-10000000.0; point_lowerbound_arr(numvariables)=-10000000.0; %Pack lower bounds point to the array of structures "toolboxstruc_arr": toolboxstruc_arr(count) = tbpsg_point_pack('point_lowerbounds', point_lowerbound_arr, header_point_bound); count = count+1; %Generate upper bounds point: point_upperbound_arr = []; for i=1:1:numvariables-4 point_upperbound_arr(i)=5000000.0; end; point_upperbound_arr(numvariables-3)=10000000.0; point_upperbound_arr(numvariables-2)=10000000.0; point_upperbound_arr(numvariables-1)=10000000.0; point_upperbound_arr(numvariables)=10000000.0; %Pack upper bounds point to the array of structures "toolboxstruc_arr": toolboxstruc_arr(count) = tbpsg_point_pack('point_upperbounds', point_upperbound_arr, header_point_bound); count = count+1; %Create function list [functionname_arr] = get_object_list(problem_statement, 'function'); if ~isempty(functionname_arr) disp('Used functions:'); fprintf('%s\n', functionname_arr{1:length(functionname_arr)}); end %Uncomment the following line to open the problem in Toolbox Window: %tbpsg_toolbox(problem_statement,toolboxstruc_arr); %Optimize Problem 1: clear solution_str; clear outargstruc_arr; clear optimal_point; [solution_str, outargstruc_arr] = tbpsg_run(problem_statement, toolboxstruc_arr); %Display times and information about optimal solution of Problem 1 fprintf('%s\n', solution_str'); %Extract optimal solution point_variables = tbpsg_optimal_point_vars(solution_str, outargstruc_arr); point_data = tbpsg_optimal_point_data(solution_str, outargstruc_arr); %Display optimal point: for i=1:size(point_variables,2) fprintf('%s = %f\n',point_variables{i},point_data(i)); end disp(' ') disp(' ') disp(' ') disp(' ') %Define problem 2 statement string: clear problem_statement; problem_statement = sprintf('%s\n', ... 'minimize',... 'objective: objective_pen, linearize = 1',... ' meanabs_pen(matrix_scenarios)',... 'constraint: <= 1000000, linearize = 1',... ' polynom_abs(matrix_constraint_budget)',... 'constraint: <= 50, linearize = 1',... ' cardn(0.001, matrix_constraint_budget)',... 'Box: >= point_lowerbounds, <= point_upperbounds',... 'Solver: CAR, precision = 5'); %Display Problem 2 statement: disp(problem_statement); %Construct matrices using raw data: [matrix_scenarios, matrix_constraint_budget]=CS_CDO_Hedging_MAD_Data_Preparation(Pi,BOCds,BOInd,DurCds,DurInd,HistCdsSpread,HistIndSpread); %Determine total number of columns in the matrix: loc_arr = size(matrix_constraint_budget); %Determine number of columns corresponding to decision variables (excluding % columns "id", "scenario_probability", and "scenario_benchmark": numvariables = loc_arr(2)-3; %Generate headers for matrices: header_matrix = {}; header_matrix{1} = 'id'; for i=1:1:numvariables-4 header_matrix{i+1}=sprintf('h%i', i); end header_matrix{numvariables-2} = 'hbar1'; header_matrix{numvariables-1} = 'hbar2'; header_matrix{numvariables} = 'hbar3'; header_matrix{numvariables+1} = 'hbar4'; header_matrix{numvariables+2} = 'scenario_probability'; header_matrix{numvariables+3} = 'scenario_benchmark'; count = 1; %Pack matrices to the array of structures "toolboxstruc_arr": clear iargstruc_arr; toolboxstruc_arr(count) = tbpsg_matrix_pack('matrix_scenarios', matrix_scenarios, header_matrix); count = count+1; toolboxstruc_arr(count) = tbpsg_matrix_pack('matrix_constraint_budget', matrix_constraint_budget, header_matrix); count = count+1; %Generate headers for points: header_point_bound = {}; for i=1:1:numvariables-4 header_point_bound{i}=sprintf('h%i', i); end header_point_bound{numvariables-3} = 'hbar1'; header_point_bound{numvariables-2} = 'hbar2'; header_point_bound{numvariables-1} = 'hbar3'; header_point_bound{numvariables} = 'hbar4'; %Generate lower bounds point: point_lowerbound_arr = []; for i=1:1:numvariables-4 point_lowerbound_arr(i)=-5000000.0; end; point_lowerbound_arr(numvariables-3)=-10000000.0; point_lowerbound_arr(numvariables-2)=-10000000.0; point_lowerbound_arr(numvariables-1)=-10000000.0; point_lowerbound_arr(numvariables)=-10000000.0; %Pack lower bounds point to the array of structures "toolboxstruc_arr": toolboxstruc_arr(count) = tbpsg_point_pack('point_lowerbounds', point_lowerbound_arr, header_point_bound); count = count+1; %Generate upper bounds point: point_upperbound_arr = []; for i=1:1:numvariables-4 point_upperbound_arr(i)=5000000.0; end; point_upperbound_arr(numvariables-3)=10000000.0; point_upperbound_arr(numvariables-2)=10000000.0; point_upperbound_arr(numvariables-1)=10000000.0; point_upperbound_arr(numvariables)=10000000.0; %Pack upper bounds point to the array of structures "toolboxstruc_arr": toolboxstruc_arr(count) = tbpsg_point_pack('point_upperbounds', point_upperbound_arr, header_point_bound); count = count+1; %Create function list: [functionname_arr] = get_object_list(problem_statement, 'function'); if ~isempty(functionname_arr) disp('Used functions:'); fprintf('%s\n', functionname_arr{1:length(functionname_arr)}); end %Uncomment the following line to open the problem in Toolbox Window: %tbpsg_toolbox(problem_statement,toolboxstruc_arr); %Optimize Problem 2: clear solution_str; clear outargstruc_arr; clear optimal_point; [solution_str, outargstruc_arr] = tbpsg_run(problem_statement, toolboxstruc_arr); %Display times and information about optimal solution of Problem 2: fprintf('%s\n', solution_str'); %Extract optimal solution point_variables_2 = tbpsg_optimal_point_vars(solution_str, outargstruc_arr); point_data_2 = tbpsg_optimal_point_data(solution_str, outargstruc_arr); %Display optimal point: for i=1:size(point_variables_2,2) fprintf('%s = %f\n',point_variables_2{i},point_data_2(i)); end %=======================================================================| %American Optimal Decisions, Inc. Copyright | %Copyright ©American Optimal Decisions, Inc. 2007-2015. | %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. | %=======================================================================|