/// <summary>
        /// Add two Gaussian Statistics.
        /// </summary>
        /// <param name="x">GaussianStatistics x.</param>
        /// <param name="y">GaussianStatistics y.</param>
        /// <returns>Returns added result of two Gaussian Statistics.</returns>
        public static GaussianStatistics Add(GaussianStatistics x, GaussianStatistics y)
        {
            if (x == null)
            {
                throw new ArgumentNullException("x");
            }

            if (y == null)
            {
                throw new ArgumentNullException("y");
            }

            int    rN    = x.SampleSize + y.SampleSize;
            double rMean = (x.SampleSize * x.Mean + y.SampleSize * y.Mean) / rN;
            double rVar  = (x.SumOfSquares + y.SumOfSquares) / rN - rMean * rMean;

            if (rVar < 0)
            {
                Helper.CheckCondition(rVar > -1e-10, "Computed negative variance! " + rVar);
                rVar = 0;
            }

            GaussianStatistics result = GaussianStatistics.GetInstance(rMean, rVar, rN);

            return(result);
        }
        /// <summary>
        /// Try Parse the value.
        /// </summary>
        /// <param name="val">The Value.</param>
        /// <param name="result">Sufficient Statistics result.</param>
        /// <returns>Returns true if parsed properly.</returns>
        new public static bool TryParse(string val, out SufficientStatistics result)
        {
            result = null;
            if (string.IsNullOrEmpty(val))
            {
                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);
            }
        }
예제 #3
0
 /// <summary>
 /// Try converting the given string into SufficientStatistics object.
 /// </summary>
 /// <param name="val">string to be converted.</param>
 /// <param name="result">SufficentStatistics object which corresponding to the given string.</param>
 /// <returns>Whether string was successfully converted.</returns>
 public static bool TryParse(string val, out SufficientStatistics result)
 {
     return
         (MissingStatistics.TryParse(val, out result) ||
          GaussianStatistics.TryParse(val, out result) ||
          BooleanStatistics.TryParse(val, out result) ||
          DiscreteStatistics.TryParse(val, out result) ||
          ContinuousStatistics.TryParse(val, out result) ||
          StatisticsList.TryParse(val, out result));
 }
        /// <summary>
        /// Compares Sufficient Statistics.
        /// </summary>
        /// <param name="stats">Sufficient Statistics.</param>
        /// <returns>Returns true if equal.</returns>
        public override bool Equals(SufficientStatistics stats)
        {
            if (stats == null)
            {
                return(false);
            }

            if (IsMissing() && stats.IsMissing())
            {
                return(true);
            }

            GaussianStatistics gaussStats = stats.AsGaussianStatistics();

            return(_mean == gaussStats._mean && _variance == gaussStats._variance && _sampleSize == gaussStats._sampleSize);
        }
 /// <summary>
 /// Converts current object As Gaussian Statistics.
 /// </summary>
 /// <returns>Gaussian Statistics.</returns>
 public override GaussianStatistics AsGaussianStatistics()
 {
     return(IsMissing() ? GaussianStatistics.GetMissingInstance : GaussianStatistics.GetInstance(Value, 0, 1));
 }
예제 #6
0
        /// <summary>
        /// Add two Gaussian Statistics.
        /// </summary>
        /// <param name="x">GaussianStatistics x.</param>
        /// <param name="y">GaussianStatistics y.</param>
        /// <returns>Returns added result of two Gaussian Statistics.</returns>
        public static GaussianStatistics Add(GaussianStatistics x, GaussianStatistics y)
        {
            if (x == null)
            {
                throw new ArgumentNullException("x");
            }

            if (y == null)
            {
                throw new ArgumentNullException("y");
            }

            int rN = x.SampleSize + y.SampleSize;
            double rMean = (x.SampleSize * x.Mean + y.SampleSize * y.Mean) / rN;
            double rVar = (x.SumOfSquares + y.SumOfSquares) / rN - rMean * rMean;
            if (rVar < 0)
            {
                Helper.CheckCondition(rVar > -1e-10, "Computed negative variance! " + rVar);
                rVar = 0;
            }

            GaussianStatistics result = GaussianStatistics.GetInstance(rMean, rVar, rN);
            return result;
        }