\( \def\R{\mathbb{R}} \def\N{\mathbb{N}} \def\Z{\mathbb{Z}} \def\Q{\mathbb{Q}} \def\eps{\varepsilon} \def\epsilon{\varepsilon} \renewcommand{\geq}{\geqslant} \renewcommand{\leq}{\leqslant} \)
Main: | Index |
Previous: | 1.2 Discrete Probability Distributions |
Next: | 2.2 Continuous Density Functions |
# 02.01 - Exercise 1 - Spinner
# Simply simulate values between [0, 1].
NSIM = 1000
simVals = runif(NSIM)
# Arc1: 0 to 1/2
A1 = sum(simVals < 1/2)/NSIM
# Arc2: 1/2 to 5/6
A2 = sum(simVals > 1/2 & simVals < 5/6)/NSIM
# Arc3: 5/6 to 1
A3 = sum(simVals > 5/6)/NSIM
A1;A2;A3
# create dummy data
df <- data.frame(
name=c("A1", "A2", "A3"),
value=c(A1, A2, A3),
area = c(2*A1, 3*A2, 6*A3)
)
# Control width:
barplot(height=df$area, names=df$name, col=rgb(0.2,0.4,0.6,0.6), width=c(1/2,1/3,1/6),
main="Adjusted bar plot")
png(filename = "~/GITHUB/CoveredInChocolate.github.io/IntroProb/img/02.01_Ex01.png",
width = 640, height=480)
barplot(height=df$area, names=df$name, col=rgb(0.2,0.4,0.6,0.6), width=c(1/2,1/3,1/6),
main="Adjusted bar plot")
dev.off()
> A1;A2;A3 [1] 0.493 [1] 0.34 [1] 0.167
# 02.01 - Exercise 2 - Spinner II
# Simply simulate values between [0, 1].
# 1/3, 1/4, 1/5, 1/6, and 1/20
NSIM = 10000
simVals = runif(NSIM)
# Arc1:
A1 = sum(simVals < 1/3)/NSIM
# Arc2:
A2 = sum(simVals > 1/3 & simVals < (1/3 + 1/4))/NSIM
# Arc3:
A3 = sum(simVals > (1/3 + 1/4) & simVals < (1/3 + 1/4 + 1/5))/NSIM
# Arc4:
A4 = sum(simVals > (1/3 + 1/4 + 1/5) & simVals < (1/3 + 1/4 + 1/5 + 1/6))/NSIM
# Arc5:
A5 = sum(simVals > (1/3 + 1/4 + 1/5 + 1/6))/NSIM
A1;A2;A3;A4;A5
# create dummy data
df <- data.frame(
name=c("A1", "A2", "A3", "A4", "A5"),
value=c(A1, A2, A3, A4, A5),
area = c(3*A1, 4*A2, 5*A3, 6*A4, 20*A5)
)
# Control width:
barplot(height=df$area, names=df$name, col=rgb(0.2,0.4,0.6,0.6), width=c(1/3,1/4,1/5,1/6,1/20),
main="Adjusted bar plot")
png(filename = "~/GITHUB/CoveredInChocolate.github.io/IntroProb/img/02.01_Ex02.png",
width = 640, height=480)
barplot(height=df$area, names=df$name, col=rgb(0.2,0.4,0.6,0.6), width=c(1/3,1/4,1/5,1/6,1/20),
main="Adjusted bar plot")
dev.off()
> A1;A2;A3;A4;A5 [1] 0.3325 [1] 0.2501 [1] 0.1964 [1] 0.1727 [1] 0.0483
# 02.01 - Exercise 3 - Estimating pi
NVALS = 1000
vlsX = runif(NVALS)
vlsY = runif(NVALS)
# Determine values within circle at (1/2, 1/2) with radius 1/2
hit = 0
hitCoords = c()
for (k in 1:NVALS) {
if ((vlsX[k]-1/2)^2 + (vlsY[k]-1/2)^2 <= 1/4) {
hit = hit + 1
hitCoords = c(hitCoords, k)
}
}
hit
length(hitCoords)
xCirc = vlsX[hitCoords]
yCirc = vlsY[hitCoords]
xOut = vlsX[-hitCoords]
yOut = vlsY[-hitCoords]
png(filename = "~/GITHUB/CoveredInChocolate.github.io/IntroProb/img/02.01_Ex03.png",
width=400, height=400)
plot(xCirc, yCirc, col="red", pch=16, cex=0.8)
points(xOut, yOut, pch=16, cex=0.8)
dev.off()
#pi/4
hit/1000
#pi
4*hit/1000
> hit/1000 [1] 0.789 > #pi > 4*hit/1000 [1] 3.156
# 02.01 - Exercise 4 - Estimating sin(pi*x)
NVALS = 10000
vlsX = runif(NVALS)
vlsY = runif(NVALS)
# Determine values within circle at (1/2, 1/2) with radius 1/2
hit = 0
hitCoords = c()
for (k in 1:NVALS) {
if (vlsY[k] <= sin(pi*vlsX[k])) {
hit = hit + 1
hitCoords = c(hitCoords, k)
}
}
hit
length(hitCoords)
xSin = vlsX[hitCoords]
ySin = vlsY[hitCoords]
xOut = vlsX[-hitCoords]
yOut = vlsY[-hitCoords]
plot(xSin, ySin, col="red", pch=16, cex=0.8)
points(xOut, yOut, pch=16, cex=0.8)
png(filename = "~/GITHUB/CoveredInChocolate.github.io/IntroProb/img/02.01_Ex04.png",
width=400, height=400)
plot(xSin, ySin, col="red", pch=16, cex=0.8)
points(xOut, yOut, pch=16, cex=0.8)
dev.off()
# 2/pi
hit/NVALS
2/pi
(hit/NVALS)/(2/pi)
> # 2/pi > hit/NVALS [1] 0.6374 > 2/pi [1] 0.6366198 > (hit/NVALS)/(2/pi) [1] 1.001226 > > # pi > 2/(hit/NVALS) [1] 3.137747
# 02.01 - Exercise 5 - Estimating ln(2)
NVALS = 10000
vlsX = runif(NVALS)
vlsY = runif(NVALS)
hit = 0
hitCoords = c()
for (k in 1:NVALS) {
if (vlsY[k] <= 1/(vlsX[k] + 1)) {
hit = hit + 1
hitCoords = c(hitCoords, k)
}
}
hit
length(hitCoords)
xLog = vlsX[hitCoords]
yLog = vlsY[hitCoords]
xOut = vlsX[-hitCoords]
yOut = vlsY[-hitCoords]
plot(xLog, yLog, col="red", pch=16, cex=0.8)
points(xOut, yOut, pch=16, cex=0.8)
png(filename = "~/GITHUB/CoveredInChocolate.github.io/IntroProb/img/02.01_Ex05.png",
width=400, height=400)
plot(xLog, yLog, col="red", pch=16, cex=0.8)
points(xOut, yOut, pch=16, cex=0.8)
dev.off()
# log(2)
hit/NVALS
log(2)
(hit/NVALS)/log(2)
> # log(2) > hit/NVALS [1] 0.6939 > log(2) [1] 0.6931472 > (hit/NVALS)/log(2) [1] 1.001086
# 02.01 - Exercise 6 - Buffon's Needle Simulation
simBuffon <- function(NSIMS) {
d = runif(NSIMS, min = 0, max = 0.5)
theta = runif(NSIMS, min = 0, max = pi/2)
a = sum(d < 0.5*sin(theta))/NSIMS
piapprox = 2/a
return(piapprox)
}
simBuffon(100)
simBuffon(1000)
simBuffon(10000)
simBuffon(100000)
simBuffon(1000000)
> simBuffon(100) [1] 3.389831 > simBuffon(1000) [1] 3.12989 > simBuffon(10000) [1] 3.137255 > simBuffon(100000) [1] 3.13952 > simBuffon(1000000) [1] 3.143656
# 02.01 - Exercise 7 - Buffon's Needle Simulation - Laplace version
simLaplace <- function(NSIMS) {
L = 1
d1 = runif(NSIMS, min = 0, max = L/2)
d2 = runif(NSIMS, min = 0, max = L/2)
theta = runif(NSIMS, min = 0, max = pi/2)
a = sum(d1 < 0.5*sin(theta) | d2 < 0.5*cos(theta))/NSIMS
piapprox = (4*L - L^2)/a
return(piapprox)
}
simLaplace(100)
simLaplace(1000)
simLaplace(10000)
simLaplace(100000)
simLaplace(1000000)
> simLaplace(100) [1] 3.061224 > simLaplace(1000) [1] 3.10559 > simLaplace(10000) [1] 3.143666 > simLaplace(100000) [1] 3.141789 > simLaplace(1000000) [1] 3.142694
# 02.01 - Exercise 8 - Buffon's Needle Simulation - Long needle version
simLongNeedle <- function(NSIMS) {
L = 100
theta = runif(NSIMS, min = 0, max = pi/2)
a = mean(L*sin(theta) + L*cos(theta))
piapprox = (4*L)/a
return(piapprox)
}
simLongNeedle(100)
simLongNeedle(1000)
simLongNeedle(10000)
simLongNeedle(100000)
simLongNeedle(1000000)
> simLongNeedle(100) [1] 3.13445 > simLongNeedle(1000) [1] 3.14688 > simLongNeedle(10000) [1] 3.143391 > simLongNeedle(100000) [1] 3.140012 > simLongNeedle(1000000) [1] 3.141351 > pi [1] 3.141593 > pi/3.141351 [1] 1.000077
# 02.01 - Exercise 10 - Triangle
simTriangle <- function(NSIMS) {
x = runif(NSIMS)
y = runif(NSIMS)
ind = x + y < 1
return(x[ind])
}
xval = simTriangle(10000)
xcut = cut(xval, c(0,1:10/10))
df = data.frame(
xcat = xcut,
cnt = 1
)
df2 = aggregate(df$cnt, by=list(df$xcat), FUN = sum)
names(df2) = c("Interval", "Freq")
barplot(height=df2$Freq/500, names=df2$Interval , col=rgb(0.2,0.4,0.6,0.6), width=1,
main="Frequency per Subinterval")
plotx = 1:120/10
lines(plotx, 2 - (1/6)*plotx, col="red", lwd=2)
png(filename = "~/GITHUB/CoveredInChocolate.github.io/IntroProb/img/02.01_Ex10.png",
width = 640, height=480)
barplot(height=df2$Freq/500, names=df2$Interval , col=rgb(0.2,0.4,0.6,0.6), width=1,
main="Frequency per Subinterval")
plotx = 1:120/10
lines(plotx, 2 - (1/6)*plotx, col="red", lwd=2)
dev.off()
# 02.01 - Exercise 11 - Chord of a circle
simChord <- function(NSIMS) {
x0 = runif(NSIMS)
y0 = runif(NSIMS)
theta = runif(NSIMS, min=-pi/2, max=pi/2)
m = tan(theta)
# Distance
d = abs((y0 - m*x0)/(sqrt(m**2 + 1)))
# Random Chords
chordInd = d < 1
chords = d[chordInd]
# As in Example 2.6, the length is larger than sqrt(3)
# if the chord intersects a circle with radius 0.5
prop = sum(chords < 0.5)/length(chords)
return(prop)
}
res = simChord(10000)
res
> res = simChord(10000) > res [1] 0.5779924