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

<beast>
	
	<!-- Data likelihoods -->
	<distributionLikelihood id="samplingDensity.random">
		<distribution>
			<normalDistributionModel>
				<mean>
   					<parameter id="mu" value="0"/>
		 		</mean>
		 		<precision>
   					<parameter id="tau" value="1"/>
				</precision>
			</normalDistributionModel>
		</distribution>
		<data>
			<parameter id="x" value="5"/>
 		</data>
	</distributionLikelihood>
	
	<distributionLikelihood id="samplingDensity.fixed">
		<distribution>
			<normalDistributionModel>
				<mean>
   					<parameter idref="mu"/>
		 		</mean>
		 		<precision>
   					<parameter id="tau.fixed" value="1"/>
				</precision>
			</normalDistributionModel>
		</distribution>
		<data>
			<parameter idref="x"/>
 		</data>
	</distributionLikelihood>
	
	<!-- Prior under fixed model -->
	<normalPrior id="normalMeanPrior.fixed" mean="0" stdev="1">
        <parameter idref="mu"/>
    </normalPrior>
    
    <!-- Prior under random model -->    
    <distributionLikelihood id="normalMeanPrior.random">
    	<distribution>
    		<normalDistributionModel id="priorDistributionModel.random">
		    	<mean>
    				<parameter value="0"/>
		    	</mean>
		    	<precision>
    				<parameter idref="tau"/>
		    	</precision>
		    </normalDistributionModel>
    	</distribution>
    	<data>
    		<parameter idref="mu"/>
    	</data>
    </distributionLikelihood>

    <gammaPrior id="gammaPrecisionPrior" shape="1" scale="1" offset="0">
        <parameter idref="tau"/>
    </gammaPrior>
    
   <operators id="operators.fixed">
        <randomWalkOperator windowSize="0.75" weight="1">
        	<parameter idref="mu"/>
        </randomWalkOperator>
	</operators>    
    
   <operators id="operators.random">
        <randomWalkOperator windowSize="0.75" weight="1">
        	<parameter idref="mu"/>
        </randomWalkOperator>
        <scaleOperator scaleFactor="0.75" weight="1">
            <parameter idref="tau"/>
        </scaleOperator>
	</operators>
	  
	<mcmc id="mcmc.fixed" chainLength="1000000" autoOptimize="true">
        <posterior id="posterior.fixed">
            <likelihood id="likelihood.fixed">
                <distributionLikelihood idref="samplingDensity.fixed"/>
            </likelihood>
            <prior id="prior.fixed">
                <normalPrior idref="normalMeanPrior.fixed"/>
                <gammaPrior idref="gammaPrecisionPrior"/>
            </prior>
        </posterior>
        <operators idref="operators.fixed"/>
		<log logEvery="100000">
			<column label="posterior" dp="4" width="12">
				<likelihood idref="likelihood.fixed"/>
                <prior idref="prior.fixed"/>
			</column>
			<column label="Mean" sf="6" width="12">
				<parameter idref="mu"/>
			</column>
			<column label="Precision" sf="6" width="12">
				<parameter idref="tau"/>
			</column>
		</log>
		<log logEvery="1000" fileName="testSimplePathSampling.fixed.log">
			<likelihood idref="likelihood.fixed"/>
            <prior idref="prior.fixed"/>
			<parameter idref="mu"/>
			<parameter idref="tau"/>            
		</log>
	</mcmc>	
	
	<traceAnalysis fileName="testSimplePathSampling.fixed.log" stdError="true"/>
	
	<marginalLikelihoodAnalysis fileName="testSimplePathSampling.fixed.log">
        <likelihoodColumn name="likelihood.fixed" harmonicOnly="true" bootstrap="true"/>
    </marginalLikelihoodAnalysis> 
    
    <marginalLikelihoodEstimator chainLength="500000" pathSteps="11" pathScheme="oneSidedBeta">
    	<samplers>
	        <mcmc idref="mcmc.fixed"/>
	    </samplers>
        <pathLikelihood id="pathLikelihood.fixed">
            <source>
                <posterior idref="posterior.fixed"/>
            </source>
            <destination>
                <prior idref="prior.fixed"/>
            </destination>
        </pathLikelihood>
        <log logEvery="100" fileName="testSimplePathSampling.fixed.log">
            <pathLikelihood idref="pathLikelihood.fixed"/>
        </log>
    </marginalLikelihoodEstimator>
    
   	<pathSamplingAnalysis fileName="testSimplePathSampling.fixed.log">
        <likelihoodColumn name="pathLikelihood.fixed.delta"/>
        <thetaColumn name="pathLikelihood.fixed.theta"/>      
    </pathSamplingAnalysis>
    
    <report>
    	Fixed logML = -0.5 * log(4 * \pi) - x^2 / 4 = -7.5155 
    	
    </report>
    
	<mcmc id="mcmc.random" chainLength="1000000" autoOptimize="true">
        <posterior id="posterior.random">
            <likelihood id="likelihood.random">
                <distributionLikelihood idref="samplingDensity.random"/>
            </likelihood>
            <prior id="prior.random">
                <distributionLikelihood idref="normalMeanPrior.random"/>
                <gammaPrior idref="gammaPrecisionPrior"/>
            </prior>
        </posterior>
        <operators idref="operators.random"/>
		<log logEvery="100000">
			<column label="posterior" dp="4" width="12">
				<likelihood idref="likelihood.random"/>
                <prior idref="prior.random"/>
			</column>
			<column label="Mean" sf="6" width="12">
				<parameter idref="mu"/>
			</column>
			<column label="Precision" sf="6" width="12">
				<parameter idref="tau"/>
			</column>
		</log>
		<log logEvery="1000" fileName="testSimplePathSampling.random.log">
			<likelihood idref="likelihood.random"/>
            <prior idref="prior.random"/>
			<parameter idref="mu"/>
			<parameter idref="tau"/>            
		</log>
	</mcmc>	
	
	<traceAnalysis fileName="testSimplePathSampling.random.log" stdError="true"/>
	
	<marginalLikelihoodAnalysis fileName="testSimplePathSampling.random.log">
        <likelihoodColumn name="likelihood.random" harmonicOnly="true" bootstrap="true"/>
    </marginalLikelihoodAnalysis> 
    
    <marginalLikelihoodEstimator chainLength="500000" pathSteps="21" 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 logEvery="100" fileName="testSimplePathSampling.random.log">
            <pathLikelihood idref="pathLikelihood.random"/>
        </log>
    </marginalLikelihoodEstimator>
    
   	<pathSamplingAnalysis fileName="testSimplePathSampling.random.log">
        <likelihoodColumn name="pathLikelihood.random.delta"/>
        <thetaColumn name="pathLikelihood.random.theta"/>      
    </pathSamplingAnalysis>

	<report>
		Fixed logML = logGamma(3/2) - 0.5*log(4 * pi) - 3/2*log(1 + x^2 / 4) = -4.3578
		
	</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 logEvery="100" fileName="testSimplePathSampling.log">
             <pathLikelihood idref="pathLikelihood"/>
             <posterior idref="posterior.random"/>
             <posterior idref="posterior.fixed"/>             
         </log>
     </marginalLikelihoodEstimator>	

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

</beast>

<!--

logML.fixed = function(x) {
	-0.5*log(4*pi) - (x^2)/4
}

logML.random = function(x) {
	-0.5*log(4*pi) + log(gamma(3/2)) - (3/2)*log(1 + (x^2)/4)
}

## Example taken from J.M. Marin and C.P. Robert "On resolving the Savage-Dickey paradox"

-->