Discrete Distributions in R

The discrete distributions of statistics are not continuous. Usually, they are constructed of a finite number of possible values for the random variable and each possibility is assigned a probability of occurrence.

The Bernoulli Distribution

One of the simplest discrete distributions is called the Bernoulli Distribution. This is a distribution with only two possible values. For example, consider the Bernoulli distribution in the table that follows:

A Bernoulli Distribution
x p
0 0.3
1 0.7

In this case, there are only two possible values of the random variable, x = 0 or x = 1. When drawing numbers from this distribution, the probability of selecting x = 0 is 0.3, while the probability of selecting x = 1 is 0.7.

There are many applications that can be represented by Bernoulli Distributions. For example, x = 0 and x = 1 might represent the states of a lamp, with zero representing the state that the lamp is switched "off" and one representing the state that the lamp is switched "on." Or x = 0 and x = 1 might represent the states of a biased coin, with zero representing "heads" and one representing "tails."

Because the values of this distribution are discrete, the probability density density function will not be a continuous curve as in the Standard Normal Distribution or the Normal Distribution. The usual way to visualize a discrete distribution is with a sequence of "spikes." Use the following code to produce the image in Figure 3.

> x=c(0,1)
> y=c(0.3,0.7)
> plot(x,y,type="h",xlim=c(-1,2),ylim=c(0,1),
+ lwd=2,col="blue",ylab="p")
> points(x,y,pch=16,cex=2,col="dark red")

There are a few new constructs introduced in the above code:

  1. We use xlim and ylim to place "limits" on the horizontal and vertical axes, respectively.
  2. In the points command, the pch argument determines the "plotting character" that is used. Experiment with the numbers 1 through 16 to see a variety of different choices for the plotting character. Try ?points to learn more.
  3. In the points command, the cex argument determines the "character expansion." We've used it here to "scale" the size of the plotting character. Try values of 1 or 3 and note the difference.

The discrete Bernoulli probability density function.

Figure 3. The discrete Bernoulli probability density function.

Note the "discrete" nature of the visualization.

  1. On the x-axis, there are only two possibilities, x = 0 or x = 1.
  2. On the vertical axis, the heights of the "spikes" represent probabilities. Note that the height of the spike at x = 0 is 0.3, the probability of selecting x = 0. The height of the spike at x = 1 is 0.7, the probability of selecting x = 1.

Key Idea: It is important to note that the sum of the probabilites equals 1, much as it did in the Standard Normal Distribution and the Normal Distribution. Only this time, it is not the area under a curve that represents the total probability, but the sum of the probabilities represented by the heights of the "spikes."

The Binomial Distribution

The Binomial Distribution is best introduced with a simple example. Suppose that we have a "fair" coin, one in which there is an equal probability of flipping "heads" or "tails." Now, suppose that we toss the coin three times. Further, suppose that we define a random variable X that equals the number of heads obtained in three tosses of the fair coin. The outcomes and the value of the random variable are listed in the table that follows.

Counting the Number of Heads
Outcome X = # Heads
HHH 3
HHT 2
HTH 2
HTT 1
THH 2
THT 1
TTH 1
TTT 0

Because the coin is "fair," each of the outcomes is equally likely, with each having a 1 in 8 chance of occurring. Thus, the probability of tossing three heads (HHH) is 1/8. Next, there are three ways of obtaining two heads (HHT, HTH, and THH), each with a 1 in 8 chance of occuring, so the probability of obtaining two heads in three tosses is 3/8. The remaining values of hte random variable and their probabilities are listed in the table that follows.

A Binomial Distribution
x p
3 1/8 = 0.125
2 3/8 = 0.375
1 3/8 = 0.375
0 1/8 = 0.125

Using dbinom

It is readily apparent that this simple approach will rapidly become quite tedious if we increase the number of times that we toss the coin. Fortunately, this "binomial distribution" is easily calculated in R. To calculate the probability of obtaining three heads in three tosses of a "fair" coin, enter the following code:

> dbinom(3,size=3,prob=1/2)
[1] 0.125

Note that this code gives a result that is identical to the first row in the table above.

The general syntax dbinom(x, size= , prob = ) is fairly straightforward.

  1. The argument x is the value of the random variable, the number of heads in the current example.
  2. The size is the "number of trials," or the number of tosses in the current example.
  3. The prob is the probability of success, or the probability of obtaining "heads" in the current example.

We can produce the entire table with the following code:

> p=dbinom(0:3,size=3,prob=1/2)
> p
[1] 0.125 0.375 0.375 0.125

In this case, the input for x is the vector 0:3, which produces upon expansion 0, 1, 2, and 3. We can create the "spikes" shown in Figure 4 with the following code:

> n=3
> p=1/2
> x=0:3
> p=dbinom(x,size=n,prob=p)
> plot(x,p,type="h",xlim=c(-1,4),ylim=c(0,1),lwd=2,col="blue",ylab="p")
> points(x,p,pch=16,cex=2,col="dark red")

The discrete binomial distribution describing the number of heads in three tosses of a 'fail' coin.

Figure 4. The discrete binomial distribution describing the number of heads in three tosses of a "fair" coin.

These commands become increasingly effective as we increase the number of trials, in this case the number of "tosses" of the fair coin.

> n=10
> p=1/2
> x=0:10
> p=dbinom(x,size=n,prob=p)
> plot(x,p,type="h",xlim=c(-1,11),ylim=c(0,0.5),lwd=2,col="blue",ylab="p")
> points(x,p,pch=16,cex=2,col="dark red")

The discrete binomial distribution describing the number of heads in ten tosses of a 'fail' coin.

Figure 4. The discrete binomial distribution describing the number of heads in ten tosses of a "fair" coin.

Using pbinom

Now, suppose that we toss a "fair" coin ten times (the distribution shown in Figure 4) and we ask the following question: "What is the probbility of obtaining 4 or fewer heads?" One approach is to realize that this probability is the sum of the individual probabilities. That is, to obtain the probability of obtaining 4 or fewer heads, we would add the probabilities of obtaining 0, 1, 2, 3, or 4 heads.

> sum(dbinom(0:4,size=10,prob=1/2))
[1] 0.3769531

Alternatively, this sounds suspiciously similar to the questions we asked in the activities The Standard Normal Distribution and The Normal Distribution. In those activities we used the pnorm command. In similar fashion, we can use the pbinom command in the current situation.

> pbinom(4,size=10,prob=1/2)
[1] 0.3769531

Note again that it's always the probability to the left of and including the number of successes.

Let's ask another question. Suppose that we want to know the probability of obtaining between 5 and 8 heads, inclusive. We could sum the probabilities of obtaining 5, 6, 7, or 8 heads.

> sum(dbinom(5:8,size=10,prob=1/2))
[1] 0.6123047

Warning: The following computation is an incorrect application of the pbinom command for this example.

> pbinom(8,size=10,prob=1/2)-pbinom(5,size=10,prob=1/2)
[1] 0.3662109

The error in this calculation is due to our failure to note that the distribution is discrete. In subtracting the probability of getting 5 or fewer heads from the probability of getting 8 or fewer heads, we're left with the probability of getting 6, 7, or 8 heads, which is not the the goal. Here is the correct calculation:

> pbinom(8,size=10,prob=1/2)-pbinom(4,size=10,prob=1/2)
[1] 0.6123047

Using the binomial distribution in Figure 4, let's look at one final question and compute the probability that X is greater than 7. One obvious way to go is to compute the sum the probabilities that X is 8, 9, or 10.

> sum(dbinom(8:10,size=10,prob=1/2))
[1] 0.0546875

A second approach uses the pbinom command, which must always sum the probabilities to the left of or equal to a given number. In this case, the probability that X is greater that or equal to 8 (8, 9, or 10) is equal to 1 minus the probability that X is less than or equal to 7 (0, 1, 2, 3, 4, 5, 6, or 7).

> 1-pbinom(7,size=10,prob=1/2)
[1] 0.0546875

Enjoy!

We hope you enjoyed this introduction to the discrete distributions in general, binomial distributions in particular, using the R system. We encourage you to explore further.