Beispiel #1
0
                /// <summary>Create a specific <see cref="ExponentialDistribution"/> object that represents a specified distribution with estimated parameters.
                /// </summary>
                /// <param name="empiricalDistribution">The sample to fit the parameters of the specified distribution in its <see cref="EmpiricalDistribution"/> representation.</param>
                /// <returns>A specific <see cref="ExponentialDistribution"/> object that represents the specified distribution with estimated parameters.</returns>
                public ExponentialDistribution Create(EmpiricalDistribution empiricalDistribution)
                {
                    if (empiricalDistribution.Mean <= 0.0)
                    {
                        throw new ArgumentException("empiricalDistribution");
                    }
                    var lambda = 1.0 / empiricalDistribution.Mean;

                    if (InfoOutputDetailLevel.IsAtLeastAsComprehensiveAs(InfoOutputDetailLevel.High) == true)
                    {
                        return(new ExponentialDistribution(lambda, (infoOutput, categoryName) =>
                        {
                            var infoOutputPackage = infoOutput.AcquirePackage(categoryName);

                            var dataTable = new DataTable("Sample");
                            dataTable.Columns.Add("Value", typeof(double));
                            foreach (var value in empiricalDistribution.Sample)
                            {
                                dataTable.Rows.Add(new object[] { value });
                            }
                            infoOutputPackage.Add(dataTable);
                        }));
                    }
                    else
                    {
                        return(new ExponentialDistribution(lambda));
                    }
                }
                /// <summary>Create a specific <see cref="StandardNormalDistribution"/> object that represents a specified distribution with estimated parameters.
                /// </summary>
                /// <param name="empiricalDistribution">The sample to fit the parameters of the specified distribution in its <see cref="EmpiricalDistribution"/> representation.</param>
                /// <returns>A specific <see cref="LogNormalDistribution"/> object that represents the specified distribution with estimated parameters.</returns>
                public LogNormalDistribution Create(EmpiricalDistribution empiricalDistribution)
                {
                    var variance = Math.Log(1 + empiricalDistribution.Moment.Variance / (empiricalDistribution.Mean * empiricalDistribution.Mean)); // = ln(1 + sn^2 / \hat{x_n}^2 )
                    var mu       = Math.Log(empiricalDistribution.Mean) - 0.5 * variance;                                                           // = ln( \hat{x_n} ) - 0.5 * ln(1 + sn^2 / \hat{x_n}^2 )

                    if (InfoOutputDetailLevel.IsAtLeastAsComprehensiveAs(InfoOutputDetailLevel.High) == true)
                    {
                        return(new LogNormalDistribution(mu, Math.Sqrt(variance), (infoOutput, categoryName) =>
                        {
                            var infoOutputPackage = infoOutput.AcquirePackage(categoryName);

                            var dataTable = new DataTable("Sample");
                            dataTable.Columns.Add("Value", typeof(double));
                            foreach (var value in empiricalDistribution.Sample)
                            {
                                dataTable.Rows.Add(new object[] { value });
                            }
                            infoOutputPackage.Add(dataTable);
                        }));
                    }
                    else
                    {
                        return(new LogNormalDistribution(mu, Math.Sqrt(variance)));
                    }
                }
                /// <summary>Create a specific <see cref="LogNormalDistribution"/> object that represents a specified distribution with estimated parameters.
                /// </summary>
                /// <param name="empiricalDistribution">The sample to fit the parameters of the specified distribution in its <see cref="EmpiricalDistribution"/> representation.</param>
                /// <returns>A specific <see cref="LogNormalDistribution"/> object that represents the specified distribution with estimated parameters.</returns>
                public LogNormalDistribution Create(EmpiricalDistribution empiricalDistribution)
                {
                    var mu       = DoMath.Average(empiricalDistribution.Sample.Select(xn => Math.Log(xn)));
                    var variance = DoMath.Average(empiricalDistribution.Sample.Select(xn => DoMath.Pow(Math.Log(xn) - mu, 2)));

                    if (InfoOutputDetailLevel.IsAtLeastAsComprehensiveAs(InfoOutputDetailLevel.High) == true)
                    {
                        return(new LogNormalDistribution(mu, Math.Sqrt(variance), (infoOutput, categoryName) =>
                        {
                            var infoOutputPackage = infoOutput.AcquirePackage(categoryName);

                            var dataTable = new DataTable("Sample");
                            dataTable.Columns.Add("Value", typeof(double));
                            foreach (var value in empiricalDistribution.Sample)
                            {
                                dataTable.Rows.Add(new object[] { value });
                            }
                            infoOutputPackage.Add(dataTable);
                        }));
                    }
                    else
                    {
                        return(new LogNormalDistribution(mu, Math.Sqrt(variance)));
                    }
                }
                /// <summary>Create a specific <see cref="NormalDistribution"/> object that represents a specified distribution with estimated parameters.
                /// </summary>
                /// <param name="empiricalDistribution">The sample to fit the parameters of the specified distribution in its <see cref="EmpiricalDistribution"/> representation.</param>
                /// <returns>A specific <see cref="NormalDistribution"/> object that represents the specified distribution with estimated parameters.</returns>
                public NormalDistribution Create(EmpiricalDistribution empiricalDistribution)
                {
                    var mu    = empiricalDistribution.Mean;
                    var sigma = empiricalDistribution.Moment.StandardDeviation;

                    if (InfoOutputDetailLevel.IsAtLeastAsComprehensiveAs(InfoOutputDetailLevel.High) == true)
                    {
                        return(new NormalDistribution(mu, sigma, (infoOutput, categoryName) =>
                        {
                            var infoOutputPackage = infoOutput.AcquirePackage(categoryName);

                            var dataTable = new DataTable("Sample");
                            dataTable.Columns.Add("Value", typeof(double));
                            foreach (var value in empiricalDistribution.Sample)
                            {
                                dataTable.Rows.Add(new object[] { value });
                            }
                            infoOutputPackage.Add(dataTable);
                        }));
                    }
                    else
                    {
                        return(new NormalDistribution(mu, sigma));
                    }
                }
                /// <summary>Create a specific <see cref="NormalDistribution"/> object that represents a specified distribution with estimated parameters.
                /// </summary>
                /// <param name="empiricalDistribution">The sample to fit the parameters of the specified distribution in its <see cref="EmpiricalDistribution"/> representation.</param>
                /// <returns>A specific <see cref="NormalDistribution"/> object that represents the specified distribution with estimated parameters.</returns>
                public NormalDistribution Create(EmpiricalDistribution empiricalDistribution)
                {
                    var n = empiricalDistribution.SampleSize;

                    var mu    = empiricalDistribution.Mean;
                    var sigma = Math.Sqrt(empiricalDistribution.Moment.Variance * (n - 1.0) / n);  // apply 1/n instead of 1/(n-1.0) in the estimator of the variance

                    if (InfoOutputDetailLevel.IsAtLeastAsComprehensiveAs(InfoOutputDetailLevel.High) == true)
                    {
                        return(new NormalDistribution(mu, sigma, (infoOutput, categoryName) =>
                        {
                            var infoOutputPackage = infoOutput.AcquirePackage(categoryName);

                            var dataTable = new DataTable("Sample");
                            dataTable.Columns.Add("Value", typeof(double));
                            foreach (var value in empiricalDistribution.Sample)
                            {
                                dataTable.Rows.Add(new object[] { value });
                            }
                            infoOutputPackage.Add(dataTable);
                        }));
                    }
                    else
                    {
                        return(new NormalDistribution(mu, sigma));
                    }
                }
        /// <summary>Initializes a new instance of the <see cref="EmpiricalDistribution" /> class.
        /// </summary>
        /// <param name="empiricalDistribution">The empirical distribution.</param>
        /// <param name="densityEstimator">The density estimator.</param>
        protected EmpiricalDistribution(EmpiricalDistribution empiricalDistribution, DensityEstimator densityEstimator)
        {
            m_Median           = empiricalDistribution.m_Median;
            m_Quantile         = empiricalDistribution.m_Quantile;
            m_MomentCalculator = empiricalDistribution.m_MomentCalculator;

            Name                  = empiricalDistribution.Name;
            LongName              = empiricalDistribution.LongName;
            m_DensityEstimator    = densityEstimator.Create(this);
            InfoOutputDetailLevel = empiricalDistribution.InfoOutputDetailLevel;
        }
        /// <summary>Gets an unbiased covariance estimator of two random samples.
        /// </summary>
        /// <param name="sample1">The first sample.</param>
        /// <param name="sample2">The second sample.</param>
        /// <returns>The estimated covariance of the two specified samples.</returns>
        /// <remarks>The implementation is based on "Formulas for Robust, One-pass parallel computation of Covariances and arbitrary-order statistic Moments", P. Pebay, Sandia National Laboratories, September 2008.</remarks>
        public static double GetCovariance(EmpiricalDistribution sample1, EmpiricalDistribution sample2)
        {
            var c2    = 0.0;
            var mean1 = 0.0;
            var mean2 = 0.0;

            var sample2Enumerator = sample2.Sample.GetEnumerator();

            int n = 1;

            foreach (var value1 in sample1.Sample)
            {
                sample2Enumerator.MoveNext();
                var value2 = sample2Enumerator.Current;

                c2 += (value1 - mean1) * (value2 - mean2) * (n - 1) / n;

                mean1 += (value1 - mean1) / n;
                mean2 += (value2 - mean2) / n;
                n++;
            }
            return(c2 / (n - 2));  // n is sample length + 1 at this time
        }
 /// <summary>Gets the estimated Pearson's correlation coefficient of two samples.
 /// </summary>
 /// <param name="sample1">The first sample.</param>
 /// <param name="sample2">The second sample.</param>
 /// <returns>The estimated Pearson's correlation coefficient, i.e. the covariance of the two specified samples divided by the product of their standard deviations.</returns>
 /// <remarks>The implementation is based on "Formulas for Robust, One-pass parallel computation of Covariances and arbitrary-order statistic Moments", P. Pebay, Sandia National Laboratories, September 2008.</remarks>
 public static double GetCorrelationCoefficient(EmpiricalDistribution sample1, EmpiricalDistribution sample2)
 {
     return(GetCovariance(sample1, sample2) / (sample1.Moment.StandardDeviation * sample2.Moment.StandardDeviation));
 }