forked from amaggiulli/QLNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT_Stats.cs
312 lines (266 loc) · 11.8 KB
/
T_Stats.cs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
Copyright (C) 2008 Siarhei Novik ([email protected])
This file is part of QLNet Project http://qlnet.sourceforge.net/
QLNet is free software: you can redistribute it and/or modify it
under the terms of the QLNet license. You should have received a
copy of the license along with this program; if not, license is
available online at <http://qlnet.sourceforge.net/License.html>.
QLNet is a based on QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
The QuantLib license is available online at http://quantlib.org/license.shtml.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QLNet;
namespace Test2008
{
[TestClass()]
public class T_Stats
{
double[] data = { 3.0, 4.0, 5.0, 2.0, 3.0, 4.0, 5.0, 6.0, 4.0, 7.0 };
double[] weights = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
[TestMethod()]
public void testStatistics()
{
check<IncrementalStatistics>("IncrementalStatistics");
check<RiskStatistics>("Statistics");
}
[TestMethod()]
public void testSequenceStatistics()
{
//("Testing sequence statistics...");
checkSequence<IncrementalStatistics>("IncrementalStatistics", 5);
checkSequence<RiskStatistics>("Statistics", 5);
}
[TestMethod()]
public void testConvergenceStatistics()
{
//("Testing convergence statistics...");
checkConvergence<IncrementalStatistics>("IncrementalStatistics");
checkConvergence<RiskStatistics>("Statistics");
}
void check<S>(string name) where S : IGeneralStatistics, new()
{
S s = new S();
for (int i = 0; i < data.Length; i++)
s.add(data[i], weights[i]);
double calculated, expected;
double tolerance;
if (s.samples() != data.Length)
Assert.Fail(name + ": wrong number of samples\n"
+ " calculated: " + s.samples() + "\n"
+ " expected: " + data.Length);
expected = weights.Sum();
calculated = s.weightSum();
if (calculated != expected)
Assert.Fail(name + ": wrong sum of weights\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = data.Min();
calculated = s.min();
if (calculated != expected)
Assert.Fail(name + ": wrong minimum value\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = data.Max();
calculated = s.max();
if (calculated != expected)
Assert.Fail(name + ": wrong maximum value\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = 4.3;
tolerance = 1.0e-9;
calculated = s.mean();
if (Math.Abs(calculated - expected) > tolerance)
Assert.Fail(name + ": wrong mean value\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = 2.23333333333;
calculated = s.variance();
if (Math.Abs(calculated - expected) > tolerance)
Assert.Fail(name + ": wrong variance\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = 1.4944341181;
calculated = s.standardDeviation();
if (Math.Abs(calculated - expected) > tolerance)
Assert.Fail(name + ": wrong standard deviation\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = 0.359543071407;
calculated = s.skewness();
if (Math.Abs(calculated - expected) > tolerance)
Assert.Fail(name + ": wrong skewness\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
expected = -0.151799637209;
calculated = s.kurtosis();
if (Math.Abs(calculated - expected) > tolerance)
Assert.Fail(name + ": wrong kurtosis\n"
+ " calculated: " + calculated + "\n"
+ " expected: " + expected);
}
void checkSequence<S>(string name, int dimension) where S : IGeneralStatistics, new()
{
GenericSequenceStatistics<S> ss = new GenericSequenceStatistics<S>(dimension);
int i;
for (i = 0; i < data.Length; i++)
{
List<double> temp = new InitializedList<double>(dimension, data[i]);
ss.add(temp, weights[i]);
}
List<double> calculated;
double expected, tolerance;
if (ss.samples() != data.Length)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ "wrong number of samples\n"
+ " calculated: " + ss.samples() + "\n"
+ " expected: " + data.Length);
expected = weights.Sum();
if (ss.weightSum() != expected)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ "wrong sum of weights\n"
+ " calculated: " + ss.weightSum() + "\n"
+ " expected: " + expected);
expected = data.Min();
calculated = ss.min();
for (i = 0; i < dimension; i++)
{
if (calculated[i] != expected)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong minimum value\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
expected = data.Max();
calculated = ss.max();
for (i = 0; i < dimension; i++)
{
if (calculated[i] != expected)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong maximun value\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
expected = 4.3;
tolerance = 1.0e-9;
calculated = ss.mean();
for (i = 0; i < dimension; i++)
{
if (Math.Abs(calculated[i] - expected) > tolerance)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong mean value\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
expected = 2.23333333333;
calculated = ss.variance();
for (i = 0; i < dimension; i++)
{
if (Math.Abs(calculated[i] - expected) > tolerance)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong variance\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
expected = 1.4944341181;
calculated = ss.standardDeviation();
for (i = 0; i < dimension; i++)
{
if (Math.Abs(calculated[i] - expected) > tolerance)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong standard deviation\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
expected = 0.359543071407;
calculated = ss.skewness();
for (i = 0; i < dimension; i++)
{
if (Math.Abs(calculated[i] - expected) > tolerance)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong skewness\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
expected = -0.151799637209;
calculated = ss.kurtosis();
for (i = 0; i < dimension; i++)
{
if (Math.Abs(calculated[i] - expected) > tolerance)
Assert.Fail("SequenceStatistics<" + name + ">: "
+ (i + 1) + " dimension: "
+ "wrong kurtosis\n"
+ " calculated: " + calculated[i] + "\n"
+ " expected: " + expected);
}
}
void checkConvergence<S>(string name) where S : IGeneralStatistics, new()
{
ConvergenceStatistics<S> stats = new ConvergenceStatistics<S>();
stats.add(1.0);
stats.add(2.0);
stats.add(3.0);
stats.add(4.0);
stats.add(5.0);
stats.add(6.0);
stats.add(7.0);
stats.add(8.0);
const int expectedSize1 = 3;
int calculatedSize = stats.convergenceTable().Count;
if (calculatedSize != expectedSize1)
Assert.Fail("ConvergenceStatistics<" + name + ">: "
+ "\nwrong convergence-table size"
+ "\n calculated: " + calculatedSize
+ "\n expected: " + expectedSize1);
const double expectedValue1 = 4.0;
const double tolerance = 1.0e-9;
double calculatedValue = stats.convergenceTable().Last().Value;
if (Math.Abs(calculatedValue - expectedValue1) > tolerance)
Assert.Fail("wrong last value in convergence table"
+ "\n calculated: " + calculatedValue
+ "\n expected: " + expectedValue1);
const int expectedSampleSize1 = 7;
int calculatedSamples = stats.convergenceTable().Last().Key;
if (calculatedSamples != expectedSampleSize1)
Assert.Fail("wrong number of samples in convergence table"
+ "\n calculated: " + calculatedSamples
+ "\n expected: " + expectedSampleSize1);
stats.reset();
stats.add(1.0);
stats.add(2.0);
stats.add(3.0);
stats.add(4.0);
const int expectedSize2 = 2;
calculatedSize = stats.convergenceTable().Count;
if (calculatedSize != expectedSize2)
Assert.Fail("wrong convergence-table size"
+ "\n calculated: " + calculatedSize
+ "\n expected: " + expectedSize2);
const double expectedValue2 = 2.0;
calculatedValue = stats.convergenceTable().Last().Value;
if (Math.Abs(calculatedValue - expectedValue2) > tolerance)
Assert.Fail("wrong last value in convergence table"
+ "\n calculated: " + calculatedValue
+ "\n expected: " + expectedValue2);
const int expectedSampleSize2 = 3;
calculatedSamples = stats.convergenceTable().Last().Key;
if (calculatedSamples != expectedSampleSize2)
Assert.Fail("wrong number of samples in convergence table"
+ "\n calculated: " + calculatedSamples
+ "\n expected: " + expectedSampleSize2);
}
}
}