The previous article describes how to perform continuous updates in the query mode. This blog article describes how to implement exponential smoothing in streaminsight.
Concepts
Before implementation, let's take a look at what the exponential smoothing method is?
Concepts: The exponential smoothing method is a computing method that can be applied to time series. It was initially proposed by Robert G. Brown. The exponential smoothing method can be used to generate smooth data for display and predict future trends. It is usually used in financial market and economic data. Of course, it can be applied to any continuous discrete set.
We usually write the original data sequence {xt} and record the result sequence of the exponential smoothing method as {st} to estimate the next occurrence of X. At T = 0, a simple exponential smoothing formula can be written:
α is a smoothing factor, and its value range is 0 <α <1.
Exponential Smoothing
Q: How to Implement exponential smoothing in streaminsight?
This section shows you how to use the user-defined operator (Udo, user-defined operator) to perform exponential smoothing on streaminsight event streams.
First, create an event stream containing 10 random floating point numbers:
var random = new Random();var startTime = DateTimeOffset.UtcNow;var xs = (from i in Enumerable.Range(0, 10) select PointEvent.CreateInsert(startTime.AddSeconds(i), random.NextDouble())).ToStream( Application, AdvanceTimeSettings.StrictlyIncreasingStartTime);
We want to implement a user-defined operator smoothingoperator and specify a smoothing factor to perform exponential smoothing on the original event sequence, as shown below:
xs.Scan(new SmoothingOperator(0.5));
The following section describes the implementation of smoothingoperator:
/// <Summary> /// achieve exponential smoothing /// </Summary> [datacontract] public sealed class smoothingoperator: ceppointstreamoperator <double, double> {[datamember] readonly double _ smoothingfactor; [datamember] double? _ Previusvalue; Public smoothingoperator (double smoothingfactor) {_ smoothingfactor = smoothingfactor;} public override ienumerable <double> processevent (pointevent <double> inputevent) {// The result is the synthesis of the previous calculation result and the current input value _ previusvalue = _ previousvalue. hasvalue? (1.0-_ smoothingfactor) * _ previusvalue. value + _ smoothingfactor * inputevent. payload: inputevent. payload; yield return _ previusvalue. value;} public override bool isempty {get {return false ;}}}
Note that the serialization flag must be added to smoothingoperator. Otherwise, the following exception occurs: "type 'userquery + smoothingoperator' cannot be serialized. consider marking it with the datacontractattribute attribute, and marking all of its members you want serialized with the datamemberattribute attribute. if the type is a collection, consider marking it with the collectiondatacontractattribute. see the Microsoft. net Framework documentation for other Su Pported types .". Of course, datacontract is not required here, or other serialization features such as XML can be used.
The running result is as follows:
The next article describes how to implement mode matching in the streaminsight query mode.