<?xml version="1.0" standalone="yes"?>
<beast>
	
	<!--<taxa id="taxa">-->
		<!--<taxon id="A">-->
			<!--<attr name="X">2 1 10 1</attr>-->
		<!--</taxon>-->
		<!--<taxon id="B">-->
			<!--<attr name="X">1 2 2 2</attr>-->
		<!--</taxon>-->
		<!--<taxon id="C">-->
			<!--<attr name="X">3 3 3 30</attr>-->
		<!--</taxon>-->
	<!--</taxa>-->
	<taxa id="taxa">
		<taxon id="A">
			<attr name="X">2 15 10 1</attr>
		</taxon>
		<taxon id="B">
			<attr name="X">1 2 2 2</attr>
		</taxon>
		<taxon id="C">
			<attr name="X">3 3 3 30</attr>
		</taxon>
	</taxa>
			
	<newick id="tree">
		((A:1,B:1):1,C:2);
<!--		(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>
		<nodeTraits name="X" rootNode="false" internalNodes="false" leafNodes="true"
                    traitDimension="4"
                >
			<parameter id="leafTraits"/>
		</nodeTraits>
	</treeModel>

    <multivariateDiffusionModel id="diffusionModel">
		<precisionMatrix>

			<!--<matrixParameter id="precisionMatrix">-->
				<!--<parameter id="prec.col1" value="1"/>-->
			<!--</matrixParameter>-->

			<!--<matrixParameter id="precisionMatrix">-->
				<!--<parameter id="col1" value="2.00 0.50"/>-->
				<!--<parameter id="col2" value="0.50 1.00"/>-->
			<!--</matrixParameter>-->

			<matrixParameter id="precisionMatrix">
				<parameter id="col1" value="0.50 0.00"/>
				<parameter id="col2" value="0.00 1.00"/>
			</matrixParameter>-->

		</precisionMatrix>
	</multivariateDiffusionModel>
	
	<multivariateTraitLikelihood id="traitLikelihood" traitName="X"
                                 integrateInternalTraits="true"
                                 ignorePhylogeny="true">
		<multivariateDiffusionModel idref="diffusionModel"/>
		<treeModel idref="treeModel"/>
		<traitParameter>
			<parameter idref="leafTraits"/>
		</traitParameter>		
        <conjugateRootPrior>
            <meanParameter>
                <parameter value="4.0 4.0"/>
                <!--<parameter value="4.0"/>-->
            </meanParameter>
            <priorSampleSize>
                <parameter value="2.0"/>
            </priorSampleSize>
        </conjugateRootPrior>
	</multivariateTraitLikelihood>
		
	<operators id="operators">
        <dirtyLikelihood weight="1">
            <treeLikelihood idref="traitLikelihood"/>
        </dirtyLikelihood>
	</operators>
	
	<report>
		logL = <multivariateTraitLikelihood idref="traitLikelihood"/>
	</report>

	<!--<mcmc id="mcmc" chainLength="10">-->
		<!--<posterior id="posterior">-->
			<!--<treeLikelihood idref="traitLikelihood"/>-->
		<!--</posterior>-->
		<!--<operators idref="operators"/>-->
		<!--<log logEvery="1000">-->
			<!--<posterior idref="posterior"/>-->
		<!--</log>-->
		<!--<logTree logEvery="1" nexusFormat="true" fileName="testIntegratedTraitLikelihood.trees">-->
			<!--<multivariateTraitLikelihood idref="traitLikelihood"/>-->
			<!--<treeModel idref="treeModel"/>-->
		<!--</logTree>				-->
	<!--</mcmc>-->

</beast>

<!--
# R script
library(mvtnorm)

x.1 = c(10,1,10,1)
x.2 = c(2,2,2,2)
x.3 = c(3,3,3,3)

x = rbind(x.1,x.2,x.3)

v = matrix(c(2,0.5,0.5,1),nrow=2)


den = function(x, tau = 1, x1 = 2, x2 = 1, x3 = 3, x4 = 4,
        weight1 = 1, weight2 = 2, weight3 = 10, weight4 = 1) {
    dnorm(x-x1,sd=sqrt(1/(weight1*tau))) *
    dnorm(x-x2,sd=sqrt(1/(weight2*tau))) *
    dnorm(x-x3,sd=sqrt(1/(weight3*tau))) *
    dnorm(x-x4,sd=sqrt(1/(weight4*tau))) *
    1
}

log(integrate(den, -Inf, Inf, tau = 0.1)$value)

den.anal = function(tau = 0.1, x1 = 2, x2 = 1, x3 = 3, x4 = 4,
        weight1 = 1, weight2 = 2, weight3 = 10, weight4 = 1) {
    delta =  x1 * weight1 + x2 * weight2 + x3 * weight3 + x4 * weight4
    weight.total = weight1 + weight2 + weight3 + weight4
    tips = 3
    (tau / (2*pi))^(tips/2) * 
    sqrt(weight1 * weight2 * weight3 * weight4 / (weight.total)) *
    exp(-0.5 * ( 0
            + weight1 * x1 * tau * x1
            + weight2 * x2 * tau * x2
            + weight3 * x3 * tau * x3
            + weight4 * x4 * tau * x4
            - delta * tau * delta / weight.total
        )
    )
}

log(den.anal(tau=1,x1=2,x2=1,x3=3,weight1=1,weight2=1,weight3=1,weight4=2)) +
log(den.anal(tau=1,x1=15,x2=2,x3=3,weight1=1,weight2=1,weight3=1,weight4=2)) +
log(den.anal(tau=1,x1=10,x2=2,x3=3,weight1=1,weight2=1,weight3=1,weight4=2)) +
log(den.anal(tau=1,x1=1,x2=2,x3=30,weight1=1,weight2=1,weight3=1,weight4=2))
 
-->

