I am currently reading a statistical procedure that tests a null hypothesis. The site that I am reading is this one .
I have constructed a function in R that calculates the likelihood accordingly:
f = function(x,T=obs) {
X_a = qchisq(0.05, df=1, ncp = 0, lower.tail = FALSE, log.p = FALSE)
p = 0.008
2*log( (((T-x)/((1-p)*T))^(T-x))*( (x/(p*T))^x ) ) - X_a
}
Now the author on the site states that " Consider the example we looked at with the recommended standard coverage test. We implement a one-day 95% value-at-risk measure and plan to backtest it at the .05 significance level after 500 trading days, so q = 0.95 and α + 1 = 500. We calculate the ε = .05 quantile of the χ^2(1,0) distribution as 3.841. Setting this equal to [14.7](i.e the likelihood), we solve for X. There are two solutions: 16.05 and 35.11. Rounding down and up, respectively, we set x1 = 16 and x2 = 36. We will reject the value-at-risk measure if X ∉ [16, 36]."
one approach is that I can take :
K = function(obs){
f = function(x,T=obs) {
X_a = qchisq(0.05, df=1, ncp = 0, lower.tail = FALSE, log.p = FALSE)
p = 0.008
2*log( (((T-x)/((1-p)*T))^(T-x))*( (x/(p*T))^x ) ) - X_a
}
if (obs>=1000) {
xlim.up = as.numeric(substr(obs,1,2))
xlim.down = as.numeric(substr(obs,1,2))
} else {
xlim.up = as.numeric(substr(obs,1,1))
xlim.down = as.numeric(substr(obs,1,1))
}
x1 = uniroot(f, lower = 0,upper = xlim.up) ### our 1st root
x2 = uniroot(f, lower = xlim.down,upper = 80) ### our 2sd root
a =ceiling(x1$root)
b = floor(x2$root)
return(c(a,b))
}
K(2000)
with result:
K(2000)
>[1] 9 24
but when I lower the observation value to say 50 R reports me :
K(50)
Error in uniroot(f, lower = xlim.down, upper = 80) :
f() values at end points not of opposite sign
and it doesn't work.
My questions are:
- how I can solve this equation in R and do the rounding?
- For different values of observations starting from 50 up to 5000 how the function can be modified appropriately in order to report me the lower and upper bound as is being show in the table 14.4 in the site ?
Any help ?