//private bool AllVarianceZero(Dictionary<string, GaussianStatistics> caseNameToTarget)
        private static bool AllVarianceZero(IEnumerable <Leaf> LeafCollection, Converter <Leaf, SufficientStatistics> caseNameToTarget)
        {
            bool varianceIsZero = false; //If empty input, then return false
            bool firstTime      = true;

            foreach (Leaf leaf in LeafCollection)
            {
                SufficientStatistics stats = caseNameToTarget(leaf);
                if (stats.IsMissing())
                {
                    continue;
                }
                GaussianStatistics gaussianStatistics = (GaussianStatistics)stats;
                SpecialFunctions.CheckCondition((gaussianStatistics.Variance == 0) == (gaussianStatistics.SampleSize == 1), "Variance must be zero exactly when the sample size is 1");
                if (firstTime)
                {
                    firstTime      = false;
                    varianceIsZero = (gaussianStatistics.Variance == 0);
                }
                else
                {
                    SpecialFunctions.CheckCondition(varianceIsZero == (gaussianStatistics.Variance == 0), "If any variances are zero, then all must be zero");
                }
            }
            return(varianceIsZero);
        }
        internal static bool Parse(string val, out SufficientStatistics result)
        {
            result = null;
            if (val.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                result = GetMissingInstance();
                return(false);
            }
            else
            {
                string[] fields = val.Split(',');
                if (!(fields.Length == 3))
                {
                    return(false);
                }
                double mean, variance;
                int    sampleSize;

                if (double.TryParse(fields[0], out mean) &&
                    double.TryParse(fields[1], out variance) &&
                    int.TryParse(fields[2], out sampleSize))
                {
                    result = GaussianStatistics.GetInstance(mean, variance, sampleSize);
                    return(true);
                }

                return(false);
            }
        }
 public static Converter <Leaf, SufficientStatistics> DictionaryToLeafMap(Dictionary <string, GaussianStatistics> caseIdToNonMissingValue)
 {
     return(delegate(Leaf leaf)
     {
         string name = leaf.CaseName;
         if (name == null || !caseIdToNonMissingValue.ContainsKey(name))
         {
             return GaussianStatistics.GetMissingInstance();
         }
         else
         {
             return caseIdToNonMissingValue[name];
         }
     });
 }
        public override bool Equals(object obj)
        {
            if (IsMissing() && obj is SufficientStatistics && ((SufficientStatistics)obj).IsMissing())
            {
                return(true);
            }
            GaussianStatistics other = obj as GaussianStatistics;

            if (other == null)
            {
                return(false);
            }

            return(_mean == other._mean && _variance == other._variance && _sampleSize == other._sampleSize);
        }
        public static SufficientStatistics Parse(string val)
        {
            SufficientStatistics result;

            if (
                MissingStatistics.Parse(val, out result) ||
                GaussianStatistics.Parse(val, out result) ||
                BooleanStatistics.Parse(val, out result) ||
                DiscreteStatistics.Parse(val, out result) ||
                ContinuousStatistics.Parse(val, out result))
            {
                return(result);
            }
            throw new ArgumentException(string.Format("Unable to parse {0} into an instance of ISufficientStatistics", val));
        }
        //public override Converter<Leaf, SufficientStatistics> CreateTargetSufficientStatisticsMap(Dictionary<string, ISufficientStatistics> caseIdToNonMissingValue)
        //{
        //    return ISufficientStatistics.DictionaryToLeafMap(caseIdToNonMissingValue);
        //}

        public override Converter <Leaf, SufficientStatistics> CreateAlternativeSufficientStatisticsMap(
            Converter <Leaf, SufficientStatistics> predictorDistributionClassFunction,
            Converter <Leaf, SufficientStatistics> targetDistributionClassFunction)
        {
            return(delegate(Leaf leaf)
            {
                SufficientStatistics predStats = predictorDistributionClassFunction(leaf);
                SufficientStatistics targetStats = targetDistributionClassFunction(leaf);


                // bail on missing data.
                if (predStats.IsMissing() || targetStats.IsMissing())
                {
                    return GaussianStatistics.GetMissingInstance();
                }
                else
                {
                    return targetStats;
                }
            });
        }
        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);
            }
        }