Probability
Release 10.4

The Sage Development TeamJul 23, 2024

CONTENTS

1

Probability Distributions

1

2

Random variables and probability spaces

19

3

Indices and Tables

23

Python Module Index

25

Index

27

i

ii

CHAPTERONEPROBABILITY DISTRIBUTIONSThis module provides three types of probability distributions:• RealDistribution: various real-valued probability distributions.
• SphericalDistribution: uniformly distributed points on the surface of an 𝑛 − 1 sphere in 𝑛 dimensional
euclidean space.
• GeneralDiscreteDistribution: user-defined discrete distributions.
AUTHORS:• Josh Kantor (2007-02): first version• William Stein (2007-02): rewrite of docs, conventions, etc.
• Carlo Hamalainen (2008-08): full doctest coverage, more documentation, GeneralDiscreteDistribution, misc fixes.
• Kwankyu Lee (2010-05-29): F-distribution support.
REFERENCES:
GNU gsl library, General discrete distributions http: www.gnu.org software gsl manual html node
General-Discrete-Distributions.html
GNU gsl library, Random number distributions http: www.gnu.org software gsl manual html nodeRandom-Number-Distributions.html
class sage.probability.probability_distribution.GeneralDiscreteDistribution
Bases: ProbabilityDistribution
Create a discrete probability distribution.
INPUT:• P – list of probabilities. The list will automatically be normalised if sum(P) is not equal to 1.
• rng – (optional) random number generator to use. May be one of default , luxury , or taus .
• seed – (optional) seed to use with the random number generator.
OUTPUT:• a probability distribution where the probability of selecting x is P[x].
EXAMPLES:
Construct a GeneralDiscreteDistribution with the probability distribution 𝑃 where 𝑃 (0) = 0.3,
𝑃 (1) = 0.4, 𝑃 (2) = 0.3:

1

Probability, Release 10.4

sage: P = [0.3, 0.4, 0.3]
sage: X = GeneralDiscreteDistribution(P)sage: X.get_random_element() in (0, 1, 2)
True>>> from sage.all import *>>> P = [RealNumber( 0.3 ), RealNumber( 0.4 ), RealNumber( 0.3 )]>>> X = GeneralDiscreteDistribution(P)>>> X.get_random_element() in (Integer(0), Integer(1), Integer(2))
True

Checking the distribution of samples:sage: P = [0.3, 0.4, 0.3]
sage: counts = [0] * len(P)sage: X = GeneralDiscreteDistribution(P)sage: nr_samples = 10000
sage: for _ in range(nr_samples):....:counts[X.get_random_element()] += 1
sage: [1.0*x/nr_samples for x in counts] # abs tol 3e-2
[0.3, 0.4, 0.3]>>> from sage.all import *>>> P = [RealNumber( 0.3 ), RealNumber( 0.4 ), RealNumber( 0.3 )]>>> counts = [Integer(0)] * len(P)>>> X = GeneralDiscreteDistribution(P)>>> nr_samples = Integer(10000)>>> for _ in range(nr_samples):...
counts[X.get_random_element()] += Integer(1)>>> [RealNumber( 1.0 )*x/nr_samples for x in counts] # abs tol 3e-2
[0.3, 0.4, 0.3]

The distribution probabilities will automatically be normalised:sage: P = [0.1, 0.3]
sage: X = GeneralDiscreteDistribution(P, seed=0)
sage: counts = [0, 0]
sage: for _ in range(10000):....:
counts[X.get_random_element()] += 1
sage: float(counts[1]/counts[0])3.042037186742118>>> from sage.all import *>>> P = [RealNumber( 0.1 ), RealNumber( 0.3 )]>>> X = GeneralDiscreteDistribution(P, seed=Integer(0))>>> counts = [Integer(0), Integer(0)]>>> for _ in range(Integer(10000)):...
counts[X.get_random_element()] += Integer(1)>>> float(counts[Integer(1)]/counts[Integer(0)])
3.042037186742118

get_random_element()Get a random sample from the probability distribution.
EXAMPLES:

2

Chapter 1. Probability Distributions

Probability, Release 10.4

sage: P = [0.3, 0.4, 0.3]
sage: X = GeneralDiscreteDistribution(P)sage: all(X.get_random_element() in (0,1,2) for _ in range(10))
True
sage: isinstance(X.get_random_element(), sage.rings.integer.Integer)
True>>> from sage.all import *>>> P = [RealNumber( 0.3 ), RealNumber( 0.4 ), RealNumber( 0.3 )]>>> X = GeneralDiscreteDistribution(P)>>> all(X.get_random_element() in (Integer(0),Integer(1),Integer(2)) for _ in␣˓→range(Integer(10)))True>>> isinstance(X.get_random_element(), sage.rings.integer.Integer)
True

reset_distribution()
This method resets the distribution.
EXAMPLES:
sage: T = GeneralDiscreteDistribution([0.1, 0.3, 0.6])
sage: T.set_seed(0)sage: [T.get_random_element() for _ in range(10)][2, 2, 2, 2, 2, 1, 2, 2, 1, 2]
sage: T.reset_distribution()sage: [T.get_random_element() for _ in range(10)][2, 2, 2, 2, 2, 1, 2, 2, 1, 2]>>> from sage.all import *>>> T = GeneralDiscreteDistribution([RealNumber( 0.1 ), RealNumber( 0.3 ),␣
˓→RealNumber( 0.6 )])>>> T.set_seed(Integer(0))>>> [T.get_random_element() for _ in range(Integer(10))][2, 2, 2, 2, 2, 1, 2, 2, 1, 2]>>> T.reset_distribution()>>> [T.get_random_element() for _ in range(Integer(10))][2, 2, 2, 2, 2, 1, 2, 2, 1, 2]

set_random_number_generator(rng='default')
Set the random number generator to be used by gsl.
EXAMPLES:
sage: X = GeneralDiscreteDistribution([0.3, 0.4, 0.3])
sage: X.set_random_number_generator( taus )>>> from sage.all import *>>> X = GeneralDiscreteDistribution([RealNumber( 0.3 ), RealNumber( 0.4 ),␣
˓→RealNumber( 0.3 )])>>> X.set_random_number_generator( taus )set_seed(seed)
Set the seed to be used by the random number generator.
EXAMPLES:

3

Probability, Release 10.4

sage: X = GeneralDiscreteDistribution([0.3, 0.4, 0.3])
sage: X.set_seed(1)sage: X.get_random_element()
1>>> from sage.all import *>>> X = GeneralDiscreteDistribution([RealNumber( 0.3 ), RealNumber( 0.4 ),␣
˓→RealNumber( 0.3 )])>>> X.set_seed(Integer(1))>>> X.get_random_element()
1class sage.probability.probability_distribution.ProbabilityDistribution
Bases: object
Concrete probability distributions should be derived from this abstract class.
generate_histogram_data(num_samples=1000, bins=50)
Compute a histogram of the probability distribution.
INPUT:• num_samples – (optional) number of times to sample from the probability distribution• bins – (optional) number of bins to divide the samples into.
OUTPUT:• a tuple. The first element of the tuple is a list of length bins, consisting of the normalised histogram of
the random samples. The second list is the bins.
EXAMPLES:
sage: set_random_seed(0)sage: from sage.probability.probability_distribution import␣
˓→GeneralDiscreteDistribution
sage: P = [0.3, 0.4, 0.3]
sage: X = GeneralDiscreteDistribution(P)sage: h, b = X.generate_histogram_data(bins=10)
˓→needs sage.plot
sage: h # rel tol 1e-08
˓→needs sage.plot[1.6299999999999999,
0.0,
0.0,
0.0,
0.0,
1.9049999999999985,
0.0,
0.0,
0.0,
1.4650000000000003]
sage: b˓→needs sage.plot
[0.0,
0.2,
0.4,
0.6000000000000001,
0.8,

#␣
#␣

#␣

(continues on next page)

4

Chapter 1. Probability Distributions

Probability, Release 10.4

(continued from previous page)

1.0,
1.2000000000000002,
1.4000000000000001,
1.6,
1.8,
2.0]
>>> from sage.all import *>>> set_random_seed(Integer(0))>>> from sage.probability.probability_distribution import␣
˓→GeneralDiscreteDistribution>>> P = [RealNumber( 0.3 ), RealNumber( 0.4 ), RealNumber( 0.3 )]>>> X = GeneralDiscreteDistribution(P)>>> h, b = X.generate_histogram_data(bins=Integer(10))˓→# needs sage.plot>>> h # rel tol 1e-08
˓→needs sage.plot[1.6299999999999999,
0.0,
0.0,
0.0,
0.0,
1.9049999999999985,
0.0,
0.0,
0.0,
1.4650000000000003]>>> b
˓→needs sage.plot
[0.0,
0.2,
0.4,
0.6000000000000001,
0.8,
1.0,
1.2000000000000002,
1.4000000000000001,
1.6,
1.8,
2.0]

␣
#␣

#␣

generate_histogram_plot(name, num_samples=1000, bins=50)
Save the histogram from generate_histogram_data() to a file.
INPUT:• name – file to save the histogram plot (as a PNG).
• num_samples – (optional) number of times to sample from the probability distribution• bins – (optional) number of bins to divide the samples into.
EXAMPLES:This saves the histogram plot to a temporary file:
sage: from sage.probability.probability_distribution import␣
˓→GeneralDiscreteDistribution(continues on next page)

5

Probability, Release 10.4

(continued from previous page)

sage: import tempfile
sage: P = [0.3, 0.4, 0.3]
sage: X = GeneralDiscreteDistribution(P)sage: with tempfile.NamedTemporaryFile() as f:
˓→needs sage.plot....:
X.generate_histogram_plot(f.name)#␣>>> from sage.all import *>>> from sage.probability.probability_distribution import␣
˓→GeneralDiscreteDistribution>>> import tempfile>>> P = [RealNumber( 0.3 ), RealNumber( 0.4 ), RealNumber( 0.3 )]>>> X = GeneralDiscreteDistribution(P)>>> with tempfile.NamedTemporaryFile() as f:
˓→needs sage.plot...
X.generate_histogram_plot(f.name)#␣

get_random_element()
To be implemented by a derived class:sage: P = sage.probability.probability_distribution.ProbabilityDistribution()sage: P.get_random_element()
Traceback (most recent call last):...
NotImplementedError: implement in derived class>>> from sage.all import *>>> P = sage.probability.probability_distribution.ProbabilityDistribution()>>> P.get_random_element()
Traceback (most recent call last):...
NotImplementedError: implement in derived classclass sage.probability.probability_distribution.RealDistribution
Bases: ProbabilityDistribution
The RealDistribution class provides a number of routines for sampling from and analyzing and visualizing
probability distributions. For precise definitions of the distributions and their parameters see the gsl reference
manuals chapter on random number generators and probability distributions.
EXAMPLES:
Uniform distribution on the interval [a, b]:
sage: a = 0
sage: b = 2
sage: T = RealDistribution( uniform , [a, b])
sage: a <= T.get_random_element() <= bTruesage: T.distribution_function(0)0.5
sage: T.cum_distribution_function(1)0.5
sage: T.cum_distribution_function_inv(.5)
1.0

6Chapter 1. Probability Distributions

Probability, Release 10.4>>> from sage.all import *>>> a = Integer(0)>>> b = Integer(2)>>> T = RealDistribution( uniform , [a, b])>>> a <= T.get_random_element() <= b
True>>> T.distribution_function(Integer(0))
0.5>>> T.cum_distribution_function(Integer(1))
0.5>>> T.cum_distribution_function_inv(RealNumber( .5 ))1.0

The gaussian distribution takes 1 parameter sigma. The standard gaussian distribution has sigma = 1:
sage: sigma = 1
sage: T = RealDistribution( gaussian , sigma)
sage: s = T.get_random_element()
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)0.3989422804014327
sage: T.cum_distribution_function(1)0.8413447460685429
sage: T.cum_distribution_function_inv(.5)
0.0>>> from sage.all import *>>> sigma = Integer(1)>>> T = RealDistribution( gaussian , sigma)>>> s = T.get_random_element()>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.3989422804014327>>> T.cum_distribution_function(Integer(1))
0.8413447460685429>>> T.cum_distribution_function_inv(RealNumber( .5 ))0.0

The rayleigh distribution has 1 parameter sigma:
sage: sigma = 3
sage: T = RealDistribution( rayleigh , sigma)
sage: s = T.get_random_element()
sage: s >= 0
True
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)0.0
sage: T.cum_distribution_function(1)0.054040531093234534
sage: T.cum_distribution_function_inv(.5)
3.532230067546424...

7

Probability, Release 10.4>>> from sage.all import *>>> sigma = Integer(3)>>> T = RealDistribution( rayleigh , sigma)>>> s = T.get_random_element()>>> s >= Integer(0)True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.0>>> T.cum_distribution_function(Integer(1))
0.054040531093234534>>> T.cum_distribution_function_inv(RealNumber( .5 ))3.532230067546424...

The lognormal distribution has two parameters sigma and zeta:
sage: zeta = 0
sage: sigma = 1
sage: T = RealDistribution( lognormal , [zeta, sigma])sage: s = T.get_random_element()
sage: s >= 0
True
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)0.0
sage: T.cum_distribution_function(1)0.5
sage: T.cum_distribution_function_inv(.5)
1.0>>> from sage.all import *>>> zeta = Integer(0)>>> sigma = Integer(1)>>> T = RealDistribution( lognormal , [zeta, sigma])>>> s = T.get_random_element()>>> s >= Integer(0)True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.0>>> T.cum_distribution_function(Integer(1))
0.5>>> T.cum_distribution_function_inv(RealNumber( .5 ))1.0

The pareto distribution has two parameters a, and b:
sage: a = 1
sage: b = 1
sage: T = RealDistribution( pareto , [a, b])
sage: s = T.get_random_element()
sage: s >= b
True
sage: s.parent()(continues on next page)

8

Chapter 1. Probability Distributions

Probability, Release 10.4

(continued from previous page)

Real Double Field
sage: T.distribution_function(0)0.0
sage: T.cum_distribution_function(1)0.0
sage: T.cum_distribution_function_inv(.5)
2.0>>> from sage.all import *>>> a = Integer(1)>>> b = Integer(1)>>> T = RealDistribution( pareto , [a, b])>>> s = T.get_random_element()>>> s >= b
True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.0>>> T.cum_distribution_function(Integer(1))
0.0>>> T.cum_distribution_function_inv(RealNumber( .5 ))2.0

The t-distribution has one parameter nu:
sage: nu = 1
sage: T = RealDistribution( t , nu)
sage: s = T.get_random_element()
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)# rel tol 1e-15
0.3183098861837906
sage: T.cum_distribution_function(1) # rel tol 1e-15
0.75
sage: T.cum_distribution_function_inv(.5)
0.0>>> from sage.all import *>>> nu = Integer(1)>>> T = RealDistribution( t , nu)>>> s = T.get_random_element()>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))# rel tol 1e-15
0.3183098861837906>>> T.cum_distribution_function(Integer(1)) # rel tol 1e-15
0.75>>> T.cum_distribution_function_inv(RealNumber( .5 ))0.0

The F-distribution has two parameters nu1 and nu2:
sage: nu1 = 9; nu2 = 17
sage: F = RealDistribution( F , [nu1,nu2])
sage: s = F.get_random_element()(continues on next page)

9

Probability, Release 10.4

(continued from previous page)

sage: s >= 0
True
sage: s.parent()
Real Double Field
sage: F.distribution_function(1) # rel tol 1e-14
0.6695025505192798
sage: F.cum_distribution_function(3.68) # rel tol 1e-14
0.9899717772300652
sage: F.cum_distribution_function_inv(0.99) # rel tol 1e-14
3.682241524045864>>> from sage.all import *>>> nu1 = Integer(9); nu2 = Integer(17)>>> F = RealDistribution( F , [nu1,nu2])>>> s = F.get_random_element()>>> s >= Integer(0)True>>> s.parent()
Real Double Field>>> F.distribution_function(Integer(1)) # rel tol 1e-14
0.6695025505192798>>> F.cum_distribution_function(RealNumber( 3.68 )) # rel tol 1e-14
0.9899717772300652>>> F.cum_distribution_function_inv(RealNumber( 0.99 )) # rel tol 1e-14
3.682241524045864

The chi-squared distribution has one parameter nu:
sage: nu = 1
sage: T = RealDistribution( chisquared , nu)
sage: s = T.get_random_element()
sage: s >= 0
True
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)+infinity
sage: T.cum_distribution_function(1) # rel tol 1e-14
0.6826894921370856
sage: T.cum_distribution_function_inv(.5) # rel tol 1e-14
0.45493642311957305>>> from sage.all import *>>> nu = Integer(1)>>> T = RealDistribution( chisquared , nu)>>> s = T.get_random_element()>>> s >= Integer(0)True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))+infinity>>> T.cum_distribution_function(Integer(1)) # rel tol 1e-14
0.6826894921370856>>> T.cum_distribution_function_inv(RealNumber( .5 )) # rel tol 1e-14
0.45493642311957305

10Chapter 1. Probability Distributions

Probability, Release 10.4

The exponential power distribution has two parameters a and b:
sage: a = 1
sage: b = 2.5
sage: T = RealDistribution( exppow , [a, b])
sage: s = T.get_random_element()
sage: s.parent()
Real Double Field
sage: T.distribution_function(0) # rel tol 1e-14
0.5635302489930136
sage: T.cum_distribution_function(1) # rel tol 1e-14
0.940263052542855>>> from sage.all import *>>> a = Integer(1)>>> b = RealNumber( 2.5 )>>> T = RealDistribution( exppow , [a, b])>>> s = T.get_random_element()>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0)) # rel tol 1e-14
0.5635302489930136>>> T.cum_distribution_function(Integer(1)) # rel tol 1e-14
0.940263052542855

The beta distribution has two parameters a and b:
sage: a = 2
sage: b = 2
sage: T = RealDistribution( beta , [a, b])
sage: s = T.get_random_element()
sage: 0 <= s <= 1
True
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)0.0
sage: T.cum_distribution_function(1)1.0>>> from sage.all import *>>> a = Integer(2)>>> b = Integer(2)>>> T = RealDistribution( beta , [a, b])>>> s = T.get_random_element()>>> Integer(0) <= s <= Integer(1)True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.0>>> T.cum_distribution_function(Integer(1))
1.0

The exponential distribution has one parameter mu:
sage: mu = 2(continues on next page)11

Probability, Release 10.4

(continued from previous page)

sage: T = RealDistribution( exponential , mu)
sage: s = T.get_random_element()
sage: 0 <= sTruesage: s.parent()
Real Double Field
sage: T.distribution_function(0)0.5>>> from sage.all import *>>> mu = Integer(2)>>> T = RealDistribution( exponential , mu)>>> s = T.get_random_element()>>> Integer(0) <= s
True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.5

The gamma distribution has two parameters a and b:
sage: a = 2
sage: b = 2
sage: T = RealDistribution( gamma , [a, b])
sage: s = T.get_random_element()
sage: 0 <= sTruesage: s.parent()
Real Double Field
sage: T.distribution_function(0)0.0>>> from sage.all import *>>> a = Integer(2)>>> b = Integer(2)>>> T = RealDistribution( gamma , [a, b])>>> s = T.get_random_element()>>> Integer(0) <= s
True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
0.0

The weibull distribution has two parameters a and b:
sage: a = 1
sage: b = 1
sage: T = RealDistribution( weibull , [a, b])
sage: s = T.get_random_element()
sage: s >= 0
True
sage: s.parent()
Real Double Field
sage: T.distribution_function(0)(continues on next page)

12

Chapter 1. Probability Distributions

Probability, Release 10.4

(continued from previous page)1.0
sage: T.cum_distribution_function(1)0.6321205588285577
sage: T.cum_distribution_function_inv(.5)
0.6931471805599453>>> from sage.all import *>>> a = Integer(1)>>> b = Integer(1)>>> T = RealDistribution( weibull , [a, b])>>> s = T.get_random_element()>>> s >= Integer(0)True>>> s.parent()
Real Double Field>>> T.distribution_function(Integer(0))
1.0>>> T.cum_distribution_function(Integer(1))
0.6321205588285577>>> T.cum_distribution_function_inv(RealNumber( .5 ))0.6931471805599453

It is possible to select which random number generator drives the sampling as well as the seed. The default is
the Mersenne twister. Also available are the RANDLXS algorithm and the Tausworthe generator (see the gsl
reference manual for more details). These are all supposed to be simulation quality generators. For RANDLXS
use rng= luxury and for tausworth use rng= taus :sage: T = RealDistribution( gaussian , 1, rng= luxury , seed=10)>>> from sage.all import *>>> T = RealDistribution( gaussian , Integer(1), rng= luxury , seed=Integer(10))

To change the seed at a later time use set_seed:sage: T.set_seed(100)>>> from sage.all import *>>> T.set_seed(Integer(100))

cum_distribution_function(x)Evaluate the cumulative distribution function of the probability distribution at x.
EXAMPLES:
sage: T = RealDistribution( uniform , [0, 2])
sage: T.cum_distribution_function(1)0.5>>> from sage.all import *>>> T = RealDistribution( uniform , [Integer(0), Integer(2)])>>> T.cum_distribution_function(Integer(1))
0.5

cum_distribution_function_inv(x)Evaluate the inverse of the cumulative distribution distribution function of the probability distribution at x.
13

Probability, Release 10.4

EXAMPLES:
sage: T = RealDistribution( uniform , [0, 2])
sage: T.cum_distribution_function_inv(.5)
1.0>>> from sage.all import *>>> T = RealDistribution( uniform , [Integer(0), Integer(2)])>>> T.cum_distribution_function_inv(RealNumber( .5 ))1.0

distribution_function(x)Evaluate the distribution function of the probability distribution at x.
EXAMPLES:
sage: T = RealDistribution( uniform , [0, 2])
sage: T.distribution_function(0)0.5
sage: T.distribution_function(1)0.5
sage: T.distribution_function(1.5)
0.5
sage: T.distribution_function(2)0.0>>> from sage.all import *>>> T = RealDistribution( uniform , [Integer(0), Integer(2)])>>> T.distribution_function(Integer(0))
0.5>>> T.distribution_function(Integer(1))
0.5>>> T.distribution_function(RealNumber( 1.5 ))0.5>>> T.distribution_function(Integer(2))
0.0

get_random_element()Get a random sample from the probability distribution.
EXAMPLES:
sage: T = RealDistribution( gaussian , 1, seed=0)
sage: T.get_random_element() # rel tol 4e-16
0.13391860811867587>>> from sage.all import *>>> T = RealDistribution( gaussian , Integer(1), seed=Integer(0))>>> T.get_random_element() # rel tol 4e-16
0.13391860811867587

plot(*args, **kwds)
Plot the distribution function for the probability distribution. Parameters to sage.plot.plot.plot()
can be passed through *args and **kwds.
EXAMPLES:

14

Chapter 1. Probability Distributions

Probability, Release 10.4

sage: T = RealDistribution( uniform , [0, 2])
sage: P = T.plot()
˓→needs sage.plot>>> from sage.all import *>>> T = RealDistribution( uniform , [Integer(0), Integer(2)])>>> P = T.plot()
˓→needs sage.plot#␣#␣

reset_distribution()
Reset the distribution.
EXAMPLES:
sage: T = RealDistribution( gaussian , 1, seed=10)
sage: [T.get_random_element() for _ in range(10)] # rel tol 4e-16[-0.7460999595745819, -0.004644606626413462, -0.8720538317207641, 0.
˓→6916259921666037, 2.67668674666043, 0.6325002813661014, -0.7974263521959355,
˓→ -0.5284976893366636, 1.1353119849528792, 0.9912505673230749]
sage: T.reset_distribution()sage: [T.get_random_element() for _ in range(10)] # rel tol 4e-16[-0.7460999595745819, -0.004644606626413462, -0.8720538317207641, 0.
˓→6916259921666037, 2.67668674666043, 0.6325002813661014, -0.7974263521959355,
˓→ -0.5284976893366636, 1.1353119849528792, 0.9912505673230749]>>> from sage.all import *>>> T = RealDistribution( gaussian , Integer(1), seed=Integer(10))>>> [T.get_random_element() for _ in range(Integer(10))] # rel tol 4e-16[-0.7460999595745819, -0.004644606626413462, -0.8720538317207641, 0.
˓→6916259921666037, 2.67668674666043, 0.6325002813661014, -0.7974263521959355,
˓→ -0.5284976893366636, 1.1353119849528792, 0.9912505673230749]>>> T.reset_distribution()>>> [T.get_random_element() for _ in range(Integer(10))] # rel tol 4e-16[-0.7460999595745819, -0.004644606626413462, -0.8720538317207641, 0.
˓→6916259921666037, 2.67668674666043, 0.6325002813661014, -0.7974263521959355,
˓→ -0.5284976893366636, 1.1353119849528792, 0.9912505673230749]

set_distribution(name='uniform', parameters=None)
This method can be called to change the current probability distribution.
EXAMPLES:
sage: T = RealDistribution( gaussian , 1)
sage: T.set_distribution( gaussian , 1)
sage: T.set_distribution( pareto , [0, 1])>>> from sage.all import *>>> T = RealDistribution( gaussian , Integer(1))>>> T.set_distribution( gaussian , Integer(1))>>> T.set_distribution( pareto , [Integer(0), Integer(1)])

set_random_number_generator(rng='default')
Set the gsl random number generator to be one of default , luxury , or taus .
EXAMPLES:

15

Probability, Release 10.4

sage: T = SphericalDistribution()sage: T.set_random_number_generator( default )sage: T.set_seed(0)sage: T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)
sage: T.set_random_number_generator( luxury )sage: T.set_seed(0)sage: T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)>>> from sage.all import *>>> T = SphericalDistribution()>>> T.set_random_number_generator( default )>>> T.set_seed(Integer(0))>>> T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)>>> T.set_random_number_generator( luxury )>>> T.set_seed(Integer(0))>>> T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)

set_seed(seed)
Set the seed for the underlying random number generator.
EXAMPLES:
sage: T = RealDistribution( gaussian , 1, rng= luxury , seed=10)
sage: T.set_seed(100)>>> from sage.all import *>>> T = RealDistribution( gaussian , Integer(1), rng= luxury ,␣˓→seed=Integer(10))>>> T.set_seed(Integer(100))class sage.probability.probability_distribution.SphericalDistribution
Bases: ProbabilityDistribution
This class is capable of producing random points uniformly distributed on the surface of an (𝑛 − 1)-sphere in𝑛-dimensional euclidean space. The dimension 𝑛 is selected via the keyword dimension. The random number
generator which drives it can be selected using the keyword rng. Valid choices are default which uses the
Mersenne-Twister, luxury which uses RANDLXS, and taus which uses the tausworth generator. The
default dimension is 3.
EXAMPLES:
sage: T = SphericalDistribution()sage: s = T.get_random_element()
sage: s.norm() # rel tol 1e-14
1.0
sage: len(s)3sage: T = SphericalDistribution(dimension=4, rng= luxury )sage: s = T.get_random_element()
sage: s.norm() # rel tol 1e-14
1.0
sage: len(s)416Chapter 1. Probability Distributions

Probability, Release 10.4>>> from sage.all import *>>> T = SphericalDistribution()>>> s = T.get_random_element()>>> s.norm() # rel tol 1e-14
1.0>>> len(s)3>>> T = SphericalDistribution(dimension=Integer(4), rng= luxury )>>> s = T.get_random_element()>>> s.norm() # rel tol 1e-14
1.0>>> len(s)4get_random_element()Get a random sample from the probability distribution.
EXAMPLES:
sage: T = SphericalDistribution(seed=0)
sage: T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)>>> from sage.all import *>>> T = SphericalDistribution(seed=Integer(0))>>> T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)

reset_distribution()
This method resets the distribution.
EXAMPLES:
sage: T = SphericalDistribution(seed=0)
sage: [T.get_random_element() for _ in range(4)] # rel tol 4e-16[(0.07961564104639995, -0.05237671627581255, 0.9954486572862178),(0.4123599490593727, 0.5606817859360097, -0.7180495855658982),(-0.9619860891623148, -0.2726473494040498, -0.015690351211529927),(0.5674297579435619, -0.011206783800420301, -0.8233455397322326)]
sage: T.reset_distribution()sage: [T.get_random_element() for _ in range(4)] # rel tol 4e-16[(0.07961564104639995, -0.05237671627581255, 0.9954486572862178),(0.4123599490593727, 0.5606817859360097, -0.7180495855658982),(-0.9619860891623148, -0.2726473494040498, -0.015690351211529927),(0.5674297579435619, -0.011206783800420301, -0.8233455397322326)]>>> from sage.all import *>>> T = SphericalDistribution(seed=Integer(0))>>> [T.get_random_element() for _ in range(Integer(4))] # rel tol 4e-16[(0.07961564104639995, -0.05237671627581255, 0.9954486572862178),(0.4123599490593727, 0.5606817859360097, -0.7180495855658982),(-0.9619860891623148, -0.2726473494040498, -0.015690351211529927),(0.5674297579435619, -0.011206783800420301, -0.8233455397322326)]>>> T.reset_distribution()>>> [T.get_random_element() for _ in range(Integer(4))] # rel tol 4e-16[(0.07961564104639995, -0.05237671627581255, 0.9954486572862178),(0.4123599490593727, 0.5606817859360097, -0.7180495855658982),(continues on next page)17

Probability, Release 10.4

(continued from previous page)(-0.9619860891623148, -0.2726473494040498, -0.015690351211529927),(0.5674297579435619, -0.011206783800420301, -0.8233455397322326)]

set_random_number_generator(rng='default')
Set the gsl random number generator to be one of default, luxury, or taus.
EXAMPLES:
sage: T = SphericalDistribution()sage: T.set_random_number_generator( default )sage: T.set_seed(0)sage: T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)
sage: T.set_random_number_generator( luxury )sage: T.set_seed(0)sage: T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)>>> from sage.all import *>>> T = SphericalDistribution()>>> T.set_random_number_generator( default )>>> T.set_seed(Integer(0))>>> T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)>>> T.set_random_number_generator( luxury )>>> T.set_seed(Integer(0))>>> T.get_random_element() # rel tol 4e-16(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)

set_seed(seed)
Set the seed for the underlying random number generator.
EXAMPLES:
sage: T = SphericalDistribution(seed=0)
sage: T.set_seed(100)>>> from sage.all import *>>> T = SphericalDistribution(seed=Integer(0))>>> T.set_seed(Integer(100))18

Chapter 1. Probability Distributions

CHAPTERTWORANDOM VARIABLES AND PROBABILITY SPACES

This introduces a class of random variables, with the focus on discrete random variables (i.e. on a discrete probability
space). This avoids the problem of defining a measure space and measurable functions.
class sage.probability.random_variable.DiscreteProbabilitySpace(X, P,
codomain=None,check=False)
Bases: ProbabilitySpace_generic, DiscreteRandomVariable
The discrete probability spaceentropy()
The entropy of the probability space.
set()
The set of values of the probability space taking possibly nonzero probability (a subset of the domain).
class sage.probability.random_variable.DiscreteRandomVariable(X, f, codomain=None,check=False)
Bases: RandomVariable_genericA random variable on a discrete probability space.
correlation(other)
The correlation of the probability space X = self with Y = other.
covariance(other)
The covariance of the discrete random variable X = self with Y = other.
Let 𝑆 be the probability space of 𝑋 = self, with probability function 𝑝, and 𝐸(𝑋) be the expectation of 𝑋.
Then the variance of 𝑋 is:∑︁
cov(𝑋, 𝑌 ) = 𝐸((𝑋 − 𝐸(𝑋) · (𝑌 − 𝐸(𝑌 )) =𝑝(𝑥)(𝑋(𝑥) − 𝐸(𝑋))(𝑌 (𝑥) − 𝐸(𝑌 ))𝑥∈𝑆

expectation()
The expectation of the discrete random variable, namely
probability space of 𝑋.

𝑥∈𝑆 𝑝(𝑥)𝑋[𝑥], where 𝑋 = self and 𝑆 is the∑︀function()
The function defining the random variable.

19

Probability, Release 10.4

standard_deviation()
The standard deviation of the discrete random variable.
Let 𝑆 be the probability space of 𝑋 = self, with probability function 𝑝, and 𝐸(𝑋) be the expectation of 𝑋.
Then the standard deviation of 𝑋 is defined to be√︃ ∑︁𝜎(𝑋) =
𝑝(𝑥)(𝑋(𝑥) − 𝐸(𝑥))2𝑥∈𝑆translation_correlation(other, map)
The correlation of the probability space X = self with image of Y = other under map.
translation_covariance(other, map)
The covariance of the probability space X = self with image of Y = other under the given map of the probability
space.
Let 𝑆 be the probability space of 𝑋 = self, with probability function 𝑝, and 𝐸(𝑋) be the expectation of 𝑋.
Then the variance of 𝑋 is:∑︁
cov(𝑋, 𝑌 ) = 𝐸((𝑋 − 𝐸(𝑋) · (𝑌 − 𝐸(𝑌 )) =𝑝(𝑥)(𝑋(𝑥) − 𝐸(𝑋))(𝑌 (𝑥) − 𝐸(𝑌 ))𝑥∈𝑆translation_expectation(map)The expectation of the discrete random variable, namely
ability space of 𝑋, and 𝑒 = map.

𝑥∈𝑆 𝑝(𝑥)𝑋[𝑒(𝑥)], where 𝑋 = self, 𝑆 is the prob-∑︀translation_standard_deviation(map)The standard deviation of the translated discrete random variable 𝑋 ∘ 𝑒, where 𝑋 = self and 𝑒 = map.
Let 𝑆 be the probability space of 𝑋 = self, with probability function 𝑝, and 𝐸(𝑋) be the expectation of 𝑋.
Then the standard deviation of 𝑋 is defined to be√︃ ∑︁𝜎(𝑋) =
𝑝(𝑥)(𝑋(𝑥) − 𝐸(𝑥))2𝑥∈𝑆translation_variance(map)The variance of the discrete random variable 𝑋 ∘ 𝑒, where 𝑋 = self, and 𝑒 = map.
Let 𝑆 be the probability space of 𝑋 = self, with probability function 𝑝, and 𝐸(𝑋) be the expectation of 𝑋.
Then the variance of 𝑋 is:∑︁var(𝑋) = 𝐸((𝑋 − 𝐸(𝑥))2 ) =𝑝(𝑥)(𝑋(𝑥) − 𝐸(𝑥))2𝑥∈𝑆

variance()
The variance of the discrete random variable.
Let 𝑆 be the probability space of 𝑋 = self, with probability function 𝑝, and 𝐸(𝑋) be the expectation of 𝑋.
Then the variance of 𝑋 is:∑︁var(𝑋) = 𝐸((𝑋 − 𝐸(𝑥))2 ) =𝑝(𝑥)(𝑋(𝑥) − 𝐸(𝑥))2𝑥∈𝑆

20

Chapter 2. Random variables and probability spaces

Probability, Release 10.4class sage.probability.random_variable.ProbabilitySpace_generic(domain, RR)
Bases: RandomVariable_genericA probability space.
domain()class sage.probability.random_variable.RandomVariable_generic(X, RR)
Bases: Parent
A random variable.
codomain()domain()field()
probability_space()sage.probability.random_variable.is_DiscreteProbabilitySpace(S)sage.probability.random_variable.is_DiscreteRandomVariable(X)sage.probability.random_variable.is_ProbabilitySpace(S)sage.probability.random_variable.is_RandomVariable(X)21

Probability, Release 10.4

22

Chapter 2. Random variables and probability spaces

CHAPTER

THREE

INDICES AND TABLES• Index• Module Index• Search Page

23

Probability, Release 10.4

24

Chapter 3. Indices and Tables

PYTHON MODULE INDEX

psage.probability.probability_distribution, 1
sage.probability.random_variable, 19

25

Probability, Release 10.4

26

Python Module Index

INDEXC

generate_histogram_data()(sage.probability.probability_distribution.ProbabilityDistribucodomain() (sage.probability.random_variable.Rantion method), 4
domVariable_generic method), 21
generate_histogram_plot()(sage.probabilcorrelation()(sage.probability.random_variity.probability_distribution.ProbabilityDistribuable.DiscreteRandomVariable method), 19
tion method), 5
covariance() (sage.probability.random_variable.Disget_random_element()(sage.probability.probacreteRandomVariable method), 19
bility_distribution.GeneralDiscreteDistribution
cum_distribution_function()(sage.probamethod), 2
bility.probability_distribution.RealDistribution
get_random_element()(sage.probability.probabilmethod), 13
ity_distribution.ProbabilityDistribution
method),cum_distribution_function_inv()
6(sage.probability.probability_distribution.RealDget_random_element() (sage.probability.probabilistribution method), 13
ity_distribution.RealDistribution method), 14
get_random_element()(sage.probability.probabilD
ity_distribution.SphericalDistribution method), 17
DiscreteProbabilitySpace (class in sage.probability.random_variable), 19
I
DiscreteRandomVariable (class in sage.probabilis_DiscreteProbabilitySpace() (in module
ity.random_variable), 19
sage.probability.random_variable), 21
distribution_function() (sage.probability.probis_DiscreteRandomVariable()(in module
ability_distribution.RealDistribution method), 14
sage.probability.random_variable),
21
domain() (sage.probability.random_variable.Probabiliis_ProbabilitySpace()(inmodulesage.probabiltySpace_generic method), 21
ity.random_variable), 21
domain() (sage.probability.random_variable.Randomis_RandomVariable()(in module sage.probabilVariable_generic method), 21
ity.random_variable), 21E

M

entropy()(sage.probability.random_variable.Dismodule
creteProbabilitySpace method), 19
sage.probability.probability_disexpectation()(sage.probability.random_varitribution, 1
able.DiscreteRandomVariable method), 19
sage.probability.random_variable, 19

FP

field()(sage.probability.random_variable.Randomplot() (sage.probability.probability_distribution.RealDVariable_generic method), 21
istribution method), 14
function()(sage.probability.random_variable.Disprobability_space()(sage.probability.rancreteRandomVariable method), 19
dom_variable.RandomVariable_genericmethod), 21
G
ProbabilityDistribution (class in sage.probabilGeneralDiscreteDistribution(classinity.probability_distribution), 4
sage.probability.probability_distribution), 1

27

Probability, Release 10.4

ProbabilitySpace_generic (class in sage.proba- translation_expectation()(sage.probabilbility.random_variable), 20
ity.random_variable.DiscreteRandomVariable
method), 20
R
translation_standard_deviation()(sage.probability.random_variable.DiscreteRanRandomVariable_generic (class in sage.probabildomVariable method), 20
ity.random_variable), 21
RealDistribution (class in sage.probability.probabil- translation_variance() (sage.probability.random_variable.DiscreteRandomVariable method),
ity_distribution), 6
20
reset_distribution()(sage.probability.probability_distribution.GeneralDiscreteDistribution
Vmethod), 3(sage.probability.random_variable.Disreset_distribution() (sage.probability.probabil- variance()creteRandomVariable method), 20
ity_distribution.RealDistribution method), 15
reset_distribution() (sage.probability.probability_distribution.SphericalDistribution method), 17

S
sage.probability.probability_distribution
module, 1
sage.probability.random_variable
module, 19set() (sage.probability.random_variable.DiscreteProbabilitySpace method), 19
set_distribution()(sage.probability.probability_distribution.RealDistribution method), 15
set_random_number_generator() (sage.probability.probability_distribution.GeneralDiscreteDistribution method), 3
set_random_number_generator() (sage.probability.probability_distribution.RealDistribution
method), 15
set_random_number_generator() (sage.probability.probability_distribution.SphericalDistribution method), 18
set_seed()(sage.probability.probability_distribution.GeneralDiscreteDistribution method), 3
set_seed()(sage.probability.probability_distribution.RealDistribution method), 16
set_seed()(sage.probability.probability_distribution.SphericalDistribution method), 18
SphericalDistribution (class in sage.probability.probability_distribution), 16
standard_deviation()(sage.probability.random_variable.DiscreteRandomVariable method),19

Ttranslation_correlation()(sage.probability.random_variable.DiscreteRandomVariable
method), 20
translation_covariance() (sage.probability.random_variable.DiscreteRandomVariable method),
20
28

Index