This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMovingDataWindow.cs
235 lines (214 loc) · 8.9 KB
/
MovingDataWindow.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
using RCNet.MathTools.Hurst;
using RCNet.Queue;
using System;
namespace RCNet.MathTools
{
/// <summary>
/// Implements the moving data window providing additional functions such as statistics, weighted average, etc.
/// </summary>
[Serializable]
public class MovingDataWindow
{
//Attributes
private readonly SimpleQueue<double> _dataQueue;
//Constructors
/// <summary>
/// Creates an uninitialized instance.
/// </summary>
/// <param name="size">The moving data window size.</param>
public MovingDataWindow(int size)
{
_dataQueue = new SimpleQueue<double>(size);
return;
}
/// <summary>
/// The copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public MovingDataWindow(MovingDataWindow source)
{
_dataQueue = source._dataQueue.ShallowClone();
return;
}
//Properties
/// <summary>
/// Gets the capacity.
/// </summary>
public int Capacity { get { return _dataQueue.Capacity; } }
/// <summary>
/// The used capacity of the windpw.
/// </summary>
public int UsedCapacity { get { return _dataQueue.Count; } }
/// <summary>
/// Indicates the capacity of the window is fully used.
/// </summary>
public bool Full { get { return _dataQueue.Full; } }
//Methods
/// <summary>
/// Checks the number of samples and throws exception in case of insufficient number of samples.
/// </summary>
/// <param name="reqNumOfSamples">The requiered number of samples.</param>
private void CheckReadyness(int reqNumOfSamples = -1)
{
reqNumOfSamples = reqNumOfSamples == -1 ? 1 : reqNumOfSamples;
if (UsedCapacity < reqNumOfSamples)
{
throw new InvalidOperationException($"Insufficient number of samples ({reqNumOfSamples}/{UsedCapacity}).");
}
return;
}
/// <summary>
/// Returns a number at the specified position within the moving data window.
/// </summary>
/// <param name="index">The zero-based index wthin the window.</param>
/// <param name="latestFirst">Specifies a logical order (latest..oldest or oldest..latest).</param>
public double GetAt(int index, bool latestFirst = false)
{
return _dataQueue.GetElementAt(index, latestFirst);
}
/// <summary>
/// Computes the weighted average of the data currently stored in the window.
/// </summary>
/// <param name="weights">The weights.</param>
public WeightedAvg GetDataWeightedAvg(double[] weights)
{
CheckReadyness(weights.Length);
int numOfSamplesToBeProcessed = weights.Length;
WeightedAvg wAvg = new WeightedAvg();
for (int i = numOfSamplesToBeProcessed - 1, j = 0; i >= 0; i--, j++)
{
wAvg.AddSample(_dataQueue.GetElementAt(i, true), weights[j]);
}
return wAvg;
}
/// <summary>
/// Computes the weighted average of the differences of the data currently stored in the window.
/// </summary>
/// <param name="weights">The weights.</param>
public WeightedAvg GetDataDiffWeightedAvg(double[] weights)
{
CheckReadyness(weights.Length + 1);
int numOfSamplesToBeProcessed = weights.Length + 1;
WeightedAvg wAvg = new WeightedAvg();
for (int i = numOfSamplesToBeProcessed - 2, j = 0; i >= 0; i--, j++)
{
wAvg.AddSample(_dataQueue.GetElementAt(i, true) - _dataQueue.GetElementAt(i + 1, true), weights[j]);
}
return wAvg;
}
/// <summary>
/// Computes the linearly weighted average of the data in the window.
/// </summary>
/// <param name="reqNumOfSamples">The requiered mumber of samples (-1 means all).</param>
public WeightedAvg GetDataLinWeightedAvg(int reqNumOfSamples = -1)
{
int numOfSamplesToBeProcessed = reqNumOfSamples == -1 ? _dataQueue.Count : reqNumOfSamples;
CheckReadyness(numOfSamplesToBeProcessed);
WeightedAvg wAvg = new WeightedAvg();
for (int i = numOfSamplesToBeProcessed - 1, w = 1; i >= 0; i--, w++)
{
wAvg.AddSample(_dataQueue.GetElementAt(i, true), w);
}
return wAvg;
}
/// <summary>
/// Computes the linearly weighted average of the differences of the data in the window.
/// </summary>
/// <param name="reqNumOfSamples">The requiered mumber of samples (-1 means all).</param>
public WeightedAvg GetDataDiffLinWeightedAvg(int reqNumOfSamples = -1)
{
int numOfSamplesToBeProcessed = reqNumOfSamples == -1 ? _dataQueue.Count : reqNumOfSamples;
CheckReadyness(numOfSamplesToBeProcessed);
WeightedAvg wAvg = new WeightedAvg();
for (int i = numOfSamplesToBeProcessed - 2, w = 1; i >= 0; i--, w++)
{
wAvg.AddSample(_dataQueue.GetElementAt(i, true) - _dataQueue.GetElementAt(i + 1, true));
}
return wAvg;
}
/// <summary>
/// Computes the statistics of the data in the window.
/// </summary>
/// <param name="reqNumOfSamples">The requiered mumber of samples (-1 means all).</param>
public BasicStat GetDataStat(int reqNumOfSamples = -1)
{
int numOfSamplesToBeProcessed = reqNumOfSamples == -1 ? _dataQueue.Count : reqNumOfSamples;
CheckReadyness(numOfSamplesToBeProcessed);
BasicStat stat = new BasicStat();
for (int i = numOfSamplesToBeProcessed - 1; i >= 0; i--)
{
stat.AddSample(_dataQueue.GetElementAt(i, true));
}
return stat;
}
/// <summary>
/// Computes the statistics of the differences of the data in the window.
/// </summary>
/// <param name="reqNumOfSamples">The requiered mumber of samples (-1 means all).</param>
public BasicStat GetDataDiffStat(int reqNumOfSamples = -1)
{
int numOfSamplesToBeProcessed = reqNumOfSamples == -1 ? _dataQueue.Count : reqNumOfSamples;
CheckReadyness(numOfSamplesToBeProcessed);
BasicStat stat = new BasicStat();
for (int i = numOfSamplesToBeProcessed - 2; i >= 0; i--)
{
stat.AddSample(_dataQueue.GetElementAt(i, true) - _dataQueue.GetElementAt(i + 1, true));
}
return stat;
}
/// <summary>
/// Computes the rescaled range of the data in the window.
/// </summary>
/// <param name="reqNumOfSamples">The requiered mumber of samples (-1 means all).</param>
public double GetDataRescaledRange(int reqNumOfSamples = -1)
{
int numOfSamplesToBeProcessed = reqNumOfSamples == -1 ? _dataQueue.Count : reqNumOfSamples;
CheckReadyness(numOfSamplesToBeProcessed);
RescaledRange rr = new RescaledRange(numOfSamplesToBeProcessed);
for (int i = numOfSamplesToBeProcessed - 1; i >= 0; i--)
{
rr.AddValue(_dataQueue.GetElementAt(i, true));
}
return rr.Compute();
}
/// <summary>
/// Computes the rescaled range of the differences of the data in the window.
/// </summary>
/// <param name="reqNumOfSamples">The requiered mumber of samples (-1 means all).</param>
public double GetDataDiffRescaledRange(int reqNumOfSamples = -1)
{
int numOfSamplesToBeProcessed = reqNumOfSamples == -1 ? _dataQueue.Count : reqNumOfSamples;
CheckReadyness(numOfSamplesToBeProcessed);
RescaledRange rr = new RescaledRange(numOfSamplesToBeProcessed - 1);
for (int i = numOfSamplesToBeProcessed - 2; i >= 0; i--)
{
rr.AddValue(_dataQueue.GetElementAt(i, true) - _dataQueue.GetElementAt(i + 1, true));
}
return rr.Compute();
}
/// <summary>
/// Creates the deep copy instance.
/// </summary>
public MovingDataWindow DeepClone()
{
return new MovingDataWindow(this);
}
/// <summary>
/// Resets the instance to the initial state.
/// </summary>
public void Reset()
{
_dataQueue.Reset();
return;
}
/// <summary>
/// Adds the sample.
/// </summary>
/// <param name="sample">The sample.</param>
public void AddSample(double sample)
{
_dataQueue.Enqueue(sample, true);
return;
}
}//MovingDataWindow
}//Namespace