You are on page 1of 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

Condence 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% condence interval for the mean is, provided that n is sufciently large, 2 2 [n , n ] n n X1 + + X n n is the sample mean. We test this condence interval for coin ips. For coin ips, i.e., B( p) random variables, we know that n = var( Xn ) = p(1 p) 1/4. We then use the upper bound 1/2 on the standard deviation in the condence intervals that are then given by 1 1 [ n , n + ]. n 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 condence interval. Figure B.3 shows the results. where
p

1 n + n

## 95% condence interval with n samples

n 1 n n n := X1 + + Xn n n

Figure B.3: Condence intervals for the bias of a coin using a bound on the variance.

matlab

167

\color{blue} \begin{verbatim} %Flip coin N times; these are B(p) random variables %Construct confidence interval % % 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])
n n + 2 n n :=
2 n :=

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

X1 + + Xn n ( X1 n ) 2 + + ( Xn n ) 2 n

I nstead of using an upper bound on the condence interval, one may use the sample standard deviation. In that case, the condence interval is 2n 2n [n , n ] n n where
2 X 2 + + Xn = 1 2 n n is the sample variance. Here is the code with the modication. Figure B.4 shows 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 condence intervals. 2 n

n n 2 n

Figure B.4: Condence intervals for the bias of a coin using the sample variance.

%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

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 flip 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); % samplestandard deviation U(j) = Y(j) + b*sigma; L(j) = Y(j) - b*sigma; end plot([Y;L;U;M])
n :=
2 n :=

X1 + + Xn n ( X1 n ) 2 + + ( Xn n ) 2 n

The code given above can be used for other distributions. Here is an example with exponential random variables. Figure B.5 shows the results.
%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 = mu*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])

n n + 2 n n

n n 2 n

Figure B.5: Condence intervals for the mean of exponential random variables using the sample variance.