public void NaiveBayesConstructorTest4() { int classes = 2; int inputCount = 3; double[] priors = { 0.4, 0.6 }; UniformDiscreteDistribution initial = new UniformDiscreteDistribution(0, 3); var target = new NaiveBayes<UniformDiscreteDistribution>(classes, inputCount, initial, priors); Assert.AreEqual(classes, target.ClassCount); Assert.AreEqual(inputCount, target.InputCount); Assert.AreEqual(priors.Length, target.Priors.Length); Assert.AreEqual(0.4, target.Priors[0]); Assert.AreEqual(0.6, target.Priors[1]); Assert.AreEqual(2, target.Distributions.GetLength(0)); Assert.AreEqual(3, target.Distributions.GetLength(1)); for (int i = 0; i < classes; i++) { for (int j = 0; j < inputCount; j++) { Assert.AreNotSame(initial, target.Distributions[i, j]); Assert.AreEqual(0, target.Distributions[i, j].Minimum); Assert.AreEqual(3, target.Distributions[i, j].Maximum); } } }
public void ConstructorTest() { // Create an uniform (discrete) distribution in [2, 6] var dist = new UniformDiscreteDistribution(a: 2, b: 6); // Common measures double mean = dist.Mean; // 4.0 double median = dist.Median; // 4.0 double var = dist.Variance; // 1.3333333333333333 // Cumulative distribution functions double cdf = dist.DistributionFunction(k: 2); // 0.2 double ccdf = dist.ComplementaryDistributionFunction(k: 2); // 0.8 // Probability mass functions double pmf1 = dist.ProbabilityMassFunction(k: 4); // 0.2 double pmf2 = dist.ProbabilityMassFunction(k: 5); // 0.2 double pmf3 = dist.ProbabilityMassFunction(k: 6); // 0.2 double lpmf = dist.LogProbabilityMassFunction(k: 2); // -1.6094379124341003 // Quantile function int icdf1 = dist.InverseDistributionFunction(p: 0.17); // 2 int icdf2 = dist.InverseDistributionFunction(p: 0.46); // 4 int icdf3 = dist.InverseDistributionFunction(p: 0.87); // 6 // Hazard (failure rate) functions double hf = dist.HazardFunction(x: 4); // 0.5 double chf = dist.CumulativeHazardFunction(x: 4); // 0.916290731874155 // String representation string str = dist.ToString(CultureInfo.InvariantCulture); // "U(x; a = 2, b = 6)" Assert.AreEqual(4.0, mean); Assert.AreEqual(4.0, median); Assert.AreEqual(1.3333333333333333, var); Assert.AreEqual(0.916290731874155, chf, 1e-10); Assert.AreEqual(0.2, cdf); Assert.AreEqual(0.2, pmf1); Assert.AreEqual(0.2, pmf2); Assert.AreEqual(0.2, pmf3); Assert.AreEqual(-1.6094379124341003, lpmf); Assert.AreEqual(0.5, hf); Assert.AreEqual(0.8, ccdf); Assert.AreEqual(2, icdf1); Assert.AreEqual(4, icdf2); Assert.AreEqual(6, icdf3); Assert.AreEqual("U(x; a = 2, b = 6)", str); var range1 = dist.GetRange(0.95); var range2 = dist.GetRange(0.99); var range3 = dist.GetRange(0.01); Assert.AreEqual(2, range1.Min); Assert.AreEqual(6, range1.Max); Assert.AreEqual(2.0, range2.Min); Assert.AreEqual(6, range2.Max); Assert.AreEqual(2.0, range3.Min); Assert.AreEqual(6, range3.Max); }
public void GetRangeTest() { var u = new UniformDiscreteDistribution(-8, 7); var range = u.GetRange(0.99); Assert.AreEqual(-8, range.Min); Assert.AreEqual(7, range.Max); }
public void IntervalTest() { var target = new UniformDiscreteDistribution(-10, 10); for (int k = -15; k < 15; k++) { double expected = target.ProbabilityMassFunction(k); double a = target.DistributionFunction(k); double b = target.DistributionFunction(k - 1); double c = a - b; Assert.AreEqual(expected, c, 1e-15); Assert.AreEqual(c, target.DistributionFunction(k - 1, k), 1e-15); } }
public void PosteriorTest1() { // Example from http://ai.stanford.edu/~serafim/CS262_2007/notes/lecture5.pdf double[,] A = { { 0.95, 0.05 }, // fair dice state { 0.05, 0.95 }, // loaded dice state }; double[,] B = { { 1 / 6.0, 1 / 6.0, 1 / 6.0, 1 / 6.0, 1 / 6.0, 1 / 6.0 }, // fair dice probabilities { 1 / 10.0, 1 / 10.0, 1 / 10.0, 1 / 10.0, 1 / 10.0, 1 / 2.0 }, // loaded probabilities }; double[] pi = { 0.5, 0.5 }; var hmm = HiddenMarkovModel.CreateGeneric(A, B, pi); int[] x = new int[] { 1, 2, 1, 5, 6, 2, 1, 5, 2, 4 }.Subtract(1); int[] y = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double py = Math.Exp(hmm.Evaluate(x, y)); Assert.AreEqual(0.00000000521158647211, py, 1e-16); x = new int[] { 1, 2, 1, 5, 6, 2, 1, 5, 2, 4 }.Subtract(1); y = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; py = Math.Exp(hmm.Evaluate(x, y)); Assert.AreEqual(0.00000000015756235243, py, 1e-16); Accord.Math.Tools.SetupGenerator(0); var u = new UniformDiscreteDistribution(0, 6); int[] sequence = u.Generate(1000); int start = 120; int end = 150; for (int i = start; i < end; i += 2) sequence[i] = 5; // Predict the next observation in sequence int[] path; double[][] p = hmm.Posterior(sequence, out path); for (int i = 0; i < path.Length; i++) Assert.AreEqual(1, p[i][0] + p[i][1], 1e-10); int loaded = 0; for (int i = 0; i < start; i++) if (p[i][1] > 0.95) loaded++; Assert.AreEqual(0, loaded); loaded = 0; for (int i = start; i < end; i++) if (p[i][1] > 0.95) loaded++; Assert.IsTrue(loaded > 15); loaded = 0; for (int i = end; i < p.Length; i++) if (p[i][1] > 0.95) loaded++; Assert.AreEqual(0, loaded); }