% filename: inference_example_2.m % This script plots the likelihood function for Example 2 in the Introduction to Inference presentation. In the example, 22 white marbles and 28 reds were drawn at random from a large bag, and the goal was to give plausible values for pi, the proportion of reds in the bag. % ********** % create a vector of candidate values of the parameter pi, and compute the likelihood L at each of those values. Note that .^ and .* are element-by-element exponentiation and multiplication respectively. p = [0:0.01:1]; L = (p.^28).*((1-p).^22); % plot the likelihood function: plot(p,L) xlabel('\pi') ylabel('Likelihood L(\pi )') % plot a red horizontal line at 10% of the maximum likelihood hold on MLE = max(L); plot([0,1],0.1*MLE*[1,1],'r') % determine ("find") the range of values of pi for which the likelihood is at least 10% of its maximum value, and then report them as an interval. indices_of_plausibles = find(L>=0.1*MLE); plausibles = p(indices_of_plausibles); disp(['Range of plausible values is from ',num2str(min(plausibles)),' to ',num2str(max(plausibles))])