Professional Documents
Culture Documents
tests
Note that the sequences generated by a
hyper cube.
Generalizing Linear Congruential Generator
we have
A specific parameter set called MT19937
produced a period of
MT19937 passed all diehard tests
It is a high quality pseudo-random number
generator.
The importance of random seed
13
New MATLAB Function
• Ex of syntax:
– A = rand(n)
– A = rand(m,n)
• Where m and n are dimensions of the matrix
Rand() con’t: 2
å xi
• Mean: m= i= 1
n
– mean(x)
å ( xi - m) 2
• Variance: s2= i= 1
– mean((x-mean(x).*(x-mean(x)))
n
å ( xi - m) 2
• Standard Deviation s = i= 1
n
– std(x)
Correlation Coefficient
s XY
• Correlation coefficient r=
s Xs Y
function r = corco(x,y)
mx = mean(x);
my = mean(y);
covxy = mean((x-mx).*(y-my));
r = covxy/(std(x)*std(y));
X N (0,1) x=normrnd(0,1,1,100);
y=2*x+1;
Y aX b r=corrcoef(x.',y.')
1 plot(x,y,'+')
N N (0,1) n=normrnd(0,1,1,100);
y=2*x+1+n;
Y aX b N r=corrcoef(x.',y.')
0.9186 plot(x,y,'+')
Joint Gaussian
1 ( x m1 )2 ( y m2 )2 2 ( x m1 )( y m2 )
1 2
2(1 ) 1 2
2 2 1 2
f X ,Y ( x, y ) e
2 1 2 (1 )
2
0
Joint Gaussian
0.5
Joint Gaussian
0.9
Joint Gaussian
0.9
Generating Random Numbers
Uniform
ìï 0 x< a
ïï
ì 1 ïï x - a
f ( x) = ïí a£ x£ b F ( x) = í a£ x£ b
ïïî b - a ïï b - a
ïï
ïî 1 b< x
1. Generate U : U (0,1)
2. Return X = a + (b - a)U
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+((maxx-
minx)/NumBins)*(k-1/2);
end
h=h/sum(h);
bar(bincenters,h)
ìï 1- e- l x x³ 0
f ( x) = { l e - lx
x³ 0 F ( x) = ïí
ïïî 0 x< 0
1. Generate U : U (0,1)
2. Return 1
X = - ln(1- U )
l
Exponential
l =5
function genexp(N,lambda)
u=rand(1,N);
x=-1/lambda.*log(1-u);
( x- m)2 x ( x- m)2
1 - 1 -
f ( x) =
2ps
e 2s 2
- ¥ < x< ¥ F ( x) = ò 2ps
e 2s 2
- ¥ < x< ¥
- ¥
Rayleigh
ìï x2
x -
x2 ïï 1- e- 2s 2
x³ 0
f ( x) = e 2s 2
, x³ 0 F ( x) = í
s 2 ïï
ïî 0 x< 0
1. Generate U1 : U (0,1)
æ 1 ö
÷
2. Set Z= 2s ln çç
2
÷
÷
çè1- U ÷
1ø
3. Generate U 2 : U (0,1)
4. Return X 1 = m+ Z cos(2pU 2 ) X 2 = m+ Z sin(2pU 2 )
Normal
m = 10 s 2 = 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
n = 20 p = 0.5
function genbino(N,n,p)
for i=1:N,
u=rand(1,n);
y=(u<=p);
x(i)=sum(y);
end
ìï 1- (1- p) k k³ 1
p(k ) = (1- p ) k- 1
p k = 1, 2,... F (k ) = ïí
ïïî 0 k<1
1. Generate U : U (0,1)
éln(1- U ) ù
2. Return X= ê ú
êln(1- p) ú
ê ú
Geometric
p = 0.5
function gengeo(N,p)
u=rand(1,N);
x=ceil(log(1-u)/log(1-p));
ìï 0 k< 0
k
l -l ïï
p(k ) = e , k = 0,1,... F (k ) = í k l i - l
k! ïï å e k³ 0
ïïî i= 0 i !
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
…...
Example of a MATLAB script
………
…
% now do the calculation using vectorization
fprintf('Vectorization calculations.....\n');
• Function rand.m
This generator can generate all the floating point numbers in the
closed interval [2^(-53), 1-2^(-53)]. Theoretically, it can generate
over 2^1492 values before repeating itself.
Example of a MATLAB function
function [sum, difference] = sumdiff(a, b);
List of
input argument values,
Name of the comma delimited
function (any form of array
List of output data type)
Tells MATLAB this values returned
is a function (can be any form of
array data type)
Example of a MATLAB function
function [sum, difference] = sumdiff(a, b);
% function [sum, difference] = sumdiff(a, b);
%
% A simple function to compute the sum and difference
% of two input arguments a and b
%
% Professor Smyth, Oct 2007
%
% INPUTS:
% a: array of size r x c
% b: array of size r x c
%
% OUTPUTS:
% sum: a + b
% difference: a - b
% error checking
[rowsa, colsa] = size(a);
[rowsb, colsb] = size(b);
if ( rowsa ~= rowsb ) | ( colsa ~= colsb)
error(‘sizes of a and b do not match’);
end
• Input variables
– variables within function are local to the function
– input variables are readable to the function, but not writable
– values of returned arguments are passed back
• Search path
– MATLAB searches in this order:
• variable name, subfunction, current directory, MATLAB search path
Example of a MATLAB function
function [meanr, stdr, z] = simulate(n);
List of
input argument values,
Name of the comma delimited
function (any form of array
List of output data type)
Tells MATLAB this values returned
is a function (can be any form of
array data type)
Example of a MATLAB function
function [meanr, stdr, z] = simulate(n);
% function [meanr, stdr, z] = simulate(n);
%
% A simple function to simulate a vector of n
% uniformly distributed random numbers and return the
% mean and standard deviation of the numbers.
%
% Professor Smyth, Oct 2007
%
% INPUTS:
% n: number of random numbers generated (positive integer)
%
% OUTPUTS:
% meanr: mean of the n random numbers
% stdr: standard deviation of the random numbers
% z: an n x 1 array of random numbers
Example of a MATLAB function
function [meanr, stdr, z] = simulate(n);
% function [meanr, stdr, z] = simulate(n);
%
% A simple function to simulate a vector of n
% uniformly distributed random numbers and return the
% mean and standard deviation of the numbers.
%
% Professor Smyth, Oct 2007
%
% INPUTS:
% n: number of random numbers generated (positive integer)
%
% OUTPUTS:
% meanr: mean of the n random numbers
% stdr: standard deviation of the random numbers
% z: an n x 1 array of random numbers
»m
m = 0.4997
»s
s = 0.2885
Another MATLAB function
function [meanr, stdr, z] = simplot(n,plotflag);
% function [meanr, stdr, z] = simplot(n,plotflag);
%
% A simple function to simulate a vector of n
% uniformly distributed random numbers and return the
% mean and standard deviation of the numbers. If
% plotflag is 1 a histogram of the numbers is plotted
%
% Professor Smyth, Oct 2007
%
% INPUTS:
% n: number of random numbers generated (positive integer)
% plotflag: if plotflag=1, a histogram of z is plotted,
% otherwise no plotting
%
% OUTPUTS:
% meanr: mean of the n random numbers
% stdr: standard deviation of the random numbers
% z: an n x 1 array of random numbers
figure % figure to see visually how the sample mean converges to 0.5
cs = cumsum(z); % generate a vector of cumulative sums
nvalues = 1:n; % generate a vector of sample sizes
runningmean = cs./nvalues; % there’s an error here (on purpose)
plot(nvaluess,runningmean); % and another error here
grid;
axis([1 n 0 1]);
xlabel('Number of random numbers generated');
ylabel('Mean value');
title('Convergence of sample mean to true mean');
end
Code that was added to simplot.m
figure % figure to see visually how the sample mean converges to 0.5
cs = cumsum(z); % generate a vector of cumulative sums
nvalues = 1:n; % generate a vector of sample sizes
runningmean = cs./nvalues; % there’s an error here (on purpose)
plot(nvaluess,runningmean); % and another error here
%correct code:
%runningmean = cs./nvalues';
%plot(nvalues, runningmean);
grid;
axis([1 n 0 1]);
xlabel('Number of random numbers generated');
ylabel('Mean value');
title('Convergence of sample mean to true mean');
end
Pseudo-random signal generation:
"rand" generates uniform PDF random numbers between 0 & 1.
Subtract 0.5 from each sample for zero mean & variance = 1/12.
To produce array of 100 samples of uniform PDF process with zero
mean & unit variance:.
x = (rand(100) - 0.5) * SQRT(12);