CalcEngine Plus

User-authored calculations for .NET

Vector Operations

Beginning in version 2.2.0.0, CalcEnginePlus began supporting vector functions. Vector functions operate on arrays of time-series data. 

A typical use case of vector function would be to take a real-world array of raw, irregularly-spaced values and transform them into an array of regular-spaced interpolated values or time-weighted averages.

At present, there are three vector functions:

 

StepInterpolate  Performs "last-known-value" interpolation at each regular timestamp
LinearInterpolate performs linear interpolation between adjacent raw values at each regular timestamp
TimeWeightedAverage Calculates time-weighted average value from the raw values in between each timestamp

 

These function exist in the CalcEnginePlus.VectorOperators namespace.

To use these vector operators, you need to construct them with 

and then access the 'VectorResult' property. This property will compute and return an array of regularly-spaced, timestamped values in the form of a Vector<ITimestampedValue>

The only unfamiliar type among the constructor arguments will be the TimeSeriesVector. This type is also defined in the CalcEnginePlus.VectorOperators namespace. It is essentially a List<ITimestampedValue>. When constructing the vector functions, this object contains the raw data for the vector function to operate on.

Note that the source vector is expected to be ordered, with the most-recent value at index [0]

Using the vector functions


        // create an array of time-series values for the vector function to operate on
        var timeseriesVector = new TimeSeriesVector();
        DateTime refTime = new DateTime(2015, 4, 1, 10, 0, 0);

        timeseriesVector.Add(refTime.AddHours(0), 10);
        timeseriesVector.Add(refTime.AddHours(-1), 13);
        timeseriesVector.Add(refTime.AddHours(-2), 11); 
        timeseriesVector.Add(refTime.AddHours(-3), 15);
        timeseriesVector.Add(refTime.AddHours(-4), 3); 
        timeseriesVector.Add(refTime.AddHours(-5), 5); 
        timeseriesVector.Add(refTime.AddHours(-6), 3);  

        // Set up a start and end point for the resultant output vector
        var endTime = refTime.AddHours(-2);     // 08:00
        var startTime = refTime.AddHours(-4);   // 06:00

         // Set up a step interval of 2 hours
        var stepSize = new TimeSpan(2, 0, 0);

        // Time-weighted average function in this example. But they're all interchangeable
        var twaFunction = new TimeWeightedAverage(startTime, endTime, stepSize, timeseriesVector);

        // The VectorResult property computes the time-weighted average array each time it is invoked
        var resultVector = twaFunction.VectorResult;