Difference between revisions of "JointDistCommands.m"

From Cohen Courses
Jump to navigationJump to search
(Created page with "%% generate the data n = 1000; % which die was used first and second roll - fair, hi, lo dice1 = randi(3,[n,1]); dice2 = randi(3,[n,1]); % did the 'loading' happen for die 1 ...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
%% generate the data
+
%% generate the data
 
+
n = 1000;
n = 1000;
+
% which die was used first and second roll - fair, hi, lo
% which die was used first and second roll - fair, hi, lo
+
dice1 = randi(3,[n,1]);
dice1 = randi(3,[n,1]);
+
dice2 = randi(3,[n,1]);
dice2 = randi(3,[n,1]);
+
% did the 'loading' happen for die 1 and die 2
% did the 'loading' happen for die 1 and die 2
+
load1 = randi(2,[n,1]);
load1 = randi(2,[n,1]);
+
load2 = randi(2,[n,1]);
load2 = randi(2,[n,1]);
+
r1 = roll(dice1,load1,randi(5,[n,1]),randi(6,[n,1]));
 
+
r2 = roll(dice2,load2,randi(5,[n,1]),randi(6,[n,1]));
r1 = roll(dice1,load1,randi(5,[n,1]),randi(6,[n,1]));
+
D = [dice1,dice2,r1,r2];
r2 = roll(dice2,load2,randi(5,[n,1]),randi(6,[n,1]));
+
%% There are lots of ways to visualize it
 
+
% show this as an image
D = [dice1,dice2,r1,r2];
+
imagesc(D);
 
+
% sort and then show it as an image  
%% There are lots of ways to visualize it
+
S = sortrows(D,1:2);
 
+
imagesc(S);
% show this as an image
+
% show a histogram
imagesc(D);
+
D34 = D(:,3:4);
 
+
hist3(D34,[6,6]);
% sort and then show it as an image
+
% get the counts and 'centers' that define the histogram
S = sortrows(D,1:2);
+
[H,C] = hist3(D34,[6,6]);
imagesc(S);
+
P = H/1000;
 
+
SP = (H + (1/36))/1001;
% show a histogram
+
[I,J]=meshgrid(1:6,1:6);
D34 = D(:,3:4);
+
surf(I,J,SP);
hist3(D34,[6,6]);
+
% we could also 'jitter' the points and do a scatter plot
 
+
E = D34 + randn(1000,2)*0.1;
% get the counts and 'centers' that define the histogram
+
plot(E(:,1),E(:,2),'r*');
[H,C] = hist3(D34,[6,6]);
+
% or combine that with a surface....
P = H/1000;
+
surf(I,J,SP);
SP = (H + (1/36))/1001;
+
hold on;
[I,J]=meshgrid(1:6,1:6);
+
plot3(E(:,1),E(:,2),zeros(1000,1)+0.1,'r*');
surf(I,J,SP);
+
%% Reasoning with the joint
 
+
% easy thing is to just look up interesting cases in the data and see how
% we could also 'jitter' the points and do a scatter plot
+
% frequent they are
E = D34 + randn(1000,2)*0.1;
+
sum(D(:,3)==D(:,4))/1000;  % doubles
plot(E(:,1),E(:,2),'r*');
+
sum(D(:,3)+D(:,4)==7)/1000; % sevens
 
+
% find all the indices of the data - in this case these are the different
% or combine that with a surface....
+
% outcomes of the joint experiment
surf(I,J,SP);
+
[I,J]=find(SP);  %all possible indices of SP
hold on;
+
IJ = [I,J];      %index 1,index 2
plot3(E(:,1),E(:,2),zeros(1000,1)+0.1,'r*');
+
% then we can find where - at what indices - we get sevens or doubles conveniently...
 
+
Sevens = IJ(I==J,:);
%% Reasoning with the joint
+
Doubles = IJ(I==J,:);
 
+
sum(SP(sub2ind(size(SP),Sevens(:,1),Sevens(:,2))));
% easy thing is to just look up interesting cases in the data and see how
+
sum(SP(sub2ind(size(SP),Doubles(:,1),Doubles(:,2))));
% frequent they are
+
% or even ask a hard question like: what is Pr(both die fair|roll>10)?
 
+
fairAnd11Or12 = sum((D(:,1)==1) & (D(:,2)==1) & (D(:,3)+D(:,4) >= 10));
sum(D(:,3)==D(:,4))/1000;  % doubles
+
is11Or12 = sum((D(:,3)+D(:,4) >= 10));
sum(D(:,3)+D(:,4)==7)/1000; % sevens
+
fairAnd11Or12/is11Or12;
 
 
 
 
% find all the indices of the data - in this case these are the different
 
% outcomes of the joint experiment
 
 
 
[I,J]=find(SP);  %all possible indices of SP
 
IJ = [I,J];      %index 1,index 2
 
 
 
% then we can find where - at what indices - we get sevens or doubles conveniently...
 
Sevens = IJ(I==J,:);
 
Doubles = IJ(I==J,:);
 
 
 
sum(SP(sub2ind(size(SP),Sevens(:,1),Sevens(:,2))));
 
sum(SP(sub2ind(size(SP),Doubles(:,1),Doubles(:,2))));
 
 
 
% or even ask a hard question like: what is Pr(both die fair|roll>10)?
 
fairAnd11Or12 = sum((D(:,1)==1) & (D(:,2)==1) & (D(:,3)+D(:,4) >= 10));
 
is11Or12 = sum((D(:,3)+D(:,4) >= 10));
 
fairAnd11Or12/is11Or12;
 

Latest revision as of 15:43, 9 September 2013

%% generate the data
n = 1000;
% which die was used first and second roll - fair, hi, lo
dice1 = randi(3,[n,1]);
dice2 = randi(3,[n,1]);
% did the 'loading' happen for die 1 and die 2
load1 = randi(2,[n,1]);
load2 = randi(2,[n,1]);
r1 = roll(dice1,load1,randi(5,[n,1]),randi(6,[n,1]));
r2 = roll(dice2,load2,randi(5,[n,1]),randi(6,[n,1]));
D = [dice1,dice2,r1,r2];
%% There are lots of ways to visualize it
% show this as an image
imagesc(D);
% sort and then show it as an image 
S = sortrows(D,1:2);
imagesc(S);
% show a histogram
D34 = D(:,3:4);
hist3(D34,[6,6]);
% get the counts and 'centers' that define the histogram
[H,C] = hist3(D34,[6,6]);
P = H/1000;
SP = (H + (1/36))/1001;
[I,J]=meshgrid(1:6,1:6);
surf(I,J,SP);
% we could also 'jitter' the points and do a scatter plot
E = D34 + randn(1000,2)*0.1;
plot(E(:,1),E(:,2),'r*');
% or combine that with a surface....
surf(I,J,SP);
hold on;
plot3(E(:,1),E(:,2),zeros(1000,1)+0.1,'r*');
%% Reasoning with the joint
% easy thing is to just look up interesting cases in the data and see how
% frequent they are
sum(D(:,3)==D(:,4))/1000;   % doubles
sum(D(:,3)+D(:,4)==7)/1000; % sevens
% find all the indices of the data - in this case these are the different
% outcomes of the joint experiment
[I,J]=find(SP);  %all possible indices of SP
IJ = [I,J];      %index 1,index 2
% then we can find where - at what indices - we get sevens or doubles conveniently...
Sevens = IJ(I==J,:);
Doubles = IJ(I==J,:);
sum(SP(sub2ind(size(SP),Sevens(:,1),Sevens(:,2))));
sum(SP(sub2ind(size(SP),Doubles(:,1),Doubles(:,2))));
% or even ask a hard question like: what is Pr(both die fair|roll>10)?
fairAnd11Or12 = sum((D(:,1)==1) & (D(:,2)==1) & (D(:,3)+D(:,4) >= 10));
is11Or12 = sum((D(:,3)+D(:,4) >= 10));
fairAnd11Or12/is11Or12;