LightJason - AgentSpeak(L++)
TestCActionMathStatistics.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
26 import org.apache.commons.math3.distribution.BetaDistribution;
27 import org.apache.commons.math3.distribution.CauchyDistribution;
28 import org.apache.commons.math3.distribution.ChiSquaredDistribution;
29 import org.apache.commons.math3.distribution.ExponentialDistribution;
30 import org.apache.commons.math3.distribution.FDistribution;
31 import org.apache.commons.math3.distribution.GammaDistribution;
32 import org.apache.commons.math3.distribution.GumbelDistribution;
33 import org.apache.commons.math3.distribution.LaplaceDistribution;
34 import org.apache.commons.math3.distribution.LevyDistribution;
35 import org.apache.commons.math3.distribution.LogNormalDistribution;
36 import org.apache.commons.math3.distribution.LogisticDistribution;
37 import org.apache.commons.math3.distribution.NakagamiDistribution;
38 import org.apache.commons.math3.distribution.NormalDistribution;
39 import org.apache.commons.math3.distribution.ParetoDistribution;
40 import org.apache.commons.math3.distribution.TDistribution;
41 import org.apache.commons.math3.distribution.TriangularDistribution;
42 import org.apache.commons.math3.distribution.UniformRealDistribution;
43 import org.apache.commons.math3.distribution.WeibullDistribution;
44 import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
45 import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.Test;
65 
66 import java.util.ArrayList;
67 import java.util.Collections;
68 import java.util.List;
69 import java.util.stream.Collectors;
70 import java.util.stream.IntStream;
71 import java.util.stream.Stream;
72 
73 
77 public final class TestCActionMathStatistics extends IBaseTest
78 {
82  private SummaryStatistics m_summarystatistic;
83 
87  private DescriptiveStatistics m_descriptivestatistic;
88 
92  @Before
93  public final void initialize()
94  {
95  m_summarystatistic = new SummaryStatistics();
96  m_descriptivestatistic = new DescriptiveStatistics();
97 
98  m_summarystatistic.addValue( 2 );
99  m_summarystatistic.addValue( 5 );
100  m_summarystatistic.addValue( 3 );
101  m_descriptivestatistic.addValue( 3 );
102  m_descriptivestatistic.addValue( 4 );
103  m_descriptivestatistic.addValue( 5 );
104  }
105 
109  @Test
110  public final void createstatistics()
111  {
112  final List<ITerm> l_return = new ArrayList<>();
113 
115  false, IContext.EMPTYPLAN,
116  Stream.of( "summary", "descriptive" ).map( CRawTerm::from ).collect( Collectors.toList() ),
117  l_return
118  );
120  true, IContext.EMPTYPLAN,
121  Stream.of( "summary", "descriptive" ).map( CRawTerm::from ).collect( Collectors.toList() ),
122  l_return
123  );
124 
125  Assert.assertEquals( l_return.size(), 4 );
126  Assert.assertTrue( l_return.get( 0 ).raw() instanceof SummaryStatistics );
127  Assert.assertTrue( l_return.get( 1 ).raw() instanceof DescriptiveStatistics );
128  Assert.assertTrue( l_return.get( 2 ).raw() instanceof SummaryStatistics );
129  Assert.assertTrue( l_return.get( 3 ).raw() instanceof DescriptiveStatistics );
130  }
131 
135  @Test
136  public final void percentile()
137  {
138  final List<ITerm> l_return = new ArrayList<>();
139  final DescriptiveStatistics l_statistic1 = new DescriptiveStatistics();
140  final DescriptiveStatistics l_statistic2 = new DescriptiveStatistics();
141 
142  IntStream.range( 0, 100 ).peek( l_statistic1::addValue ).forEach( i -> l_statistic2.addValue( i * 10 ) );
143 
144 
146  false, IContext.EMPTYPLAN,
147  Stream.of( 50, l_statistic1, l_statistic2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
148  l_return
149  );
150 
152  false, IContext.EMPTYPLAN,
153  Stream.of( l_statistic1, 25, 75 ).map( CRawTerm::from ).collect( Collectors.toList() ),
154  l_return
155  );
156 
157 
158  Assert.assertEquals( l_return.size(), 4 );
159  Assert.assertArrayEquals(
160  l_return.stream().map( i -> i.<Number>raw().doubleValue() ).toArray(),
161  Stream.of( 49.5, 495, 24.25, 74.75 ).mapToDouble( Number::doubleValue ).boxed().toArray()
162  );
163  }
164 
168  @Test
169  public final void clear()
170  {
171  new CClearStatistic().execute(
172  false, IContext.EMPTYPLAN,
173  Stream.of( m_summarystatistic, m_descriptivestatistic ).map( CRawTerm::from ).collect( Collectors.toList() ),
174  Collections.emptyList()
175  );
176 
177  Assert.assertEquals( m_summarystatistic.getSum(), 0, 0 );
178  Assert.assertEquals( m_descriptivestatistic.getSum(), 0, 0 );
179  }
180 
184  @Test
185  public final void createdistribution()
186  {
187  final List<ITerm> l_return = new ArrayList<>();
188 
190  false, IContext.EMPTYPLAN,
191  Stream.of(
192  "normal", "ISAAC", 20, 10,
193  "beta", "SYNCHRONIZEDMERSENNETWISTER", 20, 10,
194  "cauchy", "SYNCHRONIZEDISAAC", 10, 20,
195  "CHISQUARE", "INTERNAL", 10,
196  "EXPONENTIAL", "SYNCHRONIZEDINTERNAL", 5,
197  "F", "WELL512A", 2, 6,
198  "GAMMA", "SYNCHRONIZEDWELL512A", 6, 9,
199  "GUMBLE", "WELL1024A", 2, 7,
200  "LAPLACE", "SYNCHRONIZEDWELL1024A", 20, 18,
201  "LEVY", "WELL19937A", 15, 20,
202  "LOGISTIC", "SYNCHRONIZEDWELL19937A", 10, 17,
203  "LOGNORMAL", "WELL19937C", 12, 14,
204  "NAKAGAMI", "SYNCHRONIZEDWELL19937C", 20, 18,
205  "PARETO", "WELL4449A", 20, 10,
206  "T", "SYNCHRONIZEDWELL4449A", 10,
207  "TRIANGULAR", "WELL44497B", 10, 15, 20,
208  "UNIFORM", "SYNCHRONIZEDWELL44497B", 10, 25,
209  "WEIBULL", 10, 23
210  ).map( CRawTerm::from ).collect( Collectors.toList() ),
211  l_return
212  );
213 
214  Assert.assertEquals( l_return.size(), 18 );
215  Assert.assertTrue( l_return.get( 0 ).raw() instanceof NormalDistribution );
216  Assert.assertTrue( l_return.get( 1 ).raw() instanceof BetaDistribution );
217  Assert.assertTrue( l_return.get( 2 ).raw() instanceof CauchyDistribution );
218  Assert.assertTrue( l_return.get( 3 ).raw() instanceof ChiSquaredDistribution );
219  Assert.assertTrue( l_return.get( 4 ).raw() instanceof ExponentialDistribution );
220  Assert.assertTrue( l_return.get( 5 ).raw() instanceof FDistribution );
221  Assert.assertTrue( l_return.get( 6 ).raw() instanceof GammaDistribution );
222  Assert.assertTrue( l_return.get( 7 ).raw() instanceof GumbelDistribution );
223  Assert.assertTrue( l_return.get( 8 ).raw() instanceof LaplaceDistribution );
224  Assert.assertTrue( l_return.get( 9 ).raw() instanceof LevyDistribution );
225  Assert.assertTrue( l_return.get( 10 ).raw() instanceof LogisticDistribution );
226  Assert.assertTrue( l_return.get( 11 ).raw() instanceof LogNormalDistribution );
227  Assert.assertTrue( l_return.get( 12 ).raw() instanceof NakagamiDistribution );
228  Assert.assertTrue( l_return.get( 13 ).raw() instanceof ParetoDistribution );
229  Assert.assertTrue( l_return.get( 14 ).raw() instanceof TDistribution );
230  Assert.assertTrue( l_return.get( 15 ).raw() instanceof TriangularDistribution );
231  Assert.assertTrue( l_return.get( 16 ).raw() instanceof UniformRealDistribution );
232  Assert.assertTrue( l_return.get( 17 ).raw() instanceof WeibullDistribution );
233  }
234 
238  @Test
239  public final void addstatisticvalue()
240  {
242  false, IContext.EMPTYPLAN,
243  Stream.of( m_descriptivestatistic, m_summarystatistic, 1, 2, 3 ).map( CRawTerm::from ).collect( Collectors.toList() ),
244  Collections.emptyList()
245  );
246 
247  Assert.assertEquals( m_descriptivestatistic.getN(), 6, 0 );
248  Assert.assertEquals( m_summarystatistic.getN(), 6 );
249  }
250 
254  @Test
255  public final void summarymultiplestatisticvalue()
256  {
257  final List<ITerm> l_return = new ArrayList<>();
258 
260  false, IContext.EMPTYPLAN,
261  Stream.of(
262  m_summarystatistic,
263  "variance", "mean",
264  "max", "geometricmean",
265  "populationvariance", "quadraticmean",
266  "secondmoment", "standarddeviation",
267  "sumlog", "sumsquare"
268  ).map( CRawTerm::from ).collect( Collectors.toList() ),
269  l_return
270  );
271 
272  Assert.assertEquals( l_return.size(), 10 );
273  Assert.assertArrayEquals(
274  Stream.of(
275  m_summarystatistic.getVariance(), m_summarystatistic.getMean(),
276  m_summarystatistic.getMax(), m_summarystatistic.getGeometricMean(),
277  m_summarystatistic.getPopulationVariance(), m_summarystatistic.getQuadraticMean(),
278  m_summarystatistic.getSecondMoment(), m_summarystatistic.getStandardDeviation(),
279  m_summarystatistic.getSumOfLogs(), m_summarystatistic.getSumsq()
280  ).toArray(),
281 
282  l_return.stream().map( ITerm::raw ).toArray()
283  );
284  }
285 
289  @Test
291  {
292  final List<ITerm> l_return = new ArrayList<>();
293 
295  false, IContext.EMPTYPLAN,
296  Stream.of(
297  m_descriptivestatistic,
298  "variance", "mean",
299  "max", "geometricmean",
300  "populationvariance", "quadraticmean",
301  "standarddeviation", "sumsquare",
302  "kurtiosis", "count", "sum"
303  ).map( CRawTerm::from ).collect( Collectors.toList() ),
304  l_return
305  );
306 
307  Assert.assertEquals( l_return.size(), 11 );
308  Assert.assertArrayEquals(
309  Stream.of(
310  m_descriptivestatistic.getVariance(), m_descriptivestatistic.getMean(),
311  m_descriptivestatistic.getMax(), m_descriptivestatistic.getGeometricMean(),
312  m_descriptivestatistic.getPopulationVariance(), m_descriptivestatistic.getQuadraticMean(),
313  m_descriptivestatistic.getStandardDeviation(), m_descriptivestatistic.getSumsq(),
314  m_descriptivestatistic.getKurtosis(), (double) m_descriptivestatistic.getN(), m_descriptivestatistic.getSum()
315  ).toArray(),
316 
317  l_return.stream().map( ITerm::raw ).toArray()
318  );
319  }
320 
324  @Test
325  public final void randomsample()
326  {
327  final List<ITerm> l_return = new ArrayList<>();
328 
329  new CRandomSample().execute(
330  false, IContext.EMPTYPLAN,
331  Stream.of( new NormalDistribution(), 3 ).map( CRawTerm::from ).collect( Collectors.toList() ),
332  l_return
333  );
334 
335  Assert.assertEquals( l_return.size(), 1 );
336  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List );
337  Assert.assertEquals( l_return.get( 0 ).<List<Number>>raw().size(), 3 );
338  }
339 
343  @Test
344  public final void randomsimple()
345  {
346  final List<ITerm> l_return = new ArrayList<>();
347 
348  new CRandomSimple().execute(
349  false, IContext.EMPTYPLAN,
350  Stream.of( 5 ).map( CRawTerm::from ).collect( Collectors.toList() ),
351  l_return
352  );
353 
354  Assert.assertEquals( l_return.size(), 1 );
355  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List );
356  Assert.assertEquals( l_return.get( 0 ).<List<Number>>raw().size(), 5 );
357  }
358 
362  @Test
363  public final void singlestatisticvalue()
364  {
365  final List<ITerm> l_return = new ArrayList<>();
366 
368  false, IContext.EMPTYPLAN,
369  Stream.of( "min", m_summarystatistic, m_descriptivestatistic ).map( CRawTerm::from ).collect( Collectors.toList() ),
370  l_return
371  );
372 
373  Assert.assertEquals( l_return.size(), 2 );
374  Assert.assertEquals( m_summarystatistic.getMin(), l_return.get( 0 ).<Double>raw(), 0 );
375  Assert.assertEquals( m_descriptivestatistic.getMin(), l_return.get( 1 ).<Double>raw(), 0 );
376  }
377 
381  @Test
382  public final void exponentialselectionstrict()
383  {
384  final List<ITerm> l_return = Collections.synchronizedList( new ArrayList<>() );
385 
386  IntStream.range( 0, 5000 )
387  .parallel()
388  .forEach( i ->
389  new CExponentialSelection().execute(
390  false, IContext.EMPTYPLAN,
391  Stream.of( Stream.of( "a", "b" ).collect( Collectors.toList() ), Stream.of( 4.5, 3.5 ).collect( Collectors.toList() ), 1 )
392  .map( CRawTerm::from ).collect( Collectors.toList() ),
393  l_return
394  ) );
395 
396 
397  Assert.assertEquals(
398  (double) Collections.frequency( l_return.stream().map( ITerm::raw ).collect( Collectors.toList() ), "a" ) / l_return.size(),
399  0.73,
400  0.02
401  );
402 
403  Assert.assertEquals(
404  (double) Collections.frequency( l_return.stream().map( ITerm::raw ).collect( Collectors.toList() ), "b" ) / l_return.size(),
405  0.27,
406  0.02
407  );
408  }
409 
410 
414  @Test
415  public final void exponentialselectionlazy()
416  {
417  final List<ITerm> l_return = Collections.synchronizedList( new ArrayList<>() );
418 
419  IntStream.range( 0, 6500 )
420  .parallel()
421  .forEach( i ->
422  new CExponentialSelection().execute(
423  false, IContext.EMPTYPLAN,
424  Stream.of( Stream.of( "a", "b" ).collect( Collectors.toList() ), Stream.of( 4.5, 3.5 ).collect( Collectors.toList() ), 0.5 )
425  .map( CRawTerm::from ).collect( Collectors.toList() ),
426  l_return
427  ) );
428 
429 
430  Assert.assertEquals(
431  (double) Collections.frequency( l_return.stream().map( ITerm::raw ).collect( Collectors.toList() ), "a" ) / l_return.size(),
432  0.73,
433  0.2
434  );
435 
436  Assert.assertEquals(
437  (double) Collections.frequency( l_return.stream().map( ITerm::raw ).collect( Collectors.toList() ), "b" ) / l_return.size(),
438  0.27,
439  0.2
440  );
441  }
442 
446  @Test
447  public final void linearselection()
448  {
449  final List<ITerm> l_return = Collections.synchronizedList( new ArrayList<>() );
450 
451  IntStream.range( 0, 6500 )
452  .parallel()
453  .forEach( i ->
454  new CLinearSelection().execute(
455  false, IContext.EMPTYPLAN,
456  Stream.of( Stream.of( "c", "d" ).collect( Collectors.toList() ), Stream.of( 3, 7 ).collect( Collectors.toList() ) )
457  .map( CRawTerm::from ).collect( Collectors.toList() ),
458  l_return
459  ) );
460 
461  Assert.assertEquals(
462  (double) Collections.frequency( l_return.stream().map( ITerm::raw ).collect( Collectors.toList() ), "c" ) / l_return.size(),
463  0.3,
464  0.05
465  );
466 
467  Assert.assertEquals(
468  (double) Collections.frequency( l_return.stream().map( ITerm::raw ).collect( Collectors.toList() ), "d" ) / l_return.size(),
469  0.7,
470  0.05
471  );
472  }
473 
474 }
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
base test class with helpers
Definition: IBaseTest.java:33
final void exponentialselectionlazy()
test exponential selection with lazy parameter
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
gets a single statistic value of multiple statistic objects.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
execution context with local data
Definition: IContext.java:42
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action to define a exponential fitness-proportinate-selection mechanism.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final void descriptivemultiplestatisticvalue()
test multiple statistics value of descriptive
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
< T > T raw()
cast to any raw value type
final void summarymultiplestatisticvalue()
test multiple statistics value of summary
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
final void exponentialselectionstrict()
test exponential selection with strict parameter
action to define a fitness-proportinate-selection mechanism.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
DescriptiveStatistics m_descriptivestatistic
testing descriptive statistic
term structure for simple datatypes
Definition: CRawTerm.java:45
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation