0% found this document useful (0 votes)
32 views

03 Random Numbers

The document describes various methods for generating random numbers from different probability distributions including uniform, exponential, normal, binomial, geometric, and Poisson distributions. For each distribution, it provides the probability density/mass function, cumulative distribution function, and a MATLAB code example to generate random numbers from that distribution. It also discusses how to generate correlated random numbers from a joint Gaussian distribution with different correlation coefficients.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

03 Random Numbers

The document describes various methods for generating random numbers from different probability distributions including uniform, exponential, normal, binomial, geometric, and Poisson distributions. For each distribution, it provides the probability density/mass function, cumulative distribution function, and a MATLAB code example to generate random numbers from that distribution. It also discusses how to generate correlated random numbers from a joint Gaussian distribution with different correlation coefficients.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Generating Random Numbers

Mean, Variance, Standard Deviation


n

Mean:

m=

mean(x)

xi

i =1

Variance:

s2 =

i=1

mean((x-mean(x).*(x-mean(x)))
n

Standard Deviation
std(x)

s=

( xi - m) 2

( xi - m) 2

i=1

Correlation Coefficient
Correlation coefficient
function r = corco(x,y)
mx = mean(x);
my = mean(y);
covxy = mean((x-mx).*(y-my));
r = covxy/(std(x)*std(y));

Or use Matlab function


corrcoef

r=

s XY
s X sY

Random Numbers
rand(M,N):

MxN matrix of uniformly distributed


random numbers on (0,1)

randn(M,N)

MxN matrix of normally distributed


random numbers (=0, 2=1)

normrnd(m,s,M,N) MxN matrix of normally distributed


random numbers (=m, =s)
x=randn(1,10000);
mean((x<=1))
ans =
0.8449

F (1) = 0.8413

Correlation Coefficient
X : N (0,1)
Y aX b
1

x=normrnd(0,1,1,100);
y=2*x+1;
r=corrcoef(x.',y.')
plot(x,y,'+')

N : N (0,1)
Y aX b N
0.9186

n=normrnd(0,1,1,100);
y=2*x+1+n;
r=corrcoef(x.',y.')
plot(x,y,'+')

Joint Gaussian
f X ,Y ( x, y )

1
2 1 2 (1 )
2

( x m1 ) 2 ( y m2 ) 2 2 ( x m1 )( y m2 )
1

2
2
2
1 2
2(1 ) 1
2

Joint Gaussian

0.5

Joint Gaussian

0.9

Joint Gaussian

0.9

Generating Random Numbers


Uniform
1
f ( x) =
b - a

1. Generate
2. Return

a x b

U : U (0,1)
X = a + (b - a )U

x - a
F ( x) =
b - a

x <a
a x b
b<x

Uniform
a = 10 b = 15
function genuni(N,a,b)
u=rand(1,N);
x=a+(b-a).*u;
minx=min(x);
maxx=max(x);
NumBins=51;
h=hist(x,NumBins);
for k=1:NumBins,
bincenters(k)=minx+((maxxminx)/NumBins)*(k-1/2);
end
h=h/sum(h);
bar(bincenters,h)

Or use Matlab function unifrnd(a,b,M,N)

Generating Random Numbers


Exponential

f ( x) = { l e

-lx

1. Generate
2. Return

x 0

U : U (0,1)
1
X =- ln(1- U )
l

1- e- l x
F ( x) =
0

x 0
x <0

Exponential
l =5
function genexp(N,lambda)
u=rand(1,N);
x=-1/lambda.*log(1-u);

Or use Matlab function exprnd(lambda,M,N)

Generating Random Numbers


Normal
f ( x) =

1
e
2ps

( x- m) 2
2s 2

F ( x) =

- < x <

Rayleigh
f ( x) =

1.
2.
3.
4.

x
e
2
s

x2
2s 2

Generate
Set
Generate
Return

1
e
2ps

x2

1- e 2s 2
F ( x) =

x 0

( x- m)2
2s 2

x 0
x <0

U1 : U (0,1)

Z = 2s ln

1- U1

U 2 : U (0,1)
2

X 1 = m+ Z cos(2pU 2 )

X 2 = m+ Z sin(2pU 2 )

- < x <

Normal
m= 10 s 2 = 4
function gennormal(N,mu,sigma)
for i=1:N
u=rand; z=sigma*(sqrt(2*log(1/(1u))));
u=rand;
x1(i)=mu+z*cos(2*pi*u);
x2(i)=mu+z*sin(2*pi*u);
end

Or use Matlab function normrnd(mu,sigma,M,N)

Generating Random Numbers


Binomial

n k
n- k

p(k ) =
p
(1
p
)

1. Generate
2. Return

k = 0,1,..., n

Y1 , Y2 ,..., Yn

k
n i
n- i

F (k ) =
p
(1
p
)

i=0
i

IID Bernoulli(p) random numbers

X = Y1 +Y2 +... +Yn

k <0
0 k n
k >n

Binomial
n = 20
function genbino(N,n,p)
for i=1:N,
u=rand(1,n);
y=(u<=p);
x(i)=sum(y);
end

Or use Matlab function binornd(n,p,M,N)

p = 0.5

Generating Random Numbers


Geometric

p(k ) = (1- p )

k- 1

1. Generate
2. Return

k =1, 2,...

U : U (0,1)
ln(1- U )

X =

ln(1- p )

1- (1- p) k
F (k ) =
0

k 1
k <1

Geometric
p = 0.5
function gengeo(N,p)
u=rand(1,N);
x=ceil(log(1-u)/log(1-p));

Or use Matlab function geornd(p,M,N)

Generating Random Numbers


Poisson
k

p(k ) =

l -l
e ,
k!

k = 0,1,...

F (k ) = k l i - l

e
i=0 i !

k <0
k 0

1. Set
k = 0, P = 1
2. Generate U k +1 : U (0,1) and replace P by P U k +1
3. If P < e- l accept X = k else increase k by one and return to
step 2

Poisson
l =5
function genpois(N,lambda)
for i=1:N,
k=0;p=1;
u=rand;
p=p*u;
while p>=exp(-lambda)
u=rand;
p=p*u;
k=k+1;
end
x(i)=k;
end

Or use Matlab function poissrnd(lambda,M,N)

You might also like