-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltercore.cpp
260 lines (207 loc) · 6.28 KB
/
filtercore.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "filtercore.h"
template <typename T> class QVector;
/**
* @brief FilterCore::FilterCore
* this class contains basic functions, that can be used both
* in signal generation and while signal filtration.
*/
FilterCore::FilterCore()
{
currentSigmaIndex = 0;
currentSigma = 0;
}
/**
* @brief FilterCore::intRand returns integer from 0 to randMax
* @param int randMax
* @return int
*/
int FilterCore::intRand(int randMax)
{
double randNumber = FilterCore::randomNumber(0,randMax);
return ceil(randNumber);
}
double FilterCore::myRand(double sigma){
int j;
double Ri=0;
double randMin = 0;
double randMax = 1;
for (j=1; j<=12; j++)
{
double randNumber = randMin + (randMax - randMin)*(qrand() / static_cast<double>( RAND_MAX ));
//qDebug() << "rand: " << randNumber;
Ri+=randNumber;
}
return sigma*(Ri-6);
}
/**
* @brief FilterCore::randomNumber returns random double value from randMin to randMax
* @param randMin
* @param randMax
* @return double
*/
double FilterCore::randomNumber(double randMin, double randMax){
double randNumber;
randNumber= randMin + (randMax - randMin)*(qrand() / static_cast<double>( RAND_MAX ));
return randNumber;
}
/**
* @brief FilterCore::normalDistribution returns normal distributed random variable. Parameters can be set
* @param mu
* @param sigma
* @return double
*/
double FilterCore::normalDistribution(double mu, double sigma) {
std::default_random_engine generator;
std::normal_distribution<double> distribution(mu,sigma);
double number = distribution(generator);
return number;
}
/**
* @brief FilterCore::setNextSigma
* @param numberOfIntervals
* @param sigmaVector
* @param lambdas
*/
void FilterCore::setNextSigma( int numberOfIntervals,
QVector <double> sigmaVector,
QVector <double> lambdas){
//probabilities
double pDown = lambdas[0]/(lambdas[0]+lambdas[2]);
double pUp = 1 - pDown;
double rn = randomNumber(0,1);
if (rn < pUp){
if (this->currentSigmaIndex != numberOfIntervals){
this->currentSigmaIndex++;
} else {
this->currentSigmaIndex--;
}
} else {
if (this->currentSigmaIndex != 0){
this->currentSigmaIndex--;
} else {
this->currentSigmaIndex++;
}
}
this->currentSigma = sigmaVector[this->currentSigmaIndex];
}
QVector <double> FilterCore::switchingRegimeLambdas(int k,
double h, double g, double startingSigma){
QVector <double> res(3);
double lambdaIIMinus1;
if (currentSigmaIndex != 0){
lambdaIIMinus1 = ( 1/(2*h*h) * g * currentSigma ) +
( (1/h) * fmax(0,k*(startingSigma - currentSigma)));
} else {
lambdaIIMinus1 = 0;
}
res[0] = lambdaIIMinus1;
double lambdaIIPlus1;
if (currentSigmaIndex != k) {
lambdaIIPlus1 = ( 1/(2*h*h) * g * currentSigma ) +
( (1/h) * fmax(0,k*(currentSigma - startingSigma)));
} else {
lambdaIIPlus1 = 0;
}
res[2] = lambdaIIPlus1;
double lambdaII = -lambdaIIMinus1 -lambdaIIPlus1;
res[1] = lambdaII;
return res;
}
QVector <QVector<double> > FilterCore::switchingRegimeSigmaGenerator(
double lowerSigma,
double sigmaStep,
int numberOfIntervals, //between lower and higher sigma
double volatility,
double exitCondition,
double discretizationStep
){
QVector <QVector<double> > sigmaGraph;
QVector <double> tmpVector;
QVector <double> tau;
QVector <double> lambdas;
QVector <double> sigmas;
double delta;
double currentTauValue;
double previousTauValue;
double roundedTauValue;
for (int i=0; i<=numberOfIntervals; i++){
sigmas.insert(i,lowerSigma + i * sigmaStep);
}
currentSigmaIndex = intRand(numberOfIntervals)+1;
currentSigma = sigmas.value(currentSigmaIndex);
double startingSigma = lowerSigma + (sigmaStep*currentSigmaIndex);
currentTauValue = 0;
tmpVector.insert(0,0);
tmpVector.insert(1,startingSigma);
sigmaGraph.insert(0,tmpVector);
tmpVector.clear();
int i = 1;
tau.insert(0,0);
while (currentTauValue < exitCondition) {
previousTauValue = tau.value(i-1);
lambdas = switchingRegimeLambdas(numberOfIntervals,
sigmaStep, volatility, startingSigma);
delta = log(randomNumber(0,1))/lambdas.value(1);
currentTauValue = previousTauValue + delta;
roundedTauValue = 0;
while (roundedTauValue < currentTauValue) {
roundedTauValue+= discretizationStep;
}
currentTauValue = roundedTauValue;
if (currentTauValue > exitCondition) {
currentTauValue = exitCondition;
}
previousTauValue = tau.value(i-1);
setNextSigma(numberOfIntervals,sigmas,lambdas);
tmpVector.insert(0,currentTauValue);
tmpVector.insert(1,currentSigma);
tau.insert(i,currentTauValue);
sigmaGraph.insert(i,tmpVector);
tmpVector.clear();
i++;
}
return sigmaGraph;
}
double FilterCore::getSignalMin(QVector <double> V){
double min = V.value(0);
for (int i=0; i<V.size(); i++)
{
if (V.value(i)<min)
min = V.value(i);
}
return min;
}
double FilterCore::getSignalMax(QVector <double> V){
double max = V.value(0);
for (int i=0; i<V.size(); i++)
{
if (V.value(i)>max)
max = V.value(i);
}
return max;
}
double FilterCore::getTwoSignalsMin(QVector <double> signal1,QVector <double> signal2){
double min1, min2, res;
min1 = this->getSignalMin(signal1);
min2 = this->getSignalMin(signal2);
res = fmin(min1,min2);
return res;
}
double FilterCore::getTwoSignalsMax(QVector <double> signal1,QVector <double> signal2){
double max1, max2, res;
max1 = this->getSignalMax(signal1);
max2 = this->getSignalMax(signal2);
res = fmax(max1,max2);
return res;
}
double FilterCore::scalarMultiplication(QVector <double> A, QVector <double> B){
if (A.size()!=B.size()){
return 0;
}
double sum;
sum = 0;
for (int i=0; i<A.size(); i++){
sum+= A.value(i)*B.value(i);
}
return sum;
}