12%let ValueAtRisk_Level = ${params.VARLEVEL};
14%let fa_path=${params.FAPATH};
15%let path_to_ucmacros=&fa_path./source/sas/ucmacros;
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;
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;
29 project mipProject projectmethods = base_project;
33 %irmst_market_risk_factors_def_1;
34 %irmst_market_risk_factors_def_2;
36 setoptions alpha=%sysevalf(1-&ValueAtRisk_Level.);
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;
50proc compile env=mipenv.risk_env outlib=mipenv.risk_env package=BetaCoefficient;
51function betacoef(dist[*], basecase, outvar $, horizonidx);
53 call get_ranked_values_top(., ., top_vector_ascending,top_index_vector);
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));
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];
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];
70 sum_current_vector = 0;
71 sum_product_vector = 0;
72 sum_topsquared_vector = 0;
73 sum_currentsquared_vector = 0;
75 n = dim(top_vector_ascending);
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];
84 E_V=sum_current_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);
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);
100 Beta_coef=CORR_PV*STD_V/STD_P;
110proc compile env=mipenv.risk_env outlib=mipenv.risk_env package=CVaRQuad;
111function CVaR_Quad(dist[*], basecase, outvar $, horizonidx);
114 call get_ranked_values_top(., ., top_vector_ascending,top_index_vector);
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));
121 do i = 1 to dim(current_vector_ascending);
122 parent_index = top_index_vector[i];
123 current_vector_toporder[i] = dist[parent_index];
133 var_value = top_vector_ascending[varindex];
135 array temporarytop[40] / nosym;
136 array temporarycurrent[40] /nosym;
139 temporarytop[i] = top_vector_ascending[i+15];
140 temporarycurrent[i] = current_vector_toporder[i+15];
143 call dynamic_array(current_vector_toporder,40);
144 call dynamic_array(top_vector_ascending,40);
147 top_vector_ascending[i] = temporarytop[i];
148 current_vector_toporder[i] = temporarycurrent[i];
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;
162 n = dim(top_vector_ascending);
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];
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;
196 CVaR_Quad = -1 * (intercept + xcoeff * var_value + x2coeff * var_value**2);