Link Search Menu Expand Document (external link)

How to plot discrete probability distributions (in Julia)

See all solutions.

Task

There are many famous discrete probability distributions, such as the binomial and geometric distributions. How can we get access to them in software, to plot the distribution as a series of points?

Related tasks:

Solution

You can import many different random variables from Julia’s Distributions package. The full list of them is online here.

If you don’t have that package installed, first run using Pkg and then Pkg.add( "Distributions" ) from within Julia.

The challenge with plotting a random variable is knowing the appropriate sample space, because some random variables have sample spaces of infinite width, which cannot be plotted.

The example below uses a geometric distribution, whose sample space is $\{1,2,3,\ldots\}$. We specify that we just want to use $x$ values in the set ${1,2,\ldots,10}$. (In some software, the geometric distribution’s sample space begins at 0, but not in SciPy.)

We style the plot below so that it is clear the sample space is discrete.

1
2
3
4
5
6
using Distributions
X = Geometric( 0.5 )      # use a geometric distribution with p=0.5
xs = 1:10                 # specify the range to be 1,2,3,...,10

using Plots
bar( xs, pdf.( X, xs ) )  # plot the shape of the distribution

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)