1 / 21

Generating Random Numbers

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));

mobando
Download Presentation

Generating Random Numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generating Random Numbers

  2. Mean, Variance, Standard Deviation • Mean: • mean(x) • Variance: • mean((x-mean(x).*(x-mean(x))) • Standard Deviation • std(x)

  3. 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

  4. 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

  5. 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

  6. Joint Gaussian

  7. Joint Gaussian

  8. Joint Gaussian

  9. Joint Gaussian

  10. Generating Random Numbers Uniform • Generate • Return

  11. 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)

  12. Generating Random Numbers Exponential • Generate • Return

  13. Exponential function genexp(N,lambda) u=rand(1,N); x=-1/lambda.*log(1-u); Or use Matlab function exprnd(lambda,M,N)

  14. Generating Random Numbers Normal • Generate • Set • Generate • Return Rayleigh

  15. 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)

  16. Generating Random Numbers Binomial • Generate IID Bernoulli(p) random numbers • Return

  17. 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)

  18. Generating Random Numbers Geometric • Generate • Return

  19. 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)

  20. Generating Random Numbers Poisson • Set • Generate and replace by • If accept else increase by one and return to step 2

  21. 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)

More Related