-
Notifications
You must be signed in to change notification settings - Fork 8
/
vasicek.py
149 lines (120 loc) · 4.13 KB
/
vasicek.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
# encoding: utf-8
"""Implementing the large pool portfolio approximation of Vasicek."""
# (c) 2014-2019 Open Risk (https://www.openriskmanagement.com)
#
# portfolioAnalytics is licensed under the Apache 2.0 license a copy of which is included
# in the source distribution of TransitionMatrix. This is notwithstanding any licenses of
# third-party software included in this distribution. You may not use this file except in
# compliance with the License.
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions and
# limitations under the License.
import math
from scipy import stats
from sympy import binomial
from portfolioAnalytics import settings
def vasicek_base(N, k, p, rho):
"""Vasicek Base Discrete distribution.
:param N:
:param k:
:param p:
:param rho:
:return:
"""
zmin = - settings.SCALE
zmax = settings.SCALE
grid = settings.GRID_POINTS
dz = float(zmax - zmin) / float(grid - 1)
a = stats.norm.ppf(p, loc=0.0, scale=1.0)
integral = 0
for i in range(1, grid):
z = zmin + dz * i
arg = (a - rho * z) / math.sqrt(1 - rho * rho)
phi_den = stats.norm.pdf(z, loc=0.0, scale=1.0)
phi_cum = stats.norm.cdf(arg, loc=0.0, scale=1.0)
integrant = phi_den * math.pow(phi_cum, k) * math.pow(1 - phi_cum, N - k) * binomial(N, k)
integral = integral + integrant
return dz * integral
def vasicek_base_el(N, p, rho):
"""Expected Loss for the Vasicek Base distribution.
:param N:
:param p:
:param rho:
:return:
"""
return N * p
def vasicek_base_ul(N, p, rho):
"""Unexpected Loss for the Vasicek Base distribution.
:param N:
:param p:
:param rho:
:return:
"""
zmin = - settings.SCALE
zmax = settings.SCALE
grid = settings.GRID_POINTS
dz = float(zmax - zmin) / float(grid - 1)
a = stats.norm.ppf(p, loc=0.0, scale=1.0)
integral = 0
for i in range(1, grid):
z = zmin + dz * i
arg = (a - rho * z) / math.sqrt(1 - rho * rho)
phi_den = stats.norm.pdf(z, loc=0.0, scale=1.0)
phi_cum = stats.norm.cdf(arg, loc=0.0, scale=1.0)
integrant = phi_den * math.pow(phi_cum, 2)
integral = integral + integrant
result = p / N - p * p + float(N - 1) / float(N) * dz * integral
return N * math.sqrt(result)
def vasicek_lim(theta, p, rho):
"""The Large-N limit of the Vasicek Distribution.
:param theta:
:param p:
:param rho:
:return:
"""
a1 = stats.norm.ppf(p, loc=0.0, scale=1.0)
arg1 = stats.norm.ppf(theta, loc=0.0, scale=1.0)
arg2 = (math.sqrt(1 - rho * rho) * arg1 - a1) / rho
result = stats.norm.cdf(arg2, loc=0.0, scale=1.0)
return result
def vasicek_lim_el(p, rho):
"""The expected loss of the large n limit of the Vasicek distribution.
:param p:
:param rho:
:return:
"""
return p
def vasicek_lim_ul(p, rho):
"""The unexpected loss of the large n limit of the Vasicek distribution.
:param p:
:param rho:
:return:
"""
zmin = - settings.SCALE
zmax = settings.SCALE
grid = settings.GRID_POINTS
dz = float(zmax - zmin) / float(grid - 1)
a = stats.norm.ppf(p, loc=0.0, scale=1.0)
integral = 0
for i in range(1, grid):
z = zmin + dz * i
arg = (a - rho * z) / math.sqrt(1 - rho * rho)
phi_den = stats.norm.pdf(z, loc=0.0, scale=1.0)
phi_cum = stats.norm.cdf(arg, loc=0.0, scale=1.0)
integrant = phi_den * math.pow(phi_cum, 2)
integral = integral + integrant
result = - p * p + dz * integral
return math.sqrt(result)
def vasicek_lim_q(alpha, p, rho):
"""The quantile of the large-n Limit of the Vasicek distribution.
:param alpha:
:param p:
:param rho:
:return:
"""
a1 = stats.norm.ppf(p, loc=0.0, scale=1.0)
a2 = stats.norm.ppf(alpha, loc=0.0, scale=1.0)
arg = (a1 + rho * a2) / math.sqrt(1 - rho * rho)
return stats.norm.cdf(arg, loc=0.0, scale=1.0)