Generating Random Numbers Mean Variance Standard Deviation Mean
Generating Random Numbers
Mean, Variance, Standard Deviation • Mean: – mean(x) • Variance: – mean((x-mean(x). *(x-mean(x))) • Standard Deviation – std(x)
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
Random Numbers • rand(M, N): Mx. N matrix of uniformly distributed random numbers on (0, 1) • randn(M, N) Mx. N matrix of normally distributed random numbers (μ=0, σ2=1) • normrnd(m, s, M, N) Mx. N matrix of normally distributed random numbers (μ=m, σ=s) x=randn(1, 10000); mean((x<=1)) ans = 0. 8449
Correlation Coefficient 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
Joint Gaussian
Joint Gaussian
Joint Gaussian
Generating Random Numbers Uniform 1. Generate 2. Return
Uniform function genuni(N, a, b) u=rand(1, N); x=a+(b-a). *u; minx=min(x); maxx=max(x); Num. Bins=51; h=hist(x, Num. Bins); for k=1: Num. Bins, bincenters(k)=minx+((maxxminx)/Num. Bins)*(k-1/2); end h=h/sum(h); bar(bincenters, h) Or use Matlab function unifrnd(a, b, M, N)
Generating Random Numbers Exponential 1. Generate 2. Return
Exponential 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 Rayleigh 1. 2. 3. 4. Generate Set Generate Return
Normal function gennormal(N, mu, sigma) for i=1: N u=rand; z=sigma*(sqrt(2*log(1/(1 u)))); u=rand; x 1(i)=mu+z*cos(2*pi*u); x 2(i)=mu+z*sin(2*pi*u); end Or use Matlab function normrnd(mu, sigma, M, N)
Generating Random Numbers Binomial 1. Generate 2. Return IID Bernoulli(p) random numbers
Binomial 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. Generate 2. Return
Geometric 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 1. Set 2. Generate 3. If accept step 2 and replace by else increase by one and return to
Poisson 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)
- Slides: 21