public override void Update(Bar bar) { double scale; if (Data.Length < WindowSize) { // always normalize the first open price as (0, 10), and normalize other data points as (N, 10 * price / first open price) scale = Data.Length == 0 ? DefaultPriceScale / bar.OpenPrice : DefaultPriceScale / Data[0].OpenPrice; _intermediateResult.Add((double)Data.Length, bar.OpenPrice * scale); _intermediateResult.Add((double)Data.Length, bar.ClosePrice * scale); Data.Add(bar); } else { // existing data have been normalized, but newly added data will cause original (0, 1) being spoiled out, // so we need to normalize it again. var bar0 = Data[0]; scale = DefaultPriceScale / bar0.OpenPrice; _intermediateResult.Remove(0.0, bar0.OpenPrice * scale); _intermediateResult.Remove(0.0, bar0.ClosePrice * scale); _intermediateResult.Add((double)WindowSize, bar.OpenPrice * scale); _intermediateResult.Add((double)WindowSize, bar.ClosePrice * scale); _intermediateResult.ShiftX(-1.0); // now we use the newly first point to normalize the points scale = bar0.OpenPrice / Data[1].OpenPrice; _intermediateResult.ScaleY(scale); Data.Add(bar); } if (Data.Length < 2) { SetValue(0.0, 0.0, 0.0, 0.0); } else { var result = _intermediateResult.Compute(); SetValue(result.Slope, result.Intercept, result.CorrelationCoefficient, result.SquareStandardError); } }
public void RemoveTest() { var ir = new LinearRegression.IntermediateResult(); ir.Add(0.0, 1.0); ir.Add(1.0, 2.0); ir.Add(2.0, 3.0); ir.Add(3.0, 4.0); var result = ir.Compute(); Assert.AreEqual(result.Intercept, 1.0); Assert.AreEqual(result.Slope, 1.0); Assert.AreEqual(result.CorrelationCoefficient, 1.0); Assert.AreEqual(result.SquareStandardError, 0.0); ir.Remove(0.0, 1.0); var result1 = ir.Compute(); Assert.AreEqual(result1.Intercept, 1.0); Assert.AreEqual(result1.Slope, 1.0); Assert.AreEqual(result1.CorrelationCoefficient, 1.0); Assert.AreEqual(result1.SquareStandardError, 0.0); }