To calculate binomial distribution in javascript, we can use the following function:
index.tsx285 chars14 lines
The binomial(n, k, p)
function takes three parameters:
n
: the number of trialsk
: the number of successful trialsp
: the probability of success in each trialThe function calculates the binomial coefficient (n
choose k
) and multiplies it by the probability of k
successes and (n-k)
failures.
Here's an example of how to use this function to calculate the probability of getting exactly 3 heads in 5 coin tosses with a fair coin:
index.tsx180 chars7 lines
The output shows that the probability of getting exactly 3 heads in 5 coin tosses is 0.3125 or 31.25%.
gistlibby LogSnag