Beispiel #1
0
        /// <summary>
        ///   Fits the underlying distribution to a given set of observations.
        /// </summary>
        ///
        /// <param name="observations">The array of observations to fit the model against.
        ///   The array elements can be either of type double (for univariate data) or type
        ///   double[] (for multivariate data).
        /// </param>
        /// <param name="weights">The weight vector containing the weight for each of the samples.</param>
        /// <param name="options">Optional arguments which may be used during fitting,
        ///   such as regularization constants and additional parameters.</param>
        ///
        public void Fit(double[] observations, int[] weights, TriangularOptions options)
        {
            bool fixMax = true;
            bool fixMin = true;
            bool sorted = false;
            int  imax   = -1;
            int  imin   = -1;
            TriangularEstimationMethod method = TriangularEstimationMethod.MeanMaxMin;

            if (options != null)
            {
                fixMax = options.FixMax;
                fixMin = options.FixMin;
                method = options.Method;
                sorted = options.IsSorted;
                imin   = options.MinIndex;
                imax   = options.MaxIndex;
            }

            double min  = this.Min;
            double max  = this.Max;
            double mode = this.Mode;


            if (!sorted)
            {
                Array.Sort(observations, weights);
            }

            if (!fixMin)
            {
                min = GetMin(observations, weights, out imin);
            }

            if (!fixMax)
            {
                max = GetMax(observations, weights, out imax);
            }

            if (imin == -1)
            {
                imin = FindMin(observations, min);
            }

            if (imax == -1)
            {
                imax = FindMax(observations, max);
            }


            mode = GetMode(observations, weights, method, min, max, mode, imax, imin);

            initialize(min, max, mode);
        }
        /// <summary>
        ///   Fits the underlying distribution to a given set of observations.
        /// </summary>
        ///
        /// <param name="observations">The array of observations to fit the model against.
        ///   The array elements can be either of type double (for univariate data) or type
        ///   double[] (for multivariate data).
        /// </param>
        /// <param name="weights">The weight vector containing the weight for each of the samples.</param>
        /// <param name="options">Optional arguments which may be used during fitting,
        ///   such as regularization constants and additional parameters.</param>
        ///
        public void Fit(double[] observations, double[] weights, TriangularOptions options)
        {
            bool fixMax = false;
            bool fixMin = false;
            bool sorted = false;
            TriangularEstimationMethod method = TriangularEstimationMethod.MeanMaxMin;

            if (options != null)
            {
                fixMax = options.FixMax;
                fixMin = options.FixMin;
                method = options.Method;
                sorted = options.IsSorted;
            }

            double min  = this.A;
            double max  = this.B;
            double mode = this.C;

            int imax = -1;
            int imin = -1;

            if (!sorted)
            {
                Array.Sort(observations, weights);
            }

            if (!fixMin)
            {
                if (weights == null)
                {
                    imin = 0;
                }
                else
                {
                    // Look for the first value whose weight is different from zero
                    for (int i = 0; i < weights.Length; i++)
                    {
                        if (weights[i] > 0)
                        {
                            imin = i;
                            break;
                        }
                    }
                }

                min = observations[imin];
            }

            if (!fixMax)
            {
                if (weights == null)
                {
                    imax = observations.Length - 1;
                }
                else
                {
                    // Look for the last value whose weight is different from zero
                    for (int i = weights.Length - 1; i >= 0; i--)
                    {
                        if (weights[i] > 0)
                        {
                            imax = i;
                            break;
                        }
                    }
                }

                max = observations[imax];
            }

            if (method == TriangularEstimationMethod.MeanMaxMin)
            {
                if (imin == -1)
                {
                    for (int i = 0; i < observations.Length; i++)
                    {
                        if (observations[i] >= min)
                        {
                            imin = i;
                            break;
                        }
                    }
                }

                if (imax == -1)
                {
                    for (int i = observations.Length - 1; i >= 0; i--)
                    {
                        if (observations[i] <= max)
                        {
                            imax = i;
                            break;
                        }
                    }
                }

                double mean;

                if (weights == null)
                {
                    double sum = 0;
                    for (int i = imin; i < imax; i++)
                    {
                        sum += observations[i];
                    }
                    mean = sum / (imax - imin);
                }
                else
                {
                    double sum       = 0;
                    double weightSum = 0;
                    for (int i = imin; i < imax; i++)
                    {
                        sum       += weights[i] * observations[i];
                        weightSum += weights[i];
                    }

                    mean = sum / weightSum;
                }

                mode = 3 * mean - max - min;

                initialize(min, max, mode);
            }
            else
            {
                throw new NotImplementedException();
            }
        }