protected override ConditionalGaussianDistributionParams GetPlainConditionalGaussianDistribution(BranchOrLeaf branchOrLeaf, OptimizationParameterList gaussianParameters) { double variance = gaussianParameters["Variance"].Value; //double alphaTimesVariance = gaussianParameters["AlphaVariance"].Value; double mean = gaussianParameters["Mean"].Value; double alpha = gaussianParameters["Alpha"].Value; double fOfBranchLength = FOfBranchLength(branchOrLeaf.Length, alpha); //ConditionalGaussianDistributionParams plainConditionalDistribution = ConditionalGaussianDistributionParams.GetInstance(); //double root1MinusAlphaTimesBranchLength = Math.Sqrt(1 - alpha * branchOrLeaf.Length); double root1MinusFofBranchLength = Math.Sqrt(1 - FOfBranchLength(branchOrLeaf.Length, alpha)); // ax + b, Mean := b double meanForGaussDistParams = mean * (1.0 - root1MinusFofBranchLength); //double meanForGausDistParams = mean * (1.0 - root1MinusAlphaTimesBranchLength); // ax + b = x, LinearCoefficent := a double linearCoeffForGaussDistParams = root1MinusFofBranchLength; //double linearCoeffForGausDistParams = root1MinusAlphaTimesBranchLength; //double varForGausDistParams = alphaTimesVariance * branchOrLeaf.Length; double varianceForGaussDistParams = fOfBranchLength * variance; ConditionalGaussianDistributionParams plainConditionalDistribution = ConditionalGaussianDistributionParams.GetInstance(meanForGaussDistParams, varianceForGaussDistParams, linearCoeffForGaussDistParams); return(plainConditionalDistribution); }
public ConditionalGaussianDistributionParams CreateDistributionGaussianOrNull( Leaf leaf, OptimizationParameterList gaussianParameters, Converter <Leaf, SufficientStatistics> predictorLeafToBoolStats) { if (leaf.Length == 0) { Debug.WriteLine("Branch length of zero observed"); } // TODO: make continuous BooleanStatistics hasPredictor = (BooleanStatistics)predictorLeafToBoolStats(leaf); if (hasPredictor.IsMissing()) { ConditionalGaussianDistributionParams.GetNullInstance(); //return null; // Predictor data is missing, so skip this leaf. } ConditionalGaussianDistributionParams plainConditionalDistribution = GetPlainConditionalGaussianDistribution(leaf, gaussianParameters); if (!hasPredictor) { return(plainConditionalDistribution); } else { double delta = GetOffset(gaussianParameters); ConditionalGaussianDistributionParams offsetConditionalDistribution = plainConditionalDistribution.AddOffsetToMean(delta); return(offsetConditionalDistribution); } //return CreateDistributionGaussianOrNull(plainConditionalDistribution, hasPredictor, delta); }
protected override ConditionalGaussianDistributionParams GetPlainConditionalGaussianDistribution(BranchOrLeaf branchOrLeaf, OptimizationParameterList gaussianParameters) { double variance = gaussianParameters["Variance"].Value; ConditionalGaussianDistributionParams plainConditionalDistribution = ConditionalGaussianDistributionParams.GetInstance(0, variance, 1); return(plainConditionalDistribution); }
public ConditionalGaussianDistributionParams CreateDistributionGaussianOrNull(Branch branch, OptimizationParameterList gaussianParameters) { if (branch.Length == 0) { Debug.WriteLine("Branch length of zero observed"); } ConditionalGaussianDistributionParams plainConditionalDistribution = GetPlainConditionalGaussianDistribution(branch, gaussianParameters); return(plainConditionalDistribution); }
public override IMessage InitializeMessage(Leaf leaf, OptimizationParameterList gaussianParameters) { if (IsMissing(leaf)) { return(null); } //ConditionalGaussianDistribution distOrNull = GaussianDistribution.CreateDistributionGaussianOrNull(leaf, gaussianParameters, _caseNameToHasPredictor); ConditionalGaussianDistributionParams distOrNull = GaussianDistribution.CreateDistributionGaussianOrNull(leaf, gaussianParameters, LeafToPredictorStatistics); if (distOrNull.IsNull) { return(null); } MessageGaussian message = Initalize(distOrNull, leaf, gaussianParameters); return(message); }
protected override ConditionalGaussianDistributionParams GetPlainConditionalGaussianDistribution(BranchOrLeaf branchOrLeaf, OptimizationParameterList gaussianParameters) { // NOTE: For BM, interpret Alpha as AlphaTimesVariance. Alpha is only used to multiply against variance, and searching is easier we we do // it this way, as otherwise alphaTimesVariance is affected by both alpha and variance when we're searching. double alphaTimesVariance = gaussianParameters["Alpha"].Value; //double alpha = gaussianParameters["Alpha"].Value; //double variance = gaussianParameters["Variance"].Value; // 0 (const part of mean) ax + b, Mean := b double mean = 0; // 1 (ax + b = x), linearCoefficent := a. double linearCoefficient = 1; //double conditionalVariance = alpha * variance * branchOrLeaf.Length; double conditionalVariance = alphaTimesVariance * branchOrLeaf.Length; ConditionalGaussianDistributionParams plainConditionalDistribution = ConditionalGaussianDistributionParams.GetInstance(mean, conditionalVariance, linearCoefficient); return(plainConditionalDistribution); }
private MessageGaussian Initalize(ConditionalGaussianDistributionParams dist, Leaf leaf, OptimizationParameterList gaussianParameters) { Debug.Assert(!LeafToTargetStatistics(leaf).IsMissing()); GaussianStatistics gaussianStatistics = (GaussianStatistics)LeafToTargetStatistics(leaf); SpecialFunctions.CheckCondition(gaussianStatistics != null, "why is caseNameToTargetOrNull unknown?"); if (gaussianStatistics.SampleSize == 1) { double z = gaussianStatistics.Mean; double logK = -Math.Log(dist.LinearCoefficient); Debug.Assert(!double.IsNaN(logK)); // real assert - in release mode, the NaN is OK. double a = (z - dist.Mean) / dist.LinearCoefficient; double v = dist.Variance / Math.Pow(dist.LinearCoefficient, 2); MessageGaussian message = MessageGaussian.GetInstance(logK, a, v); return(message); } else { double vNoise = GaussianDistribution.GetSamplingVariance(gaussianParameters); double logKMult = ( -Math.Log(gaussianStatistics.SampleSize) - gaussianStatistics.SampleSize * (gaussianStatistics.Variance + vNoise * Log2PI + vNoise * Math.Log(vNoise)) / vNoise + Math.Log(2 * Math.PI * vNoise) ) / 2.0; double aMult = gaussianStatistics.Mean; double vMult = vNoise / (double)gaussianStatistics.SampleSize; double logK = logKMult - Math.Log(dist.LinearCoefficient); double a = (aMult - dist.Mean) / dist.LinearCoefficient; double v = (vMult + dist.Variance) / Math.Pow(dist.LinearCoefficient, 2); MessageGaussian message = MessageGaussian.GetInstance(logK, a, v); return(message); } }
internal ConditionalGaussianDistributionParams AddOffsetToMean(double delta) { return(ConditionalGaussianDistributionParams.GetInstance(Mean + delta, Variance, LinearCoefficient)); }