public void ConstructorTest4() { // Suppose we have the following data, and we would // like to estimate a distribution from this data double[][] samples = { new double[] { 0, 1 }, new double[] { 1, 2 }, new double[] { 5, 1 }, new double[] { 7, 1 }, new double[] { 6, 1 }, new double[] { 5, 7 }, new double[] { 2, 1 }, }; // Start by specifying a density kernel IDensityKernel kernel = new EpanechnikovKernel(dimension: 2); // Create a multivariate Empirical distribution from the samples var dist = new MultivariateEmpiricalDistribution(kernel, samples); // Common measures double[] mean = dist.Mean; // { 3.71, 2.00 } double[] median = dist.Median; // { 3.71, 2.00 } double[] var = dist.Variance; // { 7.23, 5.00 } (diagonal from cov) double[,] cov = dist.Covariance; // { { 7.23, 0.83 }, { 0.83, 5.00 } } // Probability mass functions double pdf1 = dist.ProbabilityDensityFunction(new double[] { 2, 1 }); // 0.039131176997318849 double pdf2 = dist.ProbabilityDensityFunction(new double[] { 4, 2 }); // 0.010212109770266639 double pdf3 = dist.ProbabilityDensityFunction(new double[] { 5, 7 }); // 0.02891906722705221 double lpdf = dist.LogProbabilityDensityFunction(new double[] { 5, 7 }); // -3.5432541357714742 Assert.AreEqual(3.7142857142857144, mean[0]); Assert.AreEqual(2.0, mean[1]); Assert.AreEqual(3.7142857142857144, median[0]); Assert.AreEqual(2.0, median[1]); Assert.AreEqual(7.2380952380952381, var[0]); Assert.AreEqual(5.0, var[1]); Assert.AreEqual(7.2380952380952381, cov[0, 0]); Assert.AreEqual(0.83333333333333337, cov[0, 1]); Assert.AreEqual(0.83333333333333337, cov[1, 0]); Assert.AreEqual(5.0, cov[1, 1]); Assert.AreEqual(0.039131176997318849, pdf1); Assert.AreEqual(0.010212109770266639, pdf2); Assert.AreEqual(0.02891906722705221, pdf3); Assert.AreEqual(-3.5432541357714742, lpdf); }
public void EpanechnikovKernelTest() { EpanechnikovKernel kernel = new EpanechnikovKernel(dimension: 1); Assert.AreEqual(3.0 / 4.0, kernel.Constant); double[] actual = new double[11]; for (int i = 0; i < actual.Length; i++) actual[i] = kernel.Function((i - 5) / 10.0); double[] expected = { 0.5625, 0.63, 0.6825, 0.72, 0.74249999999999994, 0.75, 0.74249999999999994, 0.72, 0.6825, 0.63, 0.5625 }; for (int i = 0; i < actual.Length; i++) Assert.AreEqual(expected[i], actual[i]); }