00001 #include "..\PartF\creditCurve.h"
00002 #include "..\common\filereader.h"
00003
00004 #include<iostream>
00005 #include<valarray>
00006 #include<time.h>
00007 using namespace std;
00008
00009 bool maincreditcurve(void) {
00010 string datadir = FileReader::getdatadirasstring();
00011 string yieldCurveData = datadir + "yctest.csv";
00012 string creditSpreadData = datadir + "ccspread.csv";
00013 string creditSpreadMixedData = datadir + "ccspreadmix.csv";
00014 string creditSpreadZeroData = datadir + "cc0spread.csv";
00015 string aigSpreadData = datadir + "cdsw_aig.csv";
00016
00017 bool result = true;
00018
00019
00020 valarray<yieldPoint> *yieldCurveArray =
00021 FileReader::buildYieldPointArray(yieldCurveData);
00022
00023
00024 valarray<CreditSpreadPoint> *creditSpreadZeroArray =
00025 FileReader::buildCreditSpreadPointArray(creditSpreadZeroData);
00026
00027
00028 valarray<CreditSpreadPoint> *creditSpreadArray =
00029 FileReader::buildCreditSpreadPointArray(creditSpreadData);
00030
00031
00032
00033 valarray<CreditSpreadPoint> *creditSpreadMixedArray =
00034 FileReader::buildCreditSpreadPointArray(creditSpreadMixedData);
00035
00036
00037 valarray<CreditSpreadPoint> *aigSpreadArray =
00038 FileReader::buildCreditSpreadPointArray(aigSpreadData);
00039
00040 if ((yieldCurveArray == NULL) ||
00041 (creditSpreadArray == NULL) ||
00042 (creditSpreadZeroArray == NULL) ||
00043 (creditSpreadMixedArray == NULL)) {
00044 cout << "error reading in credit curve test files" << endl;
00045 return false;
00046 }
00047
00048
00049
00050
00051 creditCurve zeroSpreadCurve(*yieldCurveArray,
00052 *creditSpreadZeroArray,
00053 "zero-spread");
00054
00055
00056 yieldCurve underlyingCurve(*yieldCurveArray, "test");
00057
00058 if (zeroSpreadCurve != underlyingCurve) {
00059 cout << "error: zero spread spot rates don't match the underlying" << endl;
00060 return false;
00061 }
00062
00063
00064
00065
00066
00067 creditCurve absCurve(*yieldCurveArray,
00068 *creditSpreadArray,
00069 "abs-spreads");
00070
00071 creditCurve mixedCurve(*yieldCurveArray,
00072 *creditSpreadMixedArray,
00073 "mixed-spreads");
00074
00075 cout << "zeroSpreadCurve:\n" << zeroSpreadCurve << endl;
00076 cout << "absCurve:\n" << absCurve << endl;
00077 cout << "mixedCurve:\n" << mixedCurve << endl;
00078 if (absCurve != mixedCurve) {
00079 cout << "error: absolute spread spot rates don't match mixed curve" << endl;
00080 return false;
00081 }
00082
00083
00084 creditCurve mixedCurve2(underlyingCurve,
00085 *creditSpreadMixedArray,
00086 "mixed-spreads2");
00087 cout << "mixedCurve2:\n" << mixedCurve2 << endl;
00088 if (mixedCurve != mixedCurve2) {
00089 cout << "error: constructing with yieldpoints gives different result"
00090 << " than constructing with yieldcurve" << endl;
00091 return false;
00092 }
00093
00094 Real flatRate = 0.04; Real flatSpread = 0.01;
00095 creditCurve flatCreditCurve(flatRate,
00096 flatSpread);
00097 yieldCurve flatYieldCurve(flatRate + flatSpread);
00098
00099 cout << "flatCreditCurve:\n" << flatCreditCurve << endl;
00100 cout << "flatYieldCurve:\n" << flatYieldCurve << endl;
00101 if (flatYieldCurve != flatCreditCurve) {
00102 cout << "error: flat 0.04 + 0.01 creditcurve gives different result"
00103 << " than flat 0.05 yieldcurve" << endl;
00104 return false;
00105 }
00106
00107
00108 creditCurve *pcreditCurve = new creditCurve(*yieldCurveArray,
00109 *creditSpreadArray,
00110 "abs-spreads");
00111 creditCurve copyCurve(*pcreditCurve);
00112 creditCurve equalCurve = copyCurve;
00113 delete pcreditCurve;
00114 cout << "copycurve: " << copyCurve << endl;
00115 cout << "equalcurve: " << equalCurve << endl;
00116
00117
00118 flatRate = 0.05; flatSpread = 0.0126;
00119 creditCurve flatCreditCurve2(flatRate, flatSpread);
00120 for (Natural i = 0; i <= 5; i++) {
00121 cout << "prob of default at time " << i << ","
00122 << flatCreditCurve2.defaultProbability(i) << ","
00123 << "survival prob,"
00124 << flatCreditCurve2.survivalProbability(i) << endl;
00125 }
00126
00127
00128 creditCurve aigCurve(flatYieldCurve, *aigSpreadArray, "aigcurve");
00129 cout << "aigCurve: " << aigCurve << endl;
00130 cout << "\n\n*** table of various values for aig curve" << endl;
00131 cout << "time,creditspread,hazardrate,survivalprob,cumdefaultprob,riskydiscount" << endl;
00132 for (Natural i = 0; i <= 10; i++) {
00133 cout << i << ","
00134 << aigCurve.creditSpread(i) << ","
00135 << aigCurve.hazardRate(i) << ","
00136 << aigCurve.survivalProbability(i) << ","
00137 << aigCurve.cumulativeDefaultProbability(i) << ","
00138 << aigCurve.riskyDiscountFactor(i) << endl;
00139 }
00140
00141 return result;
00142 }