Link Search Menu Expand Document (external link)

How to compute probabilities from a distribution (in Julia)

See all solutions.

Task

There are many famous continuous probability distributions, such as the normal and exponential distributions. How can we get access to them in software, to compute the probability of a value/values occurring?

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.

To compute a probability from a discrete distribution, create a random variable, then use the pdf function. (This is a slight misnomer, because PDF stands for Probability Density Function, which is a concept related to continuous random variables, but it’s the function Julia uses.)

1
2
3
4
5
6
7
8
using Distributions

# Create a binomial random variable with 10 trials
# and probability 0.5 of success on each trial
X = Binomial( 10, 0.5 )

# What is the probability of exactly 3 successes?
pdf( X, 3 )
1
0.1171875000000004

To compute a probability from a continuous distribution, create a random variable, then use its Cumulative Density Function, cdf. You can only compute the probability that a random value will fall in an interval $[a,b]$, not the probability that it will equal a specific value.

1
2
3
4
5
6
7
using Distributions

# Create a normal random variable with mean μ=10 and standard deviation σ=5
X = Normal( 10, 5 )

# What is the probability of the value lying in the interval [12,13]?
cdf( X, 13 ) - cdf( X, 12 )
1
0.07032514063960227

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)