Generate() public method

Generates a random vector of observations from the current distribution.
public Generate ( int samples ) : double[][]
samples int The number of samples to generate.
return double[][]
        public void NormalGenerateTest()
        {
            // Create a Normal with mean 2 and sigma 5
            var normal = new NormalDistribution(2, 5);

            // Generate 1000000 samples from it
            double[] samples = normal.Generate(1000000);

            // Try to estimate a new Normal distribution from the 
            // generated samples to check if they indeed match
            var actual = NormalDistribution.Estimate(samples);

            string result = actual.ToString("N2"); // N(x; μ = 2.01, σ² = 25.03)

            Assert.AreEqual("N(x; μ = 2.01, σ² = 25.03)", result);
        }
Esempio n. 2
0
        public void GenerateTest()
        {
            NormalDistribution target = new NormalDistribution(2, 5);

            double[] samples = target.Generate(1000000);

            var actual = NormalDistribution.Estimate(samples);
            actual.Fit(samples);

            Assert.AreEqual(2, actual.Mean, 0.01);
            Assert.AreEqual(5, actual.StandardDeviation, 0.01);
        }
Esempio n. 3
0
        public void GenerateTest2()
        {
            NormalDistribution target = new NormalDistribution(4, 2);

            double[] samples = new double[1000000];
            for (int i = 0; i < samples.Length; i++)
                samples[i] = target.Generate();

            var actual = NormalDistribution.Estimate(samples);
            actual.Fit(samples);

            Assert.AreEqual(4, actual.Mean, 0.01);
            Assert.AreEqual(2, actual.StandardDeviation, 0.01);
        }
Esempio n. 4
0
        public void ConstructorTest3()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Create a normal distribution with mean 2 and sigma 3
            var normal = new NormalDistribution(mean: 2, stdDev: 3);

            // In a normal distribution, the median and
            // the mode coincide with the mean, so

            double mean = normal.Mean;     // 2
            double mode = normal.Mode;     // 2
            double median = normal.Median; // 2

            // The variance is the square of the standard deviation
            double variance = normal.Variance; // 3² = 9

            // Let's check what is the cumulative probability of
            // a value less than 3 occurring in this distribution:
            double cdf = normal.DistributionFunction(3); // 0.63055

            // Finally, let's generate 1000 samples from this distribution
            // and check if they have the specified mean and standard dev.

            double[] samples = normal.Generate(1000);

            double sampleMean = samples.Mean();             // 1.92
            double sampleDev = samples.StandardDeviation(); // 3.00

            Assert.AreEqual(2, mean);
            Assert.AreEqual(2, mode);
            Assert.AreEqual(2, median);

            Assert.AreEqual(9, variance);
            Assert.AreEqual(1000, samples.Length);
            Assert.AreEqual(1.9245, sampleMean, 1e-4);
            Assert.AreEqual(3.0008, sampleDev, 1e-4);
        }