コード例 #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
ファイル: ZipfTests.cs プロジェクト: larzw/mathnet-numerics
 public void CanSample()
 {
     var d = new Zipf(0.7, 5);
     var s = d.Sample();
     Assert.LessOrEqual(s, 5);
     Assert.GreaterOrEqual(s, 0);
 }