www.gusucode.com > fininst 案例源码程序 matlab代码 > fininst/ComputetheCreditExposureandDeterminetheIncrementalExposuExample.m

    %% Compute the Credit Exposure and Determine the Incremental Exposure for a New Trade  

% Copyright 2015 The MathWorks, Inc.


%% 
% Load data containing the mark-to-market contract values for a portfolio
% of swaps over many scenarios. 
load ccr.mat  

%% 
% Look at one counterparty. 
cpID = 4;
cpIdx = swaps.Counterparty == cpID;
cpValues = values(:,cpIdx,:);
plot(simulationDates,squeeze(sum(cpValues,2)));
grid on;
title(sprintf('Potential Mark-to-Market Portfolio Values for Counterparty: %d',cpID));
datetick('x','mmmyy')
ylabel('Portfolio Value ($)')     

%% 
% Compute the exposures. 
netting = swaps.NettingID(cpIdx);
exposures = creditexposures(cpValues,cpID,'NettingID',netting);  

%% 
% View the credit exposure over time for the counterparty. 
figure;
plot(simulationDates,squeeze(exposures));
grid on
title(sprintf('Exposure for counterparty: %d',cpID));
datetick('x','mmmyy')
ylabel('Exposure ($)')
xlabel('Simulation Dates')     

%% 
% Compute the credit exposure profiles. 
profilesBefore = exposureprofiles(simulationDates,exposures)  

%% 
% Consider a new trade with a counterparty. For this example, take another
% trade from the original swap portfolio and "copy" it for a new counterparty.
% This example is only for illustrative purposes. 
newTradeIdx = 3;
newTradeValues = values(:,newTradeIdx,:);

% Append a new trade to your existing portfolio.
cpValues = [cpValues newTradeValues];
netting = [netting; cpID];
exposures = creditexposures(cpValues,cpID,'NettingID',netting);  

%% 
% Compute the new credit exposure profiles. 
profilesAfter = exposureprofiles(simulationDates,exposures)  

%% 
% Visualize the expected exposures and the new trade's incremental exposure.
% The incremental exposure is used to compute the incremental credit value adjustment (CVA) charge. 
figure;
subplot(2,1,1)
plot(simulationDates,profilesBefore.EE,...
    simulationDates,profilesAfter.EE);
grid on;
legend({'EE before','EE with trade'})
datetick('x','mmmyy','keeplimits')
title('Expected Exposure before and after new trade');
ylabel('Exposure ($)')

subplot(2,1,2)
incrementalEE = profilesAfter.EE - profilesBefore.EE;
plot(simulationDates,incrementalEE);
grid on;
legend('incremental EE')
datetick('x','mmmyy','keeplimits')
ylabel('Exposure ($)')
xlabel('Simulation Dates')