% Script file for Math 305 - lab assignment 3 % % Copy into your command window each of the following commands. % Try to understand the commands as you go. % Do the exercises and answer all the questions ; copy the solutions of the exercises % and the solutions to the questions into an email and % send it to me at gautier@math.hawaii.edu % Save your work either using 'Save workspace' in the Edit Menu % or by copying all your work into a file using Edit and saving % it. % The solution of the birthday problem with n people: % The probabilty of no two having the same birthday is % 365!/(365^n*(365-n)!) % In Matlab the function factorial computes factorials. % Use the above formula to solve for n=17 (our class size) factorial(365) 365^17 factorial(365)/(365^17*factorial(365-17)) % Clearly this doesn't work; the numbers are unworkable; % You have to be smarter in how you compute things % Here is an m-file that computes the solutions for various n % and then plots it. Study how the program works. ------------------------------------------------------ % Save the following as n m-file called birthdayproblem.m %Solution to birthday probability problem assuming 365 days % in the year, each equally likely of arising as a birthday % If there are n people in a classroom, what is the probability that % at least two of them have the same birthday? % General solution: % P = 1-365!/(365-n)!/365^n % If you try to solve this with large n (e.g. 30, for which the solution is % 29%) with the factorial function like so: % P = 1-factorial(365)/factorial(365-30)/365^30, Matlab will output NaN % because Matlab tries to compute very large values here. % The approach below is to start from n=1 and at each step use the % previous step's result to avoid dealing with large numbers. clc % this clears the command window clear all % this clears all the variables (don't do this if you want to % keep your previous work) nMax = 70; %maximum number of people in classroom nArray = 1:nMax; prevPnot = 1; %initialize probability for iN = 1:nMax Pnot = prevPnot*(365-iN+1)/365; %probability that no birthdays are the same P(iN) = 1-Pnot; %probability that at least two birthdays are the same prevPnot = Pnot; end plot(nArray, P, '.-') xlabel('nb. of people') ylabel('prob. that at least two have same birthday') grid on % end of birthdayproblem.m % Exercise: write a program to compute combinations of 200 objects % taken 90 at a time: the formula is 200!/(90!*110!). % Your answer should be 3.3422e+058 % Once this works, make it into a function y=comb(n,k) % and use it to compute combinations of 7 objects taken 5 at a time % answer: comb(7,5)= 21 % How many combinations of 26 letters of the alphabet can be made % taking 5 at a time. % Save the following function which computes the binomial pmf; % study how this function works, looking up any commands you don't know. function y = mbinom(n,p,k) % MBINOM y = mbinom(n,p,k) binomial probability mass function (hence m) % n is a positive integer; p is a probability % k a matrix of integers between 0 and n % y = P(X>=k) (a matrix of probabilities) if p > 0.5 a = [1 ((1-p)/p)*ones(1,n)]; b = [1 n:-1:1]; c = [1 1:n]; br = (p^n)*cumprod(a.*b./c); bi = fliplr(br); else a = [1 (p/(1-p))*ones(1,n)]; b = [1 n:-1:1]; c = [1 1:n]; bi = ((1-p)^n)*cumprod(a.*b./c); end y = bi(k+1); % What is the probability that in five rolls of a fair die, %a number 1 appears twice? % Number of trials: 5 % Exact number of successes: 2 % Probability of success: 1/6 function y = cbinom(n,p,k) % CBINOM y = cbinom(n,p,k) Cumulative binomial probabilities % n is a positive integer; p is a probability % k is a matrix of integers between 0 and n % y = P(X<=k) (a matrix of probabilities) if p > 0.5 a = [1 ((1-p)/p)*ones(1,n)]; b = [1 n:-1:1]; c = [1 1:n]; br = (p^n)*cumprod(a.*b./c); bcr = cumsum(br); bc = fliplr(bcr); else a = [1 (p/(1-p))*ones(1,n)]; b = [1 n:-1:1]; c = [1 1:n]; bi = ((1-p)^n)*cumprod(a.*b./c); b = cumsum(bi); bc = [1 1-b(1:n)]; end y = 1- bc(k+2); % BINOMIAL_TABLES file binomial_tables.m Generates binomial tables % Calculates a TABLE of binomial probabilities % for specified n, p, and row vector k, % Uses the m-functions ibinom and cbinom. n = input('Enter n, the number of trials '); p = input('Enter p, the probability of success '); k = input('Enter k