View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.random;
18  
19  import org.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Test;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  class GaussMarkovGeneratorTest {
25  
26      @Test
27      void testExpectation() {
28          final double          tau             = 3600.0;
29          final double          stationarySigma = 0.2;
30          final RandomGenerator random          = new Well1024a(0xb8005f29892534a8L);
31          GaussMarkovGenerator  gm              = new GaussMarkovGenerator(tau,
32                                                                           stationarySigma,
33                                                                           random);
34          assertEquals(tau,             gm.getTau(),             1.0e-15);
35          assertEquals(stationarySigma, gm.getStationarySigma(), 1.0e-15);
36  
37          double sum = 0;
38          int count = 0;
39          final double deltaT = 0.1;
40          for (double t = 0; t < 1000000; t += deltaT) {
41              sum += gm.next(deltaT);
42  
43              ++count;
44          }
45          assertEquals(0.0, sum / count, 0.014);
46      }
47  
48      @Test
49      void testVariance() {
50          final double          tau             = 3600.0;
51          final double          stationarySigma = 0.2;
52          final RandomGenerator random          = new Well1024a(0x09efbd4e87e7791eL);
53          GaussMarkovGenerator gm = new GaussMarkovGenerator(tau,
54                                                             stationarySigma,
55                                                             random);
56  
57          // here, we already assume expectation is 0
58          double sum2 = 0;
59          int count = 0;
60          final double deltaT = 0.1;
61          for (double t = 0; t < 1000000; t += deltaT) {
62              final double v = gm.next(tau);
63              ++count;
64              sum2 += v * v;
65          }
66  
67          assertEquals(gm.getStationarySigma(), FastMath.sqrt(sum2 / count), 3.7e-5);
68  
69      }
70  
71  }