9_A

Create a simulation with graphics to convince yourself of the pointwise convergence of the empirical CDF(9A1) and Generate sample paths of jump processes(9A2)

header

9_A1 assignament

Request

Create a simulation with graphics to convince yourself of the pointwise convergence of the empirical CDF to the theoretical distribution (Glivenko-Cantelli theorem). Use a simple random variable of your chooice for such a demonstration.

My Solution

Code in C#

Methods for calculate empirical CDS 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
    
     public List<PointF> CDFtoViewport(Rectangle viewPort,int n)
        {
            List<PointF> cdfs=new List<PointF>();
            List<int> values=new List<int>();
            for (int i=0; i < n; i++)
            {
              values.Add(R.Next(149, 220));
            }
                       
            float tmpy = 0;
            float tmpx = -1;

            for (int i = 150; i <= 220; i = i + 5)
            {
                tmpy = 0;
                tmpx = tmpx + 1 ;

                foreach (int p in values)
                {
                    if (p < i) tmpy++;

                    //empirical CDF


                }
                cdfs.Add(new PointF(tmpx, tmpy/n));
               

            }
            PointF[] viewPortArraycdf = cdfs.ToArray();
            this.m2.TransformPoints(viewPortArraycdf);
            return(viewPortArraycdf.ToList());
           
        }

Method to draw both empirical and theorical rapresentation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    
    private void disegnaCDFPaths(List<PointF> viewPortCDF)
        {

            Pen pen = new Pen(Color.Red);
            Pen pen2 = new Pen(Color.Green);

            //empirical cdf
            for (int j = 1; j < viewPortCDF.Count; j++)
            {
                g2.DrawLine(pen, (float)viewPortCDF[j-1].X, (float)viewPortCDF[j-1].Y,
                    (float)viewPortCDF[j].X, (float)viewPortCDF[j].Y);

                g2.DrawEllipse(pen, new Rectangle((int)viewPortCDF[j].X, (int)viewPortCDF[j].Y, 4,4));
            }

            //theorical cdf
           
                g2.DrawLine(pen2,(viewPort.Left+viewPort.Width), viewPort.Top,
                      viewPort.Left, viewPort.Top+viewPort.Height);
            

        }

9_A2 assignament

Request

Generate sample paths of jump processes which at each time considered t = 1, …, n perform jumps computed as:

and see what happens as n (simulation parameter) becomes larger.

[As before, at time n (last time) and one other chosen inner time 1<j<n (j is a program parameter) create and represent with histogram the distribution of the process ]

My Solution

Code in C#

Interface Pathfinder in C#

1
2
3
4
5
6
  interface Pathfinder
    {
        abstract public List<Strade> Get_paths();

    }
    

Class Rademacher Pathfinder 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
    
     public class RademacherPathfinder : Pathfinder
    {
       

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

        public RademacherPathfinder(int n, int m)
        {
            this.m = n;
            this.n = m;
            this.p = 0.5;

            this.R = new Random();

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

        }

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

            if (random_outcome < p) return 1;

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

        private List<double> createRademacherList()
        {
            List<double> rademacher = new List<double>();

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

            return rademacher;
        }
        
         public List<Strade> Get_paths()
        {
            return this.paths;
        }

    }

Class Normal Pathfinder 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
        public class NormalPathfinder: Pathfinder
    {
       

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

        public NormalPathfinder(int n, int m)
        {
            this.m = n;
            this.n = m;
            this.p = 0.5;

            this.R = new Random();

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

        }

        private double normal_Result(double p)
        {
           double random_outcome = R.NextDouble();
            double normal_distrbAtOut;
            double v = R.NextDouble();
            //create a value between 1 and -1
            random_outcome = random_outcome * 2 - 1;
            //get the standard normal for that point 
            normal_distrbAtOut= Math.Pow(Math.E, (Math.Pow(-random_outcome, 2) / 2))/Math.Sqrt(2*Math.PI) ;
            //then use the other generated random
            if (v < normal_distrbAtOut) return random_outcome;
            else return 0;

        }

        private List<double> createNormalList()
        {
            List<double> normal = new List<double>();

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

            return normal;
        }


       public List<Strade> Get_paths()
        {
            return this.paths;
        }


    }
    

Disegna grafici 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
       public class DisegnaGrafici
    {
        public Bitmap bitmap;
        public Graphics g2;
        public PictureBox pictureBox;

        private int SCALE = 4;

        private Random R = new Random();

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

        private Rectangle viewPort;
        int n;
        Matrix m1;
        Matrix m2;
        Pathfinder distrubution;

        public DisegnaGrafici(int m, int n, int j, Graphics graphics, double epsilon, Rectangle vPort, int dinamicleft, int dinamictop, int contgw, int contgh, TextBox boxassfreq, TextBox boxrelfreq, bool is_abs,bool is_norml)
        {
            this.g2 = graphics;
            this.viewPort = vPort;
            this.viewPort.X = dinamicleft;
            this.viewPort.Y = dinamictop;
            this.viewPort.Width = contgw;
            this.viewPort.Height = contgh;
            this.n = n;
            this.m1 = new Matrix();
            this.m2 = 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((float)-0, -(float)0, MatrixOrder.Append);
            m1.Scale((float)(viewPort.Width /m ), (float)(-viewPort.Height /1), MatrixOrder.Append);
            m1.Translate(viewPort.Left, viewPort.Top + viewPort.Height, MatrixOrder.Append);

            //Matrix for the cdf
            m2.Reset();
            m2.Translate((float)-0, -(float)0, MatrixOrder.Append);
            m2.Scale((float)(viewPort.Width /14), (float)(-viewPort.Height / 1), MatrixOrder.Append);
            m2.Translate(viewPort.Left, viewPort.Top + viewPort.Height, MatrixOrder.Append);

            //genero le "strade"
            if (is_norml == false)
                distrubution = new RademacherPathfinder(n, m);
            else distrubution = new NormalPathfinder(n, m);
            //disegno i vari path convertendoli per il viewport
            if (is_abs)
                disegnaCDFPaths(CDFtoViewport(viewPort,n));
            else
            {
                disegnaPaths(fromPathstoViewport(distrubution.Get_paths(), viewPort));

                disegnaHistogramma(viewPort, getDistribution(distrubution.Get_paths(), m / SCALE, j), n, j);
                disegnaHistogramma(viewPort, getDistribution(distrubution.Get_paths(), m / SCALE, n), n, n);
            }

            getAbsoluteFrequencies(distrubution.Get_paths(), n, m, epsilon, boxassfreq, boxrelfreq);


        }

        private void disegnaCDFPaths(List<PointF> viewPortCDF)
        {

            Pen pen = new Pen(Color.Red);
            Pen pen2 = new Pen(Color.Green);

            //empirical cdf
            for (int j = 1; j < viewPortCDF.Count; j++)
            {
                g2.DrawLine(pen, (float)viewPortCDF[j-1].X, (float)viewPortCDF[j-1].Y,
                    (float)viewPortCDF[j].X, (float)viewPortCDF[j].Y);

                g2.DrawEllipse(pen, new Rectangle((int)viewPortCDF[j].X, (int)viewPortCDF[j].Y, 4,4));
            }

            //theorical cdf
           
                g2.DrawLine(pen2,(viewPort.Left+viewPort.Width), viewPort.Top,
                      viewPort.Left, viewPort.Top+viewPort.Height);
            

        }
                
        public List<PointF> CDFtoViewport(Rectangle viewPort,int n)
        {
            List<PointF> cdfs=new List<PointF>();
            List<int> values=new List<int>();
            for (int i=0; i < n; i++)
            {
              values.Add(R.Next(149, 220));
            }
                       
            float tmpy = 0;
            float tmpx = -1;

            for (int i = 150; i <= 220; i = i + 5)
            {
                tmpy = 0;
                tmpx = tmpx + 1 ;

                foreach (int p in values)
                {
                    if (p < i) tmpy++;

                    //empirical CDF


                }
                cdfs.Add(new PointF(tmpx, tmpy/n));
               

            }
            PointF[] viewPortArraycdf = cdfs.ToArray();
            this.m2.TransformPoints(viewPortArraycdf);
            return(viewPortArraycdf.ToList());
           
        }

        public void getAbsoluteFrequencies(List<Strade> paths, int n, int m, double epsilon, TextBox boxassfreq, TextBox boxrelfreq)
        {
            double p=0.5 ;
            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/2 * ((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= -noIntervals; 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();
                for (int i = 0;i < viewPortArrayPath.Length; i++)
                {

                  viewPortArrayPath[i].Y= (viewPortArrayPath[i].Y + 1) / 2;
                    
                }

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

            return viewPortPaths;
        }




        public Graphics getGrapichs()
        {
            return this.g2;
        }
    } 
    
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy