Resources

SAS Products

SAS Risk Dimensions - Inserting Rare Events into a Simulation



Overview

Objective

Set Up Your Environment

Register Your Portfolio Data

Register Market Data

Define Valuation Methods

Specify the Analysis and Create the Project

Run the Project

View Results

Example Code (ZIP file)



Overview

When you perform risk analysis, two common assessment techniques are simulation and stress scenarios. Simulations are often based on behavior that you have observed in the past. A shortcoming to that approach is that simulations can fail to capture extreme events. These are events that might never have occurred, or might have occurred with such infrequency that they are not realized in simulation. Stress scenarios, on the other hand, look exclusively at extreme events. They predict the likelihood that such an event will occur.

SAS Risk Dimensions enables you to incorporate stress scenarios into a simulation. You can define a rare event and assign it a probability of occurrence. As a simulation model runs, it randomly triggers the rare event. For example, you can define a rare event such as the .005% chance that a stock price will decrease $10 in one day. On average, you should see that the rare event occurs in the simulation with a frequency equal to the probability that you specify .

You can also assign a conditional probability to the rare event that assigns the likelihood of occurrence given that the event occurred in a previous horizon. This captures the notion of increased risk following an extreme event.



Objective

In this example you simulate the value of a portfolio containing two shares of stock (see "Assessing Market Risk" for more information). You generate 50,000 market states and calculate the portfolio's value and profit/loss at each market state. You also define two stress scenarios, a BankCompany shock in which BNK stock drops $0.50/share and a TechCompany shock in which TEC stock drops 20%.

You assign a probability and conditional probability to the stress scenarios by creating a rare event probability data set. When the simulation is run, the stress scenarios are randomly triggered according to the probabilities you specify in that data set.

When the project has run, look at the output data set named RareEvents to see when the stress scenarios were triggered during simulation.



Set Up Your Environment

First, specify the library for analysis and the name of the SAS Risk Dimensions environment. Use the directory C:\users\sasdemo\RD_Examples. You can assign the library to any path as long as you have Write access to that directory.

libname RDExamp "C:\users\sasdemo\RD_Examples";
%let test_env = rareevent;

In this code, you use the SAS Macro Language Facility to create the macro variable test_env. You assign the value rareevent to the variable test_env. Using macro variables in this way gives you the flexibility to change the physical location of the target library and environment name in just two lines of code.

Next, create a new SAS Risk Dimensions environment assigned the name rareevent in the library RDExamp.

proc risk;
env new=RDExamp.&test_env;
env save;
run;


Register Your Portfolio Data

The portfolio has two instruments, 1M shares of TechCompany stock and 1M shares of BankCompany stock.

data RDExamp.instdata;
input insttype $12. instid $8. holding 8.;
datalines;
TechCompany instid1 1000000
BankCompany instid2 1000000
;

proc risk;
env open=RDExamp.&test_env;
instdata Instruments file=RDExamp.instdata format=simple;
env save;
run;



Register Market Data

The current value of TechCompany stock is $15/share and BankCompany stock is $3/share. The following code inputs the market data and the covariance matrix for the risk factors.

data RDExamp.mktdata;
TEC=15;
BNK=3;
run;

data RDExamp.covdata;
length _name_ _type_ $8;
_type_ = "COV";
input _name_ $ TEC BNK;
datalines;
TEC .0004 .00006
BNK .00006 .0001
;

Now, add the rare events. Define the stress scenarios using the SCENARIO statement, and then assign them probabilities and conditional probabilities using the rare events probability data set.

proc risk;
env open=RDExamp.&test_env;
scenario BNKStress type=abs
changes=( BNK -.50 );
scenario TECStress type=rel
changes=( TEC -.20);
env save;
run;

data RDExamp.RareEventMap;
length eventname $32.;
input eventname $ uncond_prob cond_prob;
datalines;
BNKStress .20 .25
TECStress .05 .10
;

The value of the portfolio depends on two risk factors, TEC and BNK, the stock prices of TechCompany and BankCompany respectively. Declare them, and then register the market data, the covariance matrix, and the rare events data set with the MARKETDATA statement. For the rare events data set, specify option values for the random seed and time interval associated with the probability.

proc risk;
env open=RDExamp.&test_env;
declare riskfactors=(TEC num var, BNK num var);
marketdata Market file=RDExamp.mktdata type=current;
marketdata Cov file=RDExamp.covdata type=covariance;
marketdata RareEvents file = RDExamp.RareEventMap
type = rareevent(seed = 1234) interval = day;
env save;
run;


Define Valuation Methods

Create pricing methods using the COMPILE procedure for use in this environment. The COMPILE procedure is similar to and supports the functionality and syntax of the FCMP procedure. For more information, see the SAS Risk Dimensions and SAS High-Performance Risk: Procedures Guide.

With the RISK procedure, specify which pricing methods to use for each of the instrument types through assignment in the INSTRUMENT statement. Also specify the instrument variable needed for pricing; insttype and instid do not need to be specified as they are included by default.

proc compile env=RDExamp.&test_env outlib=RDExamp.&test_env;
method Tech_Price kind=price;
_value_ = TEC;
endmethod;
method Bank_Price kind=price;
_value_ = BNK; endmethod;
run;

proc risk;
env open=RDExamp.&test_env; instrument TechCompany methods=(Price Tech_Price)
variables = (holding);
instrument BankCompany methods=(Price Bank_Price)
variables = (holding);
env save;
run;



Specify an Analysis and Create the Project

Set up a project to simulate values for the portfolio based on the current market data, the covariance data, and the rare events data set. In the SIMULATION statement, reference the covariance data set and the rare events data set that you created and registered previously. Add the option RAREEVENTS to the PROJECT statement in order to output the rare events output data set.

proc risk; env open=RDExamp.&test_env;
sources InstDataSource (Instruments);
read sources=InstDataSource out=Portfolio;
crossclass RareEventCC (Insttype);
simulation Covariance method=covariance
ndraws=10000
outvars=(pl)
horizon = (1 2 3 4 5)
data= (Cov RareEvents)
;
project Project
analysis=(Covariance)
portfolio=Portfolio
crossclass=RareEventCC
data=( Market )
;
env save;
run;


Run the Project

Run the project, putting output files in the RareEvt subdirectory of the environment directory.

proc risk;
env open=RDExamp.&test_env;
setoptions keeplib;
runproject Project
out = RareEvt
options=(outall rareevents)
;
env save;
run;


View Results

The output data set RareEvents shows you when the rare events were triggered. This is a screenshot of the RareEvents data set.

output data set RareEvents


Parsing this data set, you can create a data set that can be input to the FREQ procedure. With the FREQ procedure you can verify that the rare events occurred with the frequency specified in the rare events probability data set.

The following SAS code produces the subsequent frequency tables:

data rareevt.TECStress;
do i = 1 to 10000;
simulationreplication = i;
do j = 1 to 5;
simulationtime = j;
output;
end;
end;
drop i j;
run;

data rareevt.TECStress;
set rareevt.TECStress;
length eventname $20.;
if _n_ eq 1 then do;
declare hash h(dataset:'rareevt.rareevents');
h.defineKey('simulationreplication' ,'simulationtime');
h.defineData('eventname');
h.defineDone();
end;
if h.find() eq 0 then output;
else do;
eventname = "Underlying Model";
output;
end;
run;

proc transpose data = rareevt.TECStress
out = rareevt.TECStressT
prefix = SimulationTime;
by simulationreplication;
id simulationtime;
var eventname;
run;

proc freq data = rareevt.tecstresst;
tables SimulationTime1/NoFreq NoCum;
tables SimulationTime1*SimulationTime2
SimulationTime2*SimulationTime3
SimulationTime3*SimulationTime4
SimulationTime4*SimulationTime5/NoFreq Nopercent NoCol ;
run;

frequency tables


The results displayed in these frequency tables show that the rare events occurred as expected. If BNKStress was triggered in a given horizon, it occurred again in the next horizon with a probability of around 25%.



Back to Examples index page