S_Local_Limit_Theorem: Simulations of Local Theorem of DeMoivre-Laplace

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/S_Local_Theorem.R

Description

Given n Bernoulli experiments, with success probability p, this function calculates and plots the exact probability and the approximate probability that a successful event occurs exactly m times (0<=m<=n). It also calculates the difference between these probabilities and shows all the computations in a table.

Usage

1
2
S_Local_Limit_Theorem(n = 170, p = 0.5, Compare = TRUE, Table = TRUE, 
     Graph = TRUE, GraphE = TRUE)

Arguments

n

An integer value representing the number of repetitions of the experiment.

p

A real value with the probability that a successful event will happen in any single Bernoulli experiment (called the probability of success).

Compare

A logical value, if TRUE the function calculates both the exact probability and the approximate probability that a successful event occurs exactly m times and compares these probabilities.

Table

A logical value, if TRUE the function shows a table with the carried out computations.

Graph

A logical value, if TRUE the function plots both the exact probability and the approximate probability that a successful event occurs exactly m times.

GraphE

A logical value, if TRUE the function shows the graphic of the errors in the approximation.

Details

Bernoulli experiments are sequences of events, in which successive experiments are independent and at each experiment the probability of appearance of a "successful" event (p) remains constant. The value of n must be high and the value of p must be small.

Value

A graph and/or a table.

Note

Department of Mathematics. University of Oriente. Cuba.

Author(s)

Larisa Zamora and Jorge Diaz

References

Gnedenko, B. V. (1978). The Theory of Probability. Mir Publishers. Moscow.

See Also

Integral_Theorem, Local_Theorem.

Examples

 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
S_Local_Limit_Theorem(n = 170, p = 0.5, Compare = TRUE, Table = TRUE, Graph = TRUE, 
     GraphE = TRUE)

## The function is currently defined as
function (n = 170, p = 0.5, Compare = TRUE, Table = TRUE, Graph = TRUE, GraphE = TRUE) 
  { layout(matrix(1))
    m <- array(0:n)
    x <- numeric()
    PNormal <- numeric()
    a <- n * p
    b <- sqrt(a * (1 - p))
    for (mi in 1:(n + 1)) {
        x[mi] <- (mi - 1 - a)/b
        PNormal[mi] <- dnorm(x[mi], 0, 1)/b
    }
    if (Compare == TRUE) {
        PBin <- numeric()
        for (mi in 1:(n + 1)) PBin[mi] <- dbinom(mi - 1, n, p)
        Dif <- abs(PBin - PNormal)
    }
    if (Graph == TRUE & GraphE == TRUE) {
        layout(matrix(c(1, 1, 2, 2), 2, 2, byrow = TRUE))
    }
    if (Graph == TRUE) {
        mfg <- c(1, 1, 2, 2)
        plot(PNormal, type = "p", main = "The Local Limit Theorem", 
            xlab = "m", ylab = "Probability", col = "red")
        mtext("Local Theorem", line = -1, side = 3, adj = 1, 
            col = "red")
        if (Compare == TRUE) {
            points(PBin, type = "p", col = "blue")
            mtext("Binomial Probability", line = -2, side = 3, 
                adj = 1, col = "blue")
        }
    }
    if (GraphE == TRUE) {
        mfg <- c(2, 1, 2, 2)
        dmini <- min(Dif) - 0.01
        dmaxi <- max(Dif) + 0.01
        plot(Dif, ylim = c(dmini, dmaxi), type = "b", main = "Errors", 
            xlab = "m", ylab = "Errors", col = "green")
        abline(a = 0, b = 0, col = "red")
    }
    if (Table == TRUE) {
        if (Compare == TRUE) 
            TablaR <- data.frame(m = m, x = x, PBinomial = PBin, 
                TLocal = PNormal, Difference = Dif)
        else TablaR <- data.frame(m = m, x = x, TLocal = PNormal)
        TablaR
    }
  }

Example output

      m          x    PBinomial       TLocal   Difference
1     0 -13.038405 6.681912e-52 7.441914e-39 7.441914e-39
2     1 -12.885012 1.135925e-49 5.434558e-38 5.434558e-38
3     2 -12.731619 9.598566e-48 3.876370e-37 3.876370e-37
4     3 -12.578226 5.375197e-46 2.700644e-36 2.700644e-36
5     4 -12.424833 2.244145e-44 1.837769e-35 1.837769e-35
6     5 -12.271440 7.450561e-43 1.221507e-34 1.221507e-34
7     6 -12.118047 2.048904e-41 7.930160e-34 7.930160e-34
8     7 -11.964654 4.800290e-40 5.028627e-33 5.028626e-33
9     8 -11.811261 9.780591e-39 3.114570e-32 3.114569e-32
10    9 -11.657868 1.760506e-37 1.884205e-31 1.884204e-31
11   10 -11.504475 2.834415e-36 1.113370e-30 1.113367e-30
12   11 -11.351082 4.122786e-35 6.425875e-30 6.425834e-30
13   12 -11.197689 5.462691e-34 3.622482e-29 3.622428e-29
14   13 -11.044296 6.639271e-33 1.994626e-28 1.994560e-28
15   14 -10.890903 7.445468e-32 1.072749e-27 1.072675e-27
16   15 -10.737510 7.743286e-31 5.635287e-27 5.634513e-27
17   16 -10.584117 7.501309e-30 2.891447e-26 2.890697e-26
18   17 -10.430724 6.795303e-29 1.449091e-25 1.448412e-25
19   18 -10.277331 5.776008e-28 7.093452e-25 7.087676e-25
20   19 -10.123938 4.620806e-27 3.391570e-24 3.386949e-24
21   20  -9.970545 3.488709e-26 1.583891e-23 1.580402e-23
22   21  -9.817152 2.491935e-25 7.224885e-23 7.199965e-23
23   22  -9.663759 1.687719e-24 3.218978e-22 3.202101e-22
24   23  -9.510366 1.086011e-23 1.400833e-21 1.389973e-21
25   24  -9.356973 6.651816e-23 5.954372e-21 5.887854e-21
26   25  -9.203580 3.884661e-22 2.472105e-20 2.433259e-20
27   26  -9.050187 2.166445e-21 1.002488e-19 9.808237e-20
28   27  -8.896794 1.155437e-20 3.970752e-19 3.855209e-19
29   28  -8.743401 5.900984e-20 1.536200e-18 1.477190e-18
30   29  -8.590008 2.889448e-19 5.805022e-18 5.516078e-18
31   30  -8.436615 1.358040e-18 2.142601e-17 2.006797e-17
32   31  -8.283222 6.133085e-18 7.724319e-17 7.111010e-17
33   32  -8.129829 2.664059e-17 2.719946e-16 2.453540e-16
34   33  -7.976436 1.114061e-16 9.354956e-16 8.240895e-16
35   34  -7.823043 4.489011e-16 3.142711e-15 2.693810e-15
36   35  -7.669650 1.744301e-15 1.031213e-14 8.567831e-15
37   36  -7.516257 6.541130e-15 3.305018e-14 2.650905e-14
38   37  -7.362864 2.368950e-14 1.034619e-13 7.977241e-14
39   38  -7.209471 8.291324e-14 3.163504e-13 2.334372e-13
40   39  -7.056078 2.806294e-13 9.447952e-13 6.641658e-13
41   40  -6.902685 9.190614e-13 2.756057e-12 1.836996e-12
42   41  -6.749292 2.914097e-12 7.852720e-12 4.938623e-12
43   42  -6.595899 8.950441e-12 2.185412e-11 1.290368e-11
44   43  -6.442506 2.664317e-11 5.940565e-11 3.276248e-11
45   44  -6.289113 7.690188e-11 1.577261e-10 8.082421e-11
46   45  -6.135720 2.153253e-10 4.090352e-10 1.937099e-10
47   46  -5.982327 5.851230e-10 1.036094e-09 4.509707e-10
48   47  -5.828934 1.543729e-09 2.563414e-09 1.019685e-09
49   48  -5.675541 3.955805e-09 6.194693e-09 2.238888e-09
50   49  -5.522148 9.849148e-09 1.462184e-08 4.772694e-09
51   50  -5.368755 2.383494e-08 3.371054e-08 9.875603e-09
52   51  -5.215362 5.608221e-08 7.591204e-08 1.982983e-08
53   52  -5.061969 1.283420e-07 1.669694e-07 3.862743e-08
54   53  -4.908576 2.857425e-07 3.587108e-07 7.296828e-08
55   54  -4.755183 6.191087e-07 7.527197e-07 1.336110e-07
56   55  -4.601790 1.305757e-06 1.542778e-06 2.370214e-07
57   56  -4.448397 2.681465e-06 3.088552e-06 4.070874e-07
58   57  -4.295004 5.362929e-06 6.039315e-06 6.763859e-07
59   58  -4.141611 1.044847e-05 1.153458e-05 1.086114e-06
60   59  -3.988218 1.983437e-05 2.151776e-05 1.683389e-06
61   60  -3.834825 3.669359e-05 3.920792e-05 2.514329e-06
62   61  -3.681432 6.616877e-05 6.978014e-05 3.611363e-06
63   62  -3.528039 1.163290e-04 1.213029e-04 4.973908e-06
64   63  -3.374646 1.994211e-04 2.059642e-04 6.543097e-06
65   64  -3.221253 3.334071e-04 3.415809e-04 8.173802e-06
66   65  -3.067860 5.437101e-04 5.533206e-04 9.610512e-06
67   66  -2.914467 8.649934e-04 8.754702e-04 1.047687e-05
68   67  -2.761074 1.342676e-03 1.352967e-03 1.029078e-05
69   68  -2.607681 2.033760e-03 2.042276e-03 8.516014e-06
70   69  -2.454288 3.006427e-03 3.011084e-03 4.656254e-06
71   70  -2.300895 4.337845e-03 4.336233e-03 1.612615e-06
72   71  -2.147502 6.109641e-03 6.099351e-03 1.029012e-05
73   72  -1.994109 8.400756e-03 8.379845e-03 2.091143e-05
74   73  -1.840716 1.127773e-02 1.124526e-02 3.246377e-05
75   74  -1.687323 1.478297e-02 1.473956e-02 4.340363e-05
76   75  -1.533930 1.892220e-02 1.887039e-02 5.180661e-05
77   76  -1.380537 2.365275e-02 2.359709e-02 5.565483e-05
78   77  -1.227144 2.887478e-02 2.882155e-02 5.322859e-05
79   78  -1.073751 3.442763e-02 3.438410e-02 4.353055e-05
80   79  -0.920358 4.009293e-02 4.006629e-02 2.664315e-05
81   80  -0.766965 4.560571e-02 4.560179e-02 3.915671e-06
82   81  -0.613572 5.067301e-02 5.069511e-02 2.209695e-05
83   82  -0.460179 5.499876e-02 5.504672e-02 4.796844e-05
84   83  -0.306786 5.831193e-02 5.838190e-02 6.996470e-05
85   84  -0.153393 6.039450e-02 6.047923e-02 8.472725e-05
86   85   0.000000 6.110503e-02 6.119495e-02 8.992592e-05
87   86   0.153393 6.039450e-02 6.047923e-02 8.472725e-05
88   87   0.306786 5.831193e-02 5.838190e-02 6.996470e-05
89   88   0.460179 5.499876e-02 5.504672e-02 4.796844e-05
90   89   0.613572 5.067301e-02 5.069511e-02 2.209695e-05
91   90   0.766965 4.560571e-02 4.560179e-02 3.915671e-06
92   91   0.920358 4.009293e-02 4.006629e-02 2.664315e-05
93   92   1.073751 3.442763e-02 3.438410e-02 4.353055e-05
94   93   1.227144 2.887478e-02 2.882155e-02 5.322859e-05
95   94   1.380537 2.365275e-02 2.359709e-02 5.565483e-05
96   95   1.533930 1.892220e-02 1.887039e-02 5.180661e-05
97   96   1.687323 1.478297e-02 1.473956e-02 4.340363e-05
98   97   1.840716 1.127773e-02 1.124526e-02 3.246377e-05
99   98   1.994109 8.400756e-03 8.379845e-03 2.091143e-05
100  99   2.147502 6.109641e-03 6.099351e-03 1.029012e-05
101 100   2.300895 4.337845e-03 4.336233e-03 1.612615e-06
102 101   2.454288 3.006427e-03 3.011084e-03 4.656254e-06
103 102   2.607681 2.033760e-03 2.042276e-03 8.516014e-06
104 103   2.761074 1.342676e-03 1.352967e-03 1.029078e-05
105 104   2.914467 8.649934e-04 8.754702e-04 1.047687e-05
106 105   3.067860 5.437101e-04 5.533206e-04 9.610512e-06
107 106   3.221253 3.334071e-04 3.415809e-04 8.173802e-06
108 107   3.374646 1.994211e-04 2.059642e-04 6.543097e-06
109 108   3.528039 1.163290e-04 1.213029e-04 4.973908e-06
110 109   3.681432 6.616877e-05 6.978014e-05 3.611363e-06
111 110   3.834825 3.669359e-05 3.920792e-05 2.514329e-06
112 111   3.988218 1.983437e-05 2.151776e-05 1.683389e-06
113 112   4.141611 1.044847e-05 1.153458e-05 1.086114e-06
114 113   4.295004 5.362929e-06 6.039315e-06 6.763859e-07
115 114   4.448397 2.681465e-06 3.088552e-06 4.070874e-07
116 115   4.601790 1.305757e-06 1.542778e-06 2.370214e-07
117 116   4.755183 6.191087e-07 7.527197e-07 1.336110e-07
118 117   4.908576 2.857425e-07 3.587108e-07 7.296828e-08
119 118   5.061969 1.283420e-07 1.669694e-07 3.862743e-08
120 119   5.215362 5.608221e-08 7.591204e-08 1.982983e-08
121 120   5.368755 2.383494e-08 3.371054e-08 9.875603e-09
122 121   5.522148 9.849148e-09 1.462184e-08 4.772694e-09
123 122   5.675541 3.955805e-09 6.194693e-09 2.238888e-09
124 123   5.828934 1.543729e-09 2.563414e-09 1.019685e-09
125 124   5.982327 5.851230e-10 1.036094e-09 4.509707e-10
126 125   6.135720 2.153253e-10 4.090352e-10 1.937099e-10
127 126   6.289113 7.690188e-11 1.577261e-10 8.082421e-11
128 127   6.442506 2.664317e-11 5.940565e-11 3.276248e-11
129 128   6.595899 8.950441e-12 2.185412e-11 1.290368e-11
130 129   6.749292 2.914097e-12 7.852720e-12 4.938623e-12
131 130   6.902685 9.190614e-13 2.756057e-12 1.836996e-12
132 131   7.056078 2.806294e-13 9.447952e-13 6.641658e-13
133 132   7.209471 8.291324e-14 3.163504e-13 2.334372e-13
134 133   7.362864 2.368950e-14 1.034619e-13 7.977241e-14
135 134   7.516257 6.541130e-15 3.305018e-14 2.650905e-14
136 135   7.669650 1.744301e-15 1.031213e-14 8.567831e-15
137 136   7.823043 4.489011e-16 3.142711e-15 2.693810e-15
138 137   7.976436 1.114061e-16 9.354956e-16 8.240895e-16
139 138   8.129829 2.664059e-17 2.719946e-16 2.453540e-16
140 139   8.283222 6.133085e-18 7.724319e-17 7.111010e-17
141 140   8.436615 1.358040e-18 2.142601e-17 2.006797e-17
142 141   8.590008 2.889448e-19 5.805022e-18 5.516078e-18
143 142   8.743401 5.900984e-20 1.536200e-18 1.477190e-18
144 143   8.896794 1.155437e-20 3.970752e-19 3.855209e-19
145 144   9.050187 2.166445e-21 1.002488e-19 9.808237e-20
146 145   9.203580 3.884661e-22 2.472105e-20 2.433259e-20
147 146   9.356973 6.651816e-23 5.954372e-21 5.887854e-21
148 147   9.510366 1.086011e-23 1.400833e-21 1.389973e-21
149 148   9.663759 1.687719e-24 3.218978e-22 3.202101e-22
150 149   9.817152 2.491935e-25 7.224885e-23 7.199965e-23
151 150   9.970545 3.488709e-26 1.583891e-23 1.580402e-23
152 151  10.123938 4.620806e-27 3.391570e-24 3.386949e-24
153 152  10.277331 5.776008e-28 7.093452e-25 7.087676e-25
154 153  10.430724 6.795303e-29 1.449091e-25 1.448412e-25
155 154  10.584117 7.501309e-30 2.891447e-26 2.890697e-26
156 155  10.737510 7.743286e-31 5.635287e-27 5.634513e-27
157 156  10.890903 7.445468e-32 1.072749e-27 1.072675e-27
158 157  11.044296 6.639271e-33 1.994626e-28 1.994560e-28
159 158  11.197689 5.462691e-34 3.622482e-29 3.622428e-29
160 159  11.351082 4.122786e-35 6.425875e-30 6.425834e-30
161 160  11.504475 2.834415e-36 1.113370e-30 1.113367e-30
162 161  11.657868 1.760506e-37 1.884205e-31 1.884204e-31
163 162  11.811261 9.780591e-39 3.114570e-32 3.114569e-32
164 163  11.964654 4.800290e-40 5.028627e-33 5.028626e-33
165 164  12.118047 2.048904e-41 7.930160e-34 7.930160e-34
166 165  12.271440 7.450561e-43 1.221507e-34 1.221507e-34
167 166  12.424833 2.244145e-44 1.837769e-35 1.837769e-35
168 167  12.578226 5.375197e-46 2.700644e-36 2.700644e-36
169 168  12.731619 9.598566e-48 3.876370e-37 3.876370e-37
170 169  12.885012 1.135925e-49 5.434558e-38 5.434558e-38
171 170  13.038405 6.681912e-52 7.441914e-39 7.441914e-39
function (n = 170, p = 0.5, Compare = TRUE, Table = TRUE, Graph = TRUE, 
    GraphE = TRUE) 
{
    layout(matrix(1))
    m <- array(0:n)
    x <- numeric()
    PNormal <- numeric()
    a <- n * p
    b <- sqrt(a * (1 - p))
    for (mi in 1:(n + 1)) {
        x[mi] <- (mi - 1 - a)/b
        PNormal[mi] <- dnorm(x[mi], 0, 1)/b
    }
    if (Compare == TRUE) {
        PBin <- numeric()
        for (mi in 1:(n + 1)) PBin[mi] <- dbinom(mi - 1, n, p)
        Dif <- abs(PBin - PNormal)
    }
    if (Graph == TRUE & GraphE == TRUE) {
        layout(matrix(c(1, 1, 2, 2), 2, 2, byrow = TRUE))
    }
    if (Graph == TRUE) {
        mfg <- c(1, 1, 2, 2)
        plot(PNormal, type = "p", main = "The Local Limit Theorem", 
            xlab = "m", ylab = "Probability", col = "red")
        mtext("Local Theorem", line = -1, side = 3, adj = 1, 
            col = "red")
        if (Compare == TRUE) {
            points(PBin, type = "p", col = "blue")
            mtext("Binomial Probability", line = -2, side = 3, 
                adj = 1, col = "blue")
        }
    }
    if (GraphE == TRUE) {
        mfg <- c(2, 1, 2, 2)
        dmini <- min(Dif) - 0.01
        dmaxi <- max(Dif) + 0.01
        plot(Dif, ylim = c(dmini, dmaxi), type = "b", main = "Errors", 
            xlab = "m", ylab = "Errors", col = "green")
        abline(a = 0, b = 0, col = "red")
    }
    if (Table == TRUE) {
        if (Compare == TRUE) 
            TablaR <- data.frame(m = m, x = x, PBinomial = PBin, 
                TLocal = PNormal, Difference = Dif)
        else TablaR <- data.frame(m = m, x = x, TLocal = PNormal)
        TablaR
    }
}

ExpRep documentation built on July 4, 2017, 9:45 a.m.