HW1 coding

Homework 1

Question 6

a)

every week for 20 years, assuming 52 weeks per year, we have: 52 * 20 = 1040 times of buying the lottery.

we want at least one success in terms of winning,

Let X be the first win out of n trials: X ~ Geometric (p = 1/292,000,000)

basically we want: P(X <= 1040):

pgeom(1040, prob = 1/292000000)
[1] 3.565062e-06

b)

so we want the smallest number of x such that P(X<=x) >= 0.5

so we know we could look into quantile function, which is classified as q_n(k) is the k-th percentile out of a n sampled dataset

qgeom(0.5, prob = 1/292000000)
[1] 202398976

this means that you would need to by these many times , and to calculate the cost, each with 2$

this is going to be:

qgeom(0.5, prob = 1/292000000) * 2 
[1] 404797952

c)

suppose we are buying this every year, what would it be like?

202398976/365
[1] 554517.7

Question 7:

a)

a quick helper to give out the density/edf of a sampled data:

dhist <- function(x) {
  n <- length(x)
  t <- table(x)
  values <- as.numeric(names(table(x)))
  counts <- as.numeric(table(x))
  masses <- counts / n
  return(list(
    "values" = values,
    "counts" = counts,
    "masses" = masses
  ))
}
x <- rbinom(1000,size=100,prob=0.65)
hist(x)

plot(
  45:85,
  dbinom(45:85, size = 100, prob = 0.65),
  col = 2,
  pch = 4,
  ylim = c(0, 0.10),
  xlab = "x",
  ylab = "probability mass",
  main = "binomial pmf and random sample"
)

points(dhist(x)$values, dhist(x)$masses, pch=16)

b)

plot(
  45:85,
  pbinom(45:85, size = 100, prob = 0.65),
  col = 2,
  pch = 4,
  ylim = c(0, 1),
  xlab = "x",
  ylab = "probability/quantile",
  main = "binomial cdf and random sample edf"
)
points(dhist(x)$values, cumsum(dhist(x)$masses), pch=16)