#ifndef RATE_HPP #define RATE_HPP // John Healy // modified by Jeffrey D. Oldham 2000Apr27 // CS3352 // ASSUMPTIONS: The first year of historical returns data is 1926. // The historical returns data is listed in chronological. #include #include #include #include #include #include "time.h" #include "types.h" #include "date.h" enum DataTypes {CA_LStocks, IR_LStocks, CA_LGBonds, IR_LGBonds, CA_IGBonds, IR_IGBonds, Treasury, Inflation}; class Rates { public: Rates(double MonthlyModificationAmount=0.0, const string & lcStkCapAppr="LCompStockCA.txt", const string & lcStkIncome="LCompStockIR.txt", const string & ltGBCapAppr="LongGovBondCA.txt", const string & ltGBIncome="LongGovBondIR.txt", const string & itGBCapAppr="IntGovBondCA.txt", const string & itGBIncome="IntGovBondIR.txt", const string & Treas="Treasury.txt", const string & Infla="Inflation.txt") : MonthlyModAmount(MonthlyModificationAmount) { // Read the historical returns into "data." data.resize(8); if (!(readDataFile(data[CA_LStocks], lcStkCapAppr) && readDataFile(data[IR_LStocks], lcStkIncome) && readDataFile(data[CA_LGBonds], ltGBCapAppr) && readDataFile(data[IR_LGBonds], ltGBIncome) && readDataFile(data[CA_IGBonds], itGBCapAppr) && readDataFile(data[IR_IGBonds], itGBIncome) && readDataFile(data[Treasury], Treas) && readDataFile(data[Inflation], Infla))) { cout << 1 << endl; throw "unable to read a data file"; } // ASSUME that we always have data for each year, i.e., Jan-Dec. beginningOfData = Date(data[Inflation][0][0], 1 /* January */); endOfData = Date(data[Inflation][data[Inflation].size()-1][0], 12 /* December */); return; } const Date& firstDate(void) const { return beginningOfData; } const Date& lastDate(void) const { return endOfData; } //iff no taxes double TotalReturn(const Time & t, const asset & Type) const { return CapApprecReturn(t, Type) + IncomeReturn(t, Type); } //iff taxes double CapApprecReturn(const Time & t, const asset & Type) const { DataTypes d; switch (Type) { case largeCoStocks: d = CA_LStocks; break; case LTGovtBonds: d = CA_LGBonds; break; case ITGovtBonds: d = CA_IGBonds; break; case Treas: case inflation: default: cout << 2 << endl; throw "illegal capital appreciation request"; break; } // TMP HERE return data[d][t.yearsSince1926()][t.month()] + MonthlyModAmount*0.5; return data[d][t.year() - beginningOfData.year()][t.month()] + MonthlyModAmount*0.5; } double IncomeReturn(const Time & t, const asset & Type) const { DataTypes d; switch (Type) { case largeCoStocks: d = IR_LStocks; break; case LTGovtBonds: d = IR_LGBonds; break; case ITGovtBonds: d = IR_IGBonds; break; case Treas: d = Treasury; break; case inflation: d = Inflation; break; default: cout << 3 << endl; throw "illegal income return request"; break; } // JDO: Change the yearsSince1926(). return data[d][t.year() - beginningOfData.year()][t.month()] + MonthlyModAmount*0.5; // TMP HERE return data[d][t.yearsSince1926()][t.month()] + MonthlyModAmount*0.5; } private: const double MonthlyModAmount; Date beginningOfData; // starting year of data Date endOfData; // ending year of data // We assume we have data for // Jan...Dec of each year. typedef vector vd; typedef vector< vd > vvd; typedef vector < vvd > vvvd; vvvd data; // historical return data // ordered same as DataTypes // Read the data from the specified file into the vector. inline bool readDataFile(vvd & assetDataVector, const string & fileName) { ifstream in(fileName.c_str()); if (!in) { cerr << "failed to read " << fileName << endl; return false; } string ignoredLine; double rate; unsigned int year; double yearReturn; char c; unsigned int currentEntry = 0; while ((c = (in>>ws).peek()) != EOF) if (c == '#') getline(in, ignoredLine); else { // Read a line of data. assetDataVector.push_back(vd()); for (unsigned int i = 0; (i <= 12) && (in >> rate); ++i) assetDataVector[currentEntry].push_back(rate); if (!(in >> year >> yearReturn)) return false; ++currentEntry; } in.close(); return true; } }; #endif //RATE_HPP