All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tommyettinger.random.distribution.ArcsineDistribution Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
/*
 * Copyright (c) 2023 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.github.tommyettinger.random.distribution;

import com.github.tommyettinger.random.EnhancedRandom;
import com.github.tommyettinger.random.AceRandom;

/**
 * A two-parameter distribution with range from 0 to 1, both exclusive.
 * This is a special case of the {@link BetaDistribution}, and has also been called a "Bathtub" distribution because of
 * its steep bias towards the edges of its range, and low bias towards the center of the range.
 * @see Wikipedia's page on this distribution.
 */
public class ArcsineDistribution extends Distribution {
    public String getTag() {
        return "Arcsine";
    }

    @Override
    public ArcsineDistribution copy() {
        return new ArcsineDistribution(generator.copy(), alpha, beta);
    }

    private double alpha;
    private double beta;

    public double getAlpha() {
        return alpha;
    }

    public double getBeta() {
        return beta;
    }

    @Override
    public double getParameterA() {
        return alpha;
    }

    @Override
    public double getParameterB() {
        return beta;
    }

    /**
     * Uses an {@link AceRandom}, alpha = 0.0, beta = 1.0 .
     */
    public ArcsineDistribution() {
        this(new AceRandom(), 0.0, 1.0);
    }

    /**
     * Uses an {@link AceRandom} and the given alpha and beta.
     */
    public ArcsineDistribution(double alpha, double beta) {
        this(new AceRandom(), alpha, beta);
    }

    /**
     * Uses the given EnhancedRandom directly. Uses the given alpha and beta.
     */
    public ArcsineDistribution(EnhancedRandom generator, double alpha, double beta)
    {
        this.generator = generator;
        if(!setParameters(alpha, beta, 0.0))
            throw new IllegalArgumentException("Given alpha and/or beta are invalid.");
    }

    @Override
    public double getMaximum() {
        return beta;
    }

    @Override
    public double getMean() {
        return 0.5 * (alpha + beta);
    }

    @Override
    public double getMedian() {
        return 0.5 * (alpha + beta);
    }

    @Override
    public double getMinimum() {
        return alpha;
    }

    @Override
    public double[] getMode() {
        return new double[]{alpha, beta};
    }

    @Override
    public double getVariance() {
        return (beta - alpha) * (beta - alpha) * 0.125;
    }
    /**
     * Sets all parameters and returns true if they are valid, otherwise leaves parameters unchanged and returns false.
     * @param a alpha; the lower bound of the range, which must be less than b (beta)
     * @param b beta; the upper bound of the range, which must be greater than a (alpha)
     * @param c ignored
     * @return true if the parameters given are valid and will be used
     */
    @Override
    public boolean setParameters(double a, double b, double c) {
        if(a < b){
            alpha = a;
            beta = b;
            return true;
        }
        return false;
    }

    @Override
    public double nextDouble() {
        return sample(generator, alpha, beta);
    }

    public static double sample(EnhancedRandom generator, double alpha, double beta) {
        double s = sinQuarterTurns(generator.nextExclusiveDouble());
        return alpha + (beta - alpha) * s * s;
    }
    /**
     * A variation on {@link Math#sin(double)} that takes its input as a fraction of a quarter-turn instead of in
     * radians; one quarter-turn is equal to 90 degrees or 0.5*PI radians.
     * 
* The technique for sine approximation is mostly from * This Stack Exchange answer by WimC. * Changes have been made to accelerate wrapping from any double to the valid input range. * @param quarterTurns an angle as a fraction of a quarter-turn as a double, with 0.5 here equivalent to PI/8.0 radians in {@link Math#sin(double)} * @return the sine of the given angle, as a double between -1.0 and 1.0 (both inclusive) */ private static double sinQuarterTurns(double quarterTurns) { // quarterTurns *= 4.0; // not needed for this specific case // final long floor = ((long) Math.floor(quarterTurns)) & -2L; // quarterTurns -= floor; // quarterTurns *= 2.0 - quarterTurns; // return quarterTurns * (-0.775 - 0.225 * quarterTurns) * ((floor & 2L) - 1L); // quarterTurns *= 4.0; // not needed for this specific case final long ceil = (long) Math.ceil(quarterTurns) & -2L; quarterTurns -= ceil; final double x2 = quarterTurns * quarterTurns, x3 = quarterTurns * x2; return (((11 * quarterTurns - 3 * x3) / (7 + x2)) * (1 - (ceil & 2))); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy