001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.distribution;
019
020import org.apache.commons.math3.exception.OutOfRangeException;
021
022/**
023 * Implementation of the constant real distribution.
024 *
025 * @since 3.4
026 */
027public class ConstantRealDistribution extends AbstractRealDistribution {
028
029    /** Serialization ID */
030    private static final long serialVersionUID = -4157745166772046273L;
031
032    /** Constant value of the distribution */
033    private final double value;
034
035    /**
036     * Create a constant real distribution with the given value.
037     *
038     * @param value the constant value of this distribution
039     */
040    public ConstantRealDistribution(double value) {
041        super(null);  // Avoid creating RandomGenerator
042        this.value = value;
043    }
044
045    /** {@inheritDoc} */
046    public double density(double x) {
047        return x == value ? 1 : 0;
048    }
049
050    /** {@inheritDoc} */
051    public double cumulativeProbability(double x)  {
052        return x < value ? 0 : 1;
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public double inverseCumulativeProbability(final double p)
058            throws OutOfRangeException {
059        if (p < 0.0 || p > 1.0) {
060            throw new OutOfRangeException(p, 0, 1);
061        }
062        return value;
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    public double getNumericalMean() {
069        return value;
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    public double getNumericalVariance() {
076        return 0;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    public double getSupportLowerBound() {
083        return value;
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    public double getSupportUpperBound() {
090        return value;
091    }
092
093    /** {@inheritDoc} */
094    public boolean isSupportLowerBoundInclusive() {
095        return true;
096    }
097
098    /** {@inheritDoc} */
099    public boolean isSupportUpperBoundInclusive() {
100        return true;
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    public boolean isSupportConnected() {
107        return true;
108    }
109
110    /** {@inheritDoc} */
111    @Override
112    public double sample()  {
113        return value;
114    }
115
116    /**
117     * Override with no-op (there is no generator).
118     * @param seed (ignored)
119     */
120    @Override
121    public void reseedRandomGenerator(long seed) {}
122}