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