diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/GHquad.c | 33 | ||||
| -rw-r--r-- | python/tranche_functions.py | 30 |
2 files changed, 53 insertions, 10 deletions
diff --git a/python/GHquad.c b/python/GHquad.c new file mode 100644 index 00000000..bb02e18c --- /dev/null +++ b/python/GHquad.c @@ -0,0 +1,33 @@ +#include "stdlib.h" +#include "math.h" +#include "stdio.h" + +extern int dstev_(char* JOBZ, int* n, double* D, double* E, double* Z, int* ldz, double* WORK, int* INFO); + +void GHquad(int n, double* Z, double* w) { + // Setup for eigenvalue computations + char JOBZ = 'V'; // Compute eigenvalues & vectors + int INFO; + int i; + // Initialize array for workspace + double * WORK = malloc(sizeof(double)*(2*n-2)); + + // Initialize array for eigenvectors + double * V = malloc(sizeof(double)*n*n); + + for(i = 0; i<n-1; i++){ + w[i] = sqrt((i+1.)/2); + } + + // Run eigen decomposition + dstev_(&JOBZ, &n, Z, w, V, &n, WORK, &INFO); + + for (i=0; i<n; i++) { + w[i] = V[i*n] * V[i*n]; + Z[i] *= sqrt(2); + } + + // Deallocate temporary arrays + free(WORK); + free(V); +} diff --git a/python/tranche_functions.py b/python/tranche_functions.py index 44244040..72a06b4b 100644 --- a/python/tranche_functions.py +++ b/python/tranche_functions.py @@ -1,16 +1,16 @@ import numpy as np from ctypes import * -lib = np.ctypeslib.load_library("lossdistribgomez", "/home/share/CorpCdos/code/R") -lib.fitprob.restype = None -lib.fitprob.argtypes = [np.ctypeslib.ndpointer('double', ndim=1, flags='F'), +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')] -lib.stochasticrecov.restype = None -lib.stochasticrecov.argtypes = [POINTER(c_double), +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'), @@ -20,18 +20,28 @@ lib.stochasticrecov.argtypes = [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) - lib.stochasticrecov(byref(c_double(R)), byref(c_double(Rtilde)), Z, w, byref(c_int(Z.size)), + 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 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) - lib.fitprob(Z, w, byref(c_int(Z.size)), byref(c_double(rho)), byref(c_double(p0)), result) + libloss.fitprob(Z, w, byref(c_int(Z.size)), byref(c_double(rho)), byref(c_double(p0)), result) return result |
