% L3B demo % Roll a fair 6-sided die n times (default n=6). % Show two plots: % Left, GENERATING distribution (of the probabilities) % Right, EMPIRICAL distribution (from the observed counts) % % Optionally, use a particular nonuniform generating distribution. % function L3Bdicedemo(n, unfair) if nargin<1, n=6; end if nargin<2, unfair=false; end if unfair % A slightly unfair die that favors 1 and 6 p = [.20 .15 .15 .15 .15 .20]; else p = ones(1,6) / 6; end x = randsample(1:6, n, true, p); nk = hist(x, 1:6); emp_p = nk/sum(nk); % Find largest probability in either plot % so we can ensure same scaling maxp = max(max(p),max(emp_p)); subplot(1,2,1) bar(1:6, p) title('Generating distribution') set(gca, 'ylim', [0 maxp+0.05]); subplot(1,2,2) bar(1:6, emp_p) title(sprintf('Empirical distribution from n=%d', n)) set(gca, 'ylim', [0 maxp+0.05]); end