# Basic Statistics and Data Analysis

## Binomial Random number Generation in R

We will learn here how to generate Bernoulli or Binomial distribution in R with example of flip of a coin. This tutorial is based on how to generate random numbers according to different statistical distributions in R. Our focus is in binomial random number generation in R.

We know that in Bernoulli distribution, either something will happen or not such as coin flip has to outcomes head or tail (either head will occur or head will not occur i.e. tail will occur). For unbiased coin there will be 50%  chances that head or tail will occur in the long run. To generate a random number that are binomial in R, use rbinom(n, size,prob) command.

rbinom(n, size, prob) command has three parameters, namely

where
n is number of observations
size is number of trials (it may be zero or more)
prob is probability of success on each trial for example 1/2

Some Examples

• One coin is tossed 10 times with probability of success=0.5
coin will be fair (unbiased coin as p=1/2)
>rbinom(n=10, size=1, prob=1/2)
OUTPUT: 1 1 0 0 1 1 1 1 0 1
• Two coins are tossed 10 times with probability of success=0.5
• > rbinom(n=10, size=2, prob=1/2)
OUTPUT: 2 1 2 1 2 0 1 0 0 1
• One coin is tossed one hundred thousand times with probability of success=0.5
> rbinom(n=100,000, size=1, prob=1/2)
• store simulation results in $x$ vector
> x<- rbinom(n=100,000, size=5, prob=1/2)
count 1’s in x vector
> sum(x)
find the frequency distribution
> table(x)
creates a frequency distribution table with frequency
> t=(table(x)/n *100)}
plot frequency distribution table
>plot(table(x),ylab=”Probability”,main=”size=5,prob=0.5″)

View Video tutorial on rbinom command

# Binomial Probability Distributions

Bernoulli Trials

Many experiments consists of repeated independent trials and each trial have only two possible outcomes such as head or tail, right or wrong, alive or dead, defective or non-defective etc. If the probability of each outcome remains the same (constant) throughout the trials, then such trials are called the Bernoulli Trials.

Binomial Probability Distribution
Binomial Probability Distribution is a discrete probability distribution describing the results of an experiment known as Bernoulli Process. The experiment having n Bernoulli trials is called a Binomial Probability experiment possessing the following four conditions/ assumptions

1. The experiment consists of n repeated task.
2. Each trial, results in an outcome that may be classified as success or failure.
3. The probability of success denoted by p remains constant from trial to trial.
4. The repeated trials are independent.

A Binomial trial can result in a success with probability p and a failure with probability 1−p  having nx number of failures, then the probability distribution of Binomial Random Variable , the number of success in n independent trial is:

\begin{align*}
P(X=x)&=\binom{n}{x} \, p^x \, q^{n-x} \\
&=\frac{n!}{x!(n-x)!}\, p^x \, q^{n-x}
\end{align*}

The Binomial probability distribution is the most widely used distributions in situation of two outcomes. It was discovered by the Swiss mathematician Jakob Bernoulli (1654—1704) whose main work on “the ars Conjectandi” (the art of conjecturing) was published posthumously in Basel in 1713.

Mean of Binomial Distribution:   Mean = μ = np

Variance of Binomial Distribution:  Variance= npq

Standard Deviation of Binomial Distribution:  Standard Deviation = $\sqrt{npq}$

Moment Coefficient of Skewness:

\begin{align*}
\beta_1 &= \frac{q-p}{\sqrt{npq}}  \\
&= \frac{1-2p}{\sqrt{npq}}
\end{align*}

Moment Coefficient of Kurtosis:  $\beta_3 = 3+\frac{1-6pq}{npq}$