SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_model_mip_preexec_SimMarket.sas
1/* --------------------------------------------------------------------------------------------------------------------
2 * Use a pre-execution program to customize the SAS Risk Dimensions environment before the analysis run is executed.
3 *
4 * Example: proc risk;
5 * env new=mipenv.risk_env inherit=(mimp_sas.sas_risk_env);
6 * setoptions NOBACKSAVE;
7 * project mipProject projectmethods = base_project;
8 * env save;
9 * run;
10 * --------------------------------------------------------------------------------------------------------------------*/
11
12%let ValueAtRisk_Level = ${params.VARLEVEL};
13
14%let fa_path=${params.FAPATH};
15%let path_to_ucmacros=&fa_path./source/sas/ucmacros;
16
17%INCLUDE "&path_to_ucmacros./irmst_market_risk_factors_def_1.sas" / source2 lrecl = 32000;
18%INCLUDE "&path_to_ucmacros./irmst_market_risk_factors_def_2.sas" / source2 lrecl = 32000;
19%include "&path_to_ucmacros./irmst_market_risk_funs_def_1.sas" / source2 lrecl = 32000;
20%include "&path_to_ucmacros./irmst_market_risk_funs_def_2.sas" / source2 lrecl = 32000;
21%include "&path_to_ucmacros./irmst_market_risk_funs_def_3.sas" / source2 lrecl = 32000;
22
23 proc risk;
24 env new=mipenv.risk_env inherit=(mimp_sas.sas_risk_env)
25 label="MIP run risk environment created by pre-execution program";
26 setoptions NOBACKSAVE;
27
28 /*-create default project-*/
29 project mipProject projectmethods = base_project;
30 /* end: project */
31
32 /*define risk factors*/
33 %irmst_market_risk_factors_def_1;
34 %irmst_market_risk_factors_def_2;
35
36 setoptions alpha=%sysevalf(1-&ValueAtRisk_Level.);
37env save;
38run;
39
40
41proc compile outlib = mipenv.risk_env env = mipenv.risk_env package = market_risk;
42 %irmst_market_risk_funs_def_1;
43 %irmst_market_risk_funs_def_2;
44 %irmst_market_risk_funs_def_3;
45run;
46
47
48/*region user defined stat example - betas/correlation risk contributions */
49
50proc compile env=mipenv.risk_env outlib=mipenv.risk_env package=BetaCoefficient;
51function betacoef(dist[*], basecase, outvar $, horizonidx);
52
53 call get_ranked_values_top(., ., top_vector_ascending,top_index_vector);
54
55 call get_ranked_values(., ., current_vector_ascending,current_index_vector);
56 array top_vector_neworder[1] / nosym;
57 call dynamic_array(top_vector_neworder,dim(top_index_vector));
58 array current_vector_neworder[1] / nosym;
59 call dynamic_array(current_vector_neworder,dim(current_index_vector));
60
61 do i = 1 to dim(top_vector_ascending);
62 top_new_index = top_index_vector[i];
63 top_vector_neworder[top_new_index] = top_vector_ascending[i];
64 end;
65 do i = 1 to dim(current_vector_ascending);
66 current_new_index = current_index_vector[i];
67 current_vector_neworder[current_new_index ] = current_vector_ascending[i];
68 end;
69 sum_top_vector = 0;
70 sum_current_vector = 0;
71 sum_product_vector = 0;
72 sum_topsquared_vector = 0;
73 sum_currentsquared_vector = 0;
74
75 n = dim(top_vector_ascending);
76 do i = 1 to n;
77 sum_top_vector = sum_top_vector + top_vector_neworder[i];
78 sum_current_vector = sum_current_vector + current_vector_neworder[i];
79 sum_product_vector = sum_product_vector + top_vector_neworder[i]*current_vector_neworder[i];
80 sum_topsquared_vector = sum_topsquared_vector + top_vector_neworder[i]*top_vector_neworder[i];
81 sum_currentsquared_vector = sum_currentsquared_vector + current_vector_neworder[i]*current_vector_neworder[i];
82 end;
83
84 E_V=sum_current_vector/n;
85 E_P=sum_top_vector/n;
86 E_PV=sum_product_vector/n;
87 E_V2=sum_currentsquared_vector/n;
88 E_P2=sum_topsquared_vector/n;
89 VAR_V=max(E_V2-(E_V)**2,0);
90 VAR_P=max(E_P2-(E_P)**2,0);
91 STD_V=VAR_V**0.5;
92 STD_P=VAR_P**0.5;
93 if STD_V>0 and STD_P>0 then do;
94 CORR_PV=max((E_PV-E_P*E_V)/(STD_P*STD_V),0);
95 end;
96 else do;
97 CORR_PV=0;
98 end;
99 if STD_P>0 then do;
100 Beta_coef=CORR_PV*STD_V/STD_P;
101 end;
102 else do;
103 Beta_coef=0;
104 end;
105
106return (Beta_coef);
107endsub;
108run;
109
110proc compile env=mipenv.risk_env outlib=mipenv.risk_env package=CVaRQuad;
111function CVaR_Quad(dist[*], basecase, outvar $, horizonidx);
112
113 /* Get states vector for top portfolio sorted ascending */
114 call get_ranked_values_top(., ., top_vector_ascending,top_index_vector);
115
116 /* Get states vector for current subportfolio - sorted in same order as top */
117 call get_ranked_values(., ., current_vector_ascending,current_index_vector);
118 array current_vector_toporder[1] / nosym;
119 call dynamic_array(current_vector_toporder,dim(current_index_vector));
120
121 do i = 1 to dim(current_vector_ascending);
122 parent_index = top_index_vector[i];
123 current_vector_toporder[i] = dist[parent_index];
124 end;
125
126
127
128 /* Subset vector to observations 16-55 */
129
130 /*call state_info('alpha',varAlpha);
131 var_index=round(dim(dist) *varAlpha);*/
132 varindex = 35;/* hardcode for now - can set dynamically by call state_info - see above */
133 var_value = top_vector_ascending[varindex];
134
135 array temporarytop[40] / nosym;
136 array temporarycurrent[40] /nosym;
137
138 do i = 1 to 40;
139 temporarytop[i] = top_vector_ascending[i+15];
140 temporarycurrent[i] = current_vector_toporder[i+15];
141 end;
142
143 call dynamic_array(current_vector_toporder,40);
144 call dynamic_array(top_vector_ascending,40);
145
146 do i = 1 to 40;
147 top_vector_ascending[i] = temporarytop[i];
148 current_vector_toporder[i] = temporarycurrent[i];
149 end;
150
151 /* Calculate quadratic fit for subset vectors: X-axis is the top portfolio, Y-axis is the current sub-portfolio */
152
153 sum_top_vector = 0;
154 sum_current_vector = 0;
155 sum_product_vector = 0;
156 sum_topsquared_vector = 0;
157 sum_currentsquared_vector = 0;
158 sum_topsquared_squared_vector = 0;
159 sum_topcubed_vector = 0;
160 sum_productsquared_vector = 0;
161
162 n = dim(top_vector_ascending);
163
164 do i = 1 to dim(top_vector_ascending);
165 sum_top_vector = sum_top_vector + top_vector_ascending[i];
166 sum_current_vector = sum_current_vector + current_vector_toporder[i];
167 sum_product_vector = sum_product_vector + top_vector_ascending[i]*current_vector_toporder[i];
168 sum_topsquared_vector = sum_topsquared_vector + top_vector_ascending[i]*top_vector_ascending[i];
169 sum_currentsquared_vector = sum_currentsquared_vector + current_vector_toporder[i]*current_vector_toporder[i];
170 sum_topsquared_squared_vector = sum_topsquared_squared_vector +
171 top_vector_ascending[i]*top_vector_ascending[i]*top_vector_ascending[i]*top_vector_ascending[i];
172 sum_topcubed_vector = sum_topcubed_vector + top_vector_ascending[i]*top_vector_ascending[i]*top_vector_ascending[i];
173 sum_productsquared_vector = sum_productsquared_vector + top_vector_ascending[i]*top_vector_ascending[i]*current_vector_toporder[i];
174 end;
175
176
177 s11 = sum_topsquared_vector - (sum_top_vector**2)/n;
178 s12 = sum_topcubed_vector - (sum_top_vector * sum_topsquared_vector)/n;
179 s22 = sum_topsquared_squared_vector - (sum_topsquared_vector**2)/n;
180 sy1 = sum_product_vector - (sum_top_vector*sum_current_vector)/n;
181 sy2 = sum_productsquared_vector - (sum_current_vector*sum_topsquared_vector)/n;
182 x1bar = sum_top_vector/n;
183 x2bar = sum_topsquared_vector/n;
184 ybar = sum_current_vector/n;
185 if (s22*s11-s12**2) eq 0 then do;
186 xcoeff = .; x2coeff = .; intercept = .; end; else do;
187 xcoeff = (sy1*s22-sy2*s12)/(s22*s11-s12**2);
188 x2coeff = (sy2*s11 - sy1*s12)/(s22*s11-s12**2);
189 intercept = ybar - xcoeff*x1bar - x2coeff*x2bar;
190 end;
191
192
193
194
195 /* Compute CVaR by plugging in VaR of top into regression equation */
196 CVaR_Quad = -1 * (intercept + xcoeff * var_value + x2coeff * var_value**2);
197return (CVaR_Quad);
198endsub;
199run;
200
201
202