StatsProb - HW2

Question 2:

x <- rnorm(500)
hist(x, probability = T)
curve(dnorm,-4,4,add=T)

mean(x)
[1] 0.004821572
sd(x)
[1] 0.9478536
quantile(x)
        0%        25%        50%        75%       100% 
-2.4343688 -0.6450704  0.0678122  0.6307740  2.5803241 
qnorm(c(0.25, 0.5, 0.75))
[1] -0.6744898  0.0000000  0.6744898

Question 3

Finv <- function(u) { return((1-u)^(-1/3)) }
f <- function(x) { return( 3*x^(-4)) }
u <- runif(1000) + 0.0000001 # q
x <- Finv(u)
hist(sort(x)[1:990], breaks=seq(1,5,1/3), probability=T)
curve( f, from=1, to=3, add=T )

Question 5:

plot(
  NULL,
  NULL,
  xlim = c(0, 7),
  ylim = c(0, 0.4),
  xlab = "x",
  ylab = "probability mass",
  main = "binomial vs Poisson for n=100, p=1/100"
)
points(0:7, dbinom(0:7, size = 100, prob = 1 / 100), pch = 1)
points(0:7, dpois(0:7, lambda = 1), pch = 4)
legend(
  "topright",
  legend = c("dbinom(x, size=100,prob=1/100)",
             "dpois(x, lambda=1)"),
  pch = c(1, 4)
)

Question 7:

for n = 10

plot(
  NULL,
  NULL,
  xlim = c(0, 1),
  ylim = c(0, 1),
  xlab = "x",
  ylab = "y",
  main = "2D Poisson process"
)
n <- 10
lambda <- 10
for (j in 0:n) {
  lines(c(0, 1), c(j / n, j / n), col = "grey")
  lines(c(j / n, j / n), c(0, 1), col = "grey")
}
for (j in 1:n) {
  x <- rbinom(n, size = 1, prob = lambda / n ^ 2)
  for (i in 1:n) {
    if (x[i] == 1) {
      points((i - 0.5) / n, (j - 0.5) / n, pch = 16)
    }
  }
}

for n = 20

plot(
  NULL,
  NULL,
  xlim = c(0, 1),
  ylim = c(0, 1),
  xlab = "x",
  ylab = "y",
  main = "2D Poisson process"
)
n <- 20
lambda <- 10
for (j in 0:n) {
  lines(c(0, 1), c(j / n, j / n), col = "grey")
  lines(c(j / n, j / n), c(0, 1), col = "grey")
}
for (j in 1:n) {
  x <- rbinom(n, size = 1, prob = lambda / n ^ 2)
  for (i in 1:n) {
    if (x[i] == 1) {
      points((i - 0.5) / n, (j - 0.5) / n, pch = 16)
    }
  }
}