Skip to content

Commit

Permalink
GC-88: General Discrete Distribution estimation function couldn't han…
Browse files Browse the repository at this point in the history
…dle observations with negative values.
  • Loading branch information
cesarsouza committed Feb 8, 2014
1 parent b4f144b commit 5b65829
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
namespace Accord.Statistics.Distributions.Univariate
{
using System;
using System.Globalization;
using Accord.Math;
using Accord.Statistics.Distributions.Fitting;
using Accord.Statistics.Distributions;
using Accord.Statistics.Distributions.Fitting;
using AForge;
using System.Globalization;

/// <summary>
/// Univariate general discrete distribution, also referred as the
Expand Down Expand Up @@ -494,15 +494,16 @@ public void Fit(double[] observations, double[] weights, GeneralDiscreteOptions
if (weights == null)
{
for (int i = 0; i < observations.Length; i++)
p[(int)observations[i]]++;
p[(int)observations[i] - start]++;
}
else
{
if (observations.Length != weights.Length)
throw new ArgumentException("The weight vector should have the same size as the observations", "weights");
throw new ArgumentException("The weight vector should have the same size as the observations",
"weights");

for (int i = 0; i < observations.Length; i++)
p[(int)observations[i]] += weights[i] * observations.Length;
p[(int)observations[i] - start] += weights[i] * observations.Length;
}

if (useLaplace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,46 @@ public void FitTest2()
Assert.IsTrue(Matrix.IsEqual(expected, actual2));
}

[TestMethod()]
public void FitTest3()
{
GeneralDiscreteDistribution target = new GeneralDiscreteDistribution(-1, 4);
double[] values = { 0.00, 1.00, 2.00, 3.00 };
double[] weights = { 0.25, 0.25, 0.25, 0.25 };

target.Fit(values.Subtract(1), weights);

double[] expected = { 0.25, 0.25, 0.25, 0.25 };
double[] actual = target.Frequencies;

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

[TestMethod()]
public void FitTest4()
{
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(-1, 4);
target.Fit(values.Subtract(1), 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(-1, 4);
target.Fit(values2.Subtract(1), weights2);
double[] actual2 = target.Frequencies;
Assert.IsTrue(Matrix.IsEqual(expected, actual2));
}


[TestMethod()]
public void DistributionFunctionTest()
Expand Down

0 comments on commit 5b65829

Please sign in to comment.