%% Temple university CIS 2033
% A modern Introduction to Probability and Statistics
% Lab 1 assignment
% URL: http://astro.temple.edu/~tuf28053/CIS2033_Spring2015/lab_assignments/lab_assignment_1.pdf
%
% Author: Djordje Gligorijevic
% email: gligorijevic@temple.edu
%% Problem 1 solutions
% create a matrix A with 5 rows and 3 columns, filled with random variables
% between 0 and 10
A = rand(5,3);
% create an identity matrix B with the size of 3 times 3
B = eye(3);
% get the submatrix C of A by extracting the elements of the last three rows
C = A(end-2:end, :);
% perform element-wise multiplication between matrix B and matrix C.
% The result is denoted as the matrix D
D = B .* C;
% concatenate A and D to form matrix E, whose first 5 rows are from A and
% the last 3 rows are from D
E = vertcat(A, D);
% plot a histogram of each column of matrix E, what can you tell about
% the distribution of values in columns? (Please label the axes and add a
% title. You should also specify other properties such as the line width,
% the font size and the color. Please save the figure as .png)
figure;
hist(E);
xlabel('Spread of Values');
ylabel('Contained Values');
title('Plot of E');
set(gca, 'LineWidth', 2);
set(gca, 'FontSize', 12);
set(gca, 'YColor', [0.2 0.4 0.6]);
set(gca, 'XColor', [0.1 0.3 0.5]);
% write a function to calculate intersection between two sets
% A = 1 : 5 : 200 and B = 1 : 3 : 190.
A2 = 1:5:200;
B2 = 1:3:190;
results = Intersection(A2,B2);
%% Problem 2 solution
% Before taking a look at potential homweork solutions please refer to this
% blog post on this no three coincident birthdays problem which was main
% inspiration of this lab assignment problem:
web('http://drmoron.org/3-birthday-problem/');
% The problem solved here is slighlty different as it measures probability
% that three people have coincident birthday.
% Statement of the author of the blog:
%
% "So what is the probability for, given a group of people, that three have
% the same birthday. The answer is… I don’t know. The trick I used for a
% pair of people doesn’t work for 3, and the counting just gets too
% complicated for my little brain.
%
% Fortunately, I have another trick up my sleeve. Rather than figure out
% the exact solution, I can write a simulator program to estimate the
% probability. Here’s how it works. The simulator sets up an empty (virtual)
% room. It then lets in people one at a time. Each person coming in has a
% random birthday. The simulation continues doing this until someone comes
% in with a birthday that completes a triple.
%
% That, of course, does not give us any probabilities. If we run the
% simulation again, we will (probably) get a different answer. But, if we
% run the simulation enough times, we can estimate the probabilities as the
% percentage of times the simulated group had 3 people with the same
% birthday. There is a very rigorous branch of computational science to
% determine the number of samples needed and the uncertainty remaining.
% But screw all that; I just ran the simulation for an excessive 100,000
% times."
maxBirthdays = 6;
numDays = 365;
numTrials = 1000;
maxGroupSize = 100;
probabilities = NCoincidentBirthdaysProbability( ...
maxBirthdays, numDays, numTrials, maxGroupSize);
colors = {'r','b','k','m','c'};
figure;
hold on;
x = 1:maxGroupSize;
% Note that making an inverse probability plot simple requires putting
% 1 - probabilities(i,:)
for i = 1:maxBirthdays-1
plot(x, 1-probabilities(i,:), colors{i}, 'lineWidth', 2);
end
title('N coincident birthday problem');
legend('2 b-days','3 b-days','4 b-days','5 b-days','6 b-days');
hold off;
%% Problem 3 solution
% For a given probability mass function please calculate and plot in MATLAB
% proper distribution function.
figure;
hold on;
title('Distribution Function from Given PMF', 'FontSize', 16);
xlabel('a', 'FontSize', 14);
ylabel('F(a)', 'FontSize', 14);
a = [-1, 0, 1, 2, 3];
F = [1/24, 13/24, 19/24, 23/24, 1];
stairs(a, F);
hold off;