Bernoulli Trial problem
This is the code our team developed for the analytical solution to the Bernoulli trials problem whose problem statement is located here. The runtime for N = 12 is rather large (~20-25 minutes), but our code computes the probabilities for all n which I have printed below.
function [probn] = Bernoulli(N,p,q) num = 0; % counter for n probn = zeros(1,N+1); for pset = 0:2^N-1 for qset = 0:2^(N-1)-1 pbinary = dec2bin(pset,N); % make the binary set for p qbinary = dec2bin(qset,N-1); % make the binary set for q pones = length(find(pbinary == '1')); % number of 1's in set of p qones = length(find(qbinary == '1')); % number of 1's in set of q probPQset = p^(pones)*(1-p)^(N-pones)*q^(qones)*(1-q)^(N-1-qones); % probability for this set of p's and q's. for i = 1:N-1 if qbinary(i) == '1' pbinary(i+1) = pbinary(i);% if q = 1, repeat last trial end end for j = 1:N if pbinary(j) == '1' num = num + 1; % Total number of 1's. end end probn(num+1) = probn(num+1) + probPQset; num = 0; end end end
For this bar chart, the x-axis are the n values plus 1. So the n value that has the greatest probability is 10 (not 11). The values for the probabilities are listed in the following table.
|n values||Probability of n|