21
Generating Random Numbers

03 Random Numbers

Embed Size (px)

DESCRIPTION

sadasfasfasjbc kjsabksabfjkabsf jkasbfjkasbjkfbajksbfjka ajkhsbfjksavfksavfjka jkasbfjkabsfjkbafa

Citation preview

  • Generating Random Numbers

  • Mean, Variance, Standard Deviation

    Mean:mean(x)Variance:mean((x-mean(x).*(x-mean(x)))Standard Deviationstd(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 functioncorrcoef
  • 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

  • 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

    Generate

    Return

    Uniform

  • 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

    Generate

    Return

    Exponential

  • 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

    Generate

    Set

    Generate

    Return

    Normal

    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

    Generate IID Bernoulli(p) random numbers

    Return

    Binomial

  • Binomial

    function genbino(N,n,p)

    for i=1:N,

    u=rand(1,n);

    y=(u

  • Generating Random Numbers

    Generate

    Return

    Geometric

  • 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

    Set

    Generate and replace by

    If accept else increase by one and return to step 2

    Poisson

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

    (1)0.8413

    F=

    0

    ()

    1

    xa

    xa

    Fxaxb

    ba

    bx