<?xml version="1.0"?>
<!-- $Id: testCoalescent.xml,v 1.2 2005/07/11 23:22:40 rambaut Exp $ -->
<!-- Tests the coalescent prior -->

<beast>

    <taxa id="taxa1">
        <taxon id="A"/>
        <taxon id="B"/>
        <taxon id="C"/>
        <taxon id="D"/>
    </taxa>

    <date id="date1" value="5000" units="days" direction="backwards"/>
    <date id="date2" value="0" units="days" direction="backwards"/>

    <taxa id="taxa2">
        <taxon id="E">
            <date idref="date1"/>
        </taxon>
        <taxon id="F">
            <date idref="date1"/>
        </taxon>
        <taxon id="G">
            <date idref="date2"/>
        </taxon>
        <taxon id="H">
            <date idref="date2"/>
        </taxon>
    </taxa>

    <!-- create a demographic model and intialize currentPopulationSize parameter -->
    <constantSize id="demo1" units="years">
        <populationSize>
            <parameter id="demo1.pop1" value="10000.0"/>
        </populationSize>
    </constantSize>

    <!-- simulate a coalescent tree -->
    <coalescentTree id="tree1">
        <constantSize idref="demo1"/>
        <taxa idref="taxa1"/>
    </coalescentTree>

    <!-- simulate a coalescent tree -->
    <coalescentTree id="tree2">
        <constantSize idref="demo1"/>
        <taxa idref="taxa2"/>
    </coalescentTree>

    <treeModel id="treeModel1">
        <coalescentTree idref="tree1"/>
        <rootHeight>
            <parameter id="treeModel1.rootHeight"/>
        </rootHeight>
        <nodeHeights rootNode="false" internalNodes="true">
            <parameter id="treeModel1.internalNodeHeights"/>
        </nodeHeights>
        <nodeHeights rootNode="true" internalNodes="true">
            <parameter id="treeModel1.allNodeHeights"/>
        </nodeHeights>
    </treeModel>

    <treeModel id="treeModel2">
        <coalescentTree idref="tree2"/>
        <rootHeight>
            <parameter id="treeModel2.rootHeight"/>
        </rootHeight>
        <nodeHeights rootNode="false" internalNodes="true">
            <parameter id="treeModel2.internalNodeHeights"/>
        </nodeHeights>
        <nodeHeights rootNode="true" internalNodes="true">
            <parameter id="treeModel2.allNodeHeights"/>
        </nodeHeights>
    </treeModel>

    <!-- create a coalescent model from a tree model and demographic model -->
    <coalescentLikelihood id="coalescent1">
        <model>
            <constantSize idref="demo1"/>
        </model>
        <populationTree>
            <treeModel idref="treeModel1"/>
        </populationTree>
    </coalescentLikelihood>

    <!-- create a second coalescent model -->
    <coalescentLikelihood id="coalescent2">
        <model>
            <constantSize idref="demo1"/>
        </model>
        <populationTree>
            <treeModel idref="treeModel2"/>
        </populationTree>
    </coalescentLikelihood>

    <mcmc id="mcmc1" chainLength="1000000" verbose="false">
        <coalescentLikelihood idref="coalescent1"/>
        <log id="logScreen2" logEvery="5000">
            <column dp="4">
                <coalescentLikelihood idref="coalescent1"/>
                <parameter idref="treeModel1.rootHeight"/>
            </column>
        </log>
        <log id="log1" logEvery="5" fileName="coalescentPrior.log">
            <coalescentLikelihood idref="coalescent1"/>
            <parameter idref="treeModel1.rootHeight"/>
        </log>
        <logTree id="logTree1" logEvery="100" nexusFormat="true"
                 fileName="coalescentPrior.trees">
            <treeModel idref="treeModel1"/>
        </logTree>
        <operators>
            <subtreeSlide weight="1" gaussian="true" size="14000">
                <treeModel idref="treeModel1"/>
            </subtreeSlide>
        </operators>
    </mcmc>

    <report>
        Time taken:
        <property name="timer">
            <mcmc idref="mcmc1"/>
        </property>
    </report>

    <traceAnalysis fileName="coalescentPrior.log" burnIn="1000">
        <expectation name="treeModel1.rootHeight" value="15000"/>
    </traceAnalysis>

    <treeTraceAnalysis fileName="coalescentPrior.trees"/>


    <mcmc id="mcmc2" chainLength="1000000" verbose="false">
        <coalescentLikelihood idref="coalescent2"/>
        <log id="logScreen" logEvery="5000">
            <column dp="4">
                <coalescentLikelihood idref="coalescent2"/>
                <parameter idref="treeModel2.rootHeight"/>
            </column>
        </log>
        <log id="log2" logEvery="5" fileName="coalescentPrior_2.log">
            <coalescentLikelihood idref="coalescent2"/>
            <parameter idref="treeModel2.rootHeight"/>
        </log>
        <logTree id="logTree2" logEvery="100" nexusFormat="true"
                 fileName="coalescentPrior_2.trees">
            <treeModel idref="treeModel2"/>
        </logTree>
        <operators>
            <subtreeSlide weight="1" gaussian="true" size="14000">
                <treeModel idref="treeModel2"/>
            </subtreeSlide>
        </operators>
    </mcmc>

    <report>
        Time taken:
        <property name="timer">
            <mcmc idref="mcmc2"/>
        </property>
    </report>

    <traceAnalysis fileName="coalescentPrior_2.log" burnIn="1000">
        <!-- the expectation for 4 leaves with Theta=10000 and tau=5000 -->
        <expectation name="treeModel2.rootHeight" value="19344.2178"/>
    </traceAnalysis>

    <treeTraceAnalysis fileName="coalescentPrior_2.trees"/>


</beast>


