Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs @ 18060

Last change on this file since 18060 was 18060, checked in by pfleck, 3 years ago

#3040 Added a subvector symbol with ranges as subtrees.

File size: 53.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31using MathNet.Numerics;
32using MathNet.Numerics.Statistics;
33using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [StorableType("DE68A1D9-5AFC-4DDD-AB62-29F3B8FC28E0")]
37  [Item("SymbolicDataAnalysisExpressionTreeVectorInterpreter", "Interpreter for symbolic expression trees including vector arithmetic.")]
38  public class SymbolicDataAnalysisExpressionTreeVectorInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
39    [StorableType("2612504E-AD5F-4AE2-B60E-98A5AB59E164")]
40    public enum Aggregation {
41      Mean,
42      Median,
43      Sum,
44      First,
45      L1Norm,
46      L2Norm,
47      NaN,
48      Exception
49    }
50    public static double Aggregate(Aggregation aggregation, DoubleVector vector) {
51      switch (aggregation) {
52        case Aggregation.Mean: return Statistics.Mean(vector);
53        case Aggregation.Median: return Statistics.Median(vector);
54        case Aggregation.Sum: return vector.Sum();
55        case Aggregation.First: return vector.First();
56        case Aggregation.L1Norm: return vector.L1Norm();
57        case Aggregation.L2Norm: return vector.L2Norm();
58        case Aggregation.NaN: return double.NaN;
59        case Aggregation.Exception: throw new InvalidOperationException("Result of the tree is not a scalar.");
60        default: throw new ArgumentOutOfRangeException(nameof(aggregation), aggregation, null);
61      }
62    }
63
64    [StorableType("73DCBB45-916F-4139-8ADC-57BA610A1B66")]
65    public enum VectorLengthStrategy {
66      ExceptionIfDifferent,
67      FillShorterWithNaN,
68      FillShorterWithNeutralElement,
69      CutLonger,
70      ResampleToLonger,
71      ResampleToShorter,
72      CycleShorter
73    }
74
75    #region Implementation VectorLengthStrategy
76    public static (DoubleVector, DoubleVector) ExceptionIfDifferent(DoubleVector lhs, DoubleVector rhs) {
77      if (lhs.Count != rhs.Count)
78        throw new InvalidOperationException($"Vector Lengths incompatible ({lhs.Count} vs. {rhs.Count}");
79      return (lhs, rhs);
80    }
81
82    public static (DoubleVector, DoubleVector) FillShorter(DoubleVector lhs, DoubleVector rhs, double fillElement) {
83      var targetLength = Math.Max(lhs.Count, rhs.Count);
84
85      DoubleVector PadVector(DoubleVector v) {
86        if (v.Count == targetLength) return v;
87        var p = DoubleVector.Build.Dense(targetLength, fillElement);
88        v.CopySubVectorTo(p, 0, 0, v.Count);
89        return p;
90      }
91
92      return (PadVector(lhs), PadVector(rhs));
93    }
94
95    public static (DoubleVector, DoubleVector) CutLonger(DoubleVector lhs, DoubleVector rhs) {
96      var targetLength = Math.Min(lhs.Count, rhs.Count);
97
98      DoubleVector CutVector(DoubleVector v) {
99        if (v.Count == targetLength) return v;
100        return v.SubVector(0, targetLength);
101      }
102
103      return (CutVector(lhs), CutVector(rhs));
104    }
105
106    private static DoubleVector ResampleToLength(DoubleVector v, int targetLength) {
107      if (v.Count == targetLength) return v;
108
109      var indices = Enumerable.Range(0, v.Count).Select(x => (double)x);
110      var interpolation = Interpolate.Linear(indices, v);
111
112      var resampledIndices = Enumerable.Range(0, targetLength).Select(i => (double)i / targetLength * v.Count);
113      var interpolatedValues = resampledIndices.Select(interpolation.Interpolate);
114
115      return DoubleVector.Build.DenseOfEnumerable(interpolatedValues);
116    }
117    public static (DoubleVector, DoubleVector) ResampleToLonger(DoubleVector lhs, DoubleVector rhs) {
118      var maxLength = Math.Max(lhs.Count, rhs.Count);
119      return (ResampleToLength(lhs, maxLength), ResampleToLength(rhs, maxLength));
120    }
121    public static (DoubleVector, DoubleVector) ResampleToShorter(DoubleVector lhs, DoubleVector rhs) {
122      var minLength = Math.Min(lhs.Count, rhs.Count);
123      return (ResampleToLength(lhs, minLength), ResampleToLength(rhs, minLength));
124    }
125
126    public static (DoubleVector, DoubleVector) CycleShorter(DoubleVector lhs, DoubleVector rhs) {
127      var targetLength = Math.Max(lhs.Count, rhs.Count);
128
129      DoubleVector CycleVector(DoubleVector v) {
130        if (v.Count == targetLength) return v;
131        var cycledValues = Enumerable.Range(0, targetLength).Select(i => v[i % v.Count]);
132        return DoubleVector.Build.DenseOfEnumerable(cycledValues);
133      }
134
135      return (CycleVector(lhs), CycleVector(rhs));
136    }
137    #endregion
138
139    public static (DoubleVector lhs, DoubleVector rhs) ApplyVectorLengthStrategy(VectorLengthStrategy strategy, DoubleVector lhs, DoubleVector rhs,
140      double neutralElement = double.NaN) {
141
142      switch (strategy) {
143        case VectorLengthStrategy.ExceptionIfDifferent: return ExceptionIfDifferent(lhs, rhs);
144        case VectorLengthStrategy.FillShorterWithNaN: return FillShorter(lhs, rhs, double.NaN);
145        case VectorLengthStrategy.FillShorterWithNeutralElement: return FillShorter(lhs, rhs, neutralElement);
146        case VectorLengthStrategy.CutLonger: return CutLonger(lhs, rhs);
147        case VectorLengthStrategy.ResampleToLonger: return ResampleToLonger(lhs, rhs);
148        case VectorLengthStrategy.ResampleToShorter: return ResampleToShorter(lhs, rhs);
149        case VectorLengthStrategy.CycleShorter: return CycleShorter(lhs, rhs);
150        default: throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null);
151      }
152    }
153
154    #region Aggregation Symbols
155    private static Type[] AggregationSymbols = new[] {
156      typeof(Sum), typeof(Mean), typeof(Length), typeof(StandardDeviation), typeof(Variance),
157      typeof(EuclideanDistance), typeof(Covariance)
158    };
159    #endregion
160
161    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
162    private const string FinalAggregationParameterName = "FinalAggregation";
163    private const string DifferentVectorLengthStrategyParameterName = "DifferentVectorLengthStrategy";
164
165    public override bool CanChangeName {
166      get { return false; }
167    }
168
169    public override bool CanChangeDescription {
170      get { return false; }
171    }
172
173    #region parameter properties
174    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
175      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
176    }
177    public IFixedValueParameter<EnumValue<Aggregation>> FinalAggregationParameter {
178      get { return (IFixedValueParameter<EnumValue<Aggregation>>)Parameters[FinalAggregationParameterName]; }
179    }
180    public IFixedValueParameter<EnumValue<VectorLengthStrategy>> DifferentVectorLengthStrategyParameter {
181      get { return (IFixedValueParameter<EnumValue<VectorLengthStrategy>>)Parameters[DifferentVectorLengthStrategyParameterName]; }
182    }
183    #endregion
184
185    #region properties
186    public int EvaluatedSolutions {
187      get { return EvaluatedSolutionsParameter.Value.Value; }
188      set { EvaluatedSolutionsParameter.Value.Value = value; }
189    }
190    public Aggregation FinalAggregation {
191      get { return FinalAggregationParameter.Value.Value; }
192      set { FinalAggregationParameter.Value.Value = value; }
193    }
194    public VectorLengthStrategy DifferentVectorLengthStrategy {
195      get { return DifferentVectorLengthStrategyParameter.Value.Value; }
196      set { DifferentVectorLengthStrategyParameter.Value.Value = value; }
197    }
198    #endregion
199
200    [StorableConstructor]
201    protected SymbolicDataAnalysisExpressionTreeVectorInterpreter(StorableConstructorFlag _) : base(_) { }
202
203    protected SymbolicDataAnalysisExpressionTreeVectorInterpreter(SymbolicDataAnalysisExpressionTreeVectorInterpreter original, Cloner cloner)
204      : base(original, cloner) { }
205
206    public override IDeepCloneable Clone(Cloner cloner) {
207      return new SymbolicDataAnalysisExpressionTreeVectorInterpreter(this, cloner);
208    }
209
210    public SymbolicDataAnalysisExpressionTreeVectorInterpreter()
211      : this("SymbolicDataAnalysisExpressionTreeVectorInterpreter", "Interpreter for symbolic expression trees including vector arithmetic.") { }
212
213    protected SymbolicDataAnalysisExpressionTreeVectorInterpreter(string name, string description)
214      : base(name, description) {
215      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
216      Parameters.Add(new FixedValueParameter<EnumValue<Aggregation>>(FinalAggregationParameterName, "If root node of the expression tree results in a Vector it is aggregated according to this parameter", new EnumValue<Aggregation>(Aggregation.Mean)));
217      Parameters.Add(new FixedValueParameter<EnumValue<VectorLengthStrategy>>(DifferentVectorLengthStrategyParameterName, "", new EnumValue<VectorLengthStrategy>(VectorLengthStrategy.ExceptionIfDifferent)));
218    }
219
220    [StorableHook(HookType.AfterDeserialization)]
221    private void AfterDeserialization() {
222      if (!Parameters.ContainsKey(FinalAggregationParameterName)) {
223        Parameters.Add(new FixedValueParameter<EnumValue<Aggregation>>(FinalAggregationParameterName, "If root node of the expression tree results in a Vector it is aggregated according to this parameter", new EnumValue<Aggregation>(Aggregation.Mean)));
224      }
225      if (!Parameters.ContainsKey(DifferentVectorLengthStrategyParameterName)) {
226        Parameters.Add(new FixedValueParameter<EnumValue<VectorLengthStrategy>>(DifferentVectorLengthStrategyParameterName, "", new EnumValue<VectorLengthStrategy>(VectorLengthStrategy.ExceptionIfDifferent)));
227      }
228    }
229
230    #region IStatefulItem
231    public void InitializeState() {
232      EvaluatedSolutions = 0;
233    }
234
235    public void ClearState() { }
236    #endregion
237
238    private readonly object syncRoot = new object();
239    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
240      lock (syncRoot) {
241        EvaluatedSolutions++; // increment the evaluated solutions counter
242      }
243      var state = PrepareInterpreterState(tree, dataset);
244
245      foreach (var rowEnum in rows) {
246        int row = rowEnum;
247        var result = Evaluate(dataset, ref row, state);
248        if (result.IsScalar)
249          yield return result.Scalar;
250        else if (result.IsVector) {
251          yield return Aggregate(FinalAggregation, result.Vector);
252        } else
253          yield return double.NaN;
254        state.Reset();
255      }
256    }
257
258    public IEnumerable<Dictionary<ISymbolicExpressionTreeNode, EvaluationResult>> GetIntermediateNodeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
259      var state = PrepareInterpreterState(tree, dataset);
260
261      foreach (var rowEnum in rows) {
262        int row = rowEnum;
263        var traceDict = new Dictionary<ISymbolicExpressionTreeNode, EvaluationResult>();
264        var result = Evaluate(dataset, ref row, state, traceDict);
265        traceDict.Add(tree.Root.GetSubtree(0), result); // Add StartSymbol
266        yield return traceDict;
267        state.Reset();
268      }
269    }
270
271    private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset) {
272      Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
273      int necessaryArgStackSize = 0;
274      foreach (Instruction instr in code) {
275        if (instr.opCode == OpCodes.Variable) {
276          var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
277          if (dataset.VariableHasType<double>(variableTreeNode.VariableName))
278            instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
279          else if (dataset.VariableHasType<DoubleVector>(variableTreeNode.VariableName))
280            instr.data = dataset.GetReadOnlyDoubleVectorValues(variableTreeNode.VariableName);
281          else throw new NotSupportedException($"Type of variable {variableTreeNode.VariableName} is not supported.");
282        } else if (instr.opCode == OpCodes.FactorVariable) {
283          var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
284          instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
285        } else if (instr.opCode == OpCodes.BinaryFactorVariable) {
286          var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
287          instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
288        } else if (instr.opCode == OpCodes.LagVariable) {
289          var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
290          instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
291        } else if (instr.opCode == OpCodes.VariableCondition) {
292          var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
293          instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
294        } else if (instr.opCode == OpCodes.Call) {
295          necessaryArgStackSize += instr.nArguments + 1;
296        }
297      }
298      return new InterpreterState(code, necessaryArgStackSize);
299    }
300
301
302    public struct EvaluationResult {
303      public double Scalar { get; }
304      public bool IsScalar => !double.IsNaN(Scalar);
305
306      public DoubleVector Vector { get; }
307      public bool IsVector => !(Vector.Count == 1 && double.IsNaN(Vector[0]));
308
309      public bool IsNaN => !IsScalar && !IsVector;
310
311      public EvaluationResult(double scalar) {
312        Scalar = scalar;
313        Vector = NaNVector;
314      }
315      public EvaluationResult(DoubleVector vector) {
316        if (vector == null) throw new ArgumentNullException(nameof(vector));
317        Vector = vector;
318        Scalar = double.NaN;
319      }
320
321      public override string ToString() {
322        if (IsScalar) return Scalar.ToString();
323        if (IsVector) return Vector.ToVectorString();
324        return "NaN";
325      }
326
327      private static readonly DoubleVector NaNVector = DoubleVector.Build.Dense(1, double.NaN);
328      public static readonly EvaluationResult NaN = new EvaluationResult(double.NaN);
329    }
330
331    private static EvaluationResult ArithmeticApply(EvaluationResult lhs, EvaluationResult rhs,
332      Func<DoubleVector, DoubleVector, (DoubleVector, DoubleVector)> lengthStrategy,
333      Func<double, double, double> ssFunc = null,
334      Func<double, DoubleVector, DoubleVector> svFunc = null,
335      Func<DoubleVector, double, DoubleVector> vsFunc = null,
336      Func<DoubleVector, DoubleVector, DoubleVector> vvFunc = null) {
337
338      if (lhs.IsScalar && rhs.IsScalar && ssFunc != null) return new EvaluationResult(ssFunc(lhs.Scalar, rhs.Scalar));
339      if (lhs.IsScalar && rhs.IsVector && svFunc != null) return new EvaluationResult(svFunc(lhs.Scalar, rhs.Vector));
340      if (lhs.IsVector && rhs.IsScalar && vsFunc != null) return new EvaluationResult(vsFunc(lhs.Vector, rhs.Scalar));
341      if (lhs.IsVector && rhs.IsVector && vvFunc != null) {
342        if (lhs.Vector.Count == rhs.Vector.Count) {
343          return new EvaluationResult(vvFunc(lhs.Vector, rhs.Vector));
344        } else {
345          var (lhsVector, rhsVector) = lengthStrategy(lhs.Vector, rhs.Vector);
346          return new EvaluationResult(vvFunc(lhsVector, rhsVector));
347        }
348      }
349      return EvaluationResult.NaN;
350    }
351
352    private static EvaluationResult FunctionApply(EvaluationResult val,
353      Func<double, double> sFunc = null,
354      Func<DoubleVector, DoubleVector> vFunc = null) {
355      if (val.IsScalar && sFunc != null) return new EvaluationResult(sFunc(val.Scalar));
356      if (val.IsVector && vFunc != null) return new EvaluationResult(vFunc(val.Vector));
357      return EvaluationResult.NaN;
358    }
359    private static EvaluationResult AggregateApply(EvaluationResult val,
360      Func<double, double> sFunc = null,
361      Func<DoubleVector, double> vFunc = null) {
362      if (val.IsScalar && sFunc != null) return new EvaluationResult(sFunc(val.Scalar));
363      if (val.IsVector && vFunc != null) return new EvaluationResult(vFunc(val.Vector));
364      return EvaluationResult.NaN;
365    }
366
367    private static EvaluationResult WindowedAggregateApply(EvaluationResult val, WindowedSymbolTreeNode node,
368      Func<double, double> sFunc = null,
369      Func<DoubleVector, double> vFunc = null) {
370
371      // Parameters are interpreted as start and end with wrapping
372      var start = node.Offset;
373      var end = node.Length;
374
375      DoubleVector SubVector(DoubleVector v) {
376        int startIdx = (int)Math.Round(start * v.Count);
377        int endIdx = (int)Math.Round(end * v.Count);
378        int size = v.Count;
379        if (startIdx < endIdx) {
380          return v.SubVector(startIdx, count: endIdx - startIdx);
381        } else { // wrap around
382          var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx));
383          v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx]
384          v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx);   // copy [0:endIdx]      to [size-startIdx:size]
385          return resultVector;
386        }
387      }
388
389      if (val.IsScalar && sFunc != null) return new EvaluationResult(sFunc(val.Scalar));
390      if (val.IsVector && vFunc != null) return new EvaluationResult(vFunc(SubVector(val.Vector)));
391      return EvaluationResult.NaN;
392    }
393    private static EvaluationResult WindowedFunctionApply(EvaluationResult val, IWindowedSymbolTreeNode node,
394      Func<double, double> sFunc = null,
395      Func<DoubleVector, DoubleVector> vFunc = null) {
396      // Parameters are interpreted as start and end with wrapping
397      var start = node.Offset;
398      var end = node.Length;
399
400      DoubleVector SubVector(DoubleVector v) {
401        int startIdx = (int)Math.Round(start * v.Count);
402        int endIdx = (int)Math.Round(end * v.Count);
403        int size = v.Count;
404        if (startIdx < endIdx) {
405          return v.SubVector(startIdx, count: endIdx - startIdx);
406        } else { // wrap around
407          var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx));
408          v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx]
409          v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx);   // copy [0:endIdx]      to [size-startIdx:size]
410          return resultVector;
411        }
412      }
413
414      if (val.IsScalar && sFunc != null) return new EvaluationResult(sFunc(val.Scalar));
415      if (val.IsVector && vFunc != null) return new EvaluationResult(vFunc(SubVector(val.Vector)));
416      return EvaluationResult.NaN;
417    }
418
419    private static EvaluationResult AggregateMultipleApply(EvaluationResult lhs, EvaluationResult rhs,
420      Func<DoubleVector, DoubleVector, (DoubleVector, DoubleVector)> lengthStrategy,
421      Func<double, double, double> ssFunc = null,
422      Func<double, DoubleVector, double> svFunc = null,
423      Func<DoubleVector, double, double> vsFunc = null,
424      Func<DoubleVector, DoubleVector, double> vvFunc = null) {
425      if (lhs.IsScalar && rhs.IsScalar && ssFunc != null) return new EvaluationResult(ssFunc(lhs.Scalar, rhs.Scalar));
426      if (lhs.IsScalar && rhs.IsVector && svFunc != null) return new EvaluationResult(svFunc(lhs.Scalar, rhs.Vector));
427      if (lhs.IsVector && rhs.IsScalar && vsFunc != null) return new EvaluationResult(vsFunc(lhs.Vector, rhs.Scalar));
428      if (lhs.IsVector && rhs.IsVector && vvFunc != null) {
429        if (lhs.Vector.Count == rhs.Vector.Count) {
430          return new EvaluationResult(vvFunc(lhs.Vector, rhs.Vector));
431        } else {
432          var (lhsVector, rhsVector) = lengthStrategy(lhs.Vector, rhs.Vector);
433          return new EvaluationResult(vvFunc(lhsVector, rhsVector));
434        }
435      }
436      return EvaluationResult.NaN;
437    }
438
439    public virtual Type GetNodeType(ISymbolicExpressionTreeNode node) {
440      if (node.DataType != null)
441        return node.DataType;
442
443      if (AggregationSymbols.Contains(node.Symbol.GetType()))
444        return typeof(double);
445
446      var argumentTypes = node.Subtrees.Select(GetNodeType);
447      if (argumentTypes.Any(t => t == typeof(DoubleVector)))
448        return typeof(DoubleVector);
449
450      return typeof(double);
451    }
452
453
454    public virtual EvaluationResult Evaluate(IDataset dataset, ref int row, InterpreterState state,
455      IDictionary<ISymbolicExpressionTreeNode, EvaluationResult> traceDict = null) {
456
457      void TraceEvaluation(Instruction instr, EvaluationResult result) {
458        traceDict?.Add(instr.dynamicNode, result);
459      }
460
461      Instruction currentInstr = state.NextInstruction();
462      switch (currentInstr.opCode) {
463        case OpCodes.Add: {
464            var cur = Evaluate(dataset, ref row, state, traceDict);
465            for (int i = 1; i < currentInstr.nArguments; i++) {
466              var op = Evaluate(dataset, ref row, state, traceDict);
467              cur = ArithmeticApply(cur, op,
468                (lhs, rhs) => ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 0.0),
469                (s1, s2) => s1 + s2,
470                (s1, v2) => s1 + v2,
471                (v1, s2) => v1 + s2,
472                (v1, v2) => v1 + v2);
473            }
474            TraceEvaluation(currentInstr, cur);
475            return cur;
476          }
477        case OpCodes.Sub: {
478            var cur = Evaluate(dataset, ref row, state, traceDict);
479            for (int i = 1; i < currentInstr.nArguments; i++) {
480              var op = Evaluate(dataset, ref row, state, traceDict);
481              cur = ArithmeticApply(cur, op,
482                (lhs, rhs) => ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 0.0),
483                (s1, s2) => s1 - s2,
484                (s1, v2) => s1 - v2,
485                (v1, s2) => v1 - s2,
486                (v1, v2) => v1 - v2);
487            }
488            if (currentInstr.nArguments == 1)
489              cur = FunctionApply(cur,
490                s => -s,
491                v => -v);
492            TraceEvaluation(currentInstr, cur);
493            return cur;
494          }
495        case OpCodes.Mul: {
496            var cur = Evaluate(dataset, ref row, state, traceDict);
497            for (int i = 1; i < currentInstr.nArguments; i++) {
498              var op = Evaluate(dataset, ref row, state, traceDict);
499              cur = ArithmeticApply(cur, op,
500                (lhs, rhs) => ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 1.0),
501                (s1, s2) => s1 * s2,
502                (s1, v2) => s1 * v2,
503                (v1, s2) => v1 * s2,
504                (v1, v2) => v1.PointwiseMultiply(v2));
505            }
506            TraceEvaluation(currentInstr, cur);
507            return cur;
508          }
509        case OpCodes.Div: {
510            var cur = Evaluate(dataset, ref row, state, traceDict);
511            for (int i = 1; i < currentInstr.nArguments; i++) {
512              var op = Evaluate(dataset, ref row, state, traceDict);
513              cur = ArithmeticApply(cur, op,
514                (lhs, rhs) => ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 1.0),
515                (s1, s2) => s1 / s2,
516                (s1, v2) => s1 / v2,
517                (v1, s2) => v1 / s2,
518                (v1, v2) => v1 / v2);
519            }
520            if (currentInstr.nArguments == 1)
521              cur = FunctionApply(cur,
522                s => 1 / s,
523                v => 1 / v);
524            TraceEvaluation(currentInstr, cur);
525            return cur;
526          }
527        case OpCodes.Absolute: {
528            var cur = Evaluate(dataset, ref row, state, traceDict);
529            cur = FunctionApply(cur, Math.Abs, DoubleVector.Abs);
530            TraceEvaluation(currentInstr, cur);
531            return cur;
532          }
533        case OpCodes.Tanh: {
534            var cur = Evaluate(dataset, ref row, state, traceDict);
535            cur = FunctionApply(cur, Math.Tanh, DoubleVector.Tanh);
536            TraceEvaluation(currentInstr, cur);
537            return cur;
538          }
539        case OpCodes.Cos: {
540            var cur = Evaluate(dataset, ref row, state, traceDict);
541            cur = FunctionApply(cur, Math.Cos, DoubleVector.Cos);
542            TraceEvaluation(currentInstr, cur);
543            return cur;
544          }
545        case OpCodes.Sin: {
546            var cur = Evaluate(dataset, ref row, state, traceDict);
547            cur = FunctionApply(cur, Math.Sin, DoubleVector.Sin);
548            TraceEvaluation(currentInstr, cur);
549            return cur;
550          }
551        case OpCodes.Tan: {
552            var cur = Evaluate(dataset, ref row, state, traceDict);
553            cur = FunctionApply(cur, Math.Tan, DoubleVector.Tan);
554            TraceEvaluation(currentInstr, cur);
555            return cur;
556          }
557        case OpCodes.Square: {
558            var cur = Evaluate(dataset, ref row, state, traceDict);
559            cur = FunctionApply(cur,
560              s => Math.Pow(s, 2),
561              v => v.PointwisePower(2));
562            TraceEvaluation(currentInstr, cur);
563            return cur;
564          }
565        case OpCodes.Cube: {
566            var cur = Evaluate(dataset, ref row, state, traceDict);
567            cur = FunctionApply(cur,
568              s => Math.Pow(s, 3),
569              v => v.PointwisePower(3));
570            TraceEvaluation(currentInstr, cur);
571            return cur;
572          }
573        case OpCodes.Power: {
574            var x = Evaluate(dataset, ref row, state, traceDict);
575            var y = Evaluate(dataset, ref row, state, traceDict);
576            var cur = ArithmeticApply(x, y,
577              (lhs, rhs) => lhs.Count < rhs.Count
578                ? CutLonger(lhs, rhs)
579                : ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 1.0),
580              (s1, s2) => Math.Pow(s1, Math.Round(s2)),
581              (s1, v2) => DoubleVector.Build.Dense(v2.Count, s1).PointwisePower(DoubleVector.Round(v2)),
582              (v1, s2) => v1.PointwisePower(Math.Round(s2)),
583              (v1, v2) => v1.PointwisePower(DoubleVector.Round(v2)));
584            TraceEvaluation(currentInstr, cur);
585            return cur;
586          }
587        case OpCodes.SquareRoot: {
588            var cur = Evaluate(dataset, ref row, state, traceDict);
589            cur = FunctionApply(cur,
590              s => Math.Sqrt(s),
591              v => DoubleVector.Sqrt(v));
592            TraceEvaluation(currentInstr, cur);
593            return cur;
594          }
595        case OpCodes.CubeRoot: {
596            var cur = Evaluate(dataset, ref row, state, traceDict);
597            cur = FunctionApply(cur,
598              s => s < 0 ? -Math.Pow(-s, 1.0 / 3.0) : Math.Pow(s, 1.0 / 3.0),
599              v => v.Map(s => s < 0 ? -Math.Pow(-s, 1.0 / 3.0) : Math.Pow(s, 1.0 / 3.0)));
600            TraceEvaluation(currentInstr, cur);
601            return cur;
602          }
603        case OpCodes.Root: {
604            var x = Evaluate(dataset, ref row, state, traceDict);
605            var y = Evaluate(dataset, ref row, state, traceDict);
606            var cur = ArithmeticApply(x, y,
607              (lhs, rhs) => lhs.Count < rhs.Count
608                ? CutLonger(lhs, rhs)
609                : ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 1.0),
610              (s1, s2) => Math.Pow(s1, 1.0 / Math.Round(s2)),
611              (s1, v2) => DoubleVector.Build.Dense(v2.Count, s1).PointwisePower(1.0 / DoubleVector.Round(v2)),
612              (v1, s2) => v1.PointwisePower(1.0 / Math.Round(s2)),
613              (v1, v2) => v1.PointwisePower(1.0 / DoubleVector.Round(v2)));
614            TraceEvaluation(currentInstr, cur);
615            return cur;
616          }
617        case OpCodes.Exp: {
618            var cur = Evaluate(dataset, ref row, state, traceDict);
619            cur = FunctionApply(cur,
620              s => Math.Exp(s),
621              v => DoubleVector.Exp(v));
622            TraceEvaluation(currentInstr, cur);
623            return cur;
624          }
625        case OpCodes.Log: {
626            var cur = Evaluate(dataset, ref row, state, traceDict);
627            cur = FunctionApply(cur,
628              s => Math.Log(s),
629              v => DoubleVector.Log(v));
630            TraceEvaluation(currentInstr, cur);
631            return cur;
632          }
633        case OpCodes.Sum: {
634            var cur = Evaluate(dataset, ref row, state, traceDict);
635            cur = AggregateApply(cur,
636              s => s,
637              v => v.Sum());
638            TraceEvaluation(currentInstr, cur);
639            return cur;
640          }
641        case OpCodes.Mean: {
642            var cur = Evaluate(dataset, ref row, state, traceDict);
643            cur = AggregateApply(cur,
644              s => s,
645              v => Statistics.Mean(v));
646            TraceEvaluation(currentInstr, cur);
647            return cur;
648          }
649        case OpCodes.StandardDeviation: {
650            var cur = Evaluate(dataset, ref row, state, traceDict);
651            cur = AggregateApply(cur,
652              s => 0,
653              v => Statistics.PopulationStandardDeviation(v));
654            TraceEvaluation(currentInstr, cur);
655            return cur;
656          }
657        case OpCodes.Length: {
658            var cur = Evaluate(dataset, ref row, state, traceDict);
659            cur = AggregateApply(cur,
660              s => 1,
661              v => v.Count);
662            TraceEvaluation(currentInstr, cur);
663            return cur;
664          }
665        case OpCodes.Min: {
666            var cur = Evaluate(dataset, ref row, state, traceDict);
667            cur = AggregateApply(cur,
668              s => s,
669              v => Statistics.Minimum(v));
670            TraceEvaluation(currentInstr, cur);
671            return cur;
672          }
673        case OpCodes.Max: {
674            var cur = Evaluate(dataset, ref row, state, traceDict);
675            cur = AggregateApply(cur,
676              s => s,
677              v => Statistics.Maximum(v));
678            TraceEvaluation(currentInstr, cur);
679            return cur;
680          }
681        case OpCodes.Variance: {
682            var cur = Evaluate(dataset, ref row, state, traceDict);
683            cur = AggregateApply(cur,
684              s => 0,
685              v => Statistics.PopulationVariance(v));
686            TraceEvaluation(currentInstr, cur);
687            return cur;
688          }
689        case OpCodes.Skewness: {
690            var cur = Evaluate(dataset, ref row, state, traceDict);
691            cur = AggregateApply(cur,
692              s => double.NaN,
693              v => Statistics.PopulationSkewness(v));
694            TraceEvaluation(currentInstr, cur);
695            return cur;
696          }
697        case OpCodes.Kurtosis: {
698            var cur = Evaluate(dataset, ref row, state, traceDict);
699            cur = AggregateApply(cur,
700              s => double.NaN,
701              v => Statistics.PopulationKurtosis(v));
702            TraceEvaluation(currentInstr, cur);
703            return cur;
704          }
705        case OpCodes.EuclideanDistance: {
706            var x1 = Evaluate(dataset, ref row, state, traceDict);
707            var x2 = Evaluate(dataset, ref row, state, traceDict);
708            var cur = AggregateMultipleApply(x1, x2,
709              (lhs, rhs) => ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 0.0),
710              (s1, s2) => s1 - s2,
711              (s1, v2) => Math.Sqrt((s1 - v2).PointwisePower(2).Sum()),
712              (v1, s2) => Math.Sqrt((v1 - s2).PointwisePower(2).Sum()),
713              (v1, v2) => Math.Sqrt((v1 - v2).PointwisePower(2).Sum()));
714            TraceEvaluation(currentInstr, cur);
715            return cur;
716          }
717        case OpCodes.Covariance: {
718            var x1 = Evaluate(dataset, ref row, state, traceDict);
719            var x2 = Evaluate(dataset, ref row, state, traceDict);
720            var cur = AggregateMultipleApply(x1, x2,
721              (lhs, rhs) => ApplyVectorLengthStrategy(DifferentVectorLengthStrategy, lhs, rhs, 0.0),
722              (s1, s2) => 0,
723              (s1, v2) => 0,
724              (v1, s2) => 0,
725              (v1, v2) => Statistics.PopulationCovariance(v1, v2));
726            TraceEvaluation(currentInstr, cur);
727            return cur;
728          }
729        case OpCodes.SubVector: {
730          DoubleVector SubVector(DoubleVector v , double start, double end) {
731            int Mod(int x, int m) => (x % m + m) % m;
732            int startIdx = Mod((int)Math.Round(start * v.Count), v.Count);
733            int endIdx = Mod((int)Math.Round(end * v.Count), v.Count);
734              int size = v.Count;
735            if (startIdx < endIdx) {
736              return v.SubVector(startIdx, count: endIdx - startIdx);
737            } else { // wrap around
738              var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx));
739              v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx]
740              v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx);   // copy [0:endIdx]      to [size-startIdx:size]
741              return resultVector;
742            }
743          }
744          var cur = Evaluate(dataset, ref row, state, traceDict);
745          TraceEvaluation(currentInstr, cur);
746          return FunctionApply(cur,
747            s => s,
748            v => {
749              var node = (IWindowedSymbolTreeNode)currentInstr.dynamicNode;
750              return SubVector(v, node.Offset, node.Length);
751            });
752          }
753        case OpCodes.SubVectorSubtree: {
754          DoubleVector SubVector(DoubleVector v, double start, double end) {
755            int Mod(int x, int m) => (x % m + m) % m;
756            int startIdx = Mod((int)Math.Round(start * v.Count), v.Count);
757            int endIdx = Mod((int)Math.Round(end * v.Count), v.Count);
758            int size = v.Count;
759            if (startIdx < endIdx) {
760              return v.SubVector(startIdx, count: endIdx - startIdx);
761            } else { // wrap around
762              var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx));
763              v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx]
764              v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx);   // copy [0:endIdx]      to [size-startIdx:size]
765              return resultVector;
766            }
767          }
768          var cur = Evaluate(dataset, ref row, state, traceDict);
769          var offset = Evaluate(dataset, ref row, state, traceDict);
770          var length = Evaluate(dataset, ref row, state, traceDict);
771          TraceEvaluation(currentInstr, cur);
772          return FunctionApply(cur,
773            s => s,
774            v => SubVector(v, offset.Scalar, length.Scalar)
775          );
776        }
777        case OpCodes.Variable: {
778            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
779            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
780            if (currentInstr.data is IList<double> doubleList) {
781              var cur = new EvaluationResult(doubleList[row] * variableTreeNode.Weight);
782              TraceEvaluation(currentInstr, cur);
783              return cur;
784            }
785            if (currentInstr.data is IList<DoubleVector> doubleVectorList) {
786              var cur = new EvaluationResult(doubleVectorList[row] * variableTreeNode.Weight);
787              TraceEvaluation(currentInstr, cur);
788              return cur;
789            }
790            throw new NotSupportedException($"Unsupported type of variable: {currentInstr.data.GetType().GetPrettyName()}");
791          }
792        case OpCodes.BinaryFactorVariable: {
793            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
794            var factorVarTreeNode = currentInstr.dynamicNode as BinaryFactorVariableTreeNode;
795            var cur = new EvaluationResult(((IList<string>)currentInstr.data)[row] == factorVarTreeNode.VariableValue ? factorVarTreeNode.Weight : 0);
796            TraceEvaluation(currentInstr, cur);
797            return cur;
798          }
799        case OpCodes.FactorVariable: {
800            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
801            var factorVarTreeNode = currentInstr.dynamicNode as FactorVariableTreeNode;
802            var cur = new EvaluationResult(factorVarTreeNode.GetValue(((IList<string>)currentInstr.data)[row]));
803            TraceEvaluation(currentInstr, cur);
804            return cur;
805          }
806        case OpCodes.Constant: {
807            var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
808            var cur = new EvaluationResult(constTreeNode.Value);
809            TraceEvaluation(currentInstr, cur);
810            return cur;
811          }
812
813        #region Time Series Symbols
814        case OpCodes.Median: {
815            var cur = Evaluate(dataset, ref row, state, traceDict);
816            cur = AggregateApply(cur,
817              s => s,
818              v => Statistics.Median(v));
819            TraceEvaluation(currentInstr, cur);
820            return cur;
821          }
822        case OpCodes.Quantile: {
823            var cur = Evaluate(dataset, ref row, state, traceDict);
824            var q = Evaluate(dataset, ref row, state, traceDict);
825            cur = AggregateApply(cur,
826              s => s,
827              v => Statistics.Quantile(v, q.Scalar));
828            TraceEvaluation(currentInstr, cur);
829            return cur;
830          }
831
832        case OpCodes.AbsoluteEnergy: {
833            var cur = Evaluate(dataset, ref row, state, traceDict);
834            cur = AggregateApply(cur,
835              s => s * s,
836              v => v.PointwisePower(2.0).Sum());
837            TraceEvaluation(currentInstr, cur);
838            return cur;
839          }
840
841        case OpCodes.BinnedEntropy: {
842            var cur = Evaluate(dataset, ref row, state, traceDict);
843            var m = Evaluate(dataset, ref row, state, traceDict);
844            cur = AggregateApply(cur,
845              s => 0,
846              v => {
847                int bins = Math.Max((int)Math.Round(m.Scalar), 1);
848                double minValue = v.Minimum();
849                double maxValue = v.Maximum();
850                double intervalWidth = (maxValue - minValue) / bins;
851                int totalValues = v.Count;
852                double sum = 0;
853                for (int i = 0; i < Math.Max(bins, v.Count); i++) {
854                  double binMin = minValue * i;
855                  double binMax = binMin + intervalWidth;
856                  double countBin = v.Map(e => (e > binMin && e < binMax) ? 1.0 : 0.0).Sum();
857                  double percBin = countBin / totalValues;
858                  sum += percBin * Math.Log(percBin);
859                }
860
861                return sum;
862              });
863            TraceEvaluation(currentInstr, cur);
864            return cur;
865          }
866        case OpCodes.HasLargeStandardDeviation: {
867            var cur = Evaluate(dataset, ref row, state, traceDict);
868            cur = AggregateApply(cur,
869              s => 0,
870              v => Statistics.PopulationStandardDeviation(v) > (Statistics.Maximum(v) - Statistics.Minimum(v)) / 2 ? 1.0 : 0.0);
871            TraceEvaluation(currentInstr, cur);
872            return cur;
873          }
874        case OpCodes.HasVarianceLargerThanStd: {
875            var cur = Evaluate(dataset, ref row, state, traceDict);
876            cur = AggregateApply(cur,
877              s => 0,
878              v => Statistics.PopulationVariance(v) > Statistics.StandardDeviation(v) ? 1.0 : 0.0);
879            TraceEvaluation(currentInstr, cur);
880            return cur;
881          }
882        case OpCodes.IsSymmetricLooking: {
883            var cur = Evaluate(dataset, ref row, state, traceDict);
884            cur = AggregateApply(cur,
885              s => 0,
886              v => Math.Abs(Statistics.Mean(v) - Statistics.Median(v)) < (Statistics.Maximum(v) - Statistics.Minimum(v)) / 2 ? 1.0 : 0.0);
887            TraceEvaluation(currentInstr, cur);
888            return cur;
889          }
890        case OpCodes.NumberDataPointsAboveMean: {
891            var cur = Evaluate(dataset, ref row, state, traceDict);
892            cur = AggregateApply(cur,
893              s => 0,
894              v => {
895                double mean = Statistics.Mean(v);
896                return v.Map(e => e > mean ? 1.0 : 0.0).Sum();
897              });
898            TraceEvaluation(currentInstr, cur);
899            return cur;
900          }
901        case OpCodes.NumberDataPointsAboveMedian: {
902            var cur = Evaluate(dataset, ref row, state, traceDict);
903            cur = AggregateApply(cur,
904              s => 0,
905              v => {
906                double median = Statistics.Median(v);
907                return v.Map(e => e > median ? 1.0 : 0.0).Sum();
908              });
909            TraceEvaluation(currentInstr, cur);
910            return cur;
911          }
912        case OpCodes.NumberDataPointsBelowMean: {
913            var cur = Evaluate(dataset, ref row, state, traceDict);
914            cur = AggregateApply(cur,
915              s => 0,
916              v => {
917                double mean = Statistics.Mean(v);
918                return v.Map(e => e < mean ? 1.0 : 0.0).Sum();
919              });
920            TraceEvaluation(currentInstr, cur);
921            return cur;
922          }
923        case OpCodes.NumberDataPointsBelowMedian: {
924            var cur = Evaluate(dataset, ref row, state, traceDict);
925            cur = AggregateApply(cur,
926              s => 0,
927              v => {
928                double median = Statistics.Median(v);
929                return v.Map(e => e < median ? 1.0 : 0.0).Sum();
930              });
931            TraceEvaluation(currentInstr, cur);
932            return cur;
933          }
934
935        case OpCodes.ArimaModelCoefficients: {
936            var cur = Evaluate(dataset, ref row, state, traceDict);
937            var i = Evaluate(dataset, ref row, state, traceDict);
938            var k = Evaluate(dataset, ref row, state, traceDict);
939            cur = AggregateApply(cur,
940              s => 0,
941              v => throw new NotImplementedException(""));
942            TraceEvaluation(currentInstr, cur);
943            return cur;
944          }
945        case OpCodes.ContinuousWaveletTransformationCoefficients: {
946            var cur = Evaluate(dataset, ref row, state, traceDict);
947            var a = Evaluate(dataset, ref row, state, traceDict);
948            var b = Evaluate(dataset, ref row, state, traceDict);
949            cur = AggregateApply(cur,
950              s => 0,
951              v => throw new NotImplementedException(""));
952            TraceEvaluation(currentInstr, cur);
953            return cur;
954          }
955        case OpCodes.FastFourierTransformationCoefficient: {
956            var cur = Evaluate(dataset, ref row, state, traceDict);
957            var k = Evaluate(dataset, ref row, state, traceDict);
958            cur = AggregateApply(cur,
959              s => 0,
960              v => throw new NotImplementedException(""));
961            TraceEvaluation(currentInstr, cur);
962            return cur;
963          }
964        case OpCodes.FirstIndexMax: {
965            var cur = Evaluate(dataset, ref row, state, traceDict);
966            cur = AggregateApply(cur,
967              s => 0,
968              v => (double)v.MaximumIndex() / v.Count);
969            TraceEvaluation(currentInstr, cur);
970            return cur;
971          }
972        case OpCodes.FirstIndexMin: {
973            var cur = Evaluate(dataset, ref row, state, traceDict);
974            cur = AggregateApply(cur,
975              s => 0,
976              v => (double)v.MinimumIndex() / v.Count);
977            TraceEvaluation(currentInstr, cur);
978            return cur;
979          }
980        case OpCodes.LastIndexMax: {
981            var cur = Evaluate(dataset, ref row, state, traceDict);
982            cur = AggregateApply(cur,
983              s => 0,
984              v => (double)(v.Count - DoubleVector.Build.DenseOfEnumerable(v.Reverse()).MaximumIndex()) / v.Count);
985
986            TraceEvaluation(currentInstr, cur);
987            return cur;
988          }
989        case OpCodes.LastIndexMin: {
990            var cur = Evaluate(dataset, ref row, state, traceDict);
991            cur = AggregateApply(cur,
992              s => 0,
993              v => (double)(v.Count - DoubleVector.Build.DenseOfEnumerable(v.Reverse()).MinimumIndex()) / v.Count);
994            TraceEvaluation(currentInstr, cur);
995            return cur;
996          }
997        case OpCodes.LongestStrikeAboveMean: {
998            var cur = Evaluate(dataset, ref row, state, traceDict);
999            cur = AggregateApply(cur,
1000              s => 0,
1001              v => LongestStrikeAbove(v, Statistics.Mean(v)));
1002            TraceEvaluation(currentInstr, cur);
1003            return cur;
1004          }
1005        case OpCodes.LongestStrikeAboveMedian: {
1006            var cur = Evaluate(dataset, ref row, state, traceDict);
1007            cur = AggregateApply(cur,
1008              s => 0,
1009              v => LongestStrikeAbove(v, Statistics.Median(v)));
1010            TraceEvaluation(currentInstr, cur);
1011            return cur;
1012          }
1013        case OpCodes.LongestStrikeBelowMean: {
1014            var cur = Evaluate(dataset, ref row, state, traceDict);
1015            cur = AggregateApply(cur,
1016              s => 0,
1017              v => LongestStrikeBelow(v, Statistics.Mean(v)));
1018            TraceEvaluation(currentInstr, cur);
1019            return cur;
1020          }
1021        case OpCodes.LongestStrikeBelowMedian: {
1022            var cur = Evaluate(dataset, ref row, state, traceDict);
1023            cur = AggregateApply(cur,
1024              s => 0,
1025              v => LongestStrikeBelow(v, Statistics.Median(v)));
1026            TraceEvaluation(currentInstr, cur);
1027            return cur;
1028          }
1029        case OpCodes.LongestStrikePositive: {
1030            var cur = Evaluate(dataset, ref row, state, traceDict);
1031            cur = AggregateApply(cur,
1032              s => 0,
1033              v => LongestStrikeAbove(v, 0));
1034            TraceEvaluation(currentInstr, cur);
1035            return cur;
1036          }
1037        case OpCodes.LongestStrikeNegative: {
1038            var cur = Evaluate(dataset, ref row, state, traceDict);
1039            cur = AggregateApply(cur,
1040              s => 0,
1041              v => LongestStrikeAbove(v, 0));
1042            TraceEvaluation(currentInstr, cur);
1043            return cur;
1044          }
1045        case OpCodes.LongestStrikeZero: {
1046            var cur = Evaluate(dataset, ref row, state, traceDict);
1047            cur = AggregateApply(cur,
1048              s => 0,
1049              v => LongestStrikeEqual(v, 0));
1050            TraceEvaluation(currentInstr, cur);
1051            return cur;
1052          }
1053        case OpCodes.MeanAbsoluteChange: {
1054            var cur = Evaluate(dataset, ref row, state, traceDict);
1055            cur = AggregateApply(cur,
1056              s => 0,
1057              v => {
1058                double sum = 0.0;
1059                for (int i = 0; i < v.Count - 1; i++) {
1060                  sum += Math.Abs(v[i + 1] - v[i]);
1061                }
1062
1063                return sum / v.Count;
1064              });
1065            TraceEvaluation(currentInstr, cur);
1066            return cur;
1067          }
1068        case OpCodes.MeanAbsoluteChangeQuantiles: {
1069            var cur = Evaluate(dataset, ref row, state, traceDict);
1070            var ql = Evaluate(dataset, ref row, state, traceDict);
1071            var qu = Evaluate(dataset, ref row, state, traceDict);
1072            cur = AggregateApply(cur,
1073              s => 0,
1074              v => {
1075                var lowerBound = Statistics.Quantile(v, ql.Scalar);
1076                var upperBound = Statistics.Quantile(v, qu.Scalar);
1077                var inBounds = v.Select(e => e > lowerBound && e < upperBound).ToList();
1078                double sum = 0.0;
1079                int count = 0;
1080                for (int i = 0; i < v.Count - 1; i++) {
1081                  if (inBounds[i] && inBounds[i + 1]) {
1082                    sum += Math.Abs(v[i + 1] - v[i]);
1083                    count++;
1084                  }
1085                }
1086
1087                return sum / count;
1088              });
1089            TraceEvaluation(currentInstr, cur);
1090            return cur;
1091          }
1092        case OpCodes.MeanAutocorrelation: {
1093            var cur = Evaluate(dataset, ref row, state, traceDict);
1094            cur = AggregateApply(cur,
1095              s => 0,
1096              v => {
1097                double sum = 0.0;
1098                double mean = Statistics.Mean(v);
1099                for (int l = 0; l < v.Count; l++) {
1100                  for (int i = 0; i < v.Count - l; i++) {
1101                    sum += (v[i] - mean) * (v[i + l] - mean);
1102                  }
1103                }
1104
1105                return sum / (v.Count - 1) / Statistics.PopulationVariance(v);
1106              });
1107            TraceEvaluation(currentInstr, cur);
1108            return cur;
1109          }
1110        case OpCodes.LaggedAutocorrelation: {
1111            var cur = Evaluate(dataset, ref row, state, traceDict);
1112            var lVal = Evaluate(dataset, ref row, state, traceDict);
1113            cur = AggregateApply(cur,
1114              s => 0,
1115              v => {
1116                double sum = 0.0;
1117                int l = Math.Max((int)Math.Round(lVal.Scalar), 0);
1118                double mean = Statistics.Mean(v);
1119                for (int i = 0; i < v.Count - l; i++) {
1120                  sum += (v[i] - mean) * (v[i + l] - mean);
1121                }
1122
1123                return sum / Statistics.PopulationVariance(v);
1124              });
1125            TraceEvaluation(currentInstr, cur);
1126            return cur;
1127          }
1128        case OpCodes.MeanSecondDerivateCentral: {
1129            var cur = Evaluate(dataset, ref row, state, traceDict);
1130            cur = AggregateApply(cur,
1131              s => 0,
1132              v => {
1133                double sum = 0.0;
1134                for (int i = 1; i < v.Count - 1; i++) {
1135                  sum += (v[i - 1] - 2 * v[i] + v[i + 1]) / 2;
1136                }
1137
1138                return sum / (v.Count - 2);
1139              });
1140            TraceEvaluation(currentInstr, cur);
1141            return cur;
1142          }
1143        case OpCodes.NumberPeaksOfSize: {
1144            var cur = Evaluate(dataset, ref row, state, traceDict);
1145            var l = Evaluate(dataset, ref row, state, traceDict);
1146            cur = AggregateApply(cur,
1147              s => 0,
1148              v => CountNumberOfPeaks(v, l.Scalar));
1149            TraceEvaluation(currentInstr, cur);
1150            return cur;
1151          }
1152        case OpCodes.LargeNumberOfPeaks: {
1153            var cur = Evaluate(dataset, ref row, state, traceDict);
1154            var l = Evaluate(dataset, ref row, state, traceDict);
1155            var m = Evaluate(dataset, ref row, state, traceDict);
1156            cur = AggregateApply(cur,
1157              s => 0,
1158              v => CountNumberOfPeaks(v, l.Scalar) > m.Scalar ? 1.0 : 0.0);
1159            TraceEvaluation(currentInstr, cur);
1160            return cur;
1161          }
1162        case OpCodes.TimeReversalAsymmetryStatistic: {
1163            var cur = Evaluate(dataset, ref row, state, traceDict);
1164            var l = Evaluate(dataset, ref row, state, traceDict);
1165            cur = AggregateApply(cur,
1166              s => 0,
1167              v => {
1168                int lag = Math.Max((int)Math.Round(l.Scalar), 0);
1169                double sum = 0.0;
1170                for (int i = 0; i < v.Count - 2 * lag; i++) {
1171                  sum += Math.Pow(v[i + 2 * lag], 2) * v[i + lag] - v[i + lag] * Math.Pow(v[i], 2);
1172                }
1173
1174                return sum / (v.Count - 2 * lag);
1175              });
1176            TraceEvaluation(currentInstr, cur);
1177            return cur;
1178          }
1179        #endregion
1180
1181        default:
1182          throw new NotSupportedException($"Unsupported OpCode: {currentInstr.opCode}");
1183      }
1184    }
1185
1186    private static int LongestStrikeAbove(DoubleVector v, double threshold) {
1187      int longestStrike = 0, currentStrike = 0;
1188      for (int i = 0; i < v.Count; i++) {
1189        if (v[i] > threshold) {
1190          currentStrike++;
1191          longestStrike = Math.Max(longestStrike, currentStrike);
1192        } else
1193          currentStrike = 0;
1194      }
1195      return longestStrike;
1196    }
1197    private static int LongestStrikeBelow(DoubleVector v, double threshold) {
1198      int longestStrike = 0, currentStrike = 0;
1199      for (int i = 0; i < v.Count; i++) {
1200        if (v[i] < threshold) {
1201          currentStrike++;
1202          longestStrike = Math.Max(longestStrike, currentStrike);
1203        } else
1204          currentStrike = 0;
1205      }
1206      return longestStrike;
1207    }
1208
1209    private static int LongestStrikeEqual(DoubleVector v, double value, double epsilon = double.Epsilon) {
1210      int longestStrike = 0, currentStrike = 0;
1211      for (int i = 0; i < v.Count; i++) {
1212        if (v[i].IsAlmost(epsilon)) {
1213          currentStrike++;
1214          longestStrike = Math.Max(longestStrike, currentStrike);
1215        } else
1216          currentStrike = 0;
1217      }
1218      return longestStrike;
1219    }
1220    private static int CountNumberOfPeaks(DoubleVector v, double heightDifference) {
1221      int count = 0;
1222      for (int i = 0; i < v.Count; i++) {
1223        bool largerThanPrev = i == 0 || v[i] > v[i - 1] + heightDifference;
1224        bool largerThanNext = i == v.Count - 1 || v[i] > v[i + 1] + heightDifference;
1225        if (largerThanPrev && largerThanNext)
1226          count++;
1227      }
1228      return count;
1229    }
1230  }
1231}
Note: See TracBrowser for help on using the repository browser.