Resources

SAS Products

Assessing Market Risk Using the HPRISK Procedure



Objective

Set Up Your Environment

Generate a Portfolio

Register Market Data

Define Valuation Methods

Specify the Analysis and Create the Project

Export the Project

Run the HPRISK Procedure

Explore Results

Example Code (ZIP file)



Objective

In the following example, you create a portfolio that consists of shares of stock in two companies. You do the following:

This example assumes that the server tier is running on a Microsoft Windows system. If you run your code on a UNIX system, you must revise pathnames accordingly.



Set Up Your Environment

  1. Set up the library for analysis and specify the name of the SAS Risk Dimensions environment.
    libname RDExamp "C:\users\sasdemo\RD_Examples";
    libname HPExpLib "C:\users\sasdemo\RD_Examples";
    %let test_env = hp_marketrisk;
    %let cube_dir = C:\users\sasdemo\RD_Examples;
    
  2. Before you run the HPRISK procedure in distributed mode, you might need to specify the following options. Contact your administrator about whether to use them and for the appropriate values to specify if you do.

    GRIDHOST

    the host name for the grid.

    GRIDINSTALLLOC

    the path to the SAS High-Performance Risk installation on the grid.

    GRIDRSHCOMMAND

    the command for running a remote shell.

    GRIDREPLYHOST

    the host name of the client node to which the grid connects. Specify a fully qualified domain name (for example, rdgrd001.orion.com). Use this option when the client node has more than one network controller, or when you need to specify a full network name.

    GRIDPORTRANGE

    the ports that a firewall permits. Specify this option when the client-to-grid connection goes through a firewall.

  3. To run the HPRISK procedure in SMP mode, set GRIDHOST and GRIDINSTALLLOC to empty. Do not set the other options.
    option set=GRIDHOST="";
    option set=GRIDINSTALLLOC="";
    
  4. Create a new SAS Risk Dimensions environment that is assigned the name HP_marketrisk.
    proc risk;
      env new=RDExamp.&test_env;
      env save;
    run;
    


Generate a Portfolio

After you have set up your environment, run the following code to generate a portfolio of 10,000 instruments. This code randomly assigns an InstType, Region, and Trader_ID to an instrument. It then registers the instrument data in the environment.

%let ninst = 10000;
data RDExamp.instdata;
		length insttype $12 instid $12 region $8 trader_id $16 holding 5;
		do i = 1 to &ninst;
			if ranuni(54321) < 0.5 then insttype = "TechCompany";
		else insttype = "BankCompany";

		region_rand = rand('table',.15, .10, .35, .40);
			select (region_rand);
			when (1) do;Region = "North";
				trader_rand = 100 + rand('table',.30,.50,.20);
				Trader_ID = cats("Trader_",trader_rand);
				end;
			when (2) do; Region = "West";
				trader_rand = 200 + rand('table',.30,.50,.20);
				Trader_ID = cats("Trader_",trader_rand);
				end;
			when (3) do; Region = "South";
				trader_rand = 300 + rand('table',.30,.50,.20);
				Trader_ID = cats("Trader_",trader_rand);
	        	end;
			when (4) do; Region = "East";
				trader_rand = 400 + rand('table',.30,.50,.20);
				Trader_ID = cats("Trader_",trader_rand);
				end;
			otherwise;
			end;

			instid = cats("instid_",i);
			holding = ceil(ranuni(54321)*100);
			output;
			end;
			drop i region_rand trader_rand;
run;

proc risk;
  env open=RDExamp.&test_env;
  instdata Instruments file=RDExamp.instdata format=simple;
  declare instvars = (Trader_ID char 16 class,
  					 		  Region char 8 class)
  ;
  env save;
run;



Register Market Data

Two risk factors, TEC and BNK, represent the prices of TechCompany stock and BankCompany stock. Define current risk factor values and the variance-covariances. The following code declares the risk factors and registers the market data in the environment:

data RDExamp.mktdata;
  TEC=15;
  BNK=30;
;

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

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;
	env save;
run;



Define Valuation Methods

Create pricing methods to use in this environment with the COMPILE procedure. The COMPILE procedure is similar to and supports the functionality and syntax of the FCMP procedure. Include a computed method that calculates the computed variable myVaR.

For more information about the COMPILE procedure, see SAS Risk Dimensions and SAS High-Performance Risk: Procedures Guide.

proc compile env=RDExamp.&test_env outlib=RDExamp.&test_env package=distortion;
  method Tech_Price kind=price;
    _value_ = TEC;
  endmethod;
  method Bank_Price kind=price;
    _value_ = BNK;
  endmethod;
  method myVaR kind=computed;
   Value_VaR = get_queryval('_value_', 'VaR');
   myVaR = Value_VaR * 1.05;
  endmethod;

Within the COMPILE block, include a FUNCTION statement to create a function to calculate a user-defined statistic named MyMean.

function myMean(dist[*]);
  n = dim(dist);
  total = 0;
  do i = 1 to n;
    total = total + dist{i};
  end;
  total = 2*(total/n);
  return (total);
endsub;
run;

The following code uses the INSTRUMENT statement to tell the RISK procedure what pricing methods to use for each of the instruments. Also, it specifies the instrument variables that are needed for pricing. You do not need to specify InstType and InstID because they are included by default.

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



Specify the Analysis and Create the Project

Set up an analysis project in order to simulate values for your portfolio, based on the current market data and the covariance data. The following code creates a portfolio file from the instrument data set and identifies the cross-classifications for portfolio aggregation. Then, it creates a covariance simulation analysis. Finally, it creates the project, specifying the portfolio and assigning the covariance simulation analysis.

Notice that the SIMULATION statement specifies NDRAWS=100. With this relatively low number of draws, you can run the project with SAS Risk Dimensions in a separate PROC RISK block. Then, you can compare those results with those that are produced by SAS High-Performance Risk.

Notice also the COMPUTEDMETHODS argument to the PROJECT statement. This argument specifies the computed method that you created with the COMPILE procedure to include in the project.

proc risk;
  env open=RDExamp.&test_env;
  sources InstDataSource (Instruments);
  read sources=InstDataSource out=Portfolio;
  crossclass MarketRiskCC (Insttype Trader_ID Region);
  simulation Covariance method=covariance
          ndraws=100
          outvars=(pl)
          data=Cov
		  interval = weekday;
  project Project
    	    analysis=(Covariance)
          computedmethods=(myVaR)
          portfolio=Portfolio
          crossclass=MarketRiskCC
          data=( Market );
  env save;
run;



Export the Project

Export the project so that you can run it with the HPRISK procedure. Use the USERSTAT statement to export the user-defined statistic that you created with the COMPILE procedure.

proc hpexport
   env = RDExamp.&test_env
   project = Project
   expprojlib = HPExpLib
;
   userstat myMean func=myMean dist_type=standard;
run;



Run the HPRISK Procedure

After you export the analysis project with the HPEXPORT procedure, run the HPRISK procedure in order to create risk cubes. In the following code samples, you create two price cubes and one portfolio cube.

  1. Run the following code to create a price cube named HPRisk_Example. You generate market states, and then you price instruments against the base case and those market states. Set the value of the FILESPREFIX option to the local folder.
    proc hprisk
    	expprojlib = HPExpLib
    	cube = "&cube_dir\HPRisk_Example"
    	filesprefix = "&cube_dir\HPRisk_Example"
    	task = RUNPROJECT
    	;
    	crossclassvars
    		Region
    		InstType
    		Trader_ID
    		InstID
    		;
    	performance nodes = 1 nthreads = 1;
    quit;
    
  2. Run the following code to query results by the Region cross-classification variable. You write summary statistics to the output data set RDExamp.HP_Summary.
    proc hprisk
    	cube = "&cube_dir\HPRisk_Example"
    	task = QUERY
    	;
    	output
    		summary = RDexamp.hp_summary
    	;
    	query
    		Region
    	;
    quit;
    
  3. Run the following code to delete the risk cube descriptor file and the risk cube data files. You must delete these files if you intend to re-create the risk cube later. Cubes cannot be overwritten.
    proc hprisk
    	task = CLEAN
    	cube = "&cube_dir\HPRisk_Example"
    	;
    quit; 


Explore Results

The risk cube descriptor file must be in the proper location in order for you to explore it through the SAS High-Performance Risk UI. This location was specified by an administrator when the product was deployed. For more information, see “Post-Installation Tasks” in SAS High-Performance Risk: Administrator’s Guide.

For information about creating risk explorations through the SAS High-Performance Risk UI, see SAS High-Performance Risk: User’s Guide.


Back to Examples index page