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);
        }
        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);
            }
        }