[Original] streaminsight query series (23)-exponential smoothing of query modes

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.