#include #include #include #include using namespace std; void cumnor( double* x, double* F, double* OmF); int randn( vector & Z, int nZ); double TrancheSim( // Return the loss from a tranche double s, // The credit spread, coupon rate - risk free discount rate double T, // The maturity date double lambda, // The default intensity double top, // The top of the tranche, in percent double bot, // The bottom of the tranche, in percent: 1 >= top >= bot >= 0 double rho, // The correlation parameter in the copula default model double r, // The risk free rate, if needed int n); // The number of loans in the pool int main() { cout << "Hello from main" << endl; double x, F, OmF, U, OmU, Y; double lambda; lambda = .05; double rho; rho = 0; double r; r = .01; // Low but not super low. double Loss, s, T, top, bot; int n; T = 10; top = .9; bot = .7; s = .05; n = 20000; double MaxRet = n * (1/s) * (exp(s*T) - 1 )*(top - bot); // **** Not the correct formula // Value if no loss. ofstream csvFile; // The file for output, will be csv format for Excel. csvFile.open ("TrancheLoss.csv"); // Max.csv is the name of the output file csvFile << "T , " << T << ","; // Write all the parameters of the run into the top row csvFile << "n , " << n << ","; csvFile << "s , " << s << ","; csvFile << "lambda , " << lambda << ","; csvFile << "top , " << top << ","; csvFile << "bot , " << bot << ","; csvFile << endl; for (int sim = 0; sim < 100; sim++) { Loss = TrancheSim( s, T, lambda, top, bot, rho, r, n); csvFile << Loss/ MaxRet << endl; } return 0; } double TrancheSim( double s, double T, double lambda, double top, double bot, double rho, double r, int n){ vector Z(n+1); // Allocate storage for the standard normals that drive the path. vector DefaultTimes(n); randn( Z, n); // Get the normals. double Z0 = Z[n]; // The market factor. double Y; // The copula Gaussian double U; // The uniform = N(Y) double OmU; // not used double Loss; int Defaults; for ( int i = 0; i < n; i++ ) { // Copula for the default times Y = Z[i]; // ********* Put in the model of correlation here. cumnor( &Y, &U, &OmU); DefaultTimes[i] = (-1/lambda) * log( 1. - U ); } sort( DefaultTimes.begin(), DefaultTimes.end() ); // Sort the times from first to last. Loss = 0; int i = 0; // double TD; double dr; Defaults = 0; while ( ( TD = DefaultTimes[i++]) < T ) { dr = ( (double) Defaults ) / ( (double) n ); // Convert ints before dividing Defaults++; // Another payer goes belly up if ( (1. - dr) < top ) Loss += ( exp(s*T) - exp(s*TD) )/s; // ***** Not the correct formula **** // Add in the losses from this tranche if ( (1. - dr) < bot ) break; // Stop counting if the tranche is used up. } return Loss; }