Binomial Test Algorithm

The Binomial Test Algorithm is a statistical method used to determine the probability of obtaining a specific number of successes in a fixed number of Bernoulli trials or binomial experiments. A Bernoulli trial is a random experiment with only two possible outcomes, typically labeled as "success" or "failure." These outcomes must be mutually exclusive and have a fixed probability of occurring in each trial. The Binomial Test is particularly useful in hypothesis testing to examine whether the observed proportion of successes differs significantly from an expected proportion or a null hypothesis. This algorithm is commonly applied in various fields, including medicine, psychology, and social science, to analyze the significance of experimental results or to verify if an observed phenomenon deviates from expected behavior. The Binomial Test Algorithm computes the probability of obtaining a given number of successes using the binomial probability formula, which depends on the number of trials, the probability of success in a single trial, and the number of successes. The algorithm then calculates the cumulative probability of observing the given number of successes or any value more extreme, either higher or lower, depending on the hypothesis being tested. If this cumulative probability, also known as the p-value, is below a predetermined significance level (commonly set at 0.05), the null hypothesis can be rejected, indicating that the observed proportion of successes is significantly different from the expected proportion. On the other hand, if the p-value is higher than the significance level, the null hypothesis cannot be rejected, meaning that there is insufficient evidence to conclude that the observed proportion of successes deviates from the expected proportion.
/*
 * Copyright (c) 2017 Kotlin Algorithm Club
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.algorithmexamples.math

import org.junit.Assert
import org.junit.Test

class BinomialTest {
    @Test
    fun test1() {
        Assert.assertEquals(0, binomial(0, 1))
        Assert.assertEquals(1, binomial(1, 1))
        Assert.assertEquals(2, binomial(2, 1))
        Assert.assertEquals(3, binomial(3, 1))
        Assert.assertEquals(3, binomial(3, 2))
        Assert.assertEquals(4, binomial(4, 1))

        Assert.assertEquals(1, binomial(5, 0))
        Assert.assertEquals(5, binomial(5, 1))
        Assert.assertEquals(10, binomial(5, 2))
        Assert.assertEquals(10, binomial(5, 3))
        Assert.assertEquals(5, binomial(5, 4))
        Assert.assertEquals(1, binomial(5, 5))

        Assert.assertEquals(1, binomial(6, 0))
        Assert.assertEquals(6, binomial(6, 1))
        Assert.assertEquals(15, binomial(6, 2))
        Assert.assertEquals(20, binomial(6, 3))
        Assert.assertEquals(15, binomial(6, 4))
        Assert.assertEquals(6, binomial(6, 5))
        Assert.assertEquals(1, binomial(6, 6))
    }
}

LANGUAGE:

DARK MODE: