function NLL=NLLPss(Bet,Y,Bss);
%
%**************************************************************************
%Illustration of non-stationary extreme value modelling in MATLAB accompanying 
%Threshold modeling of nonstationary extremes
%Paul Northrop, Philip Jonathan and David Randell
%in 
%Extreme Value Modeling and Risk Analysis: Methods and Applications
%Dipak Dey and Jun Yan (Editors)
%2015
%**************************************************************************
%
%**************************************************************************
%NEGATIVE LOG LIKELIHOOD FOR POISSON RATE
%
%Bet      p x 1 Spline parameter vector (for Poisson rate)
%Y        n x 1 Responses
%Bss      n x p Spline basis evaluated at the covariate values corresponding to responses
%
%NLL      1 x 1 Negative log likelihood
%**************************************************************************

Rat=Bss*Bet; %Rate at covariate values corresponding to responses

%Tolerance
tol=1e-5; %tolerance value

%Exception handling
if any(Rat<tol); %rate too near to zero
    NLL=10^10;
    return;
end;
if any(mod(Y,1)~=0); %non-integer data
    error('Poisson distribution is not defined for non-integer data');
end;

%Calculate the sample penalised negative log likelihood
%Sum over individuals to get negative log likelihood
%Add roughness penalty term
NLL=sum(Rat-Y.*log(Rat)); %negative log likelihood

return;