clear all

%% Part A
customers = 10000; % Specifies the number of trials: try this for different
                % values to see how the estimates change. 

burger_fry = importdata('burgerfry.mat'); 
XY = zeros(6, 4); % This initializes an array for your outcomes

for c = 1:customers
    % This counts up for each (X,Y) location how many times it appeared in
    % the data.
    XY(burger_fry(c,1), burger_fry(c,2)) = XY(burger_fry(c,1), burger_fry(c,2)) + 1;
end
XY_prob = XY ./ customers;


% The following code then plots the matrix above
figure(1); % Loads a new figure
bar3(XY_prob);  % Plots a 3-dimensional bar graph
xlabel('Fries'); % Places labels on the x-axis of bar graph
ylabel('Burgers'); % Places labels on the y-axis of bar graph
zlabel('Probability Mass'); % Places labels on the z-axis of bar graph
view(-100, 50); % Sets the vantage point for viewing the bar graph
title('Original Joint PMF');

%% Part B

fprintf('Part B: Probability person order 3 burgers 2 fries is %f\n ', XY_prob(3,2));

%% Part C and Part D

figure(2); % Loads a new figure.
marg_y = sum(XY_prob); % This sums over each column of the matrix XY.
marg_x = sum(XY_prob'); % This sums over each column of the transposed matrix XY.
% Note that the ' transposes a matrix.

subplot(2,1,1); % This produces a subplot in the figure in top position.
bar(marg_x);    % Produces bar graph of marginal PMF for X.
title('Marginal PMF for Burgers');
xlabel('Burgers');
ylabel('Probability Mass');

subplot(2,1,2); % This produces a subplot in the figure in bottom position.
bar(marg_y);    % Produces bar graph of marginal PMF for Y.
title('Marginal PMF for Fries');
xlabel('Fries');
ylabel('Probability Mass');


%% Part e:  Expected number of burgers that a normal customer will buy? 
expectation_burger = 0; 
for n = 1:6
    expectation_burger = expectation_burger + (marg_x(n)*n);
end


fprintf('Part E: Expectation of someone buying a burger: %f burgers\n ', expectation_burger);

expectation_fries = 0;
for n = 1:4
    expectation_fries = expectation_fries + (marg_y(n)*n);
end

fprintf('Part F: Expectation of someone buying a order of fries: %f orders of fries\n ', expectation_fries);

%% Part g

burger_cost = expectation_burger*2; 
frie_cost = expectation_fries*1; 

fprintf('Part G: Toatl cost: $%.2f \n ', frie_cost + burger_cost);

%% Part h 

fry_total = sum(XY(:, 2)); 

figure(3);

bar(XY(:, 2)/fry_total);
title('PMF of the number of burgers bought given 2 fries already bought');
xlabel('Burgers');
ylabel('Probability Mass');


