Specify the Analysis and Create the Project
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.
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;
GRIDHOST
GRIDINSTALLLOC
GRIDRSHCOMMAND
GRIDREPLYHOST
GRIDPORTRANGE
option set=GRIDHOST=""; option set=GRIDINSTALLLOC="";
proc risk; env new=RDExamp.&test_env; env save; run;
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;
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;
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;
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 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;
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.
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;
proc hprisk cube = "&cube_dir\HPRisk_Example" task = QUERY ; output summary = RDexamp.hp_summary ; query Region ; quit;
proc hprisk task = CLEAN cube = "&cube_dir\HPRisk_Example" ; quit;
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.