Accord.Statistics.Distributions.Univariate.GeneralDiscreteDistribution.Fit C# (CSharp) Method

Fit() public method

Fits the underlying distribution to a given set of observations.
public Fit ( double observations, double weights ) : IDistribution
observations double /// The array of observations to fit the model against. ///
weights double /// The weight vector containing the weight for each of the samples. ///
return IDistribution
        public override IDistribution Fit(double[] observations, double[] weights)
        {
            if (observations.Length != weights.Length)
                throw new ArgumentException("The weight vector should have the same size as the observations", "weights");

            var p = new double[probabilities.Length];
            for (int i = 0; i < observations.Length; i++)
            {
                var j = (int) observations[i];
                p[j] += weights[i];
            }

            return new GeneralDiscreteDistribution(p);
        }

Usage Example

        public void FitTest2()
        {
            double[] expected = { 0.50, 0.00, 0.25, 0.25 };

            GeneralDiscreteDistribution target;

            double[] values = { 0.00, 2.00, 3.00 };
            double[] weights = { 0.50, 0.25, 0.25 };
            target = new GeneralDiscreteDistribution(4);
            target.Fit(values, weights);
            double[] actual = target.Frequencies;

            Assert.IsTrue(Matrix.IsEqual(expected, actual));

            // --

            double[] values2 = { 0.00, 0.00, 2.00, 3.00 };
            double[] weights2 = { 0.25, 0.25, 0.25, 0.25 };
            target = new GeneralDiscreteDistribution(4);
            target.Fit(values2, weights2);
            double[] actual2 = target.Frequencies;
            Assert.IsTrue(Matrix.IsEqual(expected, actual2));
        }
All Usage Examples Of Accord.Statistics.Distributions.Univariate.GeneralDiscreteDistribution::Fit