Random variables

Contents

Generate independent random variables with various distributions

Generate n=100 i.i.d (independent and identically distributed) random variables with distribution U(0,1)

u=rand(100,1);
figure(1)
plot(u)
title('100 uniform distributed i.i.d samples')
xlabel('index k')
ylabel('the value u(k)')
grid
% Generate n=100 i.i.d (independent and identically distributed) random variables with distribution N(0,1)
figure(2)
un=randn(100,1);
plot(un)
title('100 normal distributed i.i.d samples')
xlabel('index k')
ylabel('the value u(k)')
grid

Generate a random variable having values in the alphabet {1,2,3,4,5,6}

N= 1200000;
u=6*rand(N,1); % generate N= 12000 uniform distributed values in [0,6)
ui = 1+floor(u); % quantize u to integer values
figure(3)
plot(ui(1:100),'or')
title('100 throwings of a dice')
xlabel('index k')
ylabel('the value u(k)')
% number of occurences of {1,2,3,4,5,6}
hc = hist(ui,min(ui):max(ui))
figure(4)
plot(1:6,hc,'or',0,0,[0 6],N/6*[1 1],'b--')
xlabel('value on the dice')
ylabel('number of occurences')

% compute the sum of two consecutive dice throwings
for i = 1:(N/2-1)
    uip(i) = ui(1+2*i) + ui(2+2*i);
    uip1(i) = ui(1+2*i);
    uip2(i) = ui(2+2*i);
end
figure(5)
plot(uip(1:100),'or')
title('100 throwings of a pair of dice')
xlabel('index k')
ylabel('the value u(k)')
% number of occurences of {2,3,4,5,6,7,8,9,10,11,12}
hc2 = hist(uip,min(uip):max(uip))
figure(6)
plot(min(uip):max(uip),36*hc2/sum(hc2),'or')
grid on
xlabel('sum of values on two dice')
ylabel('36*empirical probability')


% Correlation of uip with uip1, and uip with uip2
N1 = length(uip);
muip = sum(uip)/N1
muip1 = sum(uip1)/N1
muip2 = sum(uip2)/N1
C1 = sum((uip1-muip1).*(uip-muip))/length(uip)
C2 = sum((uip2-muip2).*(uip-muip))/length(uip)

% Correlation of uip1 with uip2
C3 = sum((uip1-muip1).*(uip2-muip2))/length(uip)
hc =

      199643      200412      199818      200609      199907      199611


hc2 =

  Columns 1 through 6

       16626       32977       49997       67179       83211      100070

  Columns 7 through 11

       83594       66813       49746       33234       16552


muip =

    6.9993


muip1 =

    3.4983


muip2 =

    3.5010


C1 =

    2.9070


C2 =

    2.9085


C3 =

   -0.0057