To infer is to obtain approximate information about a population from a sample.
Simply put, by choosing a random sample from a population and calculating its mean, we can infer the mean of the population. The larger the sample size, the closer these values tend to be. This fact is known as the Central Limit Theorem.
Estimating the value of π
Consider a circle of radius 1 inscribed in a square of side 2.
Suppose we randomly select a point from inside the square. If all points have the same probability of being selected, the probability of choosing a point inside the circle is:
(area of circle / area of square) = π/4
Now, imagine that we randomly select n points inside the square. If we count how many points fall inside the circle, we can expect that:
(number of points inside the circle / total number of points chosen) ~ π/4
Furthermore, as the total number of points chosen increases, the computed value of the ratio:
(number of points inside the circle / total number of points chosen)
will get closer to the true value of π/4.
This experiment can be implemented in R using a Monte Carlo method:
n <- 1000
x.pos <- runif(n, min = -1, max = 1)
y.pos <- runif(n, min = -1, max = 1)
local.pos <- ifelse(x.pos^2 + y.pos^2 <= 1, TRUE, FALSE)
inside <- length (which(local.pos == TRUE))
4*(inside/n)
The larger the value assigned to n in the first line, the closer the output will be to π.
4.29 kB