----------------------THE BASIC AGE MODELS---------------------------------

The central age model and 3-component minimum-age model (Galbraith et al., 1999),
+ the unlogged version (Arnold et al., 2009) are included here. 
These have been translated from various other versions,
I assume the originals were written by Rex Galbraith.

Data must be in the matlab workspace in two columns with no headers
To do this type these two lines in the Command Window:
de=[];
openvar de
then paste the two columns [de se] in the Variable Editor

CENTRAL AGE MODEL:--------------------------------
With your data in the variable called de, type/paste this in the command line:

CAM=getCAM(de)

This will return four numbers in the variable CAM.
The numbers are [Central_age error Overdispersion error]

MINIMUM AGE MODEL:--------------------------------
type/paste the following:

MAM3=getmam3(de,sigma_b,start)

Where sigma_b is a scalar value e.g. 0.20, and start is a suitable starting value for the minimum age.
e.g. MAM3=getmam3(de,0.20,1) for sigma_b of 20% and De expected somewhere near 1 Gy.
The output will be 4 numbers in the variable MAM3
Output numbers: [min_age error sigma P],
where sigma and P are the 2 other components in the model (see Galbraith et al. 1999)
To call the unlogged version, enter:

MAM3ul=getmam3ul(de,sigma_b,start)


----------------------BOOTSTRAP UNCERTAINTIES -----------------------------
Estimate the uncertainty on the age model using a simple non-parametric bootstrap.
For the minimum age models, this option allows sigma_b to be stated with error term
When using the MAM3ul, the Poor Bleaching score of Wallinga et al. (2012) will also be calculated


for BOOTSTRAP CAM enter the following line:
argout = Bootmodel(de)
The output format will be [mean_central_age boot_error mean_overdispersion boot_error]
These numbers should be very similar to the basic CAM output.


for BOOTSTRAP MAM3 enter one of the following lines:
% argout = Bootmodel(de,'mam3',0.2)           % for sigma_b of 20%
% argout = Bootmodel(de,'mam3',0.2,1)         % for sigma_b of 20% and start_val of 1
% argout = Bootmodel(de,'mam3',[0.2 0.03],1)  % for sigma_b of 20+-3% and start_val of 1

Entering an uncertainty on sigma_b is optional, so is the starting value. 
If no starting value is given, a reasonable one will be generated automatically.
The output format is [mean_min_age boot_error mean_sigma mean_P]


for BOOTSTRAP MAM3ul enter one of the following lines:
% argout = Bootmodel(de,'mam3ul',0.2)           % for sigma_b of 20%
% argout = Bootmodel(de,'mam3ul',0.2,1)         % for sigma_b of 20% and start_val of 1
% argout = Bootmodel(de,'mam3ul',[0.2 0.03],1)  % for sigma_b of 20+-3% and start_val of 1

i.e. the same input as with the mam3. The output has two extra values
[mean_min_age boot_error mean_sigma mean_P PB_score boot_error]
Where PB_score is the Poor Bleaching score of Wallinga et al. (2012).



-------------------------BOOTSTRAP LIKELIHOODS-----------------------------
---------------------------------------------------------------------------
A step-by-step guide to creating bootstrap likelihoods follows
A complete example is given at the end.
Please cite Cunningham & Wallinga (2012); feedback should be directed to Alastair Cunningham



DATA FORMAT:
The data must be in a structured array. Create by pasting the following at the command line:

struc(1).de=[];
struc(1).dr=1;            % or your calculated Dr
struc(1).name='Sample1';  % give the sample a name, use single quotes.
struc(1).start=1;         % give a suitable starting value for the MAM3

Now insert the De data by typing

openvar struc(1).de

and pasting the 2 columns of data in the variable editor. 
Another valid variable name may be used in place of 'struc'.
You might want to save the structure by entering:

save struc struc % this will create a file called struc.mat 
or e.g. 
save NewName struc  % to save struc to a file called NewName.mat

The benifit of the structure array is that multiple samples can be processed.
Enter data for a second sample by replacing (1) with (2) in the above input.


MAKE BOOTSTRAP REPLICATES:
enter the following line at the command window:

struc = BootRecycling(struc,Psigb,M,mamflag)

Inputs:
    struc   the structured array created above
    Psigb   sigma_b and uncertainty in square brackets, e.g. [0.20 0.04]
    M       Number of first-level bootstrap replicates. N is set to 3M. 
            keep M small to start with (e.g. 200), make it bigger for final processing (e.g 2000)
    mamflag 1 = MAM3
            2 = MAM3ul

The output will overwrite the variable 'struc', adding a field called 'pairs'.
give the output variable a new name if you don't want to overwrite the original.


MAKE THE LIKELIHOOD FILES
enter the following line at the command window:

struc=MakeLikelihood(struc,deg,USS,datum,formatflag)

Inputs:
    struc   the structured array after running BootRecycling
    deg     the degree of the polynomial fit; suggest 6 or 7
    USS     unshared systematic error; suggest about 0.035
    datum   Year AD of measurement
    formatflag  date format of the output text file:
                1 = years ad
                2 = years before datum
                3 = ka before datum
                4 = on the De scale

This script does two things:
1. fits a curve through the bootstrap data, storing it in the field 'struc.likelihood'.
2. Creates a textfile 'SampleName.prior' suitable for OxCal

A plot of the fit will be shown. If it doesn't look good, change the degree of the polynomial,
or redo the BootRecycling with larger M.



---------------------------------------------------------------------------
EXAMPLE paste the following:

% create the data structure:
load SGstruc    % load the SG data from our paper
clear struc     % clear anything else called struc

struc.de=SGstruc(4).de;     % copy de data from our SG sample 4
struc.dr=SGstruc(4).dr;
struc.name='Sample4';
struc.start=0.8;

% make the bootstrap pairs:
struc=BootRecycling(struc,[0.20 0.04],200,2)
save struc struc    % save the variable 'struc' to a file called 'struc.mat' 

% Fit the data and create text file:
struc=MakeLikelihood(struc,6,0.035,2010,1)
save struc struc









