summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt2/Aufgabe2und3/src/CholeskyDecomposition.cpp
blob: f74c99c679dc313c733711720b9a79fed24842d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * CholeskyDecomposition.cpp
 *
 *  Created on: 20.05.2011
 *      Author: sven
 */
#ifndef PARAMETRISATION_H_
	#include "Parametrisation.h"
#endif

#include <iostream>
#include <iomanip>
#include <cmath>

#include "CholeskyDecomposition.h"

CholeskyDecomposition::CholeskyDecomposition()
{
	for (UInt32 c = nenDaimler; c < nenEndOfCompanies ; c++)
	{
		mCholeskyMatrixOneDay.push_back(std::vector<double>(nenEndOfCompanies));
		mCholeskyMatrixTenDays.push_back(std::vector<double>(nenEndOfCompanies));
	}
}

CholeskyDecomposition::~CholeskyDecomposition() {
}

void CholeskyDecomposition::dumpCholeskyMatrices()
{
	std::cout << "Cholesky Matrix for one day" << std::endl;
	int width = 12;
	int prec = 6;
	for(UInt32 i = 0; i < nenEndOfCompanies ; i++)
	{
		for (UInt32 j = 0; j < nenEndOfCompanies ; j++)
		{
			std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << mCholeskyMatrixOneDay.at(i).at(j);
		}
		std::cout << std::endl;
	}
	std::cout << "Cholesky Matrix for ten days" << std::endl;
	for(UInt32 i = 0; i < nenEndOfCompanies ; i++)
	{
		for (UInt32 j = 0; j < nenEndOfCompanies ; j++)
		{
			std::cout << std::setw(width) << std::setprecision(prec) << std::fixed << mCholeskyMatrixTenDays.at(i).at(j);
		}
		std::cout << std::endl;
	}
}

void CholeskyDecomposition::calcCholeskyMatrix(CholeskyMatrix& d, const CovarianceMatrix& b)
{
	double sum,dp,diff;
	for(int i = 0; i < nenEndOfCompanies ; i++)
	{
		for (int j = 0; j < nenEndOfCompanies ; j++)
		{
			if ( i < j)
			{
				d.at(i).at(j) = 0.0;
			}
			else if (i == j)
			{
				sum = 0.0;
				int k=0;
				while ( k < (i-1) )
				{
					dp = d.at(i).at(k);
					sum += dp*dp;
					k++;
				}
				diff = b.at(i).at(i) - sum;
				d.at(i).at(j) = std::sqrt(diff);
			}
			else if (i > j)
			{
				sum = 0.0;
				int k=0;
				while (k < (i-1) )
				{
					sum += d.at(i).at(k) * d.at(j).at(k);
					k++;
				}
				diff = b.at(i).at(j) - sum;
				d.at(i).at(j) = (1.0 / d.at(j).at(j) ) * diff;
			}
		}
	}
}

void CholeskyDecomposition::calcCholeskyDecompostions(const Parametrisation& params)
{
	calcCholeskyMatrix(mCholeskyMatrixOneDay,params.getCovarianceMatrix(nenOneDay));
	calcCholeskyMatrix(mCholeskyMatrixTenDays,params.getCovarianceMatrix(nenTenDays));
	dumpCholeskyMatrices();
}