forked from djbolder/credit-risk-modelling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarianceReduction.py
191 lines (176 loc) · 6.68 KB
/
varianceReduction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
import cmUtilities as util
import importlib
from scipy.stats import norm
from scipy.stats import t as myT
import scipy
import thresholdModels as th
import varContributions as vc
importlib.reload(th)
importlib.reload(vc)
importlib.reload(util)
def myFunction(x):
return 7*np.sin(5*x) + 5*np.power(x,3) + 3*np.exp(x/2)
def mcIntegration(M):
U = np.random.uniform(0,1,M)
return np.mean(myFunction(U))
def naiveNumericalIntegration(K,a,b):
myGrid = np.linspace(a,b,K)
myIntegral = 0
for n in range(0,K-1):
dx = myGrid[n+1]-myGrid[n]
myIntegral += myFunction(myGrid[n+1])*dx
return myIntegral
def getQ(theta,c,p):
return np.divide(p*np.exp(c*theta),vc.computeMGF(theta,p,c))
def meanShiftOF(mu,c,p,l,myRho):
pZ = th.computeP(p,myRho,mu)
theta = vc.getSaddlePoint(pZ,c,l,0.0)
f_l = -theta*l + vc.computeCGF(theta,pZ,c)
return -(f_l - 0.5*np.dot(mu,mu))
def getOptimalMeanShift(c,p,l,myRho):
r = scipy.optimize.minimize(meanShiftOF,-1.0,args=(c,p,l,myRho),
method='SLSQP',jac=None,bounds=[(-4.0,4.0)])
return r.x
def isIndDefault(N,M,p,c,l):
U = np.random.uniform(0,1,[M,N])
theta = vc.getSaddlePoint(p,c,l,0.0)
qZ = getQ(theta,c,p)
cgf = vc.computeCGF(theta,p,c)
I = np.transpose(1*np.less(U,qZ))
L = np.dot(c,I)
rn = computeRND(theta,L,cgf)
tailProb = np.mean(np.multiply(L>l,rn))
eShortfall = np.mean(np.multiply(L*(L>l),rn))/tailProb
return tailProb,eShortfall
def isThreshold(N,M,p,c,l,myRho,nu,shiftMean,isT,invVector=0):
mu = 0.0
gamma = 0.0
if shiftMean==1:
mu = getOptimalMeanShift(c,p,l,myRho)
theta = np.zeros(M)
cgf = np.zeros(M)
qZ = np.zeros([M,N])
G = np.transpose(np.tile(np.random.normal(mu,1,M),(N,1)))
e = np.random.normal(0,1,[M,N])
if isT==1:
gamma = -2
W = np.random.chisquare(nu,M)
myV = W/(1-2*gamma)
V = np.transpose(np.sqrt(np.tile(myV,(N,1))/nu))
num = (1/V)*myT.ppf(p,nu)*np.ones((M,1))-np.multiply(np.sqrt(myRho),G)
pZ = norm.cdf(np.divide(num,np.sqrt(1-myRho)))
elif isT==2:
V = np.transpose(np.sqrt(np.tile(np.random.gamma(nu,1/nu,M),(N,1))))
num = (1/V)*invVector*np.ones((M,1))-np.multiply(np.sqrt(myRho),G)
pZ = norm.cdf(np.divide(num,np.sqrt(1-myRho)))
else:
pZ = th.computeP(p,myRho,G)
for n in range(0,M):
theta[n] = vc.getSaddlePoint(pZ[n,:],c,l,0.0)
qZ[n,:] = getQ(theta[n],c,pZ[n,:])
cgf[n] = vc.computeCGF(theta[n],pZ[n,:],c)
I = np.transpose(1*np.less(e,norm.ppf(qZ)))
L = np.dot(c,I)
if isT==1:
rnChi = np.exp(-gamma*myV-(nu/2)*np.log(1-2*gamma))
else:
rnChi = np.ones(M)
if shiftMean==1:
rn = computeRND(theta,L,cgf)*np.exp(-mu*G[:,0]+0.5*(mu**2))*rnChi
else:
rn = computeRND(theta,L,cgf)*rnChi
tailProb = np.mean(np.multiply(L>l,rn))
eShortfall = np.mean(np.multiply(L*(L>l),rn))/tailProb
return tailProb,eShortfall
def isMixture(N,M,p,c,l,p1,p2):
theta = np.zeros(M)
cgf = np.zeros(M)
qS = np.zeros([M,N])
S = np.random.gamma(p1, 1/p1, [M])
wS = np.transpose(np.tile(1-p2 + p2*S,[N,1]))
pS = np.tile(p,[M,1])*wS
for n in range(0,M):
theta[n] = vc.getSaddlePoint(pS[n,:],c,l,-0.2)
qS[n,:] = getQ(theta[n],c,pS[n,:])
cgf[n] = vc.computeCGF(theta[n],pS[n,:],c)
I = np.transpose(1*np.greater_equal(np.random.poisson(qS,[M,N]),1))
L = np.dot(c,I)
rn = computeRND(theta,L,cgf)
tailProb = np.mean(np.multiply(L>l,rn))
eShortfall = np.mean(np.multiply(L*(L>l),rn))/tailProb
return tailProb,eShortfall
def computeRND(theta,L,cgf):
return np.exp(-np.multiply(theta,L)+cgf)
def isThresholdSimple(N,M,p,c,l,myRho):
mu = getOptimalMeanShift(c,p,l,myRho)
theta = np.zeros(M)
cgf = np.zeros(M)
qZ = np.zeros([M,N])
e = np.random.normal(0,1,[M,N])
G = np.transpose(np.tile(np.random.normal(mu,1,M),(N,1)))
num = (norm.ppf(p)*np.ones((M,1)))-np.sqrt(myRho)*G
pZ = norm.cdf(np.divide(num,np.sqrt(1-myRho)))
for n in range(0,M):
theta[n] = vc.getSaddlePoint(pZ[n,:],c,l,0.0)
qZ[n,:] = getQ(theta[n],c,pZ[n,:])
cgf[n] = vc.computeCGF(theta[n],pZ[n,:],c)
I = np.transpose(1*np.less(e,norm.ppf(qZ)))
L = np.dot(c,I)
rn = np.exp(-mu*G[:,0]+0.5*(mu**2))*computeRND(theta,L,cgf)
tailProb = np.mean(np.multiply(L>l,rn))
eShortfall = np.mean(np.multiply(L*(L>l),rn))/tailProb
return tailProb,eShortfall
def isThresholdT(N,M,p,c,l,myRho,nu,cm=0):
myShift = (1-2*cm)
mu = getOptimalMeanShift(c,p,l,myRho)
W = np.random.chisquare(nu,M)
myV = W/myShift
theta = np.zeros(M)
cgf = np.zeros(M)
qZ = np.zeros([M,N])
V = np.transpose(np.sqrt(np.tile(myV,(N,1))/nu))
e = np.random.normal(0,1,[M,N])
G = np.transpose(np.tile(np.random.normal(mu,1,M),(N,1)))
num = V*(myT.ppf(p,nu)*np.ones((M,1)))-np.sqrt(myRho)*G
pZ = norm.cdf(np.divide(num,np.sqrt(1-myRho)))
for n in range(0,M):
theta[n] = vc.getSaddlePoint(pZ[n,:],c,l,0.0)
qZ[n,:] = getQ(theta[n],c,pZ[n,:])
cgf[n] = vc.computeCGF(theta[n],pZ[n,:],c)
I = np.transpose(1*np.less(e,norm.ppf(qZ)))
L = np.dot(c,I)
rnChi = np.exp(-cm*myV-(nu/2)*np.log(myShift))
rnMu=np.exp(-mu*G[:,0]+0.5*(mu**2))
rnTwist = computeRND(theta,L,cgf)
rn = rnChi*rnMu*rnTwist
tailProb = np.mean(np.multiply(L>l,rn))
eShortfall = np.mean(np.multiply(L*(L>l),rn))/tailProb
return tailProb,eShortfall
def isThresholdContr(N,M,p,c,l,myRho,nu,cm=0):
myShift = (1-2*cm)
xhat = scipy.optimize.minimize(meanShiftOF,0.01,
args=(c,p,l,myRho),
method='SLSQP', jac=None)
mu = xhat.x
theta = np.zeros(M)
cgf = np.zeros(M)
qZ = np.zeros([M,N])
G = np.transpose(np.tile(np.random.normal(mu,1,M),(N,1)))
e = np.random.normal(0,1,[M,N])
W = np.random.chisquare(nu,M)
myV = W/myShift
V = np.transpose(np.sqrt(np.tile(myV,(N,1))/nu))
num = V*myT.ppf(p,nu)*np.ones((M,1))-np.multiply(np.sqrt(myRho),G)
pZ = norm.cdf(np.divide(num,np.sqrt(1-myRho)))
for n in range(0,M):
theta[n] = vc.getSaddlePoint(pZ[n,:],c,l,0.0)
qZ[n,:] = getQ(theta[n],c,pZ[n,:])
cgf[n] = vc.computeCGF(theta[n],pZ[n,:],c)
I = np.transpose(1*np.less(e,norm.ppf(qZ)))
L = np.dot(c,I)
rnChi=np.exp(-cm*myV-(nu/2)*np.log(myShift))
rnMu=np.exp(-mu*G[:,0]+0.5*mu**2)
rnTwist = computeRND(theta,L,cgf)
rn = rnChi*rnMu*rnTwist
return I,theta,pZ,qZ,cgf,rn