Quiz 1.11
Solution:
For this problem MATLAB function mnrnd(n,p) can be used. r =
mnrnd(n,p) returns random values r from the multinomial distribution with
parameters n and p. n is a positive integer specifying the number of trials
(sample size) for each multinomial outcome. p is a 1-by-k vector of
multinomial probabilities, where k is the number of multinomial bins or
categories. p must sum to one
Code
>> n=100;
>> p=[0.4, 0.5, 0.1];
>> X=mnrnd(n,p)
X=
38
51
11
Problem 1
Solution:
Function Code:
function [ N ] = countequal( G,T )
%countequal function to duplicate the funcionality of hist function
%
Using ndgrid function we first convert input arguments in to grid.
%
When G has n elements, and T has m elements, ndgrid(G,T) creates
%
a grid (an n m array) of all possible pairs [G(i) T(j)]. This
%
grid is represented by two separate n m matrices: MX and MY
%
which indicate the x and y values at each grid point.
%
Mathematically,
%
MX(i,j) = G(i), MY(i,j)=H(j)
%
then defined c=(MX==MY), which gives n x m array, whose values are
%
1 or 0 depending whether elements of MX and MY are equal or not. %
Summing this c across coloums gives the derised result N
[MX,MY]=ndgrid(G,T);
c=(MX==MY);
N=sum(c,1);
N=N';
N=
21
29
33
17
Problem 2
Solution:
In C 1 indicate biased coin is chosen and 0 indicates unbiased coin is chosen
First we take equal probability event in which coin no 1 or 2 is selected
Then based selection coin occurrence of head is represented by 1 in output H
>>
>>
>>
>>
>>
n=50;
C=ceil(2*rand(n,1));
p=1-(C/4);
H=(rand(n,1)<p);
C=C'
C=
Columns 1 through 22
2
2
1
1
1
1
1
1
1
Columns 23 through 44
1
2
1
1
1
2
1
1
1
Columns 45 through 50
1
1
2
1
1
1
>> H=H'
H=
Columns 1 through 22
1
1
0
1
1
1
1
1
1
Columns 23 through 44
1
0
1
1
1
0
1
0
0
Columns 45 through 50
1
1
1
1
0
0
Part 2
Quiz 2.10
Solution:
First generated a column vector from 1 to n. then generated a zero matrix M of
order n x 5. For every column of matrix generated random n random samples.
Evaluating cumulative sum of random samples and dividing it element wise with k
and plotting the result gives desired result
For n=100
K=(1:100)';
M=zeros(100,5);
for i=1:5,
X=(ceil(10*rand(1,100)))';
M(:,i)=cumsum(X)./K;
end;
plot(K,M);
7
10
20
For n=1000
K=(1:1000)';
M=zeros(1000,5);
for i=1:5,
X=(ceil(10*rand(1,1000)))';
M(:,i)=cumsum(X)./K;
30
40
50
60
70
80
90
100
end;
plot(K,M);
10
100
200
300
400
500
600
700
800
900
1000
Problem 1
Solution:
1 - (1 - p) k * -1 <R 1 - (1 - p) k *
Subtracting 1 from each side and then multiplying by -1 through (which
reverses the inequality), we obtain
(1 - p) k * -1> 1 - R (1 - p) k *
Next we take the logarithm of each side. Since logarithms are monotonic
functions, we
(k * - 1) ln (1 - p)> ln (1 - R) k * ln (1 - p)
Since 0 <p <1, we have ln (1 - p) <0. So dividing by ln (1 - p) reverses
inequalities, producing
Results:
geometricrv(.5,5)
ans =
3
1
1
1
1
Problem 2
Solution:
Using matlab to find n* we use following code
>> i=0;
>> n=0;
>> while i==0
if poissonpmf(n,n) == Inf
i=1;
else
n=n+1;
end
end
>> n
n=
714
From this we find n*=714 for which answers becomes infinity, now using hint the new function code is
given below
function pmf=poissonpmf(alpha,x)
x=x(:);
if (alpha==0)
pmf=1.0*(x==0);
else
k=(1:ceil(max(x)))';
logfacts =cumsum(log(k));
pb=exp([-alpha; -alpha+ (k*log(alpha))-logfacts]);
okx=(x>=0).*(x==floor(x));
x=okx.*x;
pmf=okx.*pb(x+1);
end
%pmf(i)=0 for zero-prob x(i)
Problem 3
Solution:
First we generate m samples of the length X. Next we convert that to m samples of the fax cost Y .
Summing these samples and dividing by m, we obtain the average cost of m samples.
The matlab code is given below:
function y=avgfax(m)
sx=1:8;
p=[0.15*ones(1,4) 0.1*ones(1,4)];
r=rand(m,1);
cdf=cumsum(p);
x=sx(1+count(cdf,r));
yy=cumsum([10 9 8 7 6]);
yy=[yy 50 50 50];
y=sum(yy(x))/m;
end
Part 3
Quiz 3.9
Solution:
A way to produce random variables with PDF f T |T >2 (t ) is to generate samples of T with
PDF f T (t ) and then to discard those samples which fail to satisfy the condition T > 2. In
below code x is evaluated as exponential random variable
function t=t2rv(m)
i=0;lambda=1/3;
t=zeros(m,1);
while (i<m),
x=-(1/lambda)*log(1-rand(m,1));
if (x>2)
t(i+1)=x;
i=i+1;
end
end
end