Constant¶
- class probnum.randvars.Constant(support)¶
Bases:
DiscreteRandomVariable
[ValueType
]Random variable representing a constant value.
Discrete random variable which (with probability one) takes a constant value. The law / image measure of this random variable is given by the Dirac delta measure which equals one in its (atomic) support and zero everywhere else.
This class has the useful property that arithmetic operations between a
Constant
random variable and an arbitraryRandomVariable
represent the same arithmetic operation with a constant.- Parameters
support – Constant value taken by the random variable. Also the (atomic) support of the associated Dirac measure.
See also
RandomVariable
Class representing random variables.
Notes
The Dirac measure formalizes the concept of a Dirac delta function as encountered in physics, where it is used to model a point mass. Another way to formalize this idea is to define the Dirac delta as a linear operator as is done in functional analysis. While related, this is not the view taken here.
Examples
>>> from probnum import randvars >>> import numpy as np >>> rv1 = randvars.Constant(support=0.) >>> rv2 = randvars.Constant(support=1.) >>> rv = rv1 + rv2 >>> rng = np.random.default_rng(seed=42) >>> rv.sample(rng, size=5) array([1., 1., 1., 1., 1.])
Attributes Summary
Transpose the random variable.
Covariance \(\operatorname{Cov}(X) = \mathbb{E}((X-\mathbb{E}(X))(X-\mathbb{E}(X))^\top)\) of the random variable.
Data type of (elements of) a realization of this random variable.
Information-theoretic entropy \(H(X)\) of the random variable.
Mean \(\mathbb{E}(X)\) of the random variable.
Median of the random variable.
The dtype of the
median
.Mode of the random variable.
The dtype of any (function of a) moment of the random variable, e.g.
Number of dimensions of realizations of the random variable.
Parameters of the associated probability distribution.
Shape of realizations of the random variable.
Size of realizations of the random variable, defined as the product over all components of
shape()
.Standard deviation of the random variable.
Constant value taken by the random variable.
Variance \(\operatorname{Var}(X) = \mathbb{E}((X-\mathbb{E}(X))^2)\) of the random variable.
Methods Summary
cdf
(x)Cumulative distribution function.
in_support
(x)Check whether the random variable takes value
x
with non-zero probability, i.e. ifx
is in the support of its distribution.infer_median_dtype
(value_dtype)Infer the dtype of the median.
infer_moment_dtype
(value_dtype)Infer the dtype of any moment.
logcdf
(x)Log-cumulative distribution function.
logpmf
(x)Natural logarithm of the probability mass function.
pmf
(x)Probability mass function.
quantile
(p)Quantile function.
reshape
(newshape)Give a new shape to a random variable.
sample
(rng[, size])Draw realizations from a random variable.
transpose
(*axes)Transpose the random variable.
Attributes Documentation
- T¶
Transpose the random variable.
- Parameters
axes – See documentation of
numpy.ndarray.transpose()
.
- cov¶
Covariance \(\operatorname{Cov}(X) = \mathbb{E}((X-\mathbb{E}(X))(X-\mathbb{E}(X))^\top)\) of the random variable.
To learn about the dtype of the covariance, see
moment_dtype
.
- cov_cholesky¶
- dtype¶
Data type of (elements of) a realization of this random variable.
- entropy¶
Information-theoretic entropy \(H(X)\) of the random variable.
- mean¶
Mean \(\mathbb{E}(X)\) of the random variable.
To learn about the dtype of the mean, see
moment_dtype
.
- median¶
Median of the random variable.
To learn about the dtype of the median, see
median_dtype
.
- median_dtype¶
The dtype of the
median
.It will be set to the dtype arising from the multiplication of values with dtypes
dtype
andnumpy.float_
. This is motivated by the fact that, even for discrete random variables, e.g. integer-valued random variables, themedian
might lie in between two values in which case these values are averaged. For example, a uniform random variable on \(\{ 1, 2, 3, 4 \}\) will have a median of \(2.5\).
- mode¶
Mode of the random variable.
- moment_dtype¶
The dtype of any (function of a) moment of the random variable, e.g. its
mean
,cov
,var
, orstd
. It will be set to the dtype arising from the multiplication of values with dtypesdtype
andnumpy.float_
. This is motivated by the mathematical definition of a moment as a sum or an integral over products of probabilities and values of the random variable, which are represented as using the dtypesnumpy.float_
anddtype
, respectively.
- ndim¶
Number of dimensions of realizations of the random variable.
- parameters¶
Parameters of the associated probability distribution.
The parameters of the probability distribution of the random variable, e.g. mean, variance, scale, rate, etc. stored in a
dict
.
- shape¶
Shape of realizations of the random variable.
- size¶
Size of realizations of the random variable, defined as the product over all components of
shape()
.
- std¶
Standard deviation of the random variable.
To learn about the dtype of the standard deviation, see
moment_dtype
.
- support¶
Constant value taken by the random variable.
- var¶
Variance \(\operatorname{Var}(X) = \mathbb{E}((X-\mathbb{E}(X))^2)\) of the random variable.
To learn about the dtype of the variance, see
moment_dtype
.
Methods Documentation
- cdf(x)¶
Cumulative distribution function.
- Parameters
x (ValueType) – Evaluation points of the cumulative distribution function. The shape of this argument should be
(..., S1, ..., SN)
, where(S1, ..., SN)
is theshape
of the random variable. The cdf evaluation will be broadcast over all additional dimensions.- Return type
float64
- in_support(x)¶
Check whether the random variable takes value
x
with non-zero probability, i.e. ifx
is in the support of its distribution.- Parameters
x (ValueType) – Input value.
- Return type
- static infer_median_dtype(value_dtype)¶
Infer the dtype of the median.
Set the dtype to the dtype arising from the multiplication of values with dtypes
dtype
andnumpy.float_
. This is motivated by the fact that, even for discrete random variables, e.g. integer-valued random variables, themedian
might lie in between two values in which case these values are averaged. For example, a uniform random variable on \(\{ 1, 2, 3, 4 \}\) will have a median of \(2.5\).
- static infer_moment_dtype(value_dtype)¶
Infer the dtype of any moment.
Infers the dtype of any (function of a) moment of the random variable, e.g. its
mean
,cov
,var
, orstd
. Returns the dtype arising from the multiplication of values with dtypesdtype
andnumpy.float_
. This is motivated by the mathematical definition of a moment as a sum or an integral over products of probabilities and values of the random variable, which are represented as using the dtypesnumpy.float_
anddtype
, respectively.
- logcdf(x)¶
Log-cumulative distribution function.
- Parameters
x (ValueType) – Evaluation points of the cumulative distribution function. The shape of this argument should be
(..., S1, ..., SN)
, where(S1, ..., SN)
is theshape
of the random variable. The logcdf evaluation will be broadcast over all additional dimensions.- Return type
float64
- logpmf(x)¶
Natural logarithm of the probability mass function.
- Parameters
x (ValueType) – Evaluation points of the log-probability mass function. The shape of this argument should be
(..., S1, ..., SN)
, where(S1, ..., SN)
is theshape
of the random variable. The logpmf evaluation will be broadcast over all additional dimensions.- Return type
float64
- pmf(x)¶
Probability mass function.
Computes the probability of the random variable being equal to the given value. For a random variable \(X\) it is defined as \(p_X(x) = P(X = x)\) for a probability measure \(P\).
Probability mass functions are the discrete analogue of probability density functions in the sense that they are the Radon-Nikodym derivative of the pushforward measure \(P \circ X^{-1}\) defined by the random variable with respect to the counting measure.
- Parameters
x (ValueType) – Evaluation points of the probability mass function. The shape of this argument should be
(..., S1, ..., SN)
, where(S1, ..., SN)
is theshape
of the random variable. The pmf evaluation will be broadcast over all additional dimensions.- Return type
float64
- quantile(p)¶
Quantile function.
The quantile function \(Q \colon [0, 1] \to \mathbb{R}\) of a random variable \(X\) is defined as \(Q(p) = \inf\{ x \in \mathbb{R} \colon p \le F_X(x) \}\), where \(F_X \colon \mathbb{R} \to [0, 1]\) is the
cdf()
of the random variable. From the definition it follows that the quantile function always returns values of the same dtype as the random variable. For instance, for a discrete distribution over the integers, the returned quantiles will also be integers. This means that, in general, \(Q(0.5)\) is not equal to themedian
as it is defined in this class. See https://en.wikipedia.org/wiki/Quantile_function for more details and examples.- Parameters
p (FloatLike) –
- Return type
ValueType
- sample(rng, size=())¶
Draw realizations from a random variable.
- transpose(*axes)[source]¶
Transpose the random variable.
- Parameters
axes (int) – See documentation of
numpy.ndarray.transpose()
.- Return type