ClearAll;Clear[f];Clear[s];Clear[Q];
$DefaultFont = {"Times-Roman",10};
Needs["Statistics`DataManipulation`"];
Needs["Graphics`Graphics`"];
ratpupilradii = {0.25,0.25,0.375,0.375,0.375,0.375,0.5,0.5,0.5, 0.5,0.5,
				 0.5, 0.5, 0.75, 0.75, 0.75, 1.0,  1.0,1.0,1.25,1.5,1.75};
xcum = {0.1875,0.3125,0.4375,0.5625,0.8125,1.0625,1.3125,1.5625,1.8125};
ycum = {0,2,6,13,16,19,20,21,22}/22;
datapoints = Table[{xcum[[i]],ycum[[i]]},{i,1,9}];
datapoints2 = Table[{Pi xcum[[i]]^2,ycum[[i]]},{i,1,9}];
smle = Sqrt[7/11];

Q[s_]:= 22 Log[2] - 44 Log[s] + Apply[Plus,Log[ratpupilradii]] -
			Apply[Plus,ratpupilradii^2]/s^2;
plotllf = Plot[Q[s],{s,0.5,1},
	PlotRange->{{0.66,1},{-10,-8}},
	AxesLabel->{"s","Q"},
	AxesOrigin->{0.65,-10.05},
	AspectRatio->2/GoldenRatio,
	PlotLabel->"(a)  Loglikelihood function"];
						
F[n_,s_,x_]:= 1 - E^(-(x/s)^n);
f[n_,s_,x_]:= (n(x/s)^(n-1) E^(-(x/s)^n))/s;
error[s_] = Apply[Plus,Table[(F[2,s,xcum[[k]]]-ycum[[k]])^2,{k,1,9}]];
leasterror = FindMinimum[error[s],{s,0.67,0.5,1}];
Plot[error[s],{s,0.5,1}];
sopt = s/.leasterror[[2]]
Print["Error is ",leasterror[[1]]];
fmle[x_]:= (2 x/(smle^2)) E^(-(x/smle)^2);
FMLE[x_]:= 1 - E^(-(x/smle)^2);
plotfmle = Plot[fmle[x],{x,0,2}];
plotFMLE = Plot[FMLE[x],{x,0,2}];
up:= Show[Plot[f[2,sopt,x],{x,0,2},
			PlotRange->{{0,2.05},Automatic},PlotStyle->Dashing[{0.001,0.01}],
			Axes->True,AxesOrigin->{-0.03,-0.04},
			PlotLabel->"(b)  p.d.f.",
			AxesLabel->{" x","f"}],plotfmle];
			
down:= Show[Plot[F[2,sopt,x],{x,0,2},
			PlotRange->{{0,2.05},{0,1.05}},PlotStyle->Dashing[{0.001,0.01}],
			Axes->True,AxesOrigin->{-0.03,-0.03},
			PlotLabel->"(c)  c.d.f.",
			AxesLabel->{" x","F"}],plotFMLE,
			ListPlot[datapoints]];
Show[GraphicsArray[{plotllf,GraphicsArray[{{up},{down}}]}],
		PlotLabel->"                                                Figure 31.3\n
		Distribution of pupil radius in rats (population solid, sample dashed)"]