Generating Random Numbers. Mean, Variance, Standard Deviation Mean: –mean(x) Variance:...
-
date post
21-Dec-2015 -
Category
Documents
-
view
267 -
download
1
Transcript of Generating Random Numbers. Mean, Variance, Standard Deviation Mean: –mean(x) Variance:...
Mean, Variance, Standard Deviation
1
n
ii
x
nm ==
å• Mean:
– mean(x)
• Variance:– mean((x-mean(x).*(x-mean(x)))
• Standard Deviation– std(x)
2
2 1
( )n
ii
x
n
ms =
-=å
2
1
( )n
ii
x
n
ms =
-=å
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
sr
s s=
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.8413F =
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 21 2 1 2
2 2 21 21 2
( ) ( ) 2 ( )( )1
2(1 ), 2
1 2
1( , )
2 (1 )
x m y m x m y m
X Yf x y e
0
Generating Random Numbers
1. Generate
2. Return
Uniform
1( )f x a x b
b a
ìïï= £ £íï -ïî
0
( )
1
x a
x aF x a x b
b ab x
ì <ïïïï -ïï= £ £íï -ïïï <ïïî
(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 15a b= =
Or use Matlab function unifrnd(a,b,M,N)
Generating Random Numbers
1. Generate
2. Return
{( ) 0xf x e xll -= ³1 0
( )0 0
xe xF x
x
l-ìï - ³ï=íï <ïî
(0,1)U U:1
ln(1 )X Ul
=- -
Exponential
Exponential
function genexp(N,lambda)
u=rand(1,N);
x=-1/lambda.*log(1-u);
5l =
Or use Matlab function exprnd(lambda,M,N)
Generating Random Numbers
2
2
( )
21
( )2
x
f x e xm
s
ps
--
= - ¥ < <¥
Normal
2
2
( )
21
( )2
x x
F x e xm
s
ps
--
- ¥
= - ¥ < <¥ò
2
222
( ) , 0xx
f x e xs
s
-= ³
2
221 0( )0 0
x
e xF xx
s-ìïïï - ³=íïï <ïî
1. Generate
2. Set
3. Generate
4. Return
1 (0,1)U U:2
1
12 ln
1Z
Us
æ ö÷ç ÷= ç ÷ç ÷ç -è ø2 (0,1)U U:
1 2 2 2cos(2 ) sin(2 )X Z U X Z Um p m p= + = +
Rayleigh
Normal
210 4m s= =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. Return1 2, ,..., nY Y Y
1 2 ... nX Y Y Y= + + +
( ) (1 ) 0,1,...,k n knp k p p k n
k-
æö÷ç ÷= - =ç ÷ç ÷çè ø 0
0 0
( ) (1 ) 0
1
ki n i
i
k
nF k p p k n
i
k n
-
=
ì <ïïïï æöïï ÷ç ÷= - £ £í ç ÷ç ÷çï è øïïï >ïïî
å
Binomial
20 0.5n 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,...kp k p p k-= - =1 (1 ) 1
( )0 1
kp kF k
k
ìï - - ³ï=íï <ïî
1. Generate
2. Return
(0,1)U U:ln(1 )
ln(1 )
UX
p
é ù-ê ú=ê ú-ê ú
Geometric
0.5p=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 kk
ll -= =
0
0 0
( )0
!
ik
i
k
F ke k
ill -
=
ì <ïïïï=íï ³ïïïîå
1. Set
2. Generate and replace by
3. If accept else increase by one and return to step 2
0, 1k P= =
1 (0,1)kU U+ : P 1kP U +×
P e l-< X k= k