Source

stats-homework / homework 4 / q2.r

Full commit
sp <- read.csv("SPhistory.short.csv")
sp = sp[,7]
sp = rev(sp)
sp = diff(log(sp))
gaussian.q0.01 = function (moments) {
  with(moments, {
    return(qnorm(0.01, mean = mean, sd = sd))
  })
}
gaussian.fit.moments = function(data) {
  return(list(mean=mean(data), sd=sd(data)))
}
gaussian.sim.moments = function(moments, n) {
  with(moments, {
    my.sim = rnorm(n, mean=mean, sd=sd)
    return(
      gaussian.fit.moments(my.sim)
    )
  })
}
estimates.q0.01 = replicate(10000,
  gaussian.q0.01(
    gaussian.sim.moments(
      list(mean=mean(sp), sd=sd(sp)),
      n=length(sp)
    )
  )
)
confidence.gaussian.q0.01 = quantile(estimates.q0.01,c(.025, 0.975))
empirical.q0.01=quantile(sp, 0.01)