<?xml version="1.0" standalone="yes"?>
<beast>

	<taxa id="taxa">
		<taxon id="A"/>
		<taxon id="B"/>
		<!--<taxon id="C"/>-->
	</taxa>
	
	<alignment id="alignment" dataType="nucleotide">
		<sequence> <taxon idref="A"/> AGAGCTCTAAGAGCTCTAAGAGCTCTAAGAGCTCTA </sequence>
		<sequence> <taxon idref="B"/> AAGGCCTTTAAGGCCTTTAAGGCCTTTAAGGCCTTT </sequence>
	</alignment>
	
	<patterns id="patterns" strip="false">
		<alignment idref="alignment"/>
	</patterns>
	
	<newick id="tree">
        (A:1,B:1);
	</newick>
	
	<treeModel id="treeModel">
		<newick idref="tree"/>
		<rootHeight>
			<parameter id="treeModel.rootHeight"/>
		</rootHeight>
		<nodeHeights internalNodes="true">
			<parameter id="treeModel.internalNodeHeights"/>
		</nodeHeights>
		<nodeHeights internalNodes="true" rootNode="true">
			<parameter id="treeModel.allInternalNodeHeights"/>
		</nodeHeights>
	</treeModel>
	
	<hkyModel id="hky.random">
	  <frequencies>
	    <frequencyModel dataType="nucleotide">
	      <frequencies>
         	<parameter id="hky.frequencies" value="0.25 0.25 0.25 0.25"/>
	      </frequencies>
	    </frequencyModel>
	  </frequencies>
	  <kappa>
	    <parameter id="hky.kappa" value="1.0" lower="1.0E-8" upper="Infinity"/>
	  </kappa>
	</hkyModel>
	
	<hkyModel id="hky.fixed">
	  <frequencies>
	    <frequencyModel dataType="nucleotide">
	      <frequencies>
		   	<parameter idref="hky.frequencies"/>
	      </frequencies>
	    </frequencyModel>
	  </frequencies>
	  <kappa>
	    <parameter id="hky.kappa.fixed" value="10.0"/>
	  </kappa>
	</hkyModel>
	
	<siteModel id="siteModel.random">
	  <substitutionModel>
	    <hkyModel idref="hky.random"/>
	  </substitutionModel>
	  <mutationRate>
	    <parameter id="clock.rate"  value="1.0" lower="0.0" upper="10.0"/>
	  </mutationRate>
	</siteModel>
	
	<siteModel id="siteModel.fixed">
	  <substitutionModel>
	    <hkyModel idref="hky.fixed"/>
	  </substitutionModel>
	  <mutationRate>
	    <parameter idref="clock.rate"/>
	  </mutationRate>
	</siteModel>	

	<treeLikelihood id="treeLikelihood.random">
	  <patterns idref="patterns"/>
	  <treeModel idref="treeModel"/>
	  <siteModel idref="siteModel.random"/>
	  <hkyModel idref="hky.random"/>	  
	</treeLikelihood>	

	<treeLikelihood id="treeLikelihood.fixed">
	  <patterns idref="patterns"/>
	  <treeModel idref="treeModel"/>
	  <siteModel idref="siteModel.fixed"/>
	  <hkyModel idref="hky.fixed"/>	  
	</treeLikelihood>	
	
	<operators id="operators.random">
        <scaleOperator weight="1" scaleFactor="0.75">
        	<parameter idref="hky.kappa"/>
        </scaleOperator>
        <scaleOperator weight="1" scaleFactor="0.75">
        	<parameter idref="treeModel.rootHeight"/>
        </scaleOperator>
	</operators>
	
	<operators id="operators.fixed">
        <scaleOperator weight="1" scaleFactor="0.75">
        	<parameter idref="treeModel.rootHeight"/>
        </scaleOperator>
	</operators>	
			
	<mcmc id="mcmc.random" chainLength="1000000">
		<posterior id="posterior.random">
			<prior id="prior.random">
				<gammaPrior offset="0" shape="0.1" scale="1.0">
					<parameter idref="hky.kappa"/>
				</gammaPrior>
				<gammaPrior offset="0" shape="1" scale="1">
					<parameter idref="treeModel.rootHeight"/>
				</gammaPrior>				
			</prior>
			<likelihood id="likelihood.random">
				<treeLikelihood idref="treeLikelihood.random"/>
			</likelihood>
		</posterior>
		<operators idref="operators.random"/>
		<log id="lograndom50000" logEvery="50000">
			<posterior idref="posterior.random"/>
			<likelihood idref="likelihood.random"/>
			<prior idref="prior.random"/>
			<parameter idref="hky.kappa"/>
			<parameter idref="treeModel.rootHeight"/>
		</log>
        <log id="lograndom1000" logEvery="1000" fileName="testTreePathSampling.random.log" >
            <posterior idref="posterior.random"/>
			<likelihood idref="likelihood.random"/>
			<prior idref="prior.random"/>
			<parameter idref="hky.kappa"/>
			<parameter idref="treeModel.rootHeight"/>
        </log>				
	</mcmc>

	<marginalLikelihoodAnalysis fileName="testTreePathSampling.random.log">
        <likelihoodColumn name="likelihood.random" harmonicOnly="true" bootstrap="true"/>
    </marginalLikelihoodAnalysis> 
    
    <marginalLikelihoodEstimator chainLength="50000" pathSteps="20" pathScheme="oneSidedBeta" beta="0.25">
    	<samplers>
	        <mcmc idref="mcmc.random"/>
	    </samplers>
        <pathLikelihood id="pathLikelihood.random">
            <source>
                <posterior idref="posterior.random"/>
            </source>
            <destination>
                <prior idref="prior.random"/>
            </destination>
        </pathLikelihood>
        <log id="testMarginalOne" logEvery="100" fileName="testTreePathSamplingMLE.random.log">
            <pathLikelihood idref="pathLikelihood.random"/>
        </log>
    </marginalLikelihoodEstimator>
    
   	<pathSamplingAnalysis fileName="testTreePathSamplingMLE.random.log">
        <likelihoodColumn name="pathLikelihood.random.delta"/>
        <thetaColumn name="pathLikelihood.random.theta"/>      
    </pathSamplingAnalysis>
    
    <report>
    	True logMarginalLikelihood = -97.9 (random kappa)
    </report>
    
    <mcmc id="mcmc.fixed" chainLength="1000000">
		<posterior id="posterior.fixed">
			<prior id="prior.fixed">
				<gammaPrior offset="0" shape="1" scale="1">
					<parameter idref="treeModel.rootHeight"/>
				</gammaPrior>	
				<!-- Pseudo-prior -->
				<gammaPrior offset="0" shape="0.1" scale="1.0">
					<parameter idref="hky.kappa"/>
				</gammaPrior>
			</prior>
			<likelihood id="likelihood.fixed">
				<treeLikelihood idref="treeLikelihood.fixed"/>
			</likelihood>
		</posterior>
		<operators idref="operators.fixed"/>
		<log id="logfixed50000" logEvery="50000">
			<posterior idref="posterior.fixed"/>
			<likelihood idref="likelihood.fixed"/>
			<prior idref="prior.fixed"/>
			<parameter idref="hky.kappa.fixed"/>
			<parameter idref="treeModel.rootHeight"/>
		</log>
        <log id="logfixed1000" logEvery="1000" fileName="testTreePathSampling.fixed.log" >
            <posterior idref="posterior.fixed"/>
			<likelihood idref="likelihood.fixed"/>
			<prior idref="prior.fixed"/>
			<parameter idref="hky.kappa.fixed"/>
			<parameter idref="treeModel.rootHeight"/>
        </log>				
	</mcmc>

	<marginalLikelihoodAnalysis fileName="testTreePathSampling.fixed.log">
        <likelihoodColumn name="likelihood.fixed" harmonicOnly="true" bootstrap="true"/>
    </marginalLikelihoodAnalysis> 
    
    <marginalLikelihoodEstimator chainLength="50000" pathSteps="20" pathScheme="betaquantile" alpha="0.30">
    	<samplers>
	        <mcmc idref="mcmc.fixed"/>
	    </samplers>
        <pathLikelihood id="pathLikelihood.fixed">
            <source>
                <posterior idref="posterior.fixed"/>
            </source>
            <destination>
                <prior idref="prior.fixed"/>
            </destination>
        </pathLikelihood>
        <log id="testMarginalTwo" logEvery="100" fileName="testTreePathSamplingMLE.fixed.log">
            <pathLikelihood idref="pathLikelihood.fixed"/>
        </log>
    </marginalLikelihoodEstimator>
    
   	<pathSamplingAnalysis fileName="testTreePathSamplingMLE.fixed.log">
        <likelihoodColumn name="pathLikelihood.fixed.delta"/>
        <thetaColumn name="pathLikelihood.fixed.theta"/>      
    </pathSamplingAnalysis>
    
    <report>
    	True logMarginalLikelihood = -89.40 (kappa = 10)
    </report>
    
    <marginalLikelihoodEstimator chainLength="50000" pathSteps="20" pathScheme="sigmoid" alpha="8.0">
    	<samplers>
	        <mcmc idref="mcmc.fixed"/>
	    </samplers>
        <pathLikelihood id="pathLikelihood.sigmoid">
            <source>
                <posterior idref="posterior.fixed"/>
            </source>
            <destination>
                <prior idref="prior.fixed"/>
            </destination>
        </pathLikelihood>
        <log id="testMarginalThree" logEvery="100" fileName="testTreePathSamplingMLE.sigmoid.log">
            <pathLikelihood idref="pathLikelihood.sigmoid"/>
        </log>
    </marginalLikelihoodEstimator>
    
   	<pathSamplingAnalysis fileName="testTreePathSamplingMLE.sigmoid.log">
        <likelihoodColumn name="pathLikelihood.sigmoid.delta"/>
        <thetaColumn name="pathLikelihood.sigmoid.theta"/>      
    </pathSamplingAnalysis>
    
    <report>
    	True logMarginalLikelihood = -89.40 (kappa = 10)
    </report>
    
    <marginalLikelihoodEstimator chainLength="500000" pathSteps="21" pathScheme="beta" alpha="0.5" beta="0.25">
		<samplers>
			<mcmc idref="mcmc.random"/>
			<mcmc idref="mcmc.fixed"/>
		</samplers>
		<pathLikelihood id="pathLikelihood">
             <source>
                 <posterior idref="posterior.fixed"/>
             </source>
             <destination>
                 <posterior idref="posterior.random"/>
             </destination>
         </pathLikelihood>
         <log id="testBayesFactor" logEvery="100" fileName="testTreePathSamplingBF.log">
             <pathLikelihood idref="pathLikelihood"/>
             <posterior idref="posterior.random"/>
             <posterior idref="posterior.fixed"/> 
         </log>
     </marginalLikelihoodEstimator>	

	 <pathSamplingAnalysis fileName="testTreePathSamplingBF.log">
        <likelihoodColumn name="pathLikelihood.delta"/>
        <thetaColumn name="pathLikelihood.theta"/>      
    </pathSamplingAnalysis>
    
    <report>
    	Analytic answer = 8.5    	
    </report>

        
</beast>
