aboutsummaryrefslogtreecommitdiffstats
path: root/python/tranche_functions.py
blob: 72a06b4b0c31d3a0f6d6bb6592b19bea77d4ffb1 (plain)
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
import numpy as np
from ctypes import *

libloss = np.ctypeslib.load_library("lossdistribgomez", "/home/share/CorpCdos/code/R")
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.stochasticrecov.restype = None
libloss.stochasticrecov.argtypes = [POINTER(c_double),
                                 POINTER(c_double),
                                 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),
                                 POINTER(c_double),
                                 np.ctypeslib.ndpointer('double', ndim=1, flags='F,writeable')]

libgq = np.ctypeslib.load_library("GHquad", ".")
libgq.GHquad.restype = None
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')
    w = np.zeros(n, dtype='double')
    libgq.GHquad(n, Z, w)
    return Z, w

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)
    return q


# def lossdistZ(p, w, S, N, defaultflag= False, rho, Z, wZ):
#     q = np.zeros_like(Z)
#     lib.lossdistrib_Z(byref())

def fitprob(Z, w, rho, p0):
    result = np.empty_like(Z)
    libloss.fitprob(Z, w, byref(c_int(Z.size)), byref(c_double(rho)), byref(c_double(p0)), result)
    return result