A class to manage a random variable.
tokenmatrix=randvar_tokens() nb=randvar_size()
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.
See below for the details of a
and b
.
"Normale" with a=0 and b=1
"Uniforme" with a=0 and b=1
"Exponentielle" with a=1
"LogNormale" with a=0 and b=1
"LogUniforme" with a=0 and b=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 scale parameter a (i.e. the mean of the random variable is 1/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" where X is a normal random variable with mean a and standard deviation b.
"Uniforme" where X i uniform in [a,b].
"LogNormale" where log(X) is a normal random variable with mean a and standard deviation b.
"LogUniforme" where log(X) is uniform in [a,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 this section, we present the mean M
and variance V
of the random variables available in the randvar
class.
We also present the suppport of the random variables, i.e. the interval which contains
the outcomes.
The support is (-%inf,+%inf)
.
M=a V=b^2
The support is [a,b]
.
M=(a+b)/2 V=(b-a)^2/12
The support is [0,+%inf)
.
M=1/a V=1/a^2
The support is [0,+%inf)
.
The mean and standard deviation of log(X)
are a
and b
.
M=exp(a+b^2/2) V=(exp(b^2)-1)*exp(2*a+b^2)
M
and variance
V
, then:
a=log(E)-0.5*log(1+V/E^2) b=sqrt(log(1+V/E^2))
The support is [exp(a),exp(b)]
.
M=(exp(b)-exp(a))/(b-a) V=0.5*(exp(b)^2-exp(a)^2)/(b-a)-M
A
and maximum
B
, then:
a=log(A) b=log(B)
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=%f (exact=%f)\n", mymean, mu); mprintf("Std. dev.=%f (exact=%f)\n", mysigma, sigma); mprintf("Var.=%f (exact=%f)\n", myvariance,sigma^2); randvar_destroy(rv); scf(); histplot(50,values) xtitle("Histogram of X","X","P(x)") | ![]() | ![]() |
The previous script produces the following output.
randvar_getlog
In the following session, we use the randvar_getlog
function
and print a Normal variable in the console.
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); a = 1.0; b = 0.5; rv=randvar_new("LogNormale",a,b); nbshots = 1000; values = zeros(nbshots); for i=1:nbshots values(i)=randvar_getvalue(rv); end // Check the mean of log(X) mymean = mean (log(values)) expected = a // Check the variance of log(X) myvariance = variance (log(values)) expected = b^2 randvar_destroy(rv); // Draw the histogram scf(); histplot(50,values); xtitle("LogNormal","X","P(x)"); // Compare with PDF x = linspace(0,10,100); p = distfun_lognpdf(x,a,b); plot(x,p,"b-"); legend(["Empirical" "PDF"]); | ![]() | ![]() |
The previous script produces the following output.
In the following example, we create a Log-Uniform variable. We compute the parameters from the parameters of the underlying Uniform variable.
nisp_initseed(0); a = 4; b = 6; rv=randvar_new("LogUniforme",a,b); nbshots = 1000; values = zeros(nbshots); for i=1:nbshots values(i)=randvar_getvalue(rv); end // Check the mean of log(X) mymean = mean (log(values)) expected = (a+b)/2 // Check the variance of log(X) myvariance = variance (log(values)) expected = (b-a)^2/12 randvar_destroy(rv); // Draw the histogram scf(); histplot(20,values); xtitle("LogUniform","X","P(x)"); x=linspace(exp(a),exp(b),100); p=distfun_logupdf(x,a,b); plot(x,p,"b-"); legend(["Empirical" "PDF"]); | ![]() | ![]() |
The previous script produces the following output.
nisp_initseed(0); // Define a Exponential variable. scale = 12.0; rv=randvar_new("Exponentielle",scale); // 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 = 1/scale // Check the variance myvariance = variance (values) expected = 1/scale^2 randvar_destroy(rv); // Draw the histogram scf(); histplot(20,values); xtitle("Histogram of X","X","P(x)"); // Compare with PDF x = linspace(0,0.5,100); p = distfun_exppdf(x,1/scale); plot(x,p,"b-"); legend(["Empirical" "PDF"]); | ![]() | ![]() |
The previous script produces the following output.
Copyright (C) 2012-2013 - Michael Baudin
Copyright (C) 2008-2011 - CEA - Jean-Marc Martinez
Copyright (C) 2008-2011 - INRIA - Michael Baudin