diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/Parametrisation.cpp | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/Parametrisation.cpp')
| -rw-r--r-- | Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/Parametrisation.cpp | 299 |
1 files changed, 299 insertions, 0 deletions
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/Parametrisation.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/Parametrisation.cpp new file mode 100644 index 0000000..a326008 --- /dev/null +++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/Parametrisation.cpp @@ -0,0 +1,299 @@ +/* + * Parametrisation.cpp + * + * Created on: 13.05.2011 + * Author: sven + */ + +#include "Parametrisation.h" + +#include <fstream> +#include <iostream> +#include <sstream> +#include <cmath> +#include <iomanip> + +const char* Parametrisation::msInputFilename = "../Kurse_SS11.csv"; + +Parametrisation::Parametrisation() +:mArithAverageVectors(nenEndOfCompanies) +,mStandardDeviationsOneDay(nenEndOfCompanies) +,mStandardDeviationsTenDays(nenEndOfCompanies) +{ + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) { + mCovarinaceMatrixOneDay.push_back(std::vector<double>(nenEndOfCompanies)); + mCovarinaceMatrixTenDays.push_back(std::vector<double>(nenEndOfCompanies)); + mCorrelationMatrixOneDay.push_back(std::vector<double>(nenEndOfCompanies)); + mCorrelationMatrixTenDays.push_back(std::vector<double>(nenEndOfCompanies)); + } +} + +Parametrisation::~Parametrisation() { +} + +void Parametrisation::readCsvFile() +{ + unsigned dayCounter = 0; + std::ifstream inFile; + inFile.open(msInputFilename, std::ios::in); + char buffer[256]; + unsigned lineNum = 0; + while(inFile.getline(buffer, sizeof(buffer))) + { + lineNum++; + if (lineNum <= 3) { + continue; + } + std::string tmp(buffer); + std::vector<std::string> tokens; + this->tokenize(tmp,tokens); + StockPrices prices; + prices.mDayIdx = dayCounter; + prices.mDate = tokens.at(0); + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) { + prices.mStock.at(static_cast<enCompany>(c)).mValue = toDouble(tokens.at(c + 1)); + } + mLastYearData.push_back(prices); + dayCounter++; + } + inFile.close(); +} +void Parametrisation::tokenize(const std::string& str, + std::vector<std::string>& tokens, + const std::string& delimiters) +{ + // Skip delimiters at beginning. + std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); + // Find first "non-delimiter". + std::string::size_type pos = str.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) + { + std::string token = str.substr(lastPos, pos - lastPos); + std::string::size_type commaPos = token.find(","); + if (commaPos != std::string::npos) + { + token.replace(commaPos,1,"."); + } + // Found a token, add it to the vector. + tokens.push_back(token); + // Skip delimiters. Note the "not_of" + lastPos = str.find_first_not_of(delimiters, pos); + // Find next "non-delimiter" + pos = str.find_first_of(delimiters, lastPos); + } +} + double Parametrisation::toDouble(const std::string& str) + { + std::istringstream ssIn(str); + double res; + ssIn >> res; + return res; + } +void Parametrisation::dumpValues() +{ + std::vector<StockPrices>::const_iterator it; + it = mLastYearData.begin(); + for (; it != mLastYearData.end() ; it++) { + StockPrices sp = *it; + std::cout << sp.mDayIdx << ";" << sp.mDate; + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) { + std::cout << ";" << sp.mStock.at(c).mValue; + } + std::cout << std::endl; + } +} + +void Parametrisation::dumpRelChanges(const enHoldingTime& holdingTime) +{ + std::ofstream outFile; + std::ostringstream ssOut; + ssOut<<holdingTime; + std::string outFilename = "relChanges" + ssOut.str() + ".csv"; + outFile.open(outFilename.c_str(), std::ios::out); + unsigned days = mLastYearData.size(); + unsigned counter = static_cast<unsigned>(holdingTime); + for ( ; counter < days ; counter++) { + StockPrices sp = mLastYearData.at(counter); + outFile << sp.mDayIdx << ";" << sp.mDate; + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) { + switch (holdingTime) + { + case nenOneDay: + outFile << ";" << sp.mStock.at(c).mValueRelChangeOne; + break; + case nenTenDays: + outFile << ";" << sp.mStock.at(c).mValueRelChangeTen; + break; + } + + } + outFile << std::endl; + } + outFile.close(); +} + +void Parametrisation::calcRelChanges(const enHoldingTime& holdingTime) +{ + unsigned days = mLastYearData.size(); + unsigned counter = static_cast<unsigned>(holdingTime); + StockPrices* prev; + StockPrices* act; + for ( ; counter < days ; counter++) { + act = &(mLastYearData.at(counter)); + prev = &(mLastYearData.at(counter - static_cast<unsigned>(holdingTime))); + act->calcRelChanges(*prev,holdingTime); + } + dumpRelChanges(holdingTime); +} + +void Parametrisation::calcArithAverageVectors() +{ + unsigned days = mLastYearData.size(); + unsigned counter = 1; + for ( ; counter < days ; counter++) { + StockPrices sp = mLastYearData.at(counter); + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) + { + if (counter >= nenOneDay) { + mArithAverageVectors.at(c).mArithAverOneDay += sp.mStock.at(c).mValueRelChangeOne; + } + if (counter >= nenTenDays) { + mArithAverageVectors.at(c).mArithAverTenDays += sp.mStock.at(c).mValueRelChangeTen; + } + } + } + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) + { + mArithAverageVectors.at(c).mArithAverOneDay /= (days - nenOneDay - 1); + mArithAverageVectors.at(c).mArithAverTenDays /= ( days - nenTenDays - 1); + } + + // standard deviation for one day + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) + { + double sum = 0.0; + double diff = 0.0; + for (unsigned counter = nenOneDay; counter < days ; counter++) { + diff = mLastYearData.at(counter).mStock.at(c).mValueRelChangeOne - mArithAverageVectors.at(c).mArithAverOneDay; + sum += (diff * diff) / (mLastYearData.size() - nenOneDay - 1); + } + mStandardDeviationsOneDay.at(c) = std::sqrt(sum); + } + + // standard deviation for ten days + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) + { + double sum = 0.0; + double diff = 0.0; + for (unsigned counter = nenTenDays; counter < days ; counter++) { + diff = mLastYearData.at(counter).mStock.at(c).mValueRelChangeTen - mArithAverageVectors.at(c).mArithAverTenDays; + sum += (diff * diff) / (mLastYearData.size() - nenTenDays - 1); + } + mStandardDeviationsTenDays.at(c) = std::sqrt(sum); + } + dumpArithAverageVectors(); +} + +void Parametrisation::dumpArithAverageVectors() +{ + int width = 16; + int prec = 6; + std::cout << std::setw(width) << "Idx" << std::setw(width) << "one day avg" << std::setw(width) << "ten days avg" << std::endl; + for (unsigned c = static_cast<unsigned>(nenDaimler) ; c < static_cast<unsigned>(nenEndOfCompanies); c++) + { + ArithAverages av = mArithAverageVectors.at(c); + std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << c + << std::setw(width) << std::setprecision(prec) << std::fixed << av.mArithAverOneDay + << std::setw(width) << std::setprecision(prec) << std::fixed << av.mArithAverTenDays << std::endl; + } +} + +void Parametrisation::calcCovarianceMatrices() +{ + double diffI = 0.0; + double diffJ = 0.0; + double sum = 0.0; + + //one day + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + if (i <= j) { + sum = 0.0; + for (unsigned n=nenOneDay ; n < mLastYearData.size() ; n++) { + diffI = mLastYearData.at(n).mStock.at(i).mValueRelChangeOne - mArithAverageVectors.at(i).mArithAverOneDay; + diffJ = mLastYearData.at(n).mStock.at(j).mValueRelChangeOne - mArithAverageVectors.at(j).mArithAverOneDay; + sum += (diffI * diffJ) / (mLastYearData.size() - nenOneDay - 1); + } + mCovarinaceMatrixOneDay.at(i).at(j) = sum; + } else { + mCovarinaceMatrixOneDay.at(i).at(j) = mCovarinaceMatrixOneDay.at(j).at(i); + } + } + } + + // ten days + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + sum = 0.0; + for (unsigned n=nenTenDays ; n < mLastYearData.size() ; n++) { + diffI = mLastYearData.at(n).mStock.at(i).mValueRelChangeTen - mArithAverageVectors.at(i).mArithAverTenDays; + diffJ = mLastYearData.at(n).mStock.at(j).mValueRelChangeTen - mArithAverageVectors.at(j).mArithAverTenDays; + sum += (diffI * diffJ) / (mLastYearData.size() - nenTenDays - 1); + } + mCovarinaceMatrixTenDays.at(i).at(j) = sum; + } + } + dumpCovarianceMatrices(); +} + +void Parametrisation::dumpCovarianceMatrices() +{ + int width = 12; + int prec = 6; + std::cout << "Covariance matrix for one day" << std::endl; + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << mCovarinaceMatrixOneDay.at(i).at(j); + } + std::cout << std::endl; + } + std::cout << "Covariance matrix for ten days" << std::endl; + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << mCovarinaceMatrixTenDays.at(i).at(j); + } + std::cout << std::endl; + } +} + +void Parametrisation::calcCorrelationMatrices() +{ + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + mCorrelationMatrixOneDay.at(i).at(j) = mCovarinaceMatrixOneDay.at(i).at(j) / (mStandardDeviationsOneDay.at(i) * mStandardDeviationsOneDay.at(j)); + mCorrelationMatrixTenDays.at(i).at(j) = mCovarinaceMatrixTenDays.at(i).at(j) / (mStandardDeviationsTenDays.at(i) * mStandardDeviationsTenDays.at(j)); + } + } + dumpCorrelationMatrices(); +} + +void Parametrisation::dumpCorrelationMatrices() +{ + int width = 12; + int prec = 6; + std::cout << "Correlation matrix for one day" << std::endl; + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << mCorrelationMatrixOneDay.at(i).at(j); + } + std::cout << std::endl; + } + std::cout << "Correlation matrix for ten days" << std::endl; + for (unsigned i = static_cast<unsigned>(nenDaimler) ; i < static_cast<unsigned>(nenEndOfCompanies); i++) { + for (unsigned j = static_cast<unsigned>(nenDaimler) ; j < static_cast<unsigned>(nenEndOfCompanies); j++) { + std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << mCorrelationMatrixTenDays.at(i).at(j); + } + std::cout << std::endl; + } +} |
