randvar — A class to manage a random variable.
rv = randvar_new ( name , a , b ) rv = randvar_new ( name , a ) rv = randvar_new ( name ) randvar_destroy ( rv )
value = randvar_getvalue ( rv ) value = randvar_getvalue ( rv , rv2 , value2 ) randvar_getlog ( rv )
This class manages a random variable.
The randvar_new function allows to create a new random variable. The randvar_destroy function allows to destroy an existing random variable. The type of random variable is chosen at the creation of the random variable to the randvar_new function. Several random variable types are available, including Normal and Uniform variables (see below for a complete list). Once a random variable is created, the randvar_getvalue function allows to generate random numbers from the associated distribution function.
The randvar_new function returns a token which is a unique identifier for the new random variable. Indeed, when a new random variable is created, a counter is updated which corresponds to the returned token. This way, each token is unique and can correspond only to one single random variable.
The functions randvar_tokens and randvar_size allow to manage the random variables which have already been created. The function randvar_size returns the number of random variables, while the randvar_tokens function returns the list of current random variables.
Returns a 1-by-n matrix of floating point integers representing the current randvar tokens, where n is the number of tokens.
Returns a 1-by-1 matrix containing the current number of randvar tokens currently in use, where n is the number of tokens.
Returns a new random variable rv which corresponds to the distribution function given by name, with default parameters.
The following is the list of possible values for the name input argument.
"Normale" with mean=0 and standard deviation=1
"Uniforme" with min=0 and max=1
"Exponentielle" with lambda=1
Returns a new random variable rv with first parameter a.
The following is the list of possible values for the name input argument.
"Exponentielle" with lambda=a
Returns a new random variable rv with first parameter a and second parameter b.
The following is the list of possible values for the name input argument.
"Normale" with mean=a and standard deviation=b
"Uniforme" with min=a and max=b
"LogNormale" with mean=a but with b for the standard deviation of the natural logarithm of the variable. The natural logarithm of the variable is a normal variable with mean=ln(a)-b*b/2 and with standard deviation=b
"LogUniforme" with min=a and max=b. Natural logarithm of the random variable is an uniform variable between ln(a) and ln(b).
Destroys the current random variable.
Returns a random value from the distribution function of the current random variable.
Returns a random value from the distribution function of the random variable rv by transformation of value2 from the distribution function of random variable rv2.
Prints a log for the current random variable.
In the following example, we call the randvar_new function in order to create a Normal random variable with mean 1.0 and standard deviation 0.5. Then, we perform a loop so that we get 1000 values from this random variable. In order to check that these values are associated with a Normal distribution function, we compute the mean and variance and check that this corresponds to the expected results. Finally, the random variable is destroyed with the randvar_destroy function.
nisp_initseed ( 0 ); mu = 1.0; sigma = 0.5; rv = randvar_new("Normale" , mu , sigma); nbshots = 1000; values = zeros(nbshots); for i=1:nbshots values(i) = randvar_getvalue(rv); end mymean = mean (values); mysigma = st_deviation(values); myvariance = variance (values); mprintf("Mean is : %f (expected = %f)\n", mymean, mu); mprintf("Standard deviation is : %f (expected = %f)\n", mysigma, sigma); mprintf("Variance is : %f (expected = %f)\n", myvariance, sigma*sigma); randvar_destroy(rv); histplot(50,values) xtitle("Histogram of X","X","P(x)")
randvar_getlog
In the following session, we use the randvar_getlog
function
and print a Normal variable in the console.
mu = 1.0; sigma = 0.5; rv = randvar_new("Normale" , mu , sigma); randvar_getlog(rv); randvar_destroy(rv);
The previous script produces the following output.
*********************************************** Nisp(RandomVariable::GetLog) for RandomVariable Normale : 1 : 0.5 ***********************************************