public void LinearPredictor1Indicator() { TimeSeries indicator = TimeSeries.CreateDailySinusoidalTimeSeries(1, 2, 0, new DateTime(2000, 1, 1), new DateTime(2000, 12, 31)); indicator.Name = "INDICATOR"; TimeSeries target = indicator.OffsetBy(new TimeSpan(10, 0, 0, 0)); target.Name = "TARGET"; CorrelationAnalysisParameters parameters = new CorrelationAnalysisParameters() { Span = new TimeSpan(1, 0, 0, 0), NumberOfSpansInthePast = 10, NumberOfSpansIntheFuture = -10, }; IEnumerable <TemporalGapTuple> analysis = TimeSeriesCorrelation.DoCorrelationAnalysis(indicator, target, parameters); TemporalGapGroup g = new TemporalGapGroup() { Pairs = analysis .Where(a => Math.Abs(a.Correlation - 1) < tolerance) .ToList(), }; TimeSpan predictionSpan = new TimeSpan(5, 0, 0, 0); DataTable table = g.GetPredictionTable(target.Name, predictionSpan); LinearPredictor linearPredictor = new LinearPredictor(); linearPredictor.Learn( table: table, trainingLabel: indicator.Name, targetLabel: target.Name); TimeSpan correlationSpan = new TimeSpan(10, 0, 0, 0); Assert.IsTrue(target.Dates .Where(day => target.ContainsValueAt(day.Add(predictionSpan)) && indicator.ContainsValueAt(day.Add(-correlationSpan)) && indicator.ContainsValueAt(day.Add(-correlationSpan).Add(predictionSpan))) .All(day => { double incrementIndicator = indicator[day.Add(-correlationSpan).Add(predictionSpan)] - indicator[day.Add(-correlationSpan)]; DataRow row = table.NewRow(); row[indicator.Name] = incrementIndicator; return(Math.Abs((double)linearPredictor.Predict(row) - incrementIndicator) < tolerance); })); }
public void CorrelationSinus() { TimeSeries sts = TimeSeries.CreateDailySinusoidalTimeSeries(1, 2, 0, new DateTime(2000, 1, 1), new DateTime(2000, 12, 31)); CorrelationAnalysisParameters parameters = new CorrelationAnalysisParameters() { Span = new TimeSpan(1, 0, 0, 0), NumberOfSpansInthePast = 10, NumberOfSpansIntheFuture = 10, }; List <TemporalGapTuple> analysis = TimeSeriesCorrelation.DoCorrelationAnalysis(sts, sts, parameters) .ToList(); TemporalGapTuple maxCorrelationAnalysis = analysis .MaxBy(an => an.Correlation); Assert.IsTrue(Math.Abs(maxCorrelationAnalysis.Correlation - 1) < tolerance); Assert.IsTrue(maxCorrelationAnalysis.Indicator == maxCorrelationAnalysis.Target && maxCorrelationAnalysis.TemporalGap.Ticks == 0); Assert.IsTrue(analysis.All(an => an.Correlation <= maxCorrelationAnalysis.Correlation)); }