public Distribution(String DistributionName, params double[] vs) // 构造函数 根据名字初始化分布 { switch (DistributionName.ToUpper()) { case "NORMAL": normalDis = new Normal(vs[0], vs[1]); // double mean, double stddev break; case "CONTINUOUS": continuousUniformDis = new ContinuousUniform(vs[0], vs[1]); // int lower, int upper break; case "TRIANGULAR": triangularDis = new Triangular(vs[0], vs[1], vs[2]); //double lower, double upper, double mode (lower ≤ mode ≤ upper) break; case "STUDENTT": studentTDis = new StudentT(vs[0], vs[1], vs[2]); //double location, double scale, double freedom break; case "BERNOULLI": bernoulliDis = new Bernoulli(vs[0]); break; case "DISCRETEUNIFORM": discreteUniform = new DiscreteUniform((int)vs[0], (int)vs[1]); // int lower, int upper break; } this.DistributionName = DistributionName; }
public void SetupDistributions() { dists = new IDistribution[8]; dists[0] = new Beta(1.0, 1.0); dists[1] = new ContinuousUniform(0.0, 1.0); dists[2] = new Gamma(1.0, 1.0); dists[3] = new Normal(0.0, 1.0); dists[4] = new Bernoulli(0.6); dists[5] = new Weibull(1.0, 1.0); dists[6] = new DiscreteUniform(1, 10); dists[7] = new LogNormal(1.0, 1.0); }
/// <summary> /// Generates A random change to the specified point /// </summary> /// <param name="reference">The reference point</param> public void GenerateChange(Point reference) { Bernoulli rBernoulli = new Bernoulli(0.5); double percentage = (rBernoulli.Sample() > 0.5 ? -1 : 1) * Simulation.Default.Data_Change_Maximum_Percentage * random.NextDouble(); GenerateChange(reference, percentage); }
/// <summary> /// Generates A random change in the field. Both the coordinates and the /// </summary> public void GenerateChange() { Bernoulli rBernoulli = new Bernoulli(0.5); // Generate A random sign double percentage = (rBernoulli.Sample() > 0.5 ? -1 : 1) * Simulation.Default.Data_Change_Maximum_Percentage * random.NextDouble(); GenerateChange(new Point(random.Next(field.Width), random.Next(field.Height)), percentage); }
protected override bool ShouldRunActivity() { var bernoulli = new Bernoulli((double) Context.GlobalSettings.SendEmailPercentage/100); return bernoulli.Sample() == 1; }
public void ValidateSkewness(double p) { var b = new Bernoulli(p); Assert.AreEqual((1.0 - (2.0 * p)) / Math.Sqrt(p * (1.0 - p)), b.Skewness); }
public void ValidateEntropy(double p) { var b = new Bernoulli(p); AssertHelpers.AlmostEqualRelative(-((1.0 - p) * Math.Log(1.0 - p)) - (p * Math.Log(p)), b.Entropy, 14); }
public void ValidateMode(double p, double m) { var b = new Bernoulli(p); Assert.AreEqual(m, b.Mode); }
public void CanCreateBernoulli(double p) { var bernoulli = new Bernoulli(p); Assert.AreEqual(p, bernoulli.P); }
public void ValidateCumulativeDistribution(double p, double x, double cdf) { var b = new Bernoulli(p); Assert.AreEqual(cdf, b.CumulativeDistribution(x)); }
public void CanSampleSequence() { var n = new Bernoulli(0.3); var ied = n.Samples(); GC.KeepAlive(ied.Take(5).ToArray()); }
public void CanSample() { var n = new Bernoulli(0.3); n.Sample(); }
public void ValidateProbabilityLn(double p, int x, double dln) { var b = new Bernoulli(p); Assert.AreEqual(dln, b.ProbabilityLn(x)); }
public void ValidateMaximum() { var b = new Bernoulli(0.3); Assert.AreEqual(1.0, b.Maximum); }
public void ValidateToString() { var b = new Bernoulli(0.3); Assert.AreEqual("Bernoulli(p = 0.3)", b.ToString()); }
/// <summary> /// Run example /// </summary> /// <a href="http://en.wikipedia.org/wiki/Bernoulli_distribution">Bernoulli distribution</a> public void Run() { // 1. Initialize the new instance of the Bernoulli distribution class with parameter P = 0.2 var bernoulli = new Bernoulli(0.2); Console.WriteLine(@"1. Initialize the new instance of the Bernoulli distribution class with parameter P = {0}", bernoulli.P); Console.WriteLine(); // 2. Distributuion properties: Console.WriteLine(@"2. {0} distributuion properties:", bernoulli); // Cumulative distribution function Console.WriteLine(@"{0} - Сumulative distribution at location '3'", bernoulli.CumulativeDistribution(3).ToString(" #0.00000;-#0.00000")); // Probability density Console.WriteLine(@"{0} - Probability mass at location '3'", bernoulli.Probability(3).ToString(" #0.00000;-#0.00000")); // Log probability density Console.WriteLine(@"{0} - Log probability mass at location '3'", bernoulli.ProbabilityLn(3).ToString(" #0.00000;-#0.00000")); // Entropy Console.WriteLine(@"{0} - Entropy", bernoulli.Entropy.ToString(" #0.00000;-#0.00000")); // Largest element in the domain Console.WriteLine(@"{0} - Largest element in the domain", bernoulli.Maximum.ToString(" #0.00000;-#0.00000")); // Smallest element in the domain Console.WriteLine(@"{0} - Smallest element in the domain", bernoulli.Minimum.ToString(" #0.00000;-#0.00000")); // Mean Console.WriteLine(@"{0} - Mean", bernoulli.Mean.ToString(" #0.00000;-#0.00000")); // Mode Console.WriteLine(@"{0} - Mode", bernoulli.Mode.ToString(" #0.00000;-#0.00000")); // Variance Console.WriteLine(@"{0} - Variance", bernoulli.Variance.ToString(" #0.00000;-#0.00000")); // Standard deviation Console.WriteLine(@"{0} - Standard deviation", bernoulli.StdDev.ToString(" #0.00000;-#0.00000")); // Skewness Console.WriteLine(@"{0} - Skewness", bernoulli.Skewness.ToString(" #0.00000;-#0.00000")); Console.WriteLine(); // 3. Generate 10 samples of the Bernoulli distribution Console.WriteLine(@"3. Generate 10 samples of the Bernoulli distribution"); for (var i = 0; i < 10; i++) { Console.Write(bernoulli.Sample().ToString("N05") + @" "); } Console.WriteLine(); Console.WriteLine(); // 4. Generate 100000 samples of the Bernoulli(0.2) distribution and display histogram Console.WriteLine(@"4. Generate 100000 samples of the Bernoulli(0.2) distribution and display histogram"); var data = new double[100000]; for (var i = 0; i < data.Length; i++) { data[i] = bernoulli.Sample(); } ConsoleHelper.DisplayHistogram(data); Console.WriteLine(); // 5. Generate 100000 samples of the Bernoulli(4) distribution and display histogram Console.WriteLine(@"5. Generate 100000 samples of the Bernoulli(0.9) distribution and display histogram"); bernoulli.P = 0.9; for (var i = 0; i < data.Length; i++) { data[i] = bernoulli.Sample(); } ConsoleHelper.DisplayHistogram(data); Console.WriteLine(); // 6. Generate 100000 samples of the Bernoulli(8) distribution and display histogram Console.WriteLine(@"6. Generate 100000 samples of the Bernoulli(0.5) distribution and display histogram"); bernoulli.P = 0.5; for (var i = 0; i < data.Length; i++) { data[i] = bernoulli.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; } }