C# - Time-series and correlation strategies
العربية
български
català
中文
čeština
dansk
Nederlands
eesti
suomi
français
Deutsch
Ελληνικά
עברית
हिंदी
magyar
Bahasa Indonesia
italiano
日本語
한국어
latviešu
lietuvių
norsk
polski
Português
română
русский
slovenčina
slovenski
español
svenska
ไทย
Türkçe
українська
Tiếng Việt
I have various time-series I'd like to correlate and present as either a csv-file or in-memory datatable (.NET). These time-series are arrays of time-value-pairs (actually these are objects containing more than just time and value). The time-series may span across different overlapping periods, and some might even have holes (missing values for given timestamps).
For those interested, I'm using the OPC HDA .NET library to extract historic time-series from an OPC HDA server.
The resulting datatable should have one column for each time-series all in chronological order based on a timestamp column. See example below:
|-------|-------|-------|-------|-------|
TIME TS1 TS2 TS3 TS4
|-------|-------|-------|-------|-------|
1 X X X
|-------|-------|-------|-------|-------|
2 X X X X
|-------|-------|-------|-------|-------|
3 X X X
|-------|-------|-------|-------|-------|
4 X X X
|-------|-------|-------|-------|-------|
5 X X X
|-------|-------|-------|-------|-------|
What would be the most effective way of achieving this? With "effective" I mean with the least amount of code. But considering that the timeseries could become quite large, memory usage might also be an issue.
Answer |
You might go with a data structure like a nested dictionary and iterate over the contents:
Dictionary <TimeSeries, Dictionary<DateTime, Value>> dict = new Dictionary<TimeSeries, Dictionary<DateTime, Value>>();
foreach (TimeSeries series in dict.Keys) {
//table row output code goes here
Dictionary<DateTime, Value> innerDict = dict[series];
foreach (DateTime date in innerDict.Keys) {
Value seriesValueAtTimeT = innerDict[date];
//table column output code goes here
}
}
Where your output code is writing out to something else, depending on your needs, and you replace the datatypes TimeSeries, Value, etc., with your actual data types.