Skip to content

Instantly share code, notes, and snippets.

@axpence
Last active August 29, 2015 13:56
Show Gist options
  • Save axpence/8797058 to your computer and use it in GitHub Desktop.
Save axpence/8797058 to your computer and use it in GitHub Desktop.
HW4 - 370 matlab
%%%%
% ECEn 370 Homework 4 Problem Example
% January 28, 2011
clear all;
load('burgerfry.mat');
BF = [0 0 0 0;...
0 0 0 0;...
0 0 0 0;...
0 0 0 0;...
0 0 0 0;...
0 0 0 0;...
];
%count number of 3 burgers 2 fries ...
count = 0;
samples = 10000;
for i=1:6
for j=1:4
for k=1:samples
if ( outcomes(k,1) == i && outcomes(k,2) == j ) %if i burgs j fries
count = count + 1;
end
end
%load it in.
BF(i,j) = count/samples;
count = 0;
end
end
coordinate = BF(3,2)
% Oftentimes it is easier to look at a matrix to determine the Joint PMF of
% a set of random variables. Suppose for example, that you have the
% following:
% Load the PMF into XY
% Y Values from 1 to 6
%XY = [ 0.00 0.15 0.00 0.00 0.05 0.01;... %
% 0.10 0.05 0.05 0.00 0.00 0.02;... %
% 0.00 0.10 0.10 0.10 0.00 0.01;... %
% 0.05 0.05 0.00 0.00 0.00 0.01;... % X-Values from 1 to 7
% 0.02 0.02 0.02 0.00 0.00 0.01;... %
% 0.02 0.01 0.00 0.00 0.01 0.02;... %
% 0.00 0.00 0.00 0.00 0.00 0.02... %
%];
%
% What this matrix is indicating is that the probability at a point, say
% (X=3,Y=4) is 0.10. In this case, the matrix is indexed from the upper
% left corner.
%
% The folowing will give you the probability for the point (X=3, Y=4)
%XY(3,4)
% The following code then plots the matrix above
figure(1); % Loads a new figure
bar3(BF); % Plots a 3-dimensional bar graph
xlabel('Y values'); % Places labels on the x-axis of bar graph
ylabel('X values'); % 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');
%
% From the probability matrix, you can calculate any of the probabilities
% you would like to find. Below is an example of doing calculations on the
% matrix to find the marginal probabilities for X and Y.
%
figure(2); % Loads a new figure.
marg_fries = sum(BF); % This sums over each column of the matrix XY.
marg_burgers = sum(BF'); % 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_burgers); % Produces bar graph of marginal PMF for X.
title('Marginal PMF for X');
xlabel('X Values');
ylabel('Probability Mass');
subplot(2,1,2); % This produces a subplot in the figure in bottom position.
bar(marg_fries); % Produces bar graph of marginal PMF for Y.
title('Marginal PMF for Y');
xlabel('Y Values');
ylabel('Probability Mass');
%
% Now suppose that you want to simulate outcomes from the above joint PMF.
% The following code allows you to do that. First, we specify the number of
% trials we want to perform. Then we actually simulate all of those trials.
% Make sure that you have simulate_joint_PMF.m in your working directory so
% that you can call that function. The simulate_joint_PMF function takes in
% two arguments, the matrix representing the probabilities and the number
% of trials to perform. It returns a matrix that has two columns
% representing the X and Y coordinates for each outcome.
%
trials = 10000; % Specifies the number of trials: try this for different
% values to see how the estimates change.
outcomes = simulate_joint_PMF(BF,trials); % Produces outcomes from the joint PMF.
%
% Suppose that you want to estimate what the original joint PMF looked
% like. You can do this by counting up all of the outcomes (each X,Y
% combintation) and then dividing by the total number of trials that were
% performed. The following code does just that.
%
XY_tally = zeros(size(BF)); % This initializes an array for your outcomes.
for c = 1:trials
% This counts up for each (X,Y) location how many times it appeared in
% the data.
XY_tally(outcomes(c,1), outcomes(c,2)) = XY_tally(outcomes(c,1), outcomes(c,2)) + 1;
end
%
% The following code estimates the probability at each X,Y location by
% dividing by the total number of trials.
XY_estimated_probability = XY_tally ./ trials;
%
% This code then plots the estimated probability mass from the outcomes
% that you obtained from your numerical simulation of the joint PMF.
%
figure(3); % Loads a new figure
bar3(XY_estimated_probability); % Plots a 3-dimensional bar graph
xlabel('Y values'); % Places labels on the x-axis of bar graph
ylabel('X values'); % 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('Estimated Probability Mass');
expected_burgers =0;
%Burgs expected
for i=1:6
expected_burgers = expected_burgers + i*marg_burgers(i);
end
expected_fries =0;
%Fries expected
for i=1:4
expected_fries = expected_fries + i*marg_fries(i);
end
expected_money = expected_burgers*2 + expected_fries;
probability_buying_two_fries =0;
for i=1:6
probability_buying_two_fries = probability_buying_two_fries + BF(i,2);
end
is_one = sum(BF(:,1)) + sum(BF(:,2)) + sum(BF(:,3)) + sum(BF(:,4)) ;
figure(4);
bar(BF(:,2))
xlabel('Quantity of burgers'); % Places labels on the x-axis of bar graph
ylabel('Probability of Purchase'); % Places labels on the y-axis of bar graph
title('Given a cust buys 2 fries,.. how many burgs?');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment