%%%%%%%%%%%%%%%%% Homework 3 Solutions %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name
% ECEn  370

close all
clear all

%% Binomial Random variable with parameters n = 20 and p = .2
%Q: What does a Binomail random variable really do?
%A: It's basically a n number of bernoulli trials 

trials = 10000; 
n = 20;
p = .2;
b_vector = random('Binomial', n, p, [1, trials]);

for i = 1:21
   b_count (i,1) = i - 1; 
   b_count (i, 2) = sum((i-1) == b_vector);
end

%Find the probability and make a bar graph
figure;
subplot(3,1,1);
pvec = b_count(:,2)/trials;%
bar(b_count(:, 1), pvec);
xlabel('Number of heads');
ylabel('Probability');
title(sprintf('Binomial R.V with Param: n = %d, p =%f ', n, p));

%%Geometric Random Variable with p = .1
p = .1;

g_vector = random('Geometric', p, [1,trials]);

for i = 1:50
  g_count (i,1) = i; 
  g_count (i, 2) = sum((i-1) == g_vector);
end

subplot(3,1,2);
pvec = g_count(:,2)/trials;%
bar(g_count(:, 1), pvec);
xlabel('Success on nth try');
ylabel('Probability');
title(sprintf('Geometric R.V with Param p =%f ', p));

%%Poisson Random Variable with parameter lambda = 3
lambda = 3;
p_vector = random('Poisson',lambda, [1, trials]);

for i = 1:21
   p_count (i,1) = i - 1; 
   p_count (i, 2) = sum((i-1) == p_vector);
end

%Find the probability and make a bar graph
subplot(3,1,3);
pvec = p_count(:,2)/trials;%
bar(p_count(:, 1), pvec);
xlabel('Number of heads');
ylabel('Probability');
title(sprintf('Poisson R.V with Param: lambda = %d', lambda));


%% Binomial R.V with n = 4 p = .25
p_1 = .25;
X_vector = random('Binomial', 4, p_1, [1,trials]);

for i = 1:4
  brv_count (i,1) = i - 1; 
  brv_count (i, 2) = sum((i-1) == X_vector);  
    
end

figure;
subplot(2,1,1);
pvec = brv_count(:,2)/trials;
bar (brv_count(:,1),pvec);
xlabel(sprintf('Number of Heads %d trials', trials));
ylabel('Probability');
title(sprintf('Binomial R.V.with p= %d ', p_1));

%%Y_vector

Y_vector = (X_vector - 1).^2;

for i = 1:10
  brv_count (i,1) = i - 1; 
  brv_count (i, 2) = sum((i-1) == Y_vector);
    
end

subplot(2,1,2);
pvec = brv_count(:,2)/trials;
bar (brv_count(:,1),pvec);
xlabel(sprintf('Number of Heads %d trials', trials));
ylabel('Probability');
title(sprintf('Binomial R.V.with p= %d ', p_1));

