<< Overview NISP setrandvar >>

NISP >> NISP > randvar

randvar

A class to manage a random variable.

Static Functions

tokenmatrix=randvar_tokens()
nb=randvar_size()

Creation/Destruction

rv=randvar_new(name,a,b)
rv=randvar_new(name,a)
rv=randvar_new(name)
randvar_destroy(rv)

Methods

value=randvar_getvalue(rv)
value=randvar_getvalue(rv,rv2,value2)
randvar_getlog(rv)

Description

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.

Static Functions

tokenmatrix=randvar_tokens()

Returns a 1-by-n matrix of floating point integers representing the current randvar tokens, where n is the number of tokens.

n=randvar_size()

Returns a 1-by-1 matrix containing the current number of randvar tokens currently in use, where n is the number of tokens.

Creation/Destruction

rv=randvar_new(name)

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

rv=randvar_new(name,a)

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).

rv=randvar_new(name,a,b)

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].

randvar_destroy(rv)

Destroys the current random variable.

Methods

value = randvar getvalue(rv)

Returns a random value from the distribution function of the current random variable.

value = randvar getvalue(rv,rv2,value2)

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.

randvar getlog(rv)

Prints a log for the current random variable.

Distributions

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.

"Normale"

The support is (-%inf,+%inf).

              M=a
              V=b^2
            

"Uniforme"

The support is [a,b].

              M=(a+b)/2
              V=(b-a)^2/12
            

"Exponentielle"

The support is [0,+%inf).

              M=1/a
              V=1/a^2
            

"LogNormale"

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)
            
If X is a LogNormale random variable with mean M and variance V, then:
              a=log(E)-0.5*log(1+V/E^2)
              b=sqrt(log(1+V/E^2))
            

"LogUniforme"

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
            
If X is a LogUniforme random variable with minimum A and maximum B, then:
              a=log(A)
              b=log(B)
            

Examples

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.

Example of 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
      ***********************************************
    

Example of Log-Normal variable.

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.

Example of Log-Uniform variable.

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.

Example of Exponential variable.

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.

Authors

Copyright (C) 2012-2013 - Michael Baudin

Copyright (C) 2008-2011 - CEA - Jean-Marc Martinez

Copyright (C) 2008-2011 - INRIA - Michael Baudin


Report an issue
<< Overview NISP setrandvar >>