ConstantRealDistribution.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) 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 ASF 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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.distribution.continuous;

  22. import org.hipparchus.exception.MathIllegalArgumentException;
  23. import org.hipparchus.util.MathUtils;

  24. /**
  25.  * Implementation of the constant real distribution.
  26.  */
  27. public class ConstantRealDistribution extends AbstractRealDistribution {

  28.     /** Serialization ID */
  29.     private static final long serialVersionUID = 20160320L;
  30.     /** Constant value of the distribution */
  31.     private final double value;

  32.     /**
  33.      * Create a constant real distribution with the given value.
  34.      *
  35.      * @param value the constant value of this distribution
  36.      */
  37.     public ConstantRealDistribution(double value) {
  38.         this.value = value;
  39.     }

  40.     /** {@inheritDoc} */
  41.     @Override
  42.     public double density(double x) {
  43.         return x == value ? 1 : 0;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public double cumulativeProbability(double x)  {
  48.         return x < value ? 0 : 1;
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public double inverseCumulativeProbability(final double p)
  53.         throws MathIllegalArgumentException {
  54.         MathUtils.checkRangeInclusive(p, 0, 1);
  55.         return value;
  56.     }

  57.     /**
  58.      * {@inheritDoc}
  59.      */
  60.     @Override
  61.     public double getNumericalMean() {
  62.         return value;
  63.     }

  64.     /**
  65.      * {@inheritDoc}
  66.      */
  67.     @Override
  68.     public double getNumericalVariance() {
  69.         return 0;
  70.     }

  71.     /**
  72.      * {@inheritDoc}
  73.      */
  74.     @Override
  75.     public double getSupportLowerBound() {
  76.         return value;
  77.     }

  78.     /**
  79.      * {@inheritDoc}
  80.      */
  81.     @Override
  82.     public double getSupportUpperBound() {
  83.         return value;
  84.     }

  85.     /**
  86.      * {@inheritDoc}
  87.      */
  88.     @Override
  89.     public boolean isSupportConnected() {
  90.         return true;
  91.     }
  92. }