%% Numerix CAIL Advanced Risk example
%
%  This example uses the Numerix CAIL API to price multiple trades
%  from MATLAB.

%% Initialize Numerix environment

import com.numerix.integration.*;
import com.numerix.integration.implementation.*;

n = numerix('i:\NumeriX_java_10_3_0\data');

%% Specify the hybrid model
hySpec = HybridModelSpecification;
hySpec.addHW1F('IR-USD', 'USD', 'LIBOR', '3M', 'MeanReversion(0.5),DiagonalSwaption(ATM, 10Y)');
hySpec.addHW1F('IR-EUR', 'EUR', 'EURIBOR', '6M', 'MeanReversion(0.5),DiagonalSwaption(ATM, 10Y)');
hySpec.addFXBlack('FX-USDEUR', 'USD', 'EUR', 'LIBOR', '3M', 'EURIBOR', '6M', 'StrikeFXEuropean(ATM, 10Y)');

%% Specify the factor correlations
hyCorrelations = HybridModelCorrelationMatrix(hySpec);
hyCorrelations.add('IR-USD', 'IR-EUR', 0.5);
hyCorrelations.add('IR-USD', 'FX-USDEUR', 0.25);
hyCorrelations.add('IR-EUR', 'FX-USDEUR', 0.25);

%% Specify the model parameters
hybridModelParameters = java.util.HashMap;
hybridModelParameters.put('Quote Type', 'MID');
hybridModelParameters.put('Payout Currency', 'USD');
hybridModelParameters.put('Specification', hySpec);
hybridModelParameters.put('Correlations', hyCorrelations);

%% Specify exposure calculation parameters
observationDates = CustomObservationSchedule;
observationDates.add(DateExtensions.date(2011, 12, 1));
for y = 2012:2013
  for m = 1:12
    observationDates.add(DateExtensions.date(y, m, 1));
  end
end

exposureParameters = java.util.HashMap;
exposureParameters.put('Model ID', 'HYBRID');
exposureParameters.put('Observation Dates', observationDates);

%% Create trade instances
tradeParameters1 = java.util.HashMap;
tradeParameters1.put('Trade ID', 'RVFL1001');
tradeParameters1.put('Quote Type', 'MID');
tradeParameters1.put('Currency', 'USD');
tradeParameters1.put('Notional', 1000000.0);
tradeParameters1.put('Effective Date', DateExtensions.date('1-Dec-2011'));
tradeParameters1.put('Termination Date', DateExtensions.date('1-Dec-2021'));
tradeParameters1.put('IR Index', 'LIBOR');
tradeParameters1.put('IR Index Tenor', '3M');
tradeParameters1.put('Structured Freq', '3M');
tradeParameters1.put('Structured Side', 'Receive');
tradeParameters1.put('Structured Coupon Floor', 0.0);
tradeParameters1.put('Structured Coupon UpBd', 0.08);
tradeParameters1.put('StructuredCoupon Multiplier', 1.4);
tradeParameters1.put('Structured Coupon Cap', 0.05);
tradeParameters1.put('Structured Basis', 'ACT/360');
tradeParameters1.put('Funding Freq', '3M');
tradeParameters1.put('Funding Side', 'Pay');
tradeParameters1.put('Funding Spread', 0.003);
tradeParameters1.put('Funding Basis', 'ACT/360');
tradeParameters1.put('Call Start Date', DateExtensions.date('1-Dec-2013'));
tradeParameters1.put('Call End Date', DateExtensions.date('1-Dec-2020'));
tradeParameters1.put('Option Side', 'Short');
tradeParameters1.put('Option Type', 'Right to Terminate');
tradeParameters1.put('Call Frequency', '3M');
tradeParameters1.put('Model', 'HYBRID');
tradeParameters1.put('Method', 'BackwardMC');
tradeInstance1 = RepositoryExtensions.createTradeInstance(n.Repository, 'TRADE.IR.CALLABLEREVERSEFLOATER', tradeParameters1);

tradeParameters2 = java.util.HashMap;
tradeParameters2.put('Trade ID', 'CASHDEP1001');
tradeParameters2.put('Quote Type', 'MID');
tradeParameters2.put('Currency', 'USD');
tradeParameters2.put('Coupon Rate', 0.05);
tradeParameters2.put('Yield', 0.044);
tradeParameters2.put('Notional', 100.0);
tradeParameters2.put('Effective Date', DateExtensions.date('1-Apr-2012'));
tradeParameters2.put('Maturity', DateExtensions.date('1-Apr-2013'));
tradeParameters2.put('IR Index', 'LIBOR');
tradeParameters2.put('IR Index Tenor', '3M');
tradeParameters2.put('Model', 'HYBRID');
tradeParameters2.put('Method', 'BACKWARDMC');
tradeInstance2 = RepositoryExtensions.createTradeInstance(n.Repository, 'IR.CASHDEPOSIT', tradeParameters2);

tradeParameters3 = java.util.HashMap;
tradeParameters3.put('Trade ID', 'FXFWD1001');
tradeParameters3.put('Quote Type', 'MID');
tradeParameters3.put('Base Currency', 'USD');
tradeParameters3.put('Term Currency', 'EUR');
tradeParameters3.put('Delivery Date', DateExtensions.date('1-Jun-2012'));
tradeParameters3.put('Contract FX Forward Rate', 80.5);
tradeParameters3.put('Base Notional', 10000000.0);
tradeParameters3.put('Base IR Index', 'LIBOR');
tradeParameters3.put('Term IR Index', 'EURIBOR');
tradeParameters3.put('Base IR Index Tenor', '3m');
tradeParameters3.put('Term IR Index Tenor', '6m');
tradeParameters3.put('Calendar', 'NewYork Target');
tradeParameters3.put('Spot Lag', '2bd');
tradeParameters3.put('Model', 'HYBRID');
tradeParameters3.put('Method', 'BACKWARDMC');
tradeInstance3 = RepositoryExtensions.createTradeInstance(n.Repository, 'FX.FXFORWARD', tradeParameters3);
tradeInstances = java.util.ArrayList;
tradeInstances.add(tradeInstance1);
tradeInstances.add(tradeInstance2);
tradeInstances.add(tradeInstance3);
n.Parameters.setInstances(tradeInstances);

%% Add a custom lookup so the trades will be able to find the hybrid model
n.Parameters.getLookups.add(0,ExactLookupRule('HYBRID','MODEL.HYBRID',hybridModelParameters.entrySet));

%% Add another custom lookup so that exposure report will know its parameters
n.Parameters.getLookups.add(1,ExactLookupRule('RISK.REPORT.EXPOSURE','REPORT.EXPOSURE',exposureParameters.entrySet));

%% Perform the calculation
results = n.Context.calculate(n.Parameters, Request.getExposure);

%% Parse the results for MATLAB and display
r = n.parseResults(results);

% This example returns results for each trade.  The results for the second
% trade instance, CASHDEP1001, with the corresponding Exposure data shown here.

disp(r.Results{1})
disp(r.Results{1}.Name)

% Get index of Exposure data
expIndex = find(strcmp(r.Results{1}.Name,'Exposure'));
disp(r.Results{1}.Data{expIndex}(:,1:3))


figure('Tag','NumerixAdvancedRiskExample');

for ii=1:3
   % Get dates
   dates = cell2mat(r.Results{ii}.Data{expIndex}(2:end,1));
   dates = dates(:,4:end);
   dates = floor(datenum(dates));
   % Get exposures
   mtm = cell2mat(r.Results{ii}.Data{expIndex}(2:end,2:end))';
   exposures = max(0,mtm); % Exposure at contract level, no netting
   EE = mean(exposures); % Expected Exposure
   PFE = prctile(exposures,95); % Potential Future Exposure
   subplot(3,1,ii)
   plot(dates,EE,dates,PFE)
   title(r.Trade{ii})
   datetick
end

%   Copyright 2014 The MathWorks, Inc. 