function I=CVGrp(nDat,nGrp,CVTyp);
%**************************************************************************
%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
%**************************************************************************
%
%**************************************************************************
%Allocate a sample to cross-validation groups
%nDat       1 x 1 sample size
%nGrp       1 x 1 number of CV groups
%CVTyp      1 x 1 'Block' (blocked by covariate) or 'Random' 
%
%I       nDat x 1 group allocation for each observation in the sample
%**************************************************************************

switch CVTyp;
    case  'Block'; %block cross-validation
        BckSze=floor(nDat/nGrp)';
        rBlc=rem(nDat,nGrp);
        CVSze=ones(nGrp,1)*BckSze+((1:nGrp)'<=rBlc);
        
        I=NaN(nDat,1);
        count=0;
        for i=1:nGrp
            I(count+(1:CVSze(i)))=i;
            count=count+CVSze(i);
        end
    case  'Random'; %random cross-validation
        I=randi(nGrp,nDat,1);
end;

return;