8_A

Generate and represent m sample paths of n point each (m, n are program parameters), where each point represents a pair of:time index t, and relative frequency of success f(t)

header

8_A assignament

Request

Generate and represent m “sample paths” of n point each (m, n are program parameters), where each point represents a pair of: time index t, and relative frequency of success f(t),

where f(t) is the sum of t Bernoulli random variables with distribution B(x, p) = p^x(1-p)^(1-x) observed at the various times up to t: j=1, …, t..

At time n (last time) and one other chosen inner time 1<j<n (where j is a user parameter) represent with a histogram the distribution of f(t). See also what happens if you replace the relative frequency f(t) with the absolute frequency n(t) or by standard relative frequency: (f(t)-p) / sqrt(p(1-p)/t) [ or simply the “normalized” sum of bernoulli r.v.’s: n(t) / Math.sqrt(t) ].

Comment briefly on the convergence results you see.

My Solution

Code in C#

Class Main Form in C#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
public partial class BernulliGraphics : Form
    {
        public BernulliGraphics()
        {
            InitializeComponent();
            contb = new Bitmap(755, 681);
            g2 = Graphics.FromImage(contb);
            comboBox1.SelectedIndex = 0;
        }



        Bitmap contb;
        Graphics g2;

        bool movable = false;
        bool resiable = false;
        // movable view port
        int contgleft = 0;
        int contgtop = 50;
        int contgwid = 400;
        int contgheight = 400;
        int mouseDeltax = 0;
        int mouseDeltay = 0;
        // window
        double minX_Window = 0;
        double maxX_Window = 400;
        double minY_Window = 0;
        double maxY_Window = 400;


        Rectangle viewPortc = new Rectangle(0, 50, 600, 600);






        private void button1_Click(object sender, EventArgs e)


        {
            creaGrafici();


        }








        private void creaGrafici()
        {
            bool is_abs = false;
            if (comboBox1.SelectedIndex == 1) is_abs = true;
            int m = (int)this.numericUpDown1.Value;
            int n = (int)this.numericUpDown2.Value;
            double p = (double)this.numericUpDown3.Value / 100;
            int j = 40;
            double epsilon = (double)this.numericUpDown4.Value;


            DisegnaGrafici gr = new DisegnaGrafici(m, n, j, g2, p, epsilon, viewPortc, contgleft, contgtop, contgwid, contgheight, textBox1, textBox2,is_abs);
            gr.getGrapichs();
            pictureBox2.Image = contb;


        }



        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if ((e.Location.Y >= viewPortc.Top && e.Location.Y <= (viewPortc.Top + viewPortc.Height)) && (e.Location.X >= viewPortc.Left && e.Location.X <= (viewPortc.Left + viewPortc.Width)))
            {
                if (e.Button == MouseButtons.Left)
                    movable = true;
                if (e.Button == MouseButtons.Right)
                    resiable = true;
                mouseDeltax = e.Location.X;
                mouseDeltay = e.Location.Y;

            }


        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            resiable = false;
            movable = false;
        }

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (movable == true)
            {

                contgleft = e.Location.X;
                contgtop = e.Location.Y;
                if (contgleft <= 0) contgleft = 0;
                if (contgtop <= 20) contgtop = 20;
                creaGrafici();
            }
            if (resiable == true)
            {
                mouseDeltax = -mouseDeltax + e.Location.X;
                mouseDeltay = -mouseDeltay + e.Location.Y;
                contgwid += mouseDeltax / 40;
                contgheight += mouseDeltay / 40;
                creaGrafici();
            }






        }



        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            resiable = false;
            movable = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            creaGrafici();

        }
    }
    
    

DisegnaGrafici class

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
 public class DisegnaGrafici
    {
        public Bitmap bitmap;
        public Graphics g2;
        public PictureBox pictureBox;

        private int SCALE = 10;

        private Random R = new Random();

        public List<Strade> viewPortPaths;
        public List<Strade> viewPortAbsolute;

        private Rectangle viewPort;
        Matrix m1;
        BernulliPathfinder bernulli;

        public DisegnaGrafici(int m, int n, int j, Graphics graphics, double p, double epsilon, Rectangle vPort, int dinamicleft, int dinamictop, int contgw, int contgh, TextBox boxassfreq, TextBox boxrelfreq, bool is_abs)
        {
            this.g2 = graphics;
            this.viewPort = vPort;
            this.viewPort.X = dinamicleft;
            this.viewPort.Y = dinamictop;
            this.viewPort.Width = contgw;
            this.viewPort.Height = contgh;
            this.m1 = new Matrix();
            g2.Clear(Color.Transparent);
            g2.FillRectangle(Brushes.Transparent, this.viewPort);
            g2.DrawRectangle(new Pen(Color.Black), this.viewPort);
            //genero la marice per le trasformazioni
            m1.Reset();
            m1.Translate(-0, -(int)0, MatrixOrder.Append);
            m1.Scale((int)(viewPort.Width / (n - 0)), (int)(-viewPort.Height / (1 - 0)), MatrixOrder.Append);
            m1.Translate(viewPort.Left, viewPort.Top + viewPort.Height, MatrixOrder.Append);

            //genero le "strade"
            bernulli = new BernulliPathfinder(n, m, p);

            //disegno i vari path convertendoli per il viewport
            if (is_abs)
                disegnaABSPaths(fromPathstoAbsViewport(bernulli.paths, viewPort,n));
            else
            {
                disegnaPaths(fromPathstoViewport(bernulli.paths, viewPort));

                disegnaHistogramma(viewPort, getDistribution(bernulli.paths, m / SCALE, j), n, j);
                disegnaHistogramma(viewPort, getDistribution(bernulli.paths, m / SCALE, n), n, n);
            }

            getAbsoluteFrequencies(bernulli.paths, n, m, p, epsilon, boxassfreq, boxrelfreq);


        }

        private void disegnaABSPaths(List<List<PointF>> viewPortABSPaths)
        {
            for (int i = 0; i < viewPortABSPaths.Count; i++)
            {
                Pen pen = new Pen(Color.FromArgb(R.Next(0, 255), R.Next(0, 255), R.Next(0, 255)));


                for (int j = 0; j < viewPortABSPaths[i].Count - 1; j++)
                {
                    g2.DrawLine(pen, (float)viewPortABSPaths[i][j].X, (float)viewPortABSPaths[i][j].Y,
                        (float)viewPortABSPaths[i][j + 1].X, (float)viewPortABSPaths[i][j + 1].Y);
                }
            }
        }

        public List<List<PointF>> fromPathstoAbsViewport(List<Strade> paths, Rectangle viewPort,int n)
        {
            List<List<PointF>> viewPortAbsPaths = new List<List<PointF>>();
           
            float tmpy = 0;
            float tmpx = 0;

            foreach (Strade path in paths)
            {
                tmpy = 0;
                tmpx = 0;
                List<PointF> abtmp = new List<PointF>();
                foreach (int p in path.values)
                {
                    if (p == 1) tmpy++;
                    tmpx++;
                    //only for graphic it rapidly
                    abtmp.Add(new PointF(tmpx, (tmpy / n)));

                }
                    PointF[] viewPortArrayPath = abtmp.ToArray();

                    this.m1.TransformPoints(viewPortArrayPath);

                    viewPortAbsPaths.Add(viewPortArrayPath.ToList());
                }
            

            return viewPortAbsPaths;
        }

        public void getAbsoluteFrequencies(List<Strade> paths, int n, int m, double p, double epsilon, TextBox boxassfreq, TextBox boxrelfreq)
        {
            Intervalli p_neighbourhood = new Intervalli(p - epsilon, p + epsilon);

            int absolute_frequency = 0;
            double relative_frequency = 0;

            foreach (Strade path in paths)
            {
                for (int i = 0; i < path.getPath().Count; i++)
                {
                    if (
                        (path.getPath()[i].X == n)
                        &&
                        (path.getPath()[i].Y >= p_neighbourhood.LowerBound)
                        &&
                        (path.getPath()[i].Y < p_neighbourhood.UpperBound)
                        )
                    {
                        absolute_frequency++;
                    }
                }
            }

            relative_frequency = (double)absolute_frequency / (double)m;

            boxassfreq.Text = absolute_frequency.ToString();
            boxrelfreq.Text = relative_frequency * 100 + "%";



        }


        public void disegnaPaths(List<Strade> viewPortPaths)
        {
            for (int i = 0; i < viewPortPaths.Count; i++)
            {
                Pen pen = new Pen(Color.FromArgb(R.Next(0, 255), R.Next(0, 255), R.Next(0, 255)));


                for (int j = 0; j < viewPortPaths[i].getPath().Count - 1; j++)
                {
                    g2.DrawLine(pen, (float)viewPortPaths[i].getPath()[j].X, (float)viewPortPaths[i].getPath()[j].Y,
                        (float)viewPortPaths[i].getPath()[j + 1].X, (float)viewPortPaths[i].getPath()[j + 1].Y);
                }
            }
        }

        public void disegnaHistogramma(Rectangle viewPort, List<Intervalli> intervals, int n, int j)
        {

            for (int i = 0; i < intervals.Count; i++)
            {
                int x, y;
                int width, height;
                // in this case on the fly trasformation is way faster
                x = (int)(this.viewPort.Left + this.viewPort.Width * (j) / n);
                y = (int)(viewPort.Top + viewPort.Height * ((100 - intervals[i].UpperBound * 100) / 100));


                width = intervals[i].Counter;
                height = viewPort.Height / intervals.Count;

                Rectangle rectangle = new Rectangle(x, y, width, height);

                g2.DrawRectangle(Pens.Black, rectangle);
                SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 150, 0, 0));
                g2.FillRectangle(semiTransBrush, rectangle);

                g2.FillRectangle(Brushes.Violet, rectangle);
            }
        }

        public List<Intervalli> getDistribution(List<Strade> paths, int noIntervals, int j)
        {


            List<double> frequencies = new List<double>();

            for (int i = 0; i < paths.Count; i++)
            {
                for (int k = 0; k < paths[i].getPath().Count; k++)
                {
                    if (paths[i].getPath()[k].X == j)
                    {
                        frequencies.Add(paths[i].getPath()[k].Y);
                    }
                }
            }

            List<Intervalli> intervals = new List<Intervalli>();

            double intervalLength = 1.0 / (double)noIntervals;

            for (int i = 0; i < noIntervals; i++)
            {
                intervals.Add(new Intervalli(i * intervalLength, (i + 1) * intervalLength));
            }

            for (int i = 0; i < intervals.Count; i++)
            {
                for (int k = 0; k < frequencies.Count; k++)
                {
                    if ((frequencies[k] >= intervals[i].LowerBound) && (frequencies[k] < intervals[i].UpperBound))
                    {
                        intervals[i].Counter++;
                    }
                }
            }

            return intervals;
        }

        public List<Strade> fromPathstoViewport(List<Strade> paths, Rectangle viewPort)
        {
            List<Strade> viewPortPaths = new List<Strade>();

            foreach (Strade path in paths)
            {

                PointF[] viewPortArrayPath = path.getPath().ToArray();

                this.m1.TransformPoints(viewPortArrayPath);
                viewPortPaths.Add(new Strade(viewPortArrayPath.ToList()));
            }

            return viewPortPaths;
        }




        public Graphics getGrapichs()
        {
            return this.g2;
        }
    }

BernoulliPathfinder (Class with bernulli random variable creation methods)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 public class BernulliPathfinder
    {
       

        int m;      //number of paths
        int n;      //number of points 
        double p;   //probability
        public List<Strade> paths = new List<Strade>();
        private Random R;

        public BernulliPathfinder(int n, int m, double p)
        {
            this.m = n;
            this.n = m;
            this.p = p;

            this.R = new Random();

            for (int i=0; i < m; i++)
            {
                paths.Add(new Strade(createBernulliList()));
            }
            
        

        }

        private int bernoulli_Result(double p)
        {
           double random_outcome = R.NextDouble();

            if (random_outcome <= p) return 1;
            else return 0;
        }

        private List<int> createBernulliList()
        {
            List<int> bernoulli = new List<int>();

            for (int i = 0; i < n; i++)
            {
                bernoulli.Add(bernoulli_Result(p));
            }

            return bernoulli;
        }
        

    }

Strade.cs (path struct creation and useful methods)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 public class Strade
    {
        public List<PointF> path = new List<PointF>();
        public List<int> values = new List<int>();

        //Dalla lista di valori passo ai punti ;
        public Strade(List<int> values)
        {
            this.values = values;
            double mean = 0;

            for (int i=0; i < values.Count; i++)
            {
                if (i == 0)
                {
                    mean = values[0];
                }
                else
                {
                    mean += (values[i] - mean) / (double)(i + 1);
                }

                path.Add(new PointF(i + 1, (float) mean));
            }
        }

        public Strade(List<PointF> points) 
        {
            path = points;
        }

        public List<double> getXs()
        {
            List<double> x_coordinates = new List<double>();

            foreach (PointF p in this.getPath())
            {
                x_coordinates.Add(p.X);
            }

            return x_coordinates;
        }

        public List<double> getYs()
        {
            List<double> y_coordinates = new List<double>();

            foreach (PointF p in this.getPath())
            {
                y_coordinates.Add(p.Y);
            }

            return y_coordinates;
        }

        public List<PointF> getPath()
        {
            return this.path;
        }

        public void setPath(List<PointF> path)
        {
            this.path = path;
        }
    }
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy