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.statistics.distribution;
019
020import org.apache.commons.rng.UniformRandomProvider;
021
022/**
023 * Implementation of the constant real distribution.
024 */
025public class ConstantContinuousDistribution extends AbstractContinuousDistribution {
026    /** Constant value of the distribution. */
027    private final double value;
028
029    /**
030     * Create a constant real distribution with the given value.
031     *
032     * @param value Value of this distribution.
033     */
034    public ConstantContinuousDistribution(double value) {
035        this.value = value;
036    }
037
038    /** {@inheritDoc} */
039    @Override
040    public double density(double x) {
041        return x == value ? 1 : 0;
042    }
043
044    /** {@inheritDoc} */
045    @Override
046    public double cumulativeProbability(double x)  {
047        return x < value ? 0 : 1;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public double inverseCumulativeProbability(final double p) {
053        if (p < 0 ||
054            p > 1) {
055            throw new DistributionException(DistributionException.INVALID_PROBABILITY, p);
056        }
057        return value;
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public double getMean() {
065        return value;
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public double getVariance() {
073        return 0;
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public double getSupportLowerBound() {
081        return value;
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public double getSupportUpperBound() {
089        return value;
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public boolean isSupportConnected() {
097        return true;
098    }
099
100    /**
101     * {@inheritDoc}
102     *
103     * @param rng Not used: distribution contains a single value.
104     * @return the value of the distribution.
105     */
106    @Override
107    public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
108        return this::getSupportLowerBound;
109    }
110}