Overview
This Sample describes a historical simulation analysis that is performed with and without using SAS Risk Dimensions. The objective is to show how a historical simulation analysis perturbs risk factors in SAS Risk Dimensions. A simple example is considered. One interval-level and one ratio-level risk factor are analyzed.
The sample is organized as follows:
The
Full Code tab contains the complete set of SAS code for this Sample, and the
Output contains the expected outputs.
A historical simulation analysis perturbs risk factors as follows for the two measurement levels of risk factors:
For interval-level risk factors, the perturbation is:
rf_simi = rf_base + (rfi - rfi-1)
where:
rf_ denotes a risk factor
simi = simulation replication i
i = ith observation of the historical time series data
rf_base = base-case value of rf
For example, if:
rf0 = 5.25
rf1 = 5.30
rf2 = 5.33
...
and if
rf_base = 5.55, then:
rf_sim1 = 5.55 + (5.30 - 5.25) = 5.60
rf_sim2 = 5.55 + (5.33 - 5.30) = 5.58
...
For ratio-level risk factors, the perturbation is:
rf_simi = rf_base * (rfi / rfi-1)
For example, if:
rf0 = 10.00
rf1 = 10.25
rf2 = 10.25
...
and if
rf_base = 11.50, then:
rf_sim1 = 11.50 * (10.25 / 10.00) = 11.7875
rf_sim2 = 11.50 * (10.25 / 10.25) = 11.50
...
The following SAS code creates historical time series data for two risk factors, y1 and y2. The time series spans 11 weekdays. Variable y1 is to be defined as an interval-level risk factor, and y2 as ratio-level.
* Synthetically produce 11 days of historical time series data for two risk factors;
data dates;
format date date9.;
label date="Date";
do d=1 to 12;
date=intnx('weekday','02jan2013'd,d);
output;
end;
drop d;
run;
data rfs;
input y1 y2;
label y1="Interval-level Risk Factor y1"
y2="Ratio-level Risk Factor y2";
datalines;
5.25 10.00
5.30 10.25
5.33 10.25
5.30 10.50
5.40 10.60
5.45 10.65
5.50 10.75
5.40 10.80
5.35 11.25
5.50 11.50
5.52 11.40
5.55 11.50
;
run;
* Merge the dates with the risk factor data;
data hist_vals;
merge dates rfs;
run;
* Use the last data point as the current market (base case) data;
data current hist_vals;
set hist_vals end=last;
if last then output current;
else output hist_vals;
run;
* Print both data sets;
proc print data=hist_vals;
title1 "Historical Time Series Data";
title2 "Work.Hist_Vals";
proc print data=current;
title1 "Current Market (Base Case) Data";
title2 "Work.Current";
run;
The following data sets are created and printed to the SAS output window:
| 03JAN2013 |
5.25 |
10.00 |
| 04JAN2013 |
5.30 |
10.25 |
| 07JAN2013 |
5.33 |
10.25 |
| 08JAN2013 |
5.30 |
10.50 |
| 09JAN2013 |
5.40 |
10.60 |
| 10JAN2013 |
5.45 |
10.65 |
| 11JAN2013 |
5.50 |
10.75 |
| 14JAN2013 |
5.40 |
10.80 |
| 15JAN2013 |
5.35 |
11.25 |
| 16JAN2013 |
5.50 |
11.50 |
| 17JAN2013 |
5.52 |
11.40 |
The following SAS code perturbs each set of risk factors, y1 and y2, without using SAS Risk Dimensions. In the first step, the base-case values for each risk factor are extracted and stored in macro variables for easy reference later:
* Store base-case risk factor values into macro variables;
data _null_;
set current;
call symput('y1base',y1);
call symput('y2base',y2);
run;
In the next step, the returns are calculated for each risk factor based on their measurement level:
* Calculate the interval-level returns for risk-factor y1 and the ratio-level
returns for risk-factor y2 from the historical time series data;
data hist_rets;
set hist_vals;
label ret_y1="Additive change of y1"
ret_y2="Relative change of y2";
ret_y1=dif(y1);
ret_y2=y2/lag(y2);
run;
In this step, each risk factor is perturbed according to its measurement level:
* Perturb base-case values by applying returns from the historical time series data;
data hist_prtrb;
set hist_rets(firstobs=2);
label perturb_y1="Perturbed value of y1 base case"
perturb_y2="Perturbed value of y2 base case";
perturb_y1=&y1base+ret_y1;
perturb_y2=&y2base*ret_y2;
run;
The resulting data set is then printed to the SAS output window:
* Print the resulting perturbed risk factor values that are used to calculate VaR;
proc print data=hist_prtrb label;
title1 "Simulated Risk Factor Values from Historical Simulation Analysis";
title2 "WITHOUT SAS Risk Dimensions";
run;
| 03JAN2013 |
5.25 |
10.00 |
. |
. |
. |
. |
| 04JAN2013 |
5.30 |
10.25 |
0.05 |
1.02500 |
5.60 |
11.7875 |
| 07JAN2013 |
5.33 |
10.25 |
0.03 |
1.00000 |
5.58 |
11.5000 |
| 08JAN2013 |
5.30 |
10.50 |
-0.03 |
1.02439 |
5.52 |
11.7805 |
| 09JAN2013 |
5.40 |
10.60 |
0.10 |
1.00952 |
5.65 |
11.6095 |
| 10JAN2013 |
5.45 |
10.65 |
0.05 |
1.00472 |
5.60 |
11.5542 |
| 11JAN2013 |
5.50 |
10.75 |
0.05 |
1.00939 |
5.60 |
11.6080 |
| 14JAN2013 |
5.40 |
10.80 |
-0.10 |
1.00465 |
5.45 |
11.5535 |
| 15JAN2013 |
5.35 |
11.25 |
-0.05 |
1.04167 |
5.50 |
11.9792 |
| 16JAN2013 |
5.50 |
11.50 |
0.15 |
1.02222 |
5.70 |
11.7556 |
| 17JAN2013 |
5.52 |
11.40 |
0.02 |
0.99130 |
5.57 |
11.4000 |
The following SAS code uses SAS Risk Dimensions to perturb the risk factors. In the first step, a simple portfolio is created. The portfolio consists of one unit of each risk factor:
* Create a portfolio that contains one unit for each risk factor;
data port;
instid='inst1';
instindex='y1';
insttype='asset';
output;
instid='inst2';
instindex='y2';
insttype='asset';
output;
run;
In the next step, a new risk environment is created. The risk factors
y1 and
y2 are registered in the environment with their respective measurement levels. An instrument variable is also registered:
* Create new risk environment. Register risk factor and instrument variable definitions;
proc risk;
env new=work.env;
declare riskfactors=(y1 num var mlevel=interval label="Interval-level Risk Factor y1",
y2 num var mlevel=ratio label="Ratio-level Risk Factor y2");
declare instvars=(instindex char 15 var);
env save;
run;
In this step, a pricing method is defined for pricing each instrument. The pricing method uses the instrument variable
instindex to distinguish between the two risk factors. The value of this variable is resolved when the risk analysis project is run later. The resulting valuation simply equals the risk factor value:
* Define a pricing method in which the instrument value equals the risk factor value;
proc compile env=work.env outlib=work.env;
method pricing_method kind=price;
_value_=riskfactor.instindex;
endmethod;
run;
In the final step, the portfolio data, historical time series data, and current market data are registered as data sources in the environment. A historical simulation analysis is defined, along with an analysis project that is based on the historical simulation analysis. The project is then run until it creates the simulated risk factor values, and only those simulated risk-factor states are outputted:
* Create and run a historical simulation analysis project;
proc risk;
env open=work.env;
instrument asset variables=(instindex) methods=(price pricing_method);
instdata port file=port format=simple;
sources port port;
read sources=port out=port;
marketdata current file=current type=current;
marketdata hist file=hist_vals type=timeseries;
simulation histsim_analysis
data=hist
method=historical
seed=54321;
project histsim_project
data=current
portfolio=port
analysis=histsim_analysis
options=(simstate);
runproject histsim_project
outlib=work
endproject=states;
env save;
run;
The relevant variables from the resulting SimState output table are then printed to the SAS output window:
* Print perturbed base-case values from SAS Risk Dimensions historical simulation;
proc print data=simstate label;
title1 "Simulated Risk Factor Values from Historical Simulation Analysis";
title2 "WITH SAS Risk Dimensions";
var y1 y2;
run;
| 5.60000 |
11.78750 |
| 5.58000 |
11.50000 |
| 5.52000 |
11.78049 |
| 5.65000 |
11.60952 |
| 5.60000 |
11.55425 |
| 5.60000 |
11.60798 |
| 5.45000 |
11.55349 |
| 5.50000 |
11.97917 |
| 5.70000 |
11.75556 |
| 5.57000 |
11.40000 |
The COMPARE procedure is then used to compare the risk factor values that were perturbed by using with SAS Risk Dimensions with the risk factor values that were perturbed by using DATA step. The results are printed to the SAS output window. The results are exactly the same:
* Compare the two sets of perturbed risk factor values;
proc compare data=hist_prtrb(firstobs=2) compare=simstate brief;
title "Comparison of Perturbed Risk Factors WITH and WITHOUT Using SAS Risk Dimensions";
var perturb_y1 perturb_y2;
with y1 y2;
run;
The COMPARE Procedure
Comparison of WORK.HIST_PRTRB with WORK.SIMSTATE
(Method=EXACT)
NOTE: No unequal values were found. All values compared are exactly equal.
|
Additional Documentation
For licensed customers of SAS Risk Dimensions, documentation is available at:
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.