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.  *      http://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.apache.commons.math3.distribution;

  18. import org.apache.commons.math3.exception.OutOfRangeException;

  19. /**
  20.  * Implementation of the constant real distribution.
  21.  *
  22.  * @since 3.4
  23.  */
  24. public class ConstantRealDistribution extends AbstractRealDistribution {

  25.     /** Serialization ID */
  26.     private static final long serialVersionUID = -4157745166772046273L;

  27.     /** Constant value of the distribution */
  28.     private final double value;

  29.     /**
  30.      * Create a constant real distribution with the given value.
  31.      *
  32.      * @param value the constant value of this distribution
  33.      */
  34.     public ConstantRealDistribution(double value) {
  35.         super(null);  // Avoid creating RandomGenerator
  36.         this.value = value;
  37.     }

  38.     /** {@inheritDoc} */
  39.     public double density(double x) {
  40.         return x == value ? 1 : 0;
  41.     }

  42.     /** {@inheritDoc} */
  43.     public double cumulativeProbability(double x)  {
  44.         return x < value ? 0 : 1;
  45.     }

  46.     @Override
  47.     public double inverseCumulativeProbability(final double p)
  48.             throws OutOfRangeException {
  49.         if (p < 0.0 || p > 1.0) {
  50.             throw new OutOfRangeException(p, 0, 1);
  51.         }
  52.         return value;
  53.     }

  54.     /**
  55.      * {@inheritDoc}
  56.      */
  57.     public double getNumericalMean() {
  58.         return value;
  59.     }

  60.     /**
  61.      * {@inheritDoc}
  62.      */
  63.     public double getNumericalVariance() {
  64.         return 0;
  65.     }

  66.     /**
  67.      * {@inheritDoc}
  68.      */
  69.     public double getSupportLowerBound() {
  70.         return value;
  71.     }

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

  78.     /** {@inheritDoc} */
  79.     public boolean isSupportLowerBoundInclusive() {
  80.         return true;
  81.     }

  82.     /** {@inheritDoc} */
  83.     public boolean isSupportUpperBoundInclusive() {
  84.         return true;
  85.     }

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

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     public double sample()  {
  95.         return value;
  96.     }

  97.     /**
  98.      * Override with no-op (there is no generator).
  99.      * @param seed (ignored)
  100.      */
  101.     @Override
  102.     public void reseedRandomGenerator(long seed) {}
  103. }