Telecom.tuc.gr - Telecoms TUC main

Download Report

Transcript Telecom.tuc.gr - Telecoms TUC main

Generating Random Numbers
Mean, Variance, Standard Deviation
n
• Mean:
е
m=
– mean(x)
xi
i= 1
n
n
• Variance:
s2=
е
( xi - m) 2
i= 1
n
– mean((x-mean(x).*(x-mean(x)))
n
• Standard Deviation
– std(x)
е
s =
( xi - m) 2
i= 1
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
r=
s XY
s Xs 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
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 ) 
 0
 ( x  m1 )2 ( y  m2 )2 2  ( x  m1 )( y  m2 ) 




2 
2
2
 1 2
2(1  )   1
2

1
1
2 1 2 (1   )
2
e
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
м
0
п
п
п
п
x- a
F ( x) = п
н
п
b- a
п
п
п
п
о1
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
function genexp(N,lambda)
u=rand(1,N);
x=-1/lambda.*log(1-u);
Or use Matlab function exprnd(lambda,M,N)
l =5
Generating Random Numbers
Normal
f ( x) =
( x- m)2
1
e
2ps
-
2s 2
x
F ( x) =
- Ґ < x< Ґ
- Ґ
Rayleigh
x f ( x) = 2 e
s
1.
2.
3.
4.
м
п
п
п
F ( x) = н1- e
п
п
п
о0
x2
2s 2
т
,
Generate
Set
Generate
Return
xі 0
U1 : U (0,1)
Z=
1
e
2ps
ж 1
2s ln зз
зи1- U
2
-
( x- m)2
2s 2
x2
2s 2
xі 0
x< 0
ц
ч
ч
ч
ч
1ш
U 2 : U (0,1)
X1 = m+ Z cos(2pU 2 )
X 2 = m+ Z sin(2pU 2 )
- Ґ < x< Ґ
Normal
function gennormal(N,mu,sigma)
m = 10 s 2 = 4
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
)
ч
зиk ш
ч
1. Generate
2. Return
k = 0,1,..., n
Y1 , Y2 ,..., Yn
м
0
п
п
п
k
п
nц
п ж
i
n- i
з
ч
F (k ) = не з ч
p
(1
p
)
ч
ч
з
п
i
и
ш
i
=
0
п
п
п
п
п
о1
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
p
k = 1, 2,...
U : U (0,1)
йln(1- U ) щ
ъ
X= к
кln(1- p) ъ
к
ъ
k
м
п
1
(1
p
)
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,...
м
0
п
п
п
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
function genpois(N,lambda)
l =5
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)