Correlogram Class |
The major difference of the class versus the Correlation is that it produces an array of correlation values corresponding to several time lags.
The following constructor creates instances to produce an expanding window Correlogram for the specified number of lagged correlations:
The input parameter determines the number of different lags to compute correlations for; the constructor allow negative and zero input parameter which is converted to the absolute value and interpreted as follows: the Correlogram class sets up also negative lag values so that the lags spectrum is presented as
-CorrelogramSize, -CorrelogramSize+1,..., -1, 0, 1, ..., CorrelogramSize.
To operate with sliding data sets, it is necessary to use the following constructors and define the sliding window size value either as a time period or a number of data points:
Correlogram(Int32, Int32, Boolean)
Correlogram(TimeSpan, Int32, Boolean)
The lags volume must be in accordance (not exceed) with the window size value, lags are initialized as described above.
To modify the results with new data points, the Add(…) methods are provided either time-assigned or not, user is restricted with selection of just one type for a given instance.
Current results are available via the property:
The returned object of double[] type contains correlation values corresponding to lags and allows access to its elements by index. e.g. Corr[i].
Here is the example of Correlogram class usage:
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using FinAnalysis.TA; 6 7namespace TwoSeriesMultipleValueIndicatorSample 8{ 9 class TwoSeriesMultipleValueIndicatorSample 10 { 11 const int Steps = 0xFF; 12 13 static void Main() 14 { 15 Correlogram correlogram = new Correlogram(12, 8); 16 Random r = new Random(); 17 18 for (int i = 0; i < Steps; ++i) 19 { 20 double value1 = r.NextDouble(); 21 double value2 = r.NextDouble(); 22 23 if (correlogram.Add(value1, value2)) 24 { 25 double []corr = correlogram.Corr; 26 string corr_string = string.Empty; 27 for (int p = 0; p < corr.Length; ++p) 28 { 29 if (p > 0) 30 corr_string += ","; 31 32 corr_string += corr[p].ToString("0.00"); 33 } 34 35 Console.WriteLine("Correlogram = {0:0.00}", corr_string); 36 } 37 } 38 39 40 Correlogram correlogramTimeBased = new Correlogram(12, 8); 41 for (int i = 0; i < Steps; ++i) 42 { 43 double value1 = r.NextDouble(); 44 double value2 = r.NextDouble(); 45 46 if (correlogramTimeBased.Add(value1, value2, DateTime.Now)) 47 { 48 double[] corr = correlogramTimeBased.Corr; 49 string corr_string = string.Empty; 50 for (int p = 0; p < corr.Length; ++p) 51 { 52 if (p > 0) 53 corr_string += ","; 54 55 corr_string += corr[p].ToString("0.00"); 56 } 57 58 Console.WriteLine("Correlogram Time Based = {0:0.00}", corr_string); 59 } 60 } 61 } 62 } 63}