Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace RVStand
- {
- struct HistItem
- {
- public double x1, x2, y;
- }
- public partial class Form1 : Form
- {
- RandomValues rv;
- List<double> RVs = new List<double>();
- List<HistItem> hist = new List<HistItem>();
- public Form1()
- {
- InitializeComponent();
- rv = new RandomValues();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int n, r;
- double p = 0, lambda = 0, a = 0, sigma = 0, alpha = 0, beta = 0;
- int runs = 1000;
- RVs.Clear();
- try
- {
- n = Int32.Parse(textBox1.Text);
- p = Double.Parse(textBox2.Text);
- r = Int32.Parse(textBox3.Text);
- lambda = Double.Parse(textBox4.Text);
- a = Double.Parse(textBox6.Text);
- sigma = Double.Parse(textBox7.Text);
- alpha = Double.Parse(textBox8.Text);
- beta = Double.Parse(textBox9.Text);
- runs = Int32.Parse(textBox5.Text);
- }
- catch (Exception exception)
- {
- MessageBox.Show("Значения были введены неправильно! Десятичные дроби введите через запятую.");
- return;
- }
- hist.Clear();
- for(int i = 0; i < dataGridView1.Rows.Count-1; i++)
- {
- HistItem dh;
- dh.x1 = Double.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
- dh.x2 = Double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
- dh.y = Double.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
- hist.Add(dh);
- }
- switch (comboBox1.SelectedIndex)
- {
- case 0:
- for(int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Uniform(n));
- }
- break;
- case 1:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Geometric(p));
- }
- break;
- case 2:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.NegativeBinomial(r, p));
- }
- break;
- case 3:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Binomial(n, p));
- }
- break;
- case 4:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Poisson(lambda));
- }
- break;
- case 5:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Exponential(lambda));
- }
- break;
- case 6:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.ByHistogram(hist));
- }
- break;
- case 7:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.HyperExponential());
- }
- break;
- case 8:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Gaussian1(a, sigma));
- }
- break;
- case 9:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Gaussian2(a, sigma));
- }
- break;
- case 10:
- for (int i = 0; i < runs; i++)
- {
- RVs.Add(rv.Gamma(alpha, beta));
- }
- break;
- }
- Dictionary<int, int> res = new Dictionary<int, int>();
- double mean = 0;
- double variance = 0;
- foreach (int value in RVs)
- {
- mean += value;
- if(res.ContainsKey(value))
- {
- res[value]++;
- }
- else
- {
- res[value] = 0;
- }
- }
- mean /= runs;
- label5.Text = "Mean: " + mean;
- foreach (int value in RVs)
- {
- variance += (mean - value) * (mean - value);
- }
- variance /= runs;
- label6.Text = "Variance: " + variance;
- chart1.Series[0].Points.Clear();
- var l = res.OrderBy(key => key.Key);
- res = l.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
- foreach (KeyValuePair<int, int> item in res)
- {
- chart1.Series[0].Points.AddXY(item.Key, (double)item.Value / runs);
- }
- if (comboBox1.SelectedIndex <= 4)
- {
- chart1.Series[0].ChartType = SeriesChartType.Line;
- chart1.Series[0].BorderWidth = 3;
- }
- else
- {
- chart1.Series[0].ChartType = SeriesChartType.Column;
- chart1.ChartAreas[0].AxisX.Interval = 1;
- chart1.ChartAreas[0].AxisX.IntervalOffset = 0;
- chart1.ChartAreas[0].AxisX.Minimum = 0;
- chart1.Series[0].BorderWidth = 1;
- chart1.Series[0].BorderColor = Color.Red;
- chart1.Series[0]["PointWidth"] = "1";
- foreach (DataPoint dp in chart1.Series[0].Points) dp.XValue += chart1.ChartAreas[0].AxisX.Interval / 2;
- }
- if(comboBox1.SelectedIndex == 8 || comboBox1.SelectedIndex == 9)
- chart1.ChartAreas[0].AxisX.Minimum = -3;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void chart1_Click(object sender, EventArgs e)
- {
- }
- }
- class RandomValues
- {
- Random rand;
- public RandomValues()
- {
- rand = new Random();
- }
- public int Uniform(int n)
- {
- double alpha = rand.NextDouble();
- return (int)(alpha * (n + 1));
- }
- public int Geometric(double p)
- {
- double alpha = rand.NextDouble();
- return (int)(Math.Log(alpha) / Math.Log(1.0 - p));
- }
- public int NegativeBinomial(int r, double p)
- {
- int x = 0;
- for(int i = 0; i < r; i++)
- {
- double alpha = rand.NextDouble();
- x += (int)(Math.Log(alpha) / Math.Log(1.0 - p));
- }
- return x;
- }
- public int Binomial(int n, double p)
- {
- int x = 0;
- for(int i = 0; i < n; i++)
- {
- double alpha = rand.NextDouble();
- x += ((p - alpha) >= 0) ? 1 : 0;
- }
- return x;
- }
- public int Poisson(double lambda)
- {
- int m = 0;
- double S = 0;
- do
- {
- double alpha = rand.NextDouble();
- S += Math.Log(alpha);
- if (S >= -lambda)
- m++;
- } while (S >= -lambda);
- return m;
- }
- public double Exponential(double lambda)
- {
- double alpha = rand.NextDouble();
- return -(Math.Log(1 - alpha) / lambda);
- }
- public double ByHistogram(List<HistItem> hist)
- {
- double M = rand.NextDouble();
- int k = 1;
- do
- {
- M -= hist[k-1].y * (hist[k - 1].x2 - hist[k - 1].x1);
- if (M >= 0)
- k++;
- } while (M >= 0);
- return hist[k - 1].x2 + M / hist[k - 1].y;
- }
- public double HyperExponential()
- {
- double[] lambdats = new double[] {0.1, 0.2, 0.3, 0.4, 0.5};
- int cLambda = rand.Next(0, lambdats.Length);
- double lambda = lambdats[cLambda];
- double alpha = rand.NextDouble();
- return -Math.Log(alpha) / lambda;
- }
- public double Gaussian1(double a, double sigma)
- {
- int n = 12;
- double v1 = 0;
- for (int i = 0; i < n; i++)
- {
- double alpha = rand.NextDouble();
- v1 += alpha;
- }
- v1 -= 6;
- double v2 = v1 + (1.0 / 240) * (v1 * v1 * v1 - 3 * v1);
- return sigma * v2 + a;
- }
- public double Gaussian2(double a, double sigma)
- {
- double alpha = rand.NextDouble();
- Func<double, double> F = d =>
- {
- double tt = Math.Sqrt(-Math.Log(d));
- return ((2.30753 + 0.27061 * tt) / (1 + 0.99229 * tt + 0.04481 * tt * tt)) - tt;
- };
- double v = F(alpha);
- alpha = rand.NextDouble();
- if (alpha < 0.5)
- v *= -1;
- return sigma * v + a;
- }
- public double Gamma(double alpha, double beta)
- {
- int d = (int) alpha;
- double delta = alpha - d;
- double dlr = 0;
- if (delta > 0)
- {
- double r = Math.E / (Math.E + delta);
- double n1 = 0, n2 = 1;
- do
- {
- double alpha1 = rand.NextDouble();
- double alpha2 = rand.NextDouble();
- if (alpha1 < r)
- {
- n1 = Math.Pow(alpha1 / r, 1.0 / delta);
- n2 = alpha2 * Math.Pow(n1, delta - 1);
- }
- else
- {
- n1 = 1 - Math.Log((alpha1 - r) / (1 - r));
- n2 = alpha2 * Math.Exp(-n1);
- }
- } while (n2 > Math.Pow(n1, delta-1) * Math.Exp(-n1));
- dlr = n1;
- }
- double dr = 0;
- for (int i = 0; i < d; i++)
- {
- dr += Exponential(1);
- }
- return (dlr + dr) / beta;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement