Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11.  
  12. namespace RVStand
  13. {
  14.     struct HistItem
  15.     {
  16.         public double x1, x2, y;
  17.     }
  18.  
  19.     public partial class Form1 : Form
  20.     {
  21.  
  22.  
  23.         RandomValues rv;
  24.         List<double> RVs = new List<double>();
  25.         List<HistItem> hist = new List<HistItem>();
  26.        
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.  
  32.             rv = new RandomValues();
  33.         }
  34.  
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.             int n, r;
  38.             double p = 0, lambda = 0, a = 0, sigma = 0, alpha = 0, beta = 0;
  39.             int runs = 1000;
  40.  
  41.             RVs.Clear();
  42.  
  43.             try
  44.             {
  45.                 n = Int32.Parse(textBox1.Text);
  46.                 p = Double.Parse(textBox2.Text);
  47.                 r = Int32.Parse(textBox3.Text);
  48.                 lambda = Double.Parse(textBox4.Text);
  49.                 a = Double.Parse(textBox6.Text);
  50.                 sigma = Double.Parse(textBox7.Text);
  51.                 alpha = Double.Parse(textBox8.Text);
  52.                 beta = Double.Parse(textBox9.Text);
  53.                 runs = Int32.Parse(textBox5.Text);
  54.             }
  55.             catch (Exception exception)
  56.             {
  57.                 MessageBox.Show("Значения были введены неправильно! Десятичные дроби введите через запятую.");
  58.                 return;
  59.             }
  60.  
  61.             hist.Clear();
  62.  
  63.             for(int i = 0; i <  dataGridView1.Rows.Count-1; i++)
  64.             {
  65.                 HistItem dh;
  66.                 dh.x1 = Double.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
  67.                 dh.x2 = Double.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
  68.                 dh.y = Double.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
  69.  
  70.                 hist.Add(dh);
  71.             }
  72.  
  73.  
  74.  
  75.             switch (comboBox1.SelectedIndex)
  76.             {
  77.                 case 0:
  78.                     for(int i = 0; i < runs; i++)
  79.                     {
  80.                         RVs.Add(rv.Uniform(n));
  81.                     }
  82.                     break;
  83.                 case 1:
  84.                     for (int i = 0; i < runs; i++)
  85.                     {
  86.                         RVs.Add(rv.Geometric(p));
  87.                     }
  88.                     break;
  89.                 case 2:
  90.                     for (int i = 0; i < runs; i++)
  91.                     {
  92.                         RVs.Add(rv.NegativeBinomial(r, p));
  93.                     }
  94.                     break;
  95.                 case 3:
  96.                     for (int i = 0; i < runs; i++)
  97.                     {
  98.                         RVs.Add(rv.Binomial(n, p));
  99.                     }
  100.                     break;
  101.                 case 4:
  102.                     for (int i = 0; i < runs; i++)
  103.                     {
  104.                         RVs.Add(rv.Poisson(lambda));
  105.                     }
  106.                     break;
  107.                 case 5:
  108.                     for (int i = 0; i < runs; i++)
  109.                     {
  110.                         RVs.Add(rv.Exponential(lambda));
  111.                     }
  112.                     break;
  113.                 case 6:
  114.                     for (int i = 0; i < runs; i++)
  115.                     {
  116.                         RVs.Add(rv.ByHistogram(hist));
  117.                     }
  118.                     break;
  119.                 case 7:
  120.                     for (int i = 0; i < runs; i++)
  121.                     {
  122.                         RVs.Add(rv.HyperExponential());
  123.                     }
  124.                     break;
  125.                 case 8:
  126.                     for (int i = 0; i < runs; i++)
  127.                     {
  128.                         RVs.Add(rv.Gaussian1(a, sigma));
  129.                     }
  130.                     break;
  131.                 case 9:
  132.                     for (int i = 0; i < runs; i++)
  133.                     {
  134.                         RVs.Add(rv.Gaussian2(a, sigma));
  135.                     }
  136.                     break;
  137.                 case 10:
  138.                     for (int i = 0; i < runs; i++)
  139.                     {
  140.                         RVs.Add(rv.Gamma(alpha, beta));
  141.                     }
  142.                     break;
  143.             }
  144.  
  145.             Dictionary<int, int> res = new Dictionary<int, int>();
  146.  
  147.             double mean = 0;
  148.             double variance = 0;
  149.  
  150.             foreach (int value in RVs)
  151.             {
  152.                 mean += value;
  153.  
  154.                 if(res.ContainsKey(value))
  155.                 {
  156.                     res[value]++;
  157.                 }
  158.                 else
  159.                 {
  160.                     res[value] = 0;
  161.                 }
  162.             }
  163.  
  164.             mean /= runs;
  165.  
  166.             label5.Text = "Mean: " + mean;
  167.  
  168.             foreach (int value in RVs)
  169.             {
  170.                 variance += (mean - value) * (mean - value);
  171.             }
  172.  
  173.             variance /= runs;
  174.  
  175.             label6.Text = "Variance: " + variance;
  176.  
  177.             chart1.Series[0].Points.Clear();
  178.  
  179.             var l = res.OrderBy(key => key.Key);
  180.             res = l.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
  181.  
  182.             foreach (KeyValuePair<int, int> item in res)
  183.             {
  184.                 chart1.Series[0].Points.AddXY(item.Key, (double)item.Value / runs);
  185.             }
  186.  
  187.             if (comboBox1.SelectedIndex <= 4)
  188.             {
  189.                 chart1.Series[0].ChartType = SeriesChartType.Line;
  190.                 chart1.Series[0].BorderWidth = 3;
  191.             }
  192.             else
  193.             {
  194.                 chart1.Series[0].ChartType = SeriesChartType.Column;
  195.                 chart1.ChartAreas[0].AxisX.Interval = 1;
  196.                 chart1.ChartAreas[0].AxisX.IntervalOffset = 0;
  197.                 chart1.ChartAreas[0].AxisX.Minimum = 0;
  198.                 chart1.Series[0].BorderWidth = 1;
  199.                 chart1.Series[0].BorderColor = Color.Red;
  200.                 chart1.Series[0]["PointWidth"] = "1";
  201.                 foreach (DataPoint dp in chart1.Series[0].Points) dp.XValue += chart1.ChartAreas[0].AxisX.Interval / 2;
  202.             }
  203.  
  204.             if(comboBox1.SelectedIndex == 8 || comboBox1.SelectedIndex == 9)
  205.                 chart1.ChartAreas[0].AxisX.Minimum = -3;
  206.  
  207.         }
  208.  
  209.         private void Form1_Load(object sender, EventArgs e)
  210.         {
  211.  
  212.         }
  213.  
  214.         private void chart1_Click(object sender, EventArgs e)
  215.         {
  216.  
  217.         }
  218.     }
  219.  
  220.     class RandomValues
  221.     {
  222.         Random rand;
  223.  
  224.         public RandomValues()
  225.         {
  226.             rand = new Random();
  227.         }
  228.  
  229.         public int Uniform(int n)
  230.         {
  231.             double alpha = rand.NextDouble();
  232.             return (int)(alpha * (n + 1));
  233.         }
  234.  
  235.         public int Geometric(double p)
  236.         {
  237.             double alpha = rand.NextDouble();
  238.             return (int)(Math.Log(alpha) / Math.Log(1.0 - p));
  239.         }
  240.  
  241.         public int NegativeBinomial(int r, double p)
  242.         {
  243.             int x = 0;
  244.  
  245.             for(int i = 0; i < r; i++)
  246.             {
  247.                 double alpha = rand.NextDouble();
  248.                 x += (int)(Math.Log(alpha) / Math.Log(1.0 - p));
  249.             }
  250.  
  251.             return x;
  252.         }
  253.  
  254.         public int Binomial(int n, double p)
  255.         {
  256.             int x = 0;
  257.  
  258.             for(int i = 0; i < n; i++)
  259.             {
  260.                 double alpha = rand.NextDouble();
  261.                 x += ((p - alpha) >= 0) ? 1 : 0;
  262.             }
  263.  
  264.             return x;
  265.         }
  266.  
  267.         public int Poisson(double lambda)
  268.         {
  269.             int m = 0;
  270.             double S = 0;
  271.  
  272.             do
  273.             {
  274.                 double alpha = rand.NextDouble();
  275.                 S += Math.Log(alpha);
  276.                 if (S >= -lambda)
  277.                     m++;
  278.             } while (S >= -lambda);
  279.  
  280.             return m;
  281.         }
  282.  
  283.         public double Exponential(double lambda)
  284.         {
  285.             double alpha = rand.NextDouble();
  286.             return -(Math.Log(1 - alpha) / lambda);
  287.         }
  288.  
  289.         public double ByHistogram(List<HistItem> hist)
  290.         {
  291.             double M = rand.NextDouble();
  292.             int k = 1;
  293.  
  294.             do
  295.             {
  296.                 M -= hist[k-1].y * (hist[k - 1].x2 - hist[k - 1].x1);
  297.                 if (M >= 0)
  298.                     k++;
  299.             } while (M >= 0);
  300.  
  301.             return hist[k - 1].x2 + M / hist[k - 1].y;
  302.         }
  303.  
  304.         public double HyperExponential()
  305.         {
  306.             double[] lambdats = new double[] {0.1, 0.2, 0.3, 0.4, 0.5};
  307.             int cLambda = rand.Next(0, lambdats.Length);
  308.             double lambda = lambdats[cLambda];
  309.             double alpha = rand.NextDouble();
  310.             return -Math.Log(alpha) / lambda;
  311.         }
  312.  
  313.         public double Gaussian1(double a, double sigma)
  314.         {
  315.             int n = 12;
  316.  
  317.             double v1 = 0;
  318.  
  319.             for (int i = 0; i < n; i++)
  320.             {
  321.                 double alpha = rand.NextDouble();
  322.                 v1 += alpha;
  323.             }
  324.  
  325.             v1 -= 6;
  326.  
  327.             double v2 = v1 + (1.0 / 240) * (v1 * v1 * v1 - 3 * v1);
  328.  
  329.             return sigma * v2 + a;
  330.         }
  331.  
  332.         public double Gaussian2(double a, double sigma)
  333.         {
  334.             double alpha = rand.NextDouble();
  335.  
  336.             Func<double, double> F = d =>
  337.             {
  338.                 double tt = Math.Sqrt(-Math.Log(d));
  339.                 return ((2.30753 + 0.27061 * tt) / (1 + 0.99229 * tt + 0.04481 * tt * tt)) - tt;
  340.             };
  341.  
  342.             double v = F(alpha);
  343.  
  344.             alpha = rand.NextDouble();
  345.  
  346.             if (alpha < 0.5)
  347.                 v *= -1;
  348.  
  349.             return sigma * v + a;
  350.         }
  351.  
  352.         public double Gamma(double alpha, double beta)
  353.         {
  354.             int d = (int) alpha;
  355.             double delta = alpha - d;
  356.  
  357.             double dlr = 0;
  358.  
  359.             if (delta > 0)
  360.             {
  361.                 double r = Math.E / (Math.E + delta);
  362.                 double n1 = 0, n2 = 1;
  363.  
  364.                 do
  365.                 {
  366.  
  367.                     double alpha1 = rand.NextDouble();
  368.                     double alpha2 = rand.NextDouble();
  369.  
  370.                    
  371.  
  372.                     if (alpha1 < r)
  373.                     {
  374.                         n1 = Math.Pow(alpha1 / r, 1.0 / delta);
  375.                         n2 = alpha2 * Math.Pow(n1, delta - 1);
  376.                     }
  377.                     else
  378.                     {
  379.                         n1 = 1 - Math.Log((alpha1 - r) / (1 - r));
  380.                         n2 = alpha2 * Math.Exp(-n1);
  381.                     }
  382.                 } while (n2 > Math.Pow(n1, delta-1) * Math.Exp(-n1));
  383.  
  384.                 dlr = n1;
  385.             }
  386.  
  387.             double dr = 0;
  388.             for (int i = 0; i < d; i++)
  389.             {
  390.                 dr += Exponential(1);
  391.             }
  392.  
  393.            
  394.  
  395.             return (dlr + dr) / beta;
  396.         }
  397.     }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement