%% Question 1 Infinite monkeys

count = 0;
for t = 1:1000000
    s = randstr(3);
    if strcmp(s, 'ROM') == 1
        count = count + 1;
    end
end
count


%% Question 2 Birthday problem

C = zeros(1, 80);
for N = 1:80
    for t = 1:10000
        birthdays = randi(365, [1 N]);
        u = unique(birthdays);
        if length(u) < N
            C(N) = C(N) + 1;
        end
    end
end
plot(1:80, C / 10000);
xlabel('number of people in the group');
ylabel('odds of at least two people having the same birthday');


%% Propogation of errors I

x1 = 10 + randn(1, 1000) * 1.5;
x2 = -10 + randn(1, 1000) * 2;
y = x1 + x2;
hist(x1, 20);
hold on
hist(x2, 20);
hist(y, 20);

dx1 = 1.5;
dx2 = 2;
dy = sqrt(dx1 * dx1 + dx2 + dx2)
std(y)


%% Propogation of errors II

figure
x1 = 6 + randn(1, 1000);
y = nthroot(x1 - 5, 3);   % if you use ^(1/3) you will get the wrong answer
hist(x1, 20);
title('x1');

figure
hist(y, 20);
title('y = f(x1)');


dx1 = 1;

% estimated standard deviation using formula (should be 0.3333)
dy = abs(((6 - 5) ^ (-2 / 3)) / 3) * dx1

% sample standard deviation (should be around 0.7)
std(y)

% The formula assumes that x1 and f(x1) are both unimodal and symmetric
% (e.g., computing the standard deviation of x1 and f(x1) is a
% sensible thing to do). The histogram of y shows that f(x1) is
% clearly not unimodal (e.g., the distribution has two distinct
% peaks). In this sort of distribution, none of the simple summary
% statistics, such as mean, median, standard deviation, or variance,
% give you an accurate picture of what is going on.


