GGGGXY / probability-and-inference-portfolio-xinyu-gao

0 stars 0 forks source link

08-coverage-probability #11

Closed murraymegan closed 2 years ago

murraymegan commented 2 years ago

Important Info: I will grade this before 12-6. Final Due Date will be 12-16 at 9am because of final grade deadlines.

murraymegan commented 2 years ago

FAIL

GGGGXY commented 2 years ago

Hi Megan, i am so confused about this step "In "sample_95_confidence" function you must first do: samp= rnorm(201,0,1) #get mle.mean and mle.sd" I think i have already calculated the mean and standard deviation. Do you mean the step is as following: use new sample -> calculate mean and sd using MLE -> generate samples based on the information from previous step-> find median -> repeat previous two steps to get median distribution-> get the 95% CI of the distribution -> compare the 95% CI with the median of the new sample from step 1? Thanks!

murraymegan commented 2 years ago
sample_95_confidence <- function(){
  # 10000 samples
  sample <- rnorm(201,mean=0,sd=1)
# use mle to calculate the parameters based on the sample
ll_norm <- function(mean,sd){
  -sum(dnorm(sample,mean = mean,sd = sd,log=T))
}
z_norm <- mle(minuslogl=ll_norm, start=list(mean=1,sd=1),
                 method = "L-BFGS-B",
                 lower = c(0,0.01))
mean <- coef(z_norm)[1]
sd <- coef(z_norm)[2]
  result <- rep(NA, 10000)
  for (i in seq_along(result)){
    # each sample has 1000 values and find the median of each sample using quantile
    result[i] = quantile(rnorm(1000,mean = mean, sd = sd),0.5)
  }
  # find the minimum value among the 95% confidence interval
  x_0.025 = quantile(result,0.025)
  # fine the maximum value among the 95% confidence interval
  x_0.975 = quantile(result,0.975)
  return(c(x_0.025, x_0.975))
}
murraymegan commented 2 years ago
soyeon-park1121 commented 2 years ago

FAIL

soyeon-park1121 commented 2 years ago

PASS

Great job!