Пример #1
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Zipf_distribution">Zipf distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the Zipf distribution class with parameters S = 5, N = 10
            var zipf = new Zipf(5, 10);
            Console.WriteLine(@"1. Initialize the new instance of the Zipf distribution class with parameters S = {0}, N = {1}", zipf.S, zipf.N);
            Console.WriteLine();

            // 2. Distributuion properties:
            Console.WriteLine(@"2. {0} distributuion properties:", zipf);

            // Cumulative distribution function
            Console.WriteLine(@"{0} - Сumulative distribution at location '3'", zipf.CumulativeDistribution(3).ToString(" #0.00000;-#0.00000"));

            // Probability density
            Console.WriteLine(@"{0} - Probability mass at location '3'", zipf.Probability(3).ToString(" #0.00000;-#0.00000"));

            // Log probability density
            Console.WriteLine(@"{0} - Log probability mass at location '3'", zipf.ProbabilityLn(3).ToString(" #0.00000;-#0.00000"));

            // Entropy
            Console.WriteLine(@"{0} - Entropy", zipf.Entropy.ToString(" #0.00000;-#0.00000"));

            // Largest element in the domain
            Console.WriteLine(@"{0} - Largest element in the domain", zipf.Maximum.ToString(" #0.00000;-#0.00000"));

            // Smallest element in the domain
            Console.WriteLine(@"{0} - Smallest element in the domain", zipf.Minimum.ToString(" #0.00000;-#0.00000"));

            // Mean
            Console.WriteLine(@"{0} - Mean", zipf.Mean.ToString(" #0.00000;-#0.00000"));

            // Mode
            Console.WriteLine(@"{0} - Mode", zipf.Mode.ToString(" #0.00000;-#0.00000"));

            // Variance
            Console.WriteLine(@"{0} - Variance", zipf.Variance.ToString(" #0.00000;-#0.00000"));

            // Standard deviation
            Console.WriteLine(@"{0} - Standard deviation", zipf.StdDev.ToString(" #0.00000;-#0.00000"));

            // Skewness
            Console.WriteLine(@"{0} - Skewness", zipf.Skewness.ToString(" #0.00000;-#0.00000"));
            Console.WriteLine();

            // 3. Generate 10 samples of the Zipf distribution
            Console.WriteLine(@"3. Generate 10 samples of the Zipf distribution");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(zipf.Sample().ToString("N05") + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 4. Generate 100000 samples of the Zipf(5, 10) distribution and display histogram
            Console.WriteLine(@"4. Generate 100000 samples of the Zipf(5, 10) distribution and display histogram");
            var data = new double[100000];
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = zipf.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 5. Generate 100000 samples of the Zipf(2, 10) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the Zipf(2, 10) distribution and display histogram");
            zipf.S = 2;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = zipf.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 6. Generate 100000 samples of the Zipf(5, 20) distribution and display histogram
            Console.WriteLine(@"6. Generate 100000 samples of the Zipf(1, 20) distribution and display histogram");
            zipf.S = 1;
            zipf.N = 20;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = zipf.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
Пример #2
0
 public void ValidateToString()
 {
     var d = new Zipf(1.0, 5);
     Assert.AreEqual("Zipf(S = 1, N = 5)", d.ToString());
 }
Пример #3
0
 public void ValidateEntropy(double s, int n, double e)
 {
     var d = new Zipf(s, n);
     AssertHelpers.AlmostEqualRelative(e, d.Entropy, 15);
 }
Пример #4
0
 public void ValidateCumulativeDistribution(double s, int n, int x)
 {
     var d = new Zipf(s, n);
     var cdf = SpecialFunctions.GeneralHarmonic(x, s) / SpecialFunctions.GeneralHarmonic(n, s);
     AssertHelpers.AlmostEqualRelative(cdf, d.CumulativeDistribution(x), 14);
 }
Пример #5
0
 public void CanCreateZipf(double s, int n)
 {
     var d = new Zipf(s, n);
     Assert.AreEqual(s, d.S);
     Assert.AreEqual(n, d.N);
 }
Пример #6
0
 public void CanSample()
 {
     var d = new Zipf(0.7, 5);
     var s = d.Sample();
     Assert.LessOrEqual(s, 5);
     Assert.GreaterOrEqual(s, 0);
 }
Пример #7
0
 public void CanSampleSequence()
 {
     var d = new Zipf(0.7, 5);
     var ied = d.Samples();
     var e = ied.Take(1000).ToArray();
     foreach (var i in e)
     {
         Assert.LessOrEqual(i, 5);
         Assert.GreaterOrEqual(i, 0);
     }
 }
Пример #8
0
 public void ValidateProbability(double s, int n, int x)
 {
     var d = new Zipf(s, n);
     Assert.AreEqual((1.0 / Math.Pow(x, s)) / SpecialFunctions.GeneralHarmonic(n, s), d.Probability(x));
 }
Пример #9
0
 public void ValidateProbabilityLn(double s, int n, int x)
 {
     var d = new Zipf(s, n);
     Assert.AreEqual(Math.Log(d.Probability(x)), d.ProbabilityLn(x));
 }
Пример #10
0
 public void ValidateMaximum(double s, int n)
 {
     var d = new Zipf(s, n);
     Assert.AreEqual(n, d.Maximum);
 }
Пример #11
0
 public void ValidateMinimum()
 {
     var d = new Zipf(1.0, 5);
     Assert.AreEqual(1, d.Minimum);
 }
Пример #12
0
 public void ValidateMedianThrowsNotSupportedException()
 {
     var d = new Zipf(1.0, 5);
     Assert.Throws<NotSupportedException>(() => { var m = d.Median; });
 }
Пример #13
0
 public void ValidateMode(double s, int n)
 {
     var d = new Zipf(s, n);
     Assert.AreEqual(1, d.Mode);
 }
Пример #14
0
 public void ValidateSkewness(double s, int n)
 {
     var d = new Zipf(s, n);
     if (s > 4)
     {
         Assert.AreEqual(((SpecialFunctions.GeneralHarmonic(n, s - 3) * Math.Pow(SpecialFunctions.GeneralHarmonic(n, s), 2)) - (SpecialFunctions.GeneralHarmonic(n, s - 1) * ((3 * SpecialFunctions.GeneralHarmonic(n, s - 2) * SpecialFunctions.GeneralHarmonic(n, s)) - Math.Pow(SpecialFunctions.GeneralHarmonic(n, s - 1), 2)))) / Math.Pow((SpecialFunctions.GeneralHarmonic(n, s - 2) * SpecialFunctions.GeneralHarmonic(n, s)) - Math.Pow(SpecialFunctions.GeneralHarmonic(n, s - 1), 2), 1.5), d.Skewness);
     }
 }