%add right sides of equations into the rows 25, 26, 54 and 58
function lotto(X)

% lotto(X)
%
% Input X is a vector listing the known winning numbers so
% far. 
%
% Function to solve the lotto problem from lecture 5. This
% code is not efficient - Matlab is optimised for matrix
% operations, so it would be better to rewrite the loops
% as vector or matrix operations. But efficient code is
% less important here than readable code!



% set up the hypothesis space
H = []; 
for l = 10:99 % lower value
	for u = l:99 % upper value
		 H = [H; l u]; % add hypothesis 
	end
end

% set up the prior over the hypothesis space
nh =  % count the number of hypotheses
priorH =  % priors

% set up the likelihood functions
L = zeros(nh,100); % initialise P(x|h) at zero for all x & h
for h = 1:nh % loop over hypotheses

	% get the relevant information
	l = H(h,1); % the lower bound as per hypothesis h
	u = H(h,2); % the upper bound as per hypothesis h
	s = u - l + 1; % the "size" of the hypothesis h, |h|
	
	% specify the likelihood
	L(h,l:u) = 1/s; % P(x|h) = 1/|h| for all x in h
end

% [an aside: notice that our matrix L is mostly zeros. That
% is, it is sparse. Matlab is much faster when dealing with 
% sparse matrices if you actually tell it to expect a sparse
% matrix. There's a function called "sparse" that does this.
% I won't bother here.] 


% likelihood of the observed data
nx = length(X); % number of observations
pX = ones(nh,1); % initialise 
for i = 1:nx % loop over all X
	xi = X(i); % the i-th datum
	pX =  % update likelihood
end

% compute posterior inefficiently...
postH = 

% generalisation probability
y = 10:99; % values people might bet on
ny = length(y); % number of such values
g = zeros(1,ny); % initialise
for i = 1:ny
	ind = y(i) >= H(:,1) & y(i) <= H(:,2); 
	g(i) = sum(postH(ind));
end



%%%% draw pictures (this part isn't as well-commented!) %%%%

% draw the generalisation picture...
figure(1); clf; hold on; set(gcf,'color','w')
plot(y,g,'k.-')
set(gca,'ylim',[0 1.02])
xlabel('number bet upon, y')
ylabel('probability y is in the true range')
title(['outcomes so far: ' num2str(X);])


% draw the two different plots showing the actual posterior distribution...
figure(2); clf; hold on; set(gcf,'color','w')
maxpost = max(postH);
P=zeros(100);
for h = 1:nh
	l = H(h,1); % the lower bound as per hypothesis h
	u = H(h,2); % the upper bound as per hypothesis h
	if postH(h) > 0
		obj = rectangle('position',[l-.5,u-.5,1,1]);	
		set(obj,'edgecolor', [1 1 1] * (1- postH(h) / maxpost))
		set(obj,'facecolor', [1 1 1] * (1- postH(h) / maxpost))
		P(l,u)=postH(h);
	end
end
axis equal
set(gca,'xlim',[0 100],'ylim',[0 100],'box','on')
xlabel('hypothesised lower bound, l')
ylabel('hypothesised upper bound, u')
title('posterior distribution over hypotheses')	

figure(3); clf; hold on; set(gcf,'color','w')
xlabel('hypothesised lower bound, l')
ylabel('hypothesised upper bound, u')
surf(P')


