0% found this document useful (0 votes)
35 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 mass or density function, the cumulative distribution function, and a MATLAB function or code to generate random numbers from that distribution. It also discusses concepts like mean, variance, and standard deviation.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 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 mass or density function, the cumulative distribution function, and a MATLAB function or code to generate random numbers from that distribution. It also discusses concepts like mean, variance, and standard deviation.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Generating Random Numbers

Mean, Variance, Standard Deviation


1
n
i
i
x
n

=
=

Mean:
mean(x)
Variance:
mean((x-mean(x)).*(x-mean(x)))
Standard Deviation
std(x)
2
2
1
( )
n
i
i
x
n

2
1
( )
n
i
i
x
n

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
XY
X Y


=
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
(1) 0.8413 =
Correlation Coefficient
(0,1) X N
(0,1) N N
= + Y aX b
= + + Y aX b N
1 =
0.9186 =
x=normrnd(0,1,1,100);
y=2*x+1;
r=corrcoef(x.',y.')
plot(x,y,'+')
n=normrnd(0,1,1,100);
y=2*x+1+n;
r=corrcoef(x.',y.')
plot(x,y,'+')
Joint Gaussian
2 2
1 2 1 2
2 2 2
1 2
1 2
( ) ( ) 2 ( )( ) 1
2(1 )
,
2
1 2
1
( , )
2 (1 )

x m y m x m y m
X Y
f x y e
0 =
Joint Gaussian
0.5 =
Joint Gaussian
0.9 =
Joint Gaussian
0.9 =
Generating Random Numbers
1. Generate
2. Return
Uniform
1
( ) f x a x b
b a
'
1
1
=
!
1

1+
0
( )
1
x a
x a
F x a x b
b a
b x
'
<
1
1
1
1

11
=
!
1

1
1
1
<
1
1+
(0,1) U U
( ) X a b a U = +
Uniform
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+((maxx-
minx)/NumBins)*(k-1/2);
end
h=h/sum(h);
bar(bincenters,h)
10 15 a b = =
Or use Matlab function unifrnd(a,b,M,N)
Generating Random Numbers
1. Generate
2. Return
{
( ) 0
x
f x e x


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

'
1
1
=
!
1
<
1+
(0,1) U U
1
ln(1 ) X U

=
Exponential
Exponential
function genexp(N,lambda)
u=rand(1,N);
x=-1/lambda.*log(1-u);
5 =
Or use Matlab function exprnd(lambda,M,N)
Generating Random Numbers
2
2
( )
2
1
( )
2
x
f x e x

= < <
Normal
2
2
( )
2
1
( )
2
x x
F x e x

= < <

2
2
2
2
( ) , 0
x
x
f x e x

=
2
2
2
1 0
( )
0 0
x
e x
F x
x

'
1
1
1

=
!
1
1
<
1
+
1. Generate
2. Set
3. Generate
4. Return
1
(0,1) U U
2
1
1
2 ln
1
Z
U



( )
2
(0,1) U U
1 2 2 2
cos(2 ) sin(2 ) X Z U X Z U = + = +
Rayleigh
Normal
2
10 4 = =
function gennormal(N,mu,sigma)
for i=1:N
u=rand;
z=sigma*(sqrt(2*log(1/(1-u))));
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
1. Generate IID Bernoulli(p) random numbers
2. Return
1 2
, ,...,
n
Y Y Y
1 2
...
n
X Y Y Y = + + +
( ) (1 ) 0,1,...,
k n k
n
p k p p k n
k

= =


( )
0
0 0
( ) (1 ) 0
1
k
i n i
i
k
n
F k p p k n
i
k n

=
'
<
1
1
1
1
1
1
1

=
!

1
( )
1
1
1
> 1
1+

Binomial
20 0.5 n p = =
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)
Generating Random Numbers
Geometric
1
( ) (1 ) 1, 2,...
k
p k p p k

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

1
=
!
1
<
1
+
1. Generate
2. Return
(0,1) U U
ln(1 )
ln(1 )
U
X
p
l

l
=
l

l
Geometric
0.5 p =
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
( ) , 0,1,...
!
k
p k e k
k


= =
0
0 0
( )
0
!
i k
i
k
F k
e k
i


=
'
<
1
1
1
1
=
!
1

1
1
1+

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

P
1 k
P U
+

P e

<
X k = k
Poisson
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