Пример #1
0
 public void CanSample()
 {
     var n = new Binomial(0.3, 5);
     var d = n.Sample();
 }
Пример #2
0
 public void CanSampleSequence()
 {
     var n = new Binomial(0.3, 5);
     var ied = n.Samples();
     var e = ied.Take(5).ToArray();
 }
Пример #3
0
 public void BinomialCreateFailsWithBadParameters(double p, int n)
 {
     var bernoulli = new Binomial(p,n);
 }
Пример #4
0
 public void CanCreateBinomial(double p, int n)
 {
     var bernoulli = new Binomial(p,n);
     AssertEx.AreEqual<double>(p, bernoulli.P);
 }
Пример #5
0
 public void ValidateSkewness(double p, int n)
 {
     var b = new Binomial(p,n);
     AssertEx.AreEqual<double>((1.0 - 2.0 * p) / Math.Sqrt(n * p * (1.0 - p)), b.Skewness);
 }
Пример #6
0
 public void ValidateToString()
 {
     var b = new Binomial(0.3, 2);
     AssertEx.AreEqual<string>("Binomial(Success Probability = 0.3, Number of Trials = 2)", b.ToString());
 }
Пример #7
0
 public void ValidateMode(double p, int n, int m)
 {
     var b = new Binomial(p,n);
     AssertEx.AreEqual<int>(m, b.Mode);
 }
Пример #8
0
 public void ValidateMaximum()
 {
     var b = new Binomial(0.3, 10);
     Assert.AreEqual(10, b.Maximum);
 }
Пример #9
0
 public void ValidateEntropy(double p, int n, double e)
 {
     var b = new Binomial(p,n);
     AssertHelpers.AlmostEqual(e, b.Entropy, 14);
 }
Пример #10
0
 public void ValidateMinimum()
 {
     var b = new Binomial(0.3, 10);
     AssertEx.AreEqual<int>(0, b.Minimum);
 }
Пример #11
0
 public void SetProbabilityOfOneFails(double p, int n)
 {
     var b = new Binomial(0.3, n);
     b.P = p;
 }
Пример #12
0
 public void ValidateToString()
 {
     var b = new Binomial(0.3, 2);
     Assert.AreEqual("Binomial(p = 0.3, n = 2)", b.ToString());
 }
Пример #13
0
 public void ValidateProbability(double p, int n, int x, double d)
 {
     var b = new Binomial(p, n);
     AssertHelpers.AlmostEqualRelative(d, b.Probability(x), 14);
 }
Пример #14
0
 public void CanSetSuccessProbability(double p, int n)
 {
     var b = new Binomial(0.3, n);
     b.P = p;
 }
Пример #15
0
 public void ValidateProbabilityLn(double p, int n, int x, double dln)
 {
     var b = new Binomial(p,n);
     AssertHelpers.AlmostEqual(dln, b.ProbabilityLn(x), 14);
 }
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Binomial_distribution">Binomial distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the Binomial distribution class with parameters P = 0.2, N = 20
            var binomial = new Binomial(0.2, 20);
            Console.WriteLine(@"1. Initialize the new instance of the Binomial distribution class with parameters P = {0}, N = {1}", binomial.P, binomial.N);
            Console.WriteLine();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // 5. Generate 100000 samples of the Binomial(0.7, 20) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the Binomial(0.7, 20) distribution and display histogram");
            binomial.P = 0.7;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = binomial.Sample();
            }

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

            // 6. Generate 100000 samples of the Binomial(0.5, 40) distribution and display histogram
            Console.WriteLine(@"6. Generate 100000 samples of the Binomial(0.5, 40) distribution and display histogram");
            binomial.P = 0.5;
            binomial.N = 40;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = binomial.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
Пример #17
0
        static void Main(string[] args)
        {
            try
            {
                Application oXL = new Application();
                oXL.DisplayAlerts = false;
            #if DEBUG
                oXL.Visible = true;
            #else
            oXL.Visible = false;
            #endif
                //Open the Excel File

                Workbook oWB = oXL.Workbooks.Open(inputFile);

                _Worksheet oSheet = oWB.Sheets[SheetName1];

                oSheet = oWB.Sheets[1]; // Gives the reference to the first sheet

                oSheet = oWB.ActiveSheet; // Gives the reference to the current opened sheet

                oSheet.Activate();

                oSheet.Copy(Type.Missing, Type.Missing);
                _Worksheet oSheet1 = oXL.Workbooks[2].Sheets[1];
                oSheet1.Name = SheetName2;

                try
                {
                    //We already know the Address Range of the cells

                    String start_range = "A2";
                    String end_range = "A164";

                    Object[,] valuesA = oSheet1.get_Range(start_range, end_range).Value2;

                    start_range = "B2";
                    end_range = "B164";

                    Object[,] valuesB = oSheet.get_Range(start_range, end_range).Value2;

                    start_range = "C2";
                    end_range = "C164";

                    Object[,] valuesC = oSheet.get_Range(start_range, end_range).Value2;

                    start_range = "D2";
                    end_range = "D164";

                    Object[,] valuesD = oSheet.get_Range(start_range, end_range).Value2;

                    //int t = valuesB.GetLength(0);

                    object duplicate = null;
                    int index = 1;
                    int count = 0;
                    int i = 1;
                    for (i = 1; i <= valuesA.GetLength(0); i++)
                    {
                        if (duplicate == null)
                        {
                            duplicate = valuesA[i, 1];
                            index = i;
                        }
                        if (Convert.ToInt64(valuesA[i, 1]) == Convert.ToInt64(duplicate))
                        {
                            count++;
                            duplicate = valuesA[i, 1];
                            continue;
                        }
                        double[] probSuc = new double[count];
                        //double max = Double.MinValue;
                        int r = 1;
                        int[] successes = new int[count];
                        int[] failures = new int[count];
                        int[] trials = new int[count];
                        for (int k = index; k <= (index + count - 1); k++)
                        {
                            double s = Convert.ToInt32(valuesC[k, 1]);
                            double n = Convert.ToInt32(valuesD[k, 1]);
                            double f = Convert.ToInt32(valuesD[k, 1]) - s;
                            trials[r - 1] = Convert.ToInt32(n);
                            successes[r - 1] = Convert.ToInt32(s);
                            failures[r - 1] = Convert.ToInt32(f);
                            probSuc[r - 1] = s / n;
                            r++;
                        }

                        r = 1;
                        for (int k = index; k <= (index + count - 1); k++)
                        {
                            var samples = new Binomial(probSuc[r-1], trials[r-1]);
                            var samples1 = new Bernoulli(probSuc[r - 1]);
                            double prob = samples.Probability(0);

                            if (prob < 0.05)
                            {
                                oSheet1.Cells[k + 1, 5] = "WINNER";
                            }
                            else
                            {
                                oSheet1.Cells[k + 1, 5] = "LOSSER";
                            }
                            r++;
                        }
                        i--;
                        duplicate = null;
                        count = 0;
                    }
                    if (count > 0 && index <= valuesA.GetLength(0))
                    {
                        double[] probSuc = new double[count];
                        //double max = Double.MinValue;
                        int r = 1;
                        int[] successes = new int[count];
                        int[] failures = new int[count];
                        int[] trials = new int[count];
                        for (int k = index; k <= (index + count - 1); k++)
                        {
                            double s = Convert.ToInt32(valuesC[k, 1]);
                            double n = Convert.ToInt32(valuesD[k, 1]);
                            double f = Convert.ToInt32(valuesD[k, 1]) - s;
                            trials[r - 1] = Convert.ToInt32(n);
                            successes[r - 1] = Convert.ToInt32(s);
                            failures[r - 1] = Convert.ToInt32(f);
                            probSuc[r - 1] = s / n;
                            r++;
                        }
                        r = 1;
                        for (int k = index; k <= (index + count - 1); k++)
                        {
                            var samples = new Binomial(probSuc[r - 1], trials[r - 1]);
                            double prob = samples.Probability(0);

                            if (prob < 0.05)
                            {
                                oSheet1.Cells[k + 1, 5] = "WINNER";
                            }
                            else
                            {
                                oSheet1.Cells[k + 1, 5] = "LOSSER";
                            }
                        }
                    }

                }
                catch (Exception e)
                {
                    var errors = e.Message;
                }
                finally
                {
                    oSheet1.SaveAs(OutputFile);
                    oXL.Quit();

                    Marshal.ReleaseComObject(oSheet);
                    Marshal.ReleaseComObject(oSheet1);
                    Marshal.ReleaseComObject(oWB);
                    Marshal.ReleaseComObject(oXL);

                    oSheet = null;
                    oSheet1 = null;
                    oWB = null;
                    oXL = null;
                    GC.GetTotalMemory(false);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.GetTotalMemory(true);
                }
            }
            catch (Exception e)
            {
                var errors = e.Message;
            }
        }