matlabci

Upload: omar-ali

Post on 04-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 MatlabCI

    1/3

    166 probability in eecs

    function [INVD] = invdist(P)

    %This function computes the invariant distribution

    % of a stochastic matrix P

    N = size(P, 2);

    B = (P - eye(N));

    B(1:N,N) = ones(N, 1);o = zeros(1, N);

    o(N) = 1;

    INVD = o*inv(B);

    As an example,

    P = [0, 0.3, 0.7; 0, 0.4, 0.6; 1, 0, 0];

    invdist(P)

    produces the invariant distribution

    ans =

    0.4000 0.2000 0.4000

    B.4 Confidence Intervals

    We experiment with confidence intervals. Let{Xn, n 1}be i.i.d. with mean and variance 2. We explained in Section 2.3

    that a 95% confidence interval for the mean is, provided that n is

    sufficiently large,

    [n 2n

    , n 2n

    ]

    where

    n = X1+ +Xn

    n

    is the sample mean. We test this confidence interval for coin flips.

    n +1n

    n 1n

    n

    p

    n

    n :=

    1 + + n

    n

    95% confidence interval

    with n samples

    Figure B.3: Confidence intervals forthe bias of a coin using a bound on the

    variance.

    For coin flips, i.e., B(p)random variables, we know that

    var(Xn) = p(1 p) 1/4.

    We then use the upper bound 1/2 on the standard deviation in the

    confidence intervals that are then given by

    [n 1n

    , n+ 1

    n].

    The code below generates i.i.d. B(p)random variables X(1), . . . , X(N)

    and computes their sample mean. It then calculates the bounds of the

    confidence interval. Figure B.3shows the results.

  • 8/13/2019 MatlabCI

    2/3

    matlab 167

    \color{blue}

    \begin{verbatim}

    %Flip coin N times; these are B(p) random variables

    %Construct confidence interval

    % based on sample mean and bound 1/4 on variance

    %N = 300;

    p = 0.3;

    Y = zeros(1,N);

    U = ones(1,N);

    M = p*U;

    L = zeros(1,N);

    Y(1) = (rand(1) < p);

    for j = 2:N

    Y(j) = ((j - 1)*Y(j-1) + (rand(1) < p))/j;

    b = 1/sqrt(j);

    U(j) = Y(j) + b;L(j) = Y(j) - b;

    end

    plot([Y;L;U;M])

    Instead of using an upper boundon the confidence interval, one

    may use the sample standard deviation.

    n:=

    X1 + +Xn

    n

    2

    n:=

    (X1 n) + + (Xn n)

    nn

    + 2nn

    n 2

    nn

    n

    Figure B.4: Confidence intervals forthe bias of a coin using the samplevariance.

    In that case, the confidence interval is

    [n 2nn

    , n 2nn

    ]

    where

    2n =

    X21+ +X2n

    n 2n

    is the sample variance. Here is the code with the modification. Figure

    B.4shows the result.

    Initially, the estimate n of the standard deviation is not very good.

    However, for large n, this estimate becomes better than an upper

    bound and results in smaller confidence intervals.

    %Flip coin N times

    %Repeat M times

    % p = P(1) = 1 - P(0)

    N = 300;

    p = 0.3;

    Y = zeros(1,N); %sample mean

    U = ones(1,N); % upper bound of CI

    M = p*U; % center of CI

  • 8/13/2019 MatlabCI

    3/3

    168 probability in eecs

    L = zeros(1,N); % lower bound of CI

    S = U; % sample second moment

    Y(1) = (rand(1) < p);

    S(1) = Y(1)^2;

    for j = 2:N

    R = (rand(1) < p); % coin flipY(j) = ((j - 1)*Y(j-1) + R )/j;

    S(j) = S(j-1) + R^2;

    b = 2/sqrt(j);

    sigma = sqrt(S(j)/j - Y(j)^2); % samplestandard deviation

    U(j) = Y(j) + b*sigma;

    L(j) = Y(j) - b*sigma;

    end

    plot([Y;L;U;M])

    The code given above can be used for other distributions. Here is

    an example with exponential random variables. Figure B.5shows theresults.

    n :=X1 + +Xn

    n

    2

    n:=

    (X1 n)2 + + (Xn n)

    2

    n

    n+ 2nn

    n 2nn

    n

    Figure B.5: Confidence intervals for themean of exponential random variablesusing the sample variance.

    %Generate N Exp with mean mu

    %Constuct confidence interval

    % based on sample mean and sample variance

    % p = P(1) = 1 - P(0)

    N = 300;

    mu = 7;

    Y = zeros(1,N); %sample mean

    U = ones(1,N); % upper bound of CI

    M = m u*U; % center of CI

    L = zeros(1,N); % lower bound of CI

    S = U; % sample second moment

    Y(1) = random(exp, mu);

    S(1) = Y(1)^2;

    for j = 2:N

    R = random(exp, mu); % exponential

    Y(j) = ((j - 1)*Y(j-1) + R )/j;

    S(j) = S(j-1) + R^2;

    b = 2/sqrt(j);

    sigma = sqrt(S(j)/j - Y(j)^2); % sample standard deviation

    U(j) = Y(j) + b*sigma;

    L(j) = Y(j) - b*sigma;

    end

    plot([Y;L;U;M])