% Example demonstrates how to hedge the target portfolio consisting of % stock options by portfolio including indices, stocks, and indices options % in optimal manner. % The function riskconstrparam is used for solving the problem. % Problem is solved for 4 values of the parameter % (CVaR constraint on the hedging error -- gamma): % $525, $540, $550, and 600. clear; format long; %Load the data for problem stored in mat file load('problem_CS_Hedging_Portfolio_of_Options_5p25_data.mat'); %Specify a set of values of parameter (upper bound in the CVaR constraint %on the hedging error (CS.2) ) for which runs should be conducted Parameters = [525 540 550 600]; %-------------------------------------------------------------------------- %Read point_price matrix and create point_price values array fulldata = deblank(textread('point_prices.txt', '%s','delimiter','\n', 'bufsize', 65535)); strdata_arr = []; count = 1; for j=2:1:length(fulldata) if ~isempty(fulldata{j}) strdata_arr{count} = strread(fulldata{j},'%s','delimiter','\t'); count = count+1; end end fulldata = []; for j=1:1:length(strdata_arr) loc_arr = str2double(strdata_arr{j}); data_arr(j, :) = loc_arr'; end point_price = data_arr(:,2); clear strdata_arr; %-------------------------------------------------------------------------- %Set solver options: options.Solver = 'VAN'; options.Precision = 5; %Set graph options: stroptions.PlotGraph = 'On'; stroptions.PlotType = 1; %Because riskconstrparam solves the problem with objective including risk function with minus sign ("-"), %we change objective sign to compensate the standard objective statement for riskconstrparam stroptions.ScaleObjective =-1; %Solve the optimization problem for specified values of the parameter gamma (CS.2) [Objectives, Parameters, Points, GraphHandle]=riskconstrparam ([], risk2, [], [], [], [], w2, H2, c2, p2,... d, r, [], [], [], [], lb, ub, 'r', [],[],[],... [], [], Parameters, [], stroptions); %Create string array for point labels. %Point labels (p_1, p_2, p_3, p_4) present points for different values of %parameter (CVaR constraint on the hedging error -- gamma) for i=1:size(Parameters,2) Point_label {i} = strcat('p_',int2str(i)); end Point_label = Point_label'; %Sort components in lexicographical order component_name_arr_sorted = sort(component_name_arr); %Sort Points and Matrices in accordance with components order for j=1:size(component_name_arr) for i=1:size(component_name_arr) k = strcmp(component_name_arr_sorted(i), component_name_arr(j)); if k == true Points_sorted(:,i)= Points (:,j); H2_sorted (:,i) = H2 (:,j); end end end %Create string array for components. for i=1:size(component_name_arr_sorted) comp_alias {i} = strcat('c_',int2str(i)); end comp_alias_sorted = comp_alias'; %-------------------------------------------------------------------------- %Calculating the avg_loss at the points pt_1...pt_4 for i = 1:4 loss(:,i) = c2 - H2_sorted*Points_sorted(i,:)'; avg_loss(i) = p2'*loss(:,i); end %-------------------------------------------------------------------------- % Create plot for linear_initial_price, cvar_risk_Hedging_constraint, and avg_loss % for points pt_1, pt_2, pt_3, and pt_4 clear x; clear y1; clear y2; clear y3; %Calculate value of the functions linear_initial_price, %cvar_risk_Hedging_constraint, and avg_loss at the points pt_1...pt_4 for i=1:4 x(i) = i; y1(i) = functionvalue('linear', [], -d', [], [], Points(i,:)'); y2(i) = functionvalue('cvar_risk', 0.94999999, H2, c2, p2, Points(i,:)'); y3(i) = avg_loss(i); end % Create plot figure; plot1 = plot(... x,y1,... 'LineStyle','none',... 'Marker','s',... 'MarkerEdgeColor',[1 0 0],... 'MarkerFaceColor',[1 0 0],... 'MarkerSize',8); fprintf('%5f', i); hold on; plot1 = plot(... x,y2,... 'LineStyle','none',... 'Marker','d',... 'MarkerEdgeColor',[0 1 0],... 'MarkerFaceColor',[0 1 0],... 'MarkerSize',8); plot1 = plot(... x,y3,... 'LineStyle','none',... 'Marker','^',... 'MarkerEdgeColor',[0 0 1],... 'MarkerFaceColor',[0 0 1],... 'MarkerSize',8); grid on; set(gca,'XLim',[0 5]); set(gca,'YLim',[-150 800]); set(gca,'XTick',1:1:size(Point_label)); hAxes = gca; set(hAxes, 'XTickLabel', Point_label); hold off; xlabel('Points'); ylabel('Values of functions f1, f2, and f3'); title('Initial price (f1),CVaR (f2), and average loss (f3)'); h = gca; legend1 = legend(h,{'f1', 'f2', 'f3'}); %-------------------------------------------------------------------------- % Create plot showing the contribution of each individual component of the % point to the CVaR of the hedging error %%Calculate increment of the function cvar_risk_Hedging_constraint at the point pt_1 [Increments] = functionincrement('cvar_risk', 0.94999999, H2_sorted, c2, p2, Points_sorted (1,:)'); %Set bound for components showing in the linear graph bound_show_comp_linear = 6.9891e-003; %Select components to be shown in the graph. These components are greater %than bound_show_comp constant defined above. clear i; clear y; others = 0; k=0; j = 1; for i=1:1:size(component_name_arr_sorted(:,1)) if abs(Increments(i)/y2(1)) > bound_show_comp_linear comp_show(j) = comp_alias_sorted(i); y(j) = Increments(i)/y2(1); j = j+1; x(j) = j; else others = others + Increments(i)/y2(1); k = k + 1; end; end; others = others/k; S = ['Others']; comp_others = cellstr(S); comp_show(length(comp_show)+1) = comp_others; y(length(comp_show)) = others; %Create Y-axis marks array Ytick(1) = -0.025; for i = 1:29 Ytick (i+1) = Ytick(i) + 0.001; end % Create plot figure; plot1 = plot(... x,y,... 'LineStyle','none',... 'Marker','s',... 'MarkerEdgeColor',[1 0 0],... 'MarkerFaceColor',[1 0 0],... 'MarkerSize',4); grid on; set(gca,'XLim',[0 length(comp_show)]); set(gca,'YLim',[-0.025 0]); set(gca,'XTick',1:length(comp_show)); set(gca,'YTick',Ytick); hAxes = gca; set(hAxes, 'XTickLabel', comp_show); xlabel('Components'); ylabel('Values of Contribution to the CVaR of the Hedging Error '); title('Contribution of Components of the Point p_1 to the CVaR of the Hedging Error'); %-------------------------------------------------------------------------- % Create pie diagram showing structure of the selected optimal point p_1 clear y; others = 0; %Set bound for component values to be displayed as different segments of the pie bound_show_comp_pie = 14.9; j = 1; for i=1:1:size(component_name_arr_sorted(:,1)) comp_pie(i) = Points_sorted(1,i)'* point_price(i); if comp_pie(i) > bound_show_comp_pie comp_show_pie(j) = comp_alias_sorted(i); y(j) = comp_pie(i); j = j+1; else others = others + Points_sorted(1,i)'*point_price(i); end; end; comp_show_pie(length(comp_show_pie)+1) = comp_others; y(length(comp_show_pie)) = others; f = figure; pie(y); h = gca; %Hide percent values on the pie textObjs = findobj(h,'Type','text'); percent = get(textObjs,{'String'}); visibility = get(textObjs,{'Visible'}); for i = 1:length(comp_show_pie) visibility{i} = 'off'; end set(textObjs,{'Visible'},visibility); % Create annotation textbox annotation1 = annotation(... f,'textbox',... 'Position',[0.4625 0.8892 0.09107 0.05301],... 'LineStyle','none',... 'FitHeightToText','off',... 'FontWeight','bold',... 'String',{'X[pt\_1]'}); % Create title title('Structure of the selected optimal point',... 'FontSize',12,... 'FontWeight','bold'); legend3 = legend(h,comp_show_pie,... 'AmbientLightColor',[0.7529 0.7529 0.7529],... 'CLim',[1 12],... 'Color',[0.8 0.8 0.8],... 'DataAspectRatio',[10 1 1],... 'Position',[0.7872 0.27 0.1732 0.5576],... 'XColor',[0.7529 0.7529 0.7529],... 'YColor',[0.7529 0.7529 0.7529],... 'EdgeColor',[0.7529 0.7529 0.7529],... 'Interpreter','none',... 'Box','off'); %-------------------------------------------------------------------------- % Create histogram for Probability Density Function of Losses. % Calculate the loss loss = c2 - H2_sorted*Points_sorted(1,:)'; %Create Y-axis marks array Xtick(1) = -150; for i = 1:41 Xtick (i+1) = Xtick(i) + 25; end %Create the histogram: figure [bins,xout] = hist(loss,41); bins = bins/sum(bins); bar(xout,bins,'r',... 'BarWidth',0.4); h = gca; set (h, 'YLim',([0.00 0.08])); set (h, 'XLim',([-150 875])); set(h,'XTick',Xtick); set(h,'XTickLabel',Xtick); set(h,'FontSize',7); grid on; set(h, 'YAxisLocation','left'); ylabel ('Probability','FontSize',9); hold on; xlabel('Losses (USD)','FontSize',9); tit =sprintf('%s%','Probability Density Function of Losses: matrix\_Options\_scenarios\_small'); title(tit,'FontSize',9,... 'FontWeight','bold'); %-------------------------------------------------------------------------- %Display the resultunt table fprintf('\n%35s','CVaR constraint on the hedging error'); for i=1:1:length(Parameters) fprintf('%12.6f', Parameters(i)); end fprintf('\n%35s','Initial price of the hedging'); fprintf('\n%35s','portfolio'); for i=1:1:length(Parameters) fprintf('%12.4f', Objectives(i)); end fprintf('\n%35s','Optimal value of components'); for j=1:size(component_name_arr) fprintf('\n%30s', component_name_arr_sorted {j}); for i=1:1:length(Parameters) fprintf('%12f', Points_sorted(i,j)); end end fprintf('\n'); %the last line of the code %=======================================================================| %American Optimal Decisions, Inc. Copyright | %Copyright ©American Optimal Decisions, Inc. 2007-2014. | %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. | %=======================================================================|