diff options
Diffstat (limited to 'lossdistrib.c')
| -rw-r--r-- | lossdistrib.c | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/lossdistrib.c b/lossdistrib.c index 5b44cbda..c8bd0068 100644 --- a/lossdistrib.c +++ b/lossdistrib.c @@ -1,15 +1,17 @@ #include <R.h>
#include <Rmath.h>
#include <string.h>
+
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-void lossdistrib(double *p, int *np, double *w, double *S, int *N, double *q) {
+void lossdistrib(double *p, int *np, double *w, double *S, int *N, int* defaultflag, double *q) {
/* recursive algorithm with first order correction for computing
the loss distribution.
p vector of default probabilities
np length of p
S vector of severities (should be same length as p)
N number of ticks in the grid
+ defaultflat if true compute the default distribution
q the loss distribution */
int i, j, d1, d2, M;
@@ -21,7 +23,7 @@ void lossdistrib(double *p, int *np, double *w, double *S, int *N, double *q) { q[0] = 1;
M = 1;
for(i=0; i<(*np); i++){
- d = S[i] * w[i]/ lu;
+ d = *defaultflag? w[i]/lu : S[i] * w[i]/ lu;
d1 = floor(d);
d2 = ceil(d);
p1 = p[i] * (d2-d);
@@ -48,7 +50,8 @@ void lossdistrib(double *p, int *np, double *w, double *S, int *N, double *q) { Free(qtemp);
}
-void lossdistrib_truncated(double *p, int *np, double *w, double *S, int *N, int *T, double *q) {
+void lossdistrib_truncated(double *p, int *np, double *w, double *S, int *N,
+ int *T, int *defaultflag, double *q) {
/* recursive algorithm with first order correction for computing
the loss distribution.
p vector of default probabilities
@@ -56,6 +59,7 @@ void lossdistrib_truncated(double *p, int *np, double *w, double *S, int *N, int S vector of severities (should be same length as p)
N number of ticks in the grid
T where to truncate the distribution
+ defaultflat if true computes the default distribution
q the loss distribution */
int i, j, d1, d2, M;
@@ -68,7 +72,7 @@ void lossdistrib_truncated(double *p, int *np, double *w, double *S, int *N, int q[0] = 1;
M = 1;
for(i=0; i<(*np); i++){
- d = S[i] * w[i] / lu;
+ d = *defaultflag? w[i] / lu : S[i] * w[i] / lu;
d1 = floor(d);
d2 = ceil(d);
p1 = p[i] * (d2-d);
@@ -90,7 +94,7 @@ void lossdistrib_truncated(double *p, int *np, double *w, double *S, int *N, int Free(q2);
}
-void lossdistrib_joint( double *p, int *np, double *w, double *S, int *N, double *q) {
+void lossdistrib_joint( double *p, int *np, double *w, double *S, int *N, int *defaultflag, double *q) {
/* recursive algorithm with first order correction
computes jointly the loss and recovery distribution
p vector of default probabilities
@@ -98,6 +102,7 @@ void lossdistrib_joint( double *p, int *np, double *w, double *S, int *N, double w vector of issuer weights (length np)
S vector of severities (should be same length as p)
N number of ticks in the grid
+ defaultflag if true computes the default distribution
q the joint probability distribution */
int i, j, k, m, n;
@@ -113,7 +118,7 @@ void lossdistrib_joint( double *p, int *np, double *w, double *S, int *N, double Mx=1;
My=1;
for(k=0; k<(*np); k++){
- x = S[k] * w[k] / lu;
+ x = *defaultflag? w[k] /lu : S[k] * w[k] / lu;
y = (1-S[k]) * w[k] / lu;
i = floor(x);
j = floor(y);
@@ -212,7 +217,8 @@ void recovdist(double *dp, double *pp, int *n, double *w, double *S, int *N, dou Free(qtemp);
}
-void lossdistrib_prepay_joint(double *dp, double *pp, int *ndp, double *w, double *S, int *N, double *q) {
+void lossdistrib_prepay_joint(double *dp, double *pp, int *ndp, double *w,
+ double *S, int *N, int *defaultflag, double *q) {
int i, j1, j2, k, m, n;
double lu, x, y1, y2, sum;
double alpha1, alpha2, beta1, beta2;
@@ -226,9 +232,9 @@ void lossdistrib_prepay_joint(double *dp, double *pp, int *ndp, double *w, doubl Mx=1;
My=1;
for(k=0; k<(*ndp); k++){
- x = S[k] * w[k]/lu;
y1 = (1-S[k]) * w[k]/lu;
y2 = w[k]/lu;
+ x = *defaultflag? y2: y2-y1;
i = floor(x);
j1 = floor(y1);
j2 = floor(y2);
@@ -292,7 +298,8 @@ void addandmultiply(double *X, double alpha, double *Y, int n) { }
void BClossdist(double *SurvProb, int *dim1, int *dim2, double *issuerweights,
- double *recov, double *Z, double *w, int *n, double *rho, int *N, double *L, double *R) {
+ double *recov, double *Z, double *w, int *n, double *rho, int *N,
+ int *defaultflag, double *L, double *R) {
/*
computes the loss and recovery distribution over time with a flat gaussiancorrelation
inputs:
@@ -303,6 +310,7 @@ void BClossdist(double *SurvProb, int *dim1, int *dim2, double *issuerweights, w: vector of factor weights (length n)
rho: correlation beta
N: number of ticks in the grid
+ defaultflag: if true, computes the default distribution
outputs:
L: matrix of size (N, dim2)
R: matrix of size (N, dim2)
@@ -328,8 +336,8 @@ void BClossdist(double *SurvProb, int *dim1, int *dim2, double *issuerweights, /* reset Lw and Rw to 0 */
memset(Lw, 0, *N * sizeof(double));
memset(Rw, 0, *N * sizeof(double));
- lossdistrib(gshocked, dim1, issuerweights, Sshocked, N, Lw);
- lossdistrib(gshocked, dim1, issuerweights, Rshocked, N, Rw);
+ lossdistrib(gshocked, dim1, issuerweights, Sshocked, N, defaultflag, Lw);
+ lossdistrib(gshocked, dim1, issuerweights, Rshocked, N, defaultflag, Rw);
addandmultiply(Lw, w[i], L + t * (*N), *N);
addandmultiply(Rw, w[i], R + t * (*N), *N);
}
|
