public void ValidateProbability(double p, int n, int x, double d) { var b = new Binomial(p,n); AssertHelpers.AlmostEqual(d, b.Probability(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); }
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; } }