

% computes maximum of a*t-log phi(t)
% where phi is the mgf and a real number

%distribution: v=values, p=probabilities
v=[1 2 3 4 5 6]; 
p=(1/6)*ones(1,6); 

%p(1)=1/3; p(2)=0;

a=4.5;
t=[0.001:0.000001:1];

%computing the mgf

phip=exp(t'*v)*p';
phip=phip';

% phi1=exp(t);
% phi2=exp(6*t)-1;
% phi3=6*(exp(t)-1);
% phi=phi1.*phi2./phi3;

psi=a*t-log(phip);
plot(t,psi,'LineWidth', 2);
[rate,index]=max(psi);
fprintf('rate=%2.4f, attained at t=%2.4f \n', rate, t(index));
n=10;
fprintf('for n=%d, P(S_n>=%2.4f n)<=%e \n', n,a,exp(-n*rate));

xlabel('$t$', 'Interpreter', 'Latex','FontSize',10);
ylabel('$at-\log(\phi(t))$', 'Interpreter', 'Latex','FontSize',10);
grid on;
title('Large deviations','FontSize',10);
hold off;

 