Random variables#

Define and work with a random variable#

A random variable in Fesslix is handled through the class flx.rv:

class flx.rv#

A random variable.

__init__(config)#

Initialize the flx.rv instance with the given configuration.

Parameters:

config (flx_rv_config) – The configuration to use for the random variable.

get_name()#

Retrieves the name of the random variable.

Returns:

name of random variable

Return type:

str

get_descr()#

Retrieves the description of the random variable.

Returns:

description of random variable

Return type:

str

get_type()#

Retrieves the type of the random variable.

Returns:

type of random variable

Return type:

flx_rv_type

get_value()#

Retrieves the value currently assigned to the random variable.

Returns:

value of the random variable

Return type:

float

pdf(x_val, safeCalc=true)#

Evaluate the probability density function (PDF) of the random variable at x_val.

Parameters:
  • x_val (float) – The value at which to evaluate the PDF.

  • safeCalc (bool) – see safeCalc

Returns:

The value of the PDF at x_val.

Return type:

float

pdf_array(x_vec, safeCalc=true)#

Evaluate the probability density function (PDF) of the random variable for input vector x_vec.

Parameters:
Returns:

The vector of PDF values associated with x_vec.

Return type:

numpy.ndarray

pdf_log(x_val)#

Evaluate the natural logarithm of the probability density function (PDF) of the random variable at x_val.

Parameters:
  • x_val (float) – The value at which to evaluate the natural logarithm of the PDF.

  • safeCalc (bool) – see safeCalc

Returns:

The value of the natural logarithm of the PDF at x_val

Return type:

float

cdf(x_val, safeCalc=true)#

Evaluate the cumulative distribution function (CDF) of the random variable at x_val.

Parameters:
  • x_val (float) – The value at which to evaluate the CDF.

  • safeCalc (bool) – see safeCalc

Returns:

The value of the CDF at x_val.

Return type:

flx_pr

cdf_array(x_vec, safeCalc=true)#

Evaluate the cumulative distribution function (CDF) of the random variable for input vector x_vec.

Parameters:
Returns:

The vector of CDF values associated with x_vec.

Return type:

numpy.ndarray

sf(x_val, safeCalc=true)#

Evaluate the survival function of the random variable at x_val, which is defined as 1.-CDf(x_val).

Parameters:
  • x_val (float) – The value at which to evaluate the survival function.

  • safeCalc (bool) – see safeCalc

Returns:

The value of the survival function at x_val

Return type:

flx_pr

sf_array(x_vec, safeCalc=true)#

Evaluate the survival function of the random variable for input vector x_vec.

Parameters:
  • x_vec (numpy.ndarray) – The vector of values at which to evaluate the survival function.

  • safeCalc (bool) – see safeCalc

Returns:

The vector of CDF values associated with x_vec.

Return type:

numpy.ndarray

icdf(p)#

Evaluates the inverse of the CDF of the random variable for probability p.

Parameters:

p (flx_pr) – The probability at which to evaluate the CDF.

Returns:

The value at which CDF(value)=p

Return type:

float

icdf_array(p_vec, safeCalc=true)#

Evaluate the inverse of the CDF of the random variable for input vector x_vec.

Parameters:
Returns:

The vector of values at which CDF(values)=p_vec

Return type:

numpy.ndarray

x2y(x_val)#

Transform x_val from original space into standard Normal space.

Parameters:

x_val (float) – A value in the domain of the random variable.

Returns:

The equivalent of x_val in standard Normal space.

Return type:

float

y2x(y_val)#

Transform y_val from standard Normal space into original space.

Parameters:

x_val (float) – Realization of a standard Normal random variable.

Returns:

The equivalent of y_val in the space of the random variable at hand.

Return type:

float

sample()#

Returns a random realization of the random variable.

Return type:

float

sample_array(x_vec)#

Assigns random realizations to the values of x_vec.

Parameters:

x_vec (numpy.ndarray) – The array to which to assign realizations. Note that the array is modified!

Return type:

None

mean()#

Returns the mean value of the random variable.

Return type:

float

sd()#

Returns the standard deviation of the random variable.

Return type:

float

median()#

Returns the median of the random variable.

Return type:

float

mode()#

Returns the mode of the random variable.

Return type:

float

entropy()#

Returns the entropy of the random variable.

Return type:

float

check_x(x_val)#

Checks if x_val is within the valid domain of the random variable at hand.

Parameters:

x_val (float) – The value to check.

Returns:

true, if x_val is within the valid domain and false otherwise.

Return type:

bool

get_HPD(p)#

Returns the lower quantile value of the HPD (highest probability density) interval of the distribution.

Parameters:

p (flx_pr) – The probability that the HPD interval should enclose.

Return type:

float

info()#

Returns information about the random variable at hand.

Return type:

dict

type flx_rv_config#
Syntax:

CONFIG

Description:

The configuration used to initialize a univariate random variable, where CONFIG is of type dict.

The following keys are allowed independent of the type of the random variable:
  • type (flx_rv_type): The type of the random variable (required).

  • name (rvID): The name to assign to the random variable. If the random variable is defined through flx.rv, specifying a name is optional.

  • eval_once (bool, default: False): Setting eval_once to true can result in a considerable performance gain. However, if the parameters of the probability distribution are functions of other random variables, eval_once must not be set to true.

  • descr (str, default:””): A arbitrary description of the random variable.

Additionally, depending on the specified type of the random variable, other keys can be required for definition.

type flx_gen_rv_config#
Syntax:

CONFIG

Description:

The configuration used to initialize a general random variable, where CONFIG is of type dict.

This type is a generalized version of flx_rv_config. The syntax is equivalent to flx_rv_config; the difference is that additionally a random variable of type Fun » function of random variables can be created.

property safeCalc: bool#

safeCalc is a parameter of some methods of flx.rv.

If set to true, it is ensured that x_val is within the valid domain of the random variable before evaluating the PDF. Setting this to false implicitly assumes that x_val is within the valid domain and can result in a performance gain (at the cost of stability if the value is outside of the valid domain).

type rvID#
Syntax:

Word

Description:

This data-type assigns a unique identifier (of type Word) to a random variable (of type flx.rv).

type flx_pr#
Syntax:

VALUE

Description:

A probability that can take a value on the interval \([0,1]\).

Example#

Let’s set up the engine of Fesslix:

import fesslix as flx
flx.load_engine()
Random Number Generator: MT19937 - initialized with rand()=1236206475;
Random Number Generator: MT19937 - initialized with 1000 initial calls.
0

A standard Normal random variable does not expect any additional parameters:

rv_0 = flx.rv({'type':'stdn'})
print( rv_0.info() )
print("  x     cdf       sf")
for x in [ -3., -1.5, 0., 1.5, 3. ]:
    print( f"{x:4.1f}  {rv_0.cdf(x):.2e}  {rv_0.sf(x):.2e}" )
{'type': 'stdn', 'name': 'name_unspecified', 'mean': 0.0, 'sd': 1.0, 'entropy': 1.4189385332046727}
  x     cdf       sf
-3.0  1.35e-03  9.99e-01
-1.5  6.68e-02  9.33e-01
 0.0  5.00e-01  5.00e-01
 1.5  9.33e-01  6.68e-02
 3.0  9.99e-01  1.35e-03

A Normal random variable requires additional parameters:

rv_1 = flx.rv({'type':'normal', 'mu':4, 'sd':1.5})
print( rv_1.info() )
print("  x     cdf       sf")
for x in [ -3., -1.5, 0., 1.5, 3. ]:
    print( f"{x:4.1f}  {rv_1.cdf(x):.2e}  {rv_1.sf(x):.2e}" )
{'type': 'normal', 'name': 'name_unspecified', 'mean': 4.0, 'sd': 1.5, 'entropy': 1.824403641312837}
  x     cdf       sf
-3.0  1.53e-06  1.00e+00
-1.5  1.23e-04  1.00e+00
 0.0  3.83e-03  9.96e-01
 1.5  4.78e-02  9.52e-01
 3.0  2.52e-01  7.48e-01

Types of random variables#

type flx_rv_type#
Syntax:

TYPE

Description:

Specifies the type of a random variable.

The following values/types for random variables can be used:

For configurations accepting type flx_gen_rv_config, additionally, the following values/types are accepted: