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:
- get_descr()#
Retrieves the description of the random variable.
- Returns:
description of random variable
- Return type:
- get_type()#
Retrieves the type of the random variable.
- Returns:
type of random variable
- Return type:
- get_value()#
Retrieves the value currently assigned to the random variable.
- Returns:
value of the random variable
- Return type:
- pdf(x_val, safeCalc=true)#
Evaluate the probability density function (PDF) of the random variable at x_val.
- pdf_array(x_vec, safeCalc=true)#
Evaluate the probability density function (PDF) of the random variable for input vector x_vec.
- Parameters:
x_vec (numpy.ndarray) – The vector of values at which to evaluate the PDF.
- Returns:
The vector of PDF values associated with x_vec.
- Return type:
- pdf_log(x_val)#
Evaluate the natural logarithm of the probability density function (PDF) of the random variable at x_val.
- cdf(x_val, safeCalc=true)#
Evaluate the cumulative distribution function (CDF) of the random variable at x_val.
- cdf_array(x_vec, safeCalc=true)#
Evaluate the cumulative distribution function (CDF) of the random variable for input vector x_vec.
- Parameters:
x_vec (numpy.ndarray) – The vector of values at which to evaluate the CDF.
- Returns:
The vector of CDF values associated with x_vec.
- Return type:
- sf(x_val, safeCalc=true)#
Evaluate the survival function of the random variable at x_val, which is defined as
1.-CDf(x_val).
- 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.
- Returns:
The vector of CDF values associated with x_vec.
- Return type:
- icdf(p)#
Evaluates the inverse of the CDF of the random variable for probability p.
- icdf_array(p_vec, safeCalc=true)#
Evaluate the inverse of the CDF of the random variable for input vector x_vec.
- Parameters:
p_vec (numpy.ndarray) – The vector of probabilities at which to evaluate the CDF.
- Returns:
The vector of values at which CDF(values)=p_vec
- Return type:
- x2y(x_val)#
Transform x_val from original space into standard Normal space.
- y2x(y_val)#
Transform y_val from standard Normal space into original space.
- 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
- check_x(x_val)#
Checks if x_val is within the valid domain of the random variable at hand.
- get_HPD(p)#
Returns the lower quantile value of the HPD (highest probability density) interval of the distribution.
- 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 throughflx.rv, specifying a name is optional.eval_once(bool, default: False): Settingeval_onceto true can result in a considerable performance gain. However, if the parameters of the probability distribution are functions of other random variables,eval_oncemust not be set to true.descr(str, default:””): A arbitrary description of the random variable.
Additionally, depending on the specified
typeof 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 toflx_rv_config; the difference is that additionally a random variable of type Fun » function of random variables can be created.
- property safeCalc: bool#
safeCalcis a parameter of some methods offlx.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#
- 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:
stdn» Standard Normal distributionnormal» Normal distributionlogn» Log-normal distributionuniform» Uniform distributionbeta» Beta distributionGumbel» Gumbel distributionexponential» Exponential distributionstudentst» Student’s t-distributionstudentstgen» generalized Student’s t-distributionlogt» Log-t distributiongenpareto» generalized Pareto distributionquantiles» quantile-based Distribution
For configurations accepting type
flx_gen_rv_config, additionally, the following values/types are accepted:fun» Fun » function of random variables (deterministic mapping conditional on other random variables)