Simultaneous increase or decrease of derivative exposure and the counterparty credit risk is generally known as wrong way risk. Since CVA depends on both risk factors, the wrong way risk [WWR is unpleasant side effect that requires special attention when quantifying the CVA measure. There are several approaches to capture the WWR - regulatory, correlation and stress testing are the ones used most frequently. We propose a different route - the Mathematica-enabled solution using logistic regression model that results in easy-to-implement application that uses market observables.
Hi Igor, I’m trying to replicate your examples in “Credit Valuation Adjustment with Mathematica 10” but I’m having difficulties with the swap expected positive exposure and mean hazard rate functions.
Mathematica complains regarding the window specification using Max[dt, x] since dt has not been defined.
May I know how to define dt?
Hello Ruben
As you can see below, the dt has actually been defined:
Swap rate & CSD joint process defintion
cW[?_]:=Refine[ItoProcess[{{0,0},IdentityMatrix[2]},{{n1,n2},{0,0}},t,{{1,?},{?,1}}],-1<?<1];
combprc=ItoProcess[
{\[DifferentialD]s[t]==? s[t] \[DifferentialD]t+?1*s[t] \[DifferentialD]n1[t],
\[DifferentialD]c[t]==? (?-c[t]) \[DifferentialD]t+?2*Sqrt[Abs[c[t]]] \[DifferentialD]n2[t]},
{s[t],c[t]},{{s,c},{s0,c0}},t,{n1,n2}\[Distributed]cW[?]];
{dt,y,R}={0.005,5,0.4};
Hi Igor,
thanks a lot for the answer!
I’m now able to generate the graphs related to the swap expected positive exposure and the mean hazard rate. I also have no problems replicating the survival probability plot and the conditional probabilities as a function of time graph. Unfortunately, when I evaluate the CVAS formula, Mathematica returns a series of errors:
CVAS[n_, fr_, r_] :=
Module[{f, g, s},
f = TemporalData[Table[{i, survfunc[i]}, {i, 0, n, fr}]];
g = MovingMap[First[#] - Last[#] &, f, {2}];
s = (1 - r)*
Sum[DF[i]*epe[i]*g["PathFunction"][i], {i, fr, n, fr}];
s];
CVAS[5, 1/2, 0.4]*10^4
InterpolatingFunction::dmval: Input value {-(1/2)} lies outside the range of data in the interpolating function. Extrapolation will be used.
InterpolatingFunction::dmval: Input value {-1} lies outside the range of data in the interpolating function. Extrapolation will be used.
InterpolatingFunction::dmval: Input value {-(3/2)} lies outside the range of data in the interpolating function. Extrapolation will be used.
General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation.
16.7952
May I know how to solve this issue?
I’m attaching the notebook as well for further reference.
Many thanks in advance
Ruben
This must be due to the data you are using. I can see that Mathematica generates negative values along the code execution, which leads to the inconsistent valuation.
Try again with my values:
zrates = {0.012, 0.015, 0.0167, 0.0176, 0.019, 0.0201, 0.0223, 0.0235};
times = {0.25, 0.5, 1, 1.5, 2, 3, 4, 5};
zrobj = TemporalData[zrates, {times}];
intrate = Interpolation[zrobj["Path"], Method -> "Spline"];
DF[x_] := Exp[-intrate[x]*x];
{dt, y, R} = {0.005, 5, 0.4};
simproc =
RandomFunction[
combprc /. {\[Mu] -> 0.022, \[Sigma]1 -> 0.2, \[Theta] ->
0.005, \[Beta] -> 0.015, \[Sigma]2 -> 0.05, \[Rho] -> 0.5,
s0 -> 0.025, c0 -> 0.0125}, {0, y, dt}, 1000,
Method -> "StochasticRungeKutta"] ;
Exposure[T_, S_, t_, v_] := (T - t)*Max[v - S, 0];
expswap = TimeSeriesMapThread[Exposure[5, 0.025, #1, #2] &, swapproc];
eeswap = TimeSeriesThread[Mean, expswap,
ResamplingMethod -> Interpolation];
eehaz = TimeSeriesThread[Mean, hazrate,
ResamplingMethod -> Interpolation];
epe[x_] :=
Mean[TimeSeriesWindow[eeswap, {0, Max[dt, x]},
ResamplingMethod -> Interpolation]];
exphazrate[x_] :=
Mean[TimeSeriesWindow[eehaz, {0, Max[dt, x]},
ResamplingMethod -> Interpolation]];
survfunc[x_] := Exp[-exphazrate[x]*x];
CVAS[n_, fr_, r_] :=
Module[{f, g, s},
f = TemporalData[Table[{i, survfunc[i]}, {i, 0, n, fr}]];
g = MovingMap[First[#] - Last[#] &, f, {2}];
s = (1 - r)*Sum[DF[i]*epe[i]*g["PathFunction"][i], {i, fr, n, fr}];
s];
As you can see, I get the following result with my values:
CVAS[5, 1/2, 0.4]*10^4
4.74152
Hi Igor,
thanks a lot for the code, unfortunately, using Mathematica 11.1.1 that’s not what I get:
In[1]:= zrates = {0.012, 0.015, 0.0167, 0.0176, 0.019, 0.0201, 0.0223,
0.0235};
times = {0.25, 0.5, 1, 1.5, 2, 3, 4, 5};
zrobj = TemporalData[zrates, {times}];
In[4]:= intrate = Interpolation[zrobj["Path"], Method -> "Spline"];
In[5]:= DF[x_] := Exp[-intrate[x]*x];
{dt, y, R} = {0.005, 5, 0.4};
In[8]:= cW[\[Rho]_] :=
Refine[ItoProcess[{{0, 0}, IdentityMatrix[2]}, {{n1, n2}, {0, 0}},
t, {{1, \[Rho]}, {\[Rho], 1}}], -1 < \[Rho] < 1];
In[9]:= combprc =
ItoProcess[{\[DifferentialD]s[
t] == \[Mu] s[t] \[DifferentialD]t + \[Sigma]1*
s[t] \[DifferentialD]n1[t], \[DifferentialD]c[
t] == \[Theta] (\[Beta] - c[t]) \[DifferentialD]t + \[Sigma]2*
Sqrt[Abs[c[t]]] \[DifferentialD]n2[t]}, {s[t],
c[t]}, {{s, c}, {s0, c0}}, t, {n1, n2} \[Distributed] cW[\[Rho]]];
In[10]:= simproc =
RandomFunction[
combprc /. {\[Mu] -> 0.022, \[Sigma]1 -> 0.2, \[Theta] ->
0.005, \[Beta] -> 0.015, \[Sigma]2 -> 0.05, \[Rho] -> 0.5,
s0 -> 0.025, c0 -> 0.0125}, {0, y, dt}, 1000,
Method -> "StochasticRungeKutta"];
In[11]:= Exposure[T_, S_, t_, v_] := (T - t)*Max[v - S, 0];
In[12]:= swapproc = simproc["PathComponent", 1];
In[13]:= expswap =
TimeSeriesMapThread[Exposure[5, 0.025, #1, #2] &, swapproc];
In[14]:= cdsproc = simproc["PathComponent", 2];
In[15]:= hazrate = TimeSeriesMap[#/(1 - R) &, cdsproc];
In[16]:= eeswap =
TimeSeriesThread[Mean, expswap, ResamplingMethod -> Interpolation];
eehaz = TimeSeriesThread[Mean, hazrate,
ResamplingMethod -> Interpolation];
In[18]:= epe[x_] :=
Mean[TimeSeriesWindow[eeswap, {0, Max[dt, x]},
ResamplingMethod -> Interpolation]];
In[19]:= exphazrate[x_] :=
Mean[TimeSeriesWindow[eehaz, {0, Max[dt, x]},
ResamplingMethod -> Interpolation]];
In[20]:= survfunc[x_] := Exp[-exphazrate[x]*x];
In[21]:= CVAS[n_, fr_, r_] :=
Module[{f, g, s},
f = TemporalData[Table[{i, survfunc[i]}, {i, 0, n, fr}]];
g = MovingMap[First[#] - Last[#] &, f, {2}];
s = (1 - r)*
Sum[DF[i]*epe[i]*g["PathFunction"][i], {i, fr, n, fr}];
s];
In[22]:= CVAS[5, 1/2, 0.4]*10^4
During evaluation of In[22]:= InterpolatingFunction::dmval: Input value {-(1/2)} lies outside the range of data in the interpolating function. Extrapolation will be used.
During evaluation of In[22]:= InterpolatingFunction::dmval: Input value {-1} lies outside the range of data in the interpolating function. Extrapolation will be used.
During evaluation of In[22]:= InterpolatingFunction::dmval: Input value {-(3/2)} lies outside the range of data in the interpolating function. Extrapolation will be used.
During evaluation of In[22]:= General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation.
Out[22]= 16.5418
Any ideas why is still not working?
Hello Ruben
Pls execute the function with the Quiet flag
CVAS[5, 1/2, 0.4]*10^4 // Quiet
That will solve your problem…
In[34]:= CVAS[n_, fr_, r_] :=
Module[{f, g, s},
f = TemporalData[Table[{i, survfunc[i]}, {i, 0, n, fr}]];
g = MovingMap[First[#] - Last[#] &, f, {2}];
s = (1 - r)*Sum[DF[i]*epe[i]*g[“PathFunction”][i], {i, fr, n, fr}];
s];
In[39]:= CVAS[5, 1/2, 0.4]*10^4 // Quiet
Out[39]= 19.0374
Hope this helps
Igor
Hi Igor,
yes, that took care of the problem. Thanks a lot!
By the way, do you have any idea why Mathematica returns the error message?
