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.Assert;
21  import org.junit.Test;
22  
23  public class GaussMarkovGeneratorTest {
24  
25      @Test
26      public void testExpectation() {
27          final double          tau             = 3600.0;
28          final double          stationarySigma = 0.2;
29          final RandomGenerator random          = new Well1024a(0xb8005f29892534a8L);
30          GaussMarkovGenerator  gm              = new GaussMarkovGenerator(tau,
31                                                                           stationarySigma,
32                                                                           random);
33          Assert.assertEquals(tau,             gm.getTau(),             1.0e-15);
34          Assert.assertEquals(stationarySigma, gm.getStationarySigma(), 1.0e-15);
35  
36          double sum = 0;
37          int count = 0;
38          final double deltaT = 0.1;
39          for (double t = 0; t < 1000000; t += deltaT) {
40              sum += gm.next(deltaT);
41  
42              ++count;
43          }
44          Assert.assertEquals(0.0, sum / count, 0.014);
45      }
46  
47      @Test
48      public void testVariance() {
49          final double          tau             = 3600.0;
50          final double          stationarySigma = 0.2;
51          final RandomGenerator random          = new Well1024a(0x09efbd4e87e7791eL);
52          GaussMarkovGenerator gm = new GaussMarkovGenerator(tau,
53                                                             stationarySigma,
54                                                             random);
55  
56          // here, we already assume expectation is 0
57          double sum2 = 0;
58          int count = 0;
59          final double deltaT = 0.1;
60          for (double t = 0; t < 1000000; t += deltaT) {
61              final double v = gm.next(tau);
62              ++count;
63              sum2 += v * v;
64          }
65  
66          Assert.assertEquals(gm.getStationarySigma(), FastMath.sqrt(sum2 / count), 3.7e-5);
67  
68      }
69  
70  }