I want to implement a way to generate scenarios which keep the relational structure between stocks. But in a way that lets me test these relations under extreme conditions.
So for example say A and B. And i want to generate a distribution where B shows extreme tail behaviour, for example it only ever returns losses less than 10% or gains greater than 10%
B \leq -0.1 \cup B \geq 0.1.
It’s unrealistic for actual assets, but for stuff like crypto on longer timescales lower frequencies (like daily returns or longer) this is not too wild.
Having a way to generate prior distributions that keep these relations under extreme events can help in the callibration of optimisations to make them less sensitive to such events.
using Copulas, Distributions
a = 0.1 # evaluation point
A = LogNormal(0,1)
B = Normal(0,1)
cop = ClaytonCopula{2}(0.7)
X = SklarDist(cop, (A,B))
Y = SklarDist(SurvivalCopula(cop, 2), (A,B)) # flips the second dimension.
x = cdf(X, [a,-0.1]) # this is P(A <= a, B <= -0.1)
y = cdf(Y, [t,0.1]) # this is P(A <= a, B >= 0.1)
z = cdf(B, -0.1) + ccdf(B, 0.1) # this is P(B \le -0.1 OR B \ge 0.1)
rez = (x+y) / z # this is therefore P(A <= a | B \le -0.1 OR B \ge 0.1)
Here i have the cdf, I think you can also get the pdf by being a bit clever. For sampling i would use truncation or even importance sampling looks the easiest.
Brother, this is it. I think that’s all i need to finally add this feature that’s been a thorn in my side for so long.
The extra dependence structures on the vines would be nice, but i might even be able to make that PR myself later down the road. Probably as an extension as it would probably take Clustering.jl as a dependency.