1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Globalization;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Tests;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Tests {
|
---|
32 |
|
---|
33 | [TestClass()]
|
---|
34 | public class SymbolicTimeSeriesPrognosisInterpreterTest {
|
---|
35 | private const int N = 1000;
|
---|
36 | private const int Rows = 100;
|
---|
37 | private const int Columns = 50;
|
---|
38 | private TestContext testContextInstance;
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | ///Gets or sets the test context which provides
|
---|
42 | ///information about and functionality for the current test run.
|
---|
43 | ///</summary>
|
---|
44 | public TestContext TestContext {
|
---|
45 | get {
|
---|
46 | return testContextInstance;
|
---|
47 | }
|
---|
48 | set {
|
---|
49 | testContextInstance = value;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [TestMethod]
|
---|
54 | public void SymbolicTimeSeriesPrognosisTreeInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
55 | TypeCoherentGrammarPerformanceTest(new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter("y"), 12.5e6);
|
---|
56 | }
|
---|
57 | [TestMethod]
|
---|
58 | public void SymbolicTimeSeriesPrognosisTreeInterpreterFullGrammarPerformanceTest() {
|
---|
59 | FullGrammarPerformanceTest(new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter("y"), 12.5e6);
|
---|
60 | }
|
---|
61 | [TestMethod]
|
---|
62 | public void SymbolicTimeSeriesPrognosisTreeInterpreterArithmeticGrammarPerformanceTest() {
|
---|
63 | ArithmeticGrammarPerformanceTest(new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter("y"), 12.5e6);
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void TypeCoherentGrammarPerformanceTest(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
67 | var twister = new MersenneTwister(31415);
|
---|
68 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
69 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
70 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
71 | grammar.MaximumFunctionArguments = 0;
|
---|
72 | grammar.MaximumFunctionDefinitions = 0;
|
---|
73 | grammar.MinimumFunctionArguments = 0;
|
---|
74 | grammar.MinimumFunctionDefinitions = 0;
|
---|
75 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
76 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
77 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
78 | }
|
---|
79 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
80 | //mkommend: commented due to performance issues on the builder
|
---|
81 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void FullGrammarPerformanceTest(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
85 | var twister = new MersenneTwister(31415);
|
---|
86 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
87 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
88 | grammar.MaximumFunctionArguments = 0;
|
---|
89 | grammar.MaximumFunctionDefinitions = 0;
|
---|
90 | grammar.MinimumFunctionArguments = 0;
|
---|
91 | grammar.MinimumFunctionDefinitions = 0;
|
---|
92 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
93 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
94 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
95 | }
|
---|
96 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
97 | //mkommend: commented due to performance issues on the builder
|
---|
98 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
99 | }
|
---|
100 |
|
---|
101 | private void ArithmeticGrammarPerformanceTest(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
102 | var twister = new MersenneTwister(31415);
|
---|
103 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
104 | var grammar = new ArithmeticExpressionGrammar();
|
---|
105 | grammar.MaximumFunctionArguments = 0;
|
---|
106 | grammar.MaximumFunctionDefinitions = 0;
|
---|
107 | grammar.MinimumFunctionArguments = 0;
|
---|
108 | grammar.MinimumFunctionDefinitions = 0;
|
---|
109 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
110 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
111 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
112 | }
|
---|
113 |
|
---|
114 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
115 | //mkommend: commented due to performance issues on the builder
|
---|
116 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /// <summary>
|
---|
121 | ///A test for Evaluate
|
---|
122 | ///</summary>
|
---|
123 | [TestMethod]
|
---|
124 | public void SymbolicDataAnalysisExpressionTreeInterpreterEvaluateTest() {
|
---|
125 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
126 | { 1.0, 1.0, 1.0 },
|
---|
127 | { 2.0, 2.0, 2.0 },
|
---|
128 | { 3.0, 1.0, 2.0 },
|
---|
129 | { 4.0, 1.0, 1.0 },
|
---|
130 | { 5.0, 2.0, 2.0 },
|
---|
131 | { 6.0, 1.0, 2.0 },
|
---|
132 | { 7.0, 1.0, 1.0 },
|
---|
133 | { 8.0, 2.0, 2.0 },
|
---|
134 | { 9.0, 1.0, 2.0 },
|
---|
135 | { 10.0, 1.0, 1.0 },
|
---|
136 | { 11.0, 2.0, 2.0 },
|
---|
137 | { 12.0, 1.0, 2.0 }
|
---|
138 | });
|
---|
139 |
|
---|
140 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
141 | EvaluateTerminals(interpreter, ds);
|
---|
142 | EvaluateOperations(interpreter, ds);
|
---|
143 | EvaluateAdf(interpreter, ds);
|
---|
144 | }
|
---|
145 |
|
---|
146 | //[TestMethod]
|
---|
147 | //public void SymbolicDataAnalysisExpressionILEmittingTreeInterpreterEvaluateTest() {
|
---|
148 | // Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
149 | // { 1.0, 1.0, 1.0 },
|
---|
150 | // { 2.0, 2.0, 2.0 },
|
---|
151 | // { 3.0, 1.0, 2.0 },
|
---|
152 | // { 4.0, 1.0, 1.0 },
|
---|
153 | // { 5.0, 2.0, 2.0 },
|
---|
154 | // { 6.0, 1.0, 2.0 },
|
---|
155 | // { 7.0, 1.0, 1.0 },
|
---|
156 | // { 8.0, 2.0, 2.0 },
|
---|
157 | // { 9.0, 1.0, 2.0 },
|
---|
158 | // { 10.0, 1.0, 1.0 },
|
---|
159 | // { 11.0, 2.0, 2.0 },
|
---|
160 | // { 12.0, 1.0, 2.0 }
|
---|
161 | // });
|
---|
162 |
|
---|
163 | // var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
164 | // EvaluateTerminals(interpreter, ds);
|
---|
165 | // EvaluateOperations(interpreter, ds);
|
---|
166 | //}
|
---|
167 |
|
---|
168 | private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
169 | // constants
|
---|
170 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
171 |
|
---|
172 | // variables
|
---|
173 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
174 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
178 |
|
---|
179 | // ADF
|
---|
180 | Evaluate(interpreter, ds, @"(PROG
|
---|
181 | (MAIN
|
---|
182 | (CALL ADF0))
|
---|
183 | (defun ADF0 1.0))", 1, 1.0);
|
---|
184 | Evaluate(interpreter, ds, @"(PROG
|
---|
185 | (MAIN
|
---|
186 | (* (CALL ADF0) (CALL ADF0)))
|
---|
187 | (defun ADF0 2.0))", 1, 4.0);
|
---|
188 | Evaluate(interpreter, ds, @"(PROG
|
---|
189 | (MAIN
|
---|
190 | (CALL ADF0 2.0 3.0))
|
---|
191 | (defun ADF0
|
---|
192 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
193 | Evaluate(interpreter, ds, @"(PROG
|
---|
194 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
195 | (defun ADF0
|
---|
196 | (- (ARG 1) (ARG 0)))
|
---|
197 | (defun ADF1
|
---|
198 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
199 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
200 | Evaluate(interpreter, ds, @"(PROG
|
---|
201 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
202 | (defun ADF0
|
---|
203 | (- (ARG 1) (ARG 0)))
|
---|
204 | (defun ADF1
|
---|
205 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
206 | Evaluate(interpreter, ds,
|
---|
207 | @"(PROG
|
---|
208 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
209 | (defun ADF0
|
---|
210 | (- (ARG 1) (ARG 0)))
|
---|
211 | (defun ADF1
|
---|
212 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
213 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
214 | }
|
---|
215 |
|
---|
216 | private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
217 | // addition
|
---|
218 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
219 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
220 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
221 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
222 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
223 |
|
---|
224 | // subtraction
|
---|
225 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
226 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
227 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
228 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
229 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
230 |
|
---|
231 | // multiplication
|
---|
232 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
233 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
234 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
235 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
236 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
237 |
|
---|
238 | // division
|
---|
239 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
240 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
241 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
242 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
243 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
244 |
|
---|
245 | // gt
|
---|
246 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
247 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
248 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
249 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
250 | Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
251 |
|
---|
252 | // lt
|
---|
253 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
254 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
255 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
256 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
257 | Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false
|
---|
258 |
|
---|
259 | // If
|
---|
260 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
261 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
262 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
263 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
264 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
265 | Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
266 |
|
---|
267 | // NOT
|
---|
268 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
269 | Evaluate(interpreter, ds, "(not -2.0)", 0, 1.0);
|
---|
270 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
271 | Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0);
|
---|
272 | Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0);
|
---|
273 | Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0);
|
---|
274 |
|
---|
275 | // AND
|
---|
276 | Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
|
---|
277 | Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
|
---|
278 | Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
|
---|
279 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
280 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
281 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
282 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
283 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
284 | Evaluate(interpreter, ds, "(and (log -1.0))", 0, -1.0); // (and NaN)
|
---|
285 | Evaluate(interpreter, ds, "(and (log -1.0) 1.0)", 0, -1.0); // (and NaN 1.0)
|
---|
286 |
|
---|
287 |
|
---|
288 | // OR
|
---|
289 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
290 | Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
|
---|
291 | Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
|
---|
292 | Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
|
---|
293 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
294 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
295 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
296 | Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN)
|
---|
297 | Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0)
|
---|
298 |
|
---|
299 | // sin, cos, tan
|
---|
300 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
301 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
302 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
303 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
304 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
305 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
306 |
|
---|
307 | // exp, log
|
---|
308 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
309 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
310 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
311 |
|
---|
312 | // power
|
---|
313 | Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0);
|
---|
314 | Evaluate(interpreter, ds, "(pow 4.0 0.5)", 0, 1.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number)
|
---|
315 | Evaluate(interpreter, ds, "(pow 4.0 2.5)", 0, 16.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number)
|
---|
316 | Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0);
|
---|
317 | Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0);
|
---|
318 | Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0);
|
---|
319 |
|
---|
320 | // root
|
---|
321 | Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0);
|
---|
322 | Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0);
|
---|
323 | Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0));
|
---|
324 |
|
---|
325 | // mean
|
---|
326 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
327 |
|
---|
328 | // lag
|
---|
329 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0));
|
---|
330 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1));
|
---|
331 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2));
|
---|
332 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1));
|
---|
333 |
|
---|
334 | // integral
|
---|
335 | Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1));
|
---|
336 | Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
337 | Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
338 | Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1));
|
---|
339 | Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0);
|
---|
340 |
|
---|
341 | // derivative
|
---|
342 | // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
343 | Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0);
|
---|
344 | Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
345 | Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, +
|
---|
346 | (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) +
|
---|
347 | 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) -
|
---|
348 | 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) -
|
---|
349 | ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
350 | Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0);
|
---|
351 |
|
---|
352 | // timelag
|
---|
353 | Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2));
|
---|
354 | Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2));
|
---|
355 | Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2));
|
---|
356 | Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0);
|
---|
357 | }
|
---|
358 |
|
---|
359 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
360 | var importer = new SymbolicExpressionImporter();
|
---|
361 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
362 |
|
---|
363 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
364 |
|
---|
365 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
366 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
367 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|