Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created April 11, 2012 22:22
Show Gist options
  • Save jasonLaster/2363148 to your computer and use it in GitHub Desktop.
Save jasonLaster/2363148 to your computer and use it in GitHub Desktop.
%% 2.2.20 BREAKS
n = 10^5;
breaks = rand(n,2);
maxbreak = max(breaks')';
minbreak = min(breaks')';
triangles = and(and(minbreak <.5, (1-maxbreak<.5)), (maxbreak - minbreak) < .5);
sum(triangles)/n
%% 3.1.22 - GUESS THE NUMBER OF WATCHES
% parameters
true_max = 100;
numfound = 16;
reps = 10^4;
% shuffle the watches around
shuffle = @(n) randperm(n);
watches = cell2mat(arrayfun(shuffle, ones(1,reps)*true_max, 'UniformOutput',false)');
% select a subset of the watches
found_watches = watches(:,1:numfound);
max_watch = max(found_watches');
% come up with guesses
watsons_guess = max_watch;
holmes_guess = max_watch*2;
% measure the average distance from the truth
dif = @(g) sum(abs(true_max -g))/reps;
watson = dif(watsons_guess)
holmes = dif(holmes_guess)
%% 3.1.24 - CANDIDATES
n = 10;
k = 3;
reps = 10^4;
shuffle = @(n) randperm(n);
applicants = cell2mat(arrayfun(shuffle, ones(1,reps)*n, 'UniformOutput',false)');
applicants = [applicants, ones(reps,1)*(n+1)];
max_left = max(applicants(:, 1:k)')';
right_side = applicants(:,(k+1):end);
best = @(ex) find(right_side(ex,:)>max_left(ex), 1);
count = 0;
for i = 1:reps
[r,c,v]= best(i);
if right_side(i,c) == 10
count = count + 1;
end
end
count/reps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment