function postexp(x) %Where L is the value of lambda %SampleSize is the sample size %Random Number Generation %x=randraw('exp',L,SampleSize); %Generate exp dist samples, lambda=L x_sum=sum(x); %Sums the values of 10 samples SampleSize=length(x); lambda=[0.001:0.001:1.5]; %Declares lamda from 0 to 5 for n=1:1500 %Generates posterior of lambda pos(n)=((lambda(n)^(SampleSize-1))*exp(-lambda(n)*x_sum)); end; norm_const=sum(pos)*0.001; %To normalise area under graph = 1 pos=pos/norm_const; plot(lambda,pos); %Plots posterior as a function of lamda Title('Posterior of lambda, P(lambda|x)') xlabel('lambda'); ylabel('Probability Density'); grid on; %To check for proper normalisation, %Calculate Area Under Graph %y2handle=@(z) (z.^(SampleSize-1)).*exp(-z.*x_sum)/norm_const; %Ensure that Q2~1 for proper normalisation %Q2=quad(y2handle,0.01,5,1.0e-2) %To Calculate Max, Mean and Variance for n=1:1500 if pos(n)==max(pos) %Locates maximum posterior lambda_max=lambda(n) %Finds lambda for max posterior end; end; %Calculates mean lambda_mean=sum(lambda.*pos)*0.001 %Calculates std dev lambda_std=sqrt(sum((lambda.^2).*(pos*0.001))-lambda_mean^2)