MerweUnscentedTransform.java

  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.util;

  18. import org.hipparchus.linear.ArrayRealVector;
  19. import org.hipparchus.linear.RealVector;

  20. /**
  21.  * Unscented transform as defined by Merwe and Wan.
  22.  * <p>
  23.  * The unscented transform uses three parameters: alpha, beta and kappa.
  24.  * Alpha determines the spread of the sigma points around the process state,
  25.  * kappa is a secondary scaling parameter, and beta is used to incorporate
  26.  * prior knowledge of the distribution of the process state.
  27.  * </p>
  28.  * @see "E. A. Wan and R. Van der Merwe, The unscented Kalman filter for nonlinear estimation,
  29.  *       in Proc. Symp. Adaptive Syst. Signal Process., Commun. Contr., Lake Louise, AB, Canada, Oct. 2000."
  30.  * @since 2.2
  31.  */
  32. public class MerweUnscentedTransform extends AbstractUnscentedTransform {

  33.     /** Default value for alpha (0.5, see reference). */
  34.     public static final double DEFAULT_ALPHA = 0.5;

  35.     /** Default value for beta (2.0, see reference). */
  36.     public static final double DEFAULT_BETA = 2.0;

  37.     /** Default value for kappa, (0.0, see reference). */
  38.     public static final double DEFAULT_KAPPA = 0.0;

  39.     /** Weights for covariance matrix. */
  40.     private final RealVector wc;

  41.     /** Weights for mean state. */
  42.     private final RealVector wm;

  43.     /** Factor applied to the covariance matrix during the unscented transform (lambda + process state size). */
  44.     private final double factor;

  45.     /**
  46.      * Default constructor.
  47.      * <p>
  48.      * This constructor uses default values for alpha, beta, and kappa.
  49.      * </p>
  50.      * @param stateDim the dimension of the state
  51.      * @see #DEFAULT_ALPHA
  52.      * @see #DEFAULT_BETA
  53.      * @see #DEFAULT_KAPPA
  54.      * @see #MerweUnscentedTransform(int, double, double, double)
  55.      */
  56.     public MerweUnscentedTransform(final int stateDim) {
  57.         this(stateDim, DEFAULT_ALPHA, DEFAULT_BETA, DEFAULT_KAPPA);
  58.     }

  59.     /**
  60.      * Simple constructor.
  61.      * @param stateDim the dimension of the state
  62.      * @param alpha scaling control parameter
  63.      *        (determines the spread of the sigma points around the process state)
  64.      * @param beta free parameter
  65.      *        (used to incorporate prior knowledge of the distribution of the process state)
  66.      * @param kappa secondary scaling factor
  67.      *        (usually set to 0.0)
  68.      */
  69.     public MerweUnscentedTransform(final int stateDim, final double alpha,
  70.                                    final double beta, final double kappa) {

  71.         // Call super constructor
  72.         super(stateDim);

  73.         // lambda = alpha² + (n + kappa) - n (see Eq. 15)
  74.         final double lambda = alpha * alpha * (stateDim + kappa) - stateDim;

  75.         // Initialize multiplication factor for covariance matrix
  76.         this.factor = stateDim + lambda;

  77.         // Initialize vectors weights
  78.         wm = new ArrayRealVector(2 * stateDim + 1);
  79.         wc = new ArrayRealVector(2 * stateDim + 1);

  80.         // Computation of unscented kalman filter weights (See Eq. 15)
  81.         wm.setEntry(0, lambda / factor);
  82.         wc.setEntry(0, lambda / factor + (1.0 - alpha * alpha + beta));
  83.         final double w = 1.0 / (2.0 * factor);
  84.         for (int i = 1; i <= 2 * stateDim; i++) {
  85.             wm.setEntry(i, w);
  86.             wc.setEntry(i, w);
  87.         }

  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public RealVector getWc() {
  92.         return wc;
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public RealVector getWm() {
  97.         return wm;
  98.     }

  99.     /** {@inheritDoc} */
  100.     @Override
  101.     protected double getMultiplicationFactor() {
  102.         return factor;
  103.     }

  104. }