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.
Here are brief informations extracted from NISP User's Manual.
If $X$ is a random variable with a normal distribution, then $Y = exp(X)$ has a log-normal distribution.
If $X$ is a random variable with a uniform distribution, then $Y = exp(X)$ has a log-uniform distribution.
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" where a is the mean of the variable rv and b is the standard deviation of
the underlying normal random variable.
The underlying normal random variable has mean m = ln(a)-b^2/2
and variance b^2
.
The log-normal random variable has mean a
and variance (exp(b^2)-1)*exp(2*m+b^2)
,
where m
is the mean of the underlying normal variable.
"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); scf(); 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 ***********************************************
In the following example, we create a Log-Normal variable. We compute the parameters from the parameters of the underlying Normal variable.
nisp_initseed ( 0 ); // Define a Normal variable. mu = 1.0; sigma = 0.5; // Define the associated Log-Normal variable. a = exp(mu+0.5*sigma^2); b = sigma; rv = randvar_new("LogNormale" , a , b); // Perform Monte-Carlo simulations nbshots = 1000; values = zeros(nbshots); for i=1:nbshots values(i) = randvar_getvalue(rv); end // Check the mean mymean = mean (values) expected = a // Check the variance myvariance = variance (values) expected = (exp(sigma^2)-1)*exp(2*mu+sigma^2) randvar_destroy(rv); // Draw the histogram scf(); histplot(50,values); xtitle("Histogram of X","X","P(x)"); // Compare with PDF x = linspace(0,10,100); p = nisp_lognormalpdf ( x , mu*ones(1,100) , sigma*ones(1,100) ); plot ( x , p , "b-"); legend(["Empirical" "Theory"]);