close all
clear all
%-----------------------------------
 disp('Example: System with Unreliable Components')
 disp('Generalized Binomial (probs different)')
  lw = 3; 
  set(0, 'DefaultAxesFontSize', 17);
  fs = 17;
  msize = 20;

% Circuit: Probability that X = k out of n elements are operational

% A system S is a parallel connection of unreliable components 
% E_i,  i = 1, ..., 10,  that work or fail independently.
% The components are operational in some fixed time interval  [0, T],  
% with probabilities  ps =[0.5 0.3 0.2 0.5 0.6  0.4 0.2 0.4 0.7 0.8];
% Let  X  be the number of components that remain operational after 
% time T  
% Find  (a) distribution of X  and (b)  E X and Var X.



ps =[0.5 0.3 0.2 0.5 0.6 0.4 0.2 0.4 0.7 0.8];
qs = 1- ps;
all = [ps' qs'];
[m n]= size(all);
Pnz = [1];  %initial polynomial = 1
for i = 1:m
    Pnz = conv(Pnz, all(i,:) );
    %polynomial multiplication as convolution
end
%at the end, Pnz is the product of p_i x + q_i
%  
sum(Pnz) %the sum is 1

k = 0:10
probs = Pnz(end:-1:1) 
EX = k * probs'     %expectation
EX2 = k.^2 * probs' %second moment
VX = EX2 - (EX)^2   %variance
