210 likes | 228 Views
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));
E N D
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): 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
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,'+') Correlation Coefficient
Generating Random Numbers Uniform • Generate • Return
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) Or use Matlab function unifrnd(a,b,M,N)
Generating Random Numbers Exponential • Generate • 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 • Generate • Set • Generate • Return Rayleigh
Normal 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 • Generate IID Bernoulli(p) random numbers • Return
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 • Generate • 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 • Set • Generate and replace by • If accept else increase by one and return to step 2
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)