diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/tranche_functions.py | 79 |
1 files changed, 42 insertions, 37 deletions
diff --git a/python/tranche_functions.py b/python/tranche_functions.py index 8cfbb116..d1297177 100644 --- a/python/tranche_functions.py +++ b/python/tranche_functions.py @@ -5,43 +5,47 @@ from quantlib.time.api import Actual360, Period, UnitedStates, Following, today from quantlib.util.converter import qldate_to_pydate, pydate_to_qldate import pandas as pd -libloss = np.ctypeslib.load_library("lossdistrib", "/home/share/CorpCDOs/code/R") +libloss = np.ctypeslib.load_library("lossdistrib", "/home/share/CorpCDOs/code/R/lossdistrib/src") libloss.fitprob.restype = None -libloss.fitprob.argtypes = [np.ctypeslib.ndpointer('double', ndim=1, flags='F'), - np.ctypeslib.ndpointer('double', ndim=1, flags='F'), - POINTER(c_int), - POINTER(c_double), - POINTER(c_double), - np.ctypeslib.ndpointer('double', ndim=1, flags='F,writeable')] +libloss.fitprob.argtypes = [ + np.ctypeslib.ndpointer('double', ndim=1, flags='F'), + np.ctypeslib.ndpointer('double', ndim=1, flags='F'), + POINTER(c_int), + POINTER(c_double), + POINTER(c_double), + np.ctypeslib.ndpointer('double', ndim=1, flags='F,writeable')] libloss.stochasticrecov.restype = None -libloss.stochasticrecov.argtypes = [POINTER(c_double), - POINTER(c_double), - np.ctypeslib.ndpointer('double', ndim=2, flags='F'), - np.ctypeslib.ndpointer('double', ndim=2, flags='F'), - POINTER(c_int), - POINTER(c_double), - POINTER(c_double), - POINTER(c_double), - np.ctypeslib.ndpointer('double', ndim=1, flags='F,writeable')] -libloss.BClossdist.restype = None -libloss.BClossdist.argtypes = [np.ctypeslib.ndpointer('double', ndim=2, flags='F'),# defaultprob - POINTER(c_int),# nrow(defaultprob) - POINTER(c_int),# ncol(defaultprob) - np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# issuerweights - np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# recovery - np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# Z - np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# w - POINTER(c_int), # len(Z) = len(w) - np.ctypeslib.ndpointer('double', ndim=1, flags='F'), # rho - POINTER(c_int), # Ngrid - POINTER(c_int), #defaultflag - np.ctypeslib.ndpointer('double', ndim=2, flags='F,writeable'),# output L - np.ctypeslib.ndpointer('double', ndim=2, flags='F,writeable')# output R - ] +libloss.stochasticrecov.argtypes = [ + POINTER(c_double), + POINTER(c_double), + np.ctypeslib.ndpointer('double', ndim=2, flags='F'), + np.ctypeslib.ndpointer('double', ndim=2, flags='F'), + POINTER(c_int), + POINTER(c_double), + POINTER(c_double), + POINTER(c_double), + np.ctypeslib.ndpointer('double', ndim=1, flags='F,writeable')] +libloss.BCloss_recov_dist.restype = None +libloss.BCloss_recov_dist.argtypes = [ + np.ctypeslib.ndpointer('double', ndim=2, flags='F'),# defaultprob + POINTER(c_int),# nrow(defaultprob) + POINTER(c_int),# ncol(defaultprob) + np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# issuerweights + np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# recovery + np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# Z + np.ctypeslib.ndpointer('double', ndim=1, flags='F'),# w + POINTER(c_int), # len(Z) = len(w) + np.ctypeslib.ndpointer('double', ndim=1, flags='F'), # rho + POINTER(c_int), # Ngrid + POINTER(c_int), #defaultflag + np.ctypeslib.ndpointer('double', ndim=2, flags='F,writeable'),# output L + np.ctypeslib.ndpointer('double', ndim=2, flags='F,writeable')# output R +] libgq = np.ctypeslib.load_library("GHquad", ".") libgq.GHquad.restype = None -libgq.GHquad.argtypes = [c_int, np.ctypeslib.ndpointer('double', ndim=1, flags='F'), +libgq.GHquad.argtypes = [c_int, + np.ctypeslib.ndpointer('double', ndim=1, flags='F'), np.ctypeslib.ndpointer('double', ndim=1, flags='F')] def GHquad(n): Z = np.zeros(n, dtype='double') @@ -52,7 +56,7 @@ def GHquad(n): def stochasticrecov(R, Rtilde, Z, w, rho, porig, pmod): q = np.zeros_like(Z) libloss.stochasticrecov(byref(c_double(R)), byref(c_double(Rtilde)), Z, w, byref(c_int(Z.size)), - byref(c_double(rho)), byref(c_double(porig)), byref(c_double(pmod)), q) + byref(c_double(rho)), byref(c_double(porig)), byref(c_double(pmod)), q) return q def fitprob(Z, w, rho, p0): @@ -60,13 +64,14 @@ def fitprob(Z, w, rho, p0): libloss.fitprob(Z, w, byref(c_int(Z.size)), byref(c_double(rho)), byref(c_double(p0)), result) return result -def BClossdist(defaultprob, issuerweights, recov, rho, Z, w, Ngrid = 101, defaultflag = False): +def BCloss_recov_dist(defaultprob, issuerweights, recov, rho, Z, w, Ngrid = 101, defaultflag = False): L = np.zeros((Ngrid, defaultprob.shape[1]), order='F') R = np.zeros_like(L) rho = np.repeat(rho, issuerweights.size) - libloss.BClossdist(defaultprob, byref(c_int(defaultprob.shape[0])), byref(c_int(defaultprob.shape[1])), - issuerweights, recov, Z, w, byref(c_int(Z.size)), rho, - byref(c_int(Ngrid)), byref(c_int(defaultflag)), L, R) + libloss.BCloss_recov_dist(defaultprob, byref(c_int(defaultprob.shape[0])), + byref(c_int(defaultprob.shape[1])), + issuerweights, recov, Z, w, byref(c_int(Z.size)), rho, + byref(c_int(Ngrid)), byref(c_int(defaultflag)), L, R) return L, R def adjust_attachments(K, losstodate, factor): |
