【发布时间】:2013-12-06 00:55:38
【问题描述】:
我在第 32 页遇到了 interesting presentation,我开始复制并理解所提供的代码
演示文稿中的代码如下:
#Unicredit banks code
library(evir)
library(fExtremes)
# Quantile function of lognormal-GPD severity distribution
qlnorm.gpd = function(p, theta, theta.gpd, u)
{
Fu = plnorm(u, meanlog=theta[1], sdlog=theta[2])
x = ifelse(p<Fu,
qlnorm( p=p, meanlog=theta[1], sdlog=theta[2] ),
qgpd( p=(p - Fu) / (1 - Fu) , xi=theta.gpd[1], mu=theta.gpd[2], beta=theta.gpd[3]) )
return(x)
}
# Random sampling function of lognormal-GPD severity distribution
rlnorm.gpd = function(n, theta, theta.gpd, u)
{
r = qlnorm.gpd(runif(n), theta, theta.gpd, u)
}
set.seed(1000)
nSim = 1000000 # Number of simulated annual losses
H = 1500 # Threshold body-tail
lambda = 791.7354 # Parameter of Poisson body
theta1 = 2.5 # Parameter mu of lognormal (body)
theta2 = 2 # Parameter sigma of lognormal (body)
theta1.tail = 0.5 # Shape parameter of GPD (tail)
theta2.tail = H # Location parameter of GPD (tail)
theta3.tail = 1000 # Scale parameter of GPD (tail)
sj = rep(0,nSim) # Annual loss distribution inizialization
freq = rpois(nSim, lambda) # Random sampling from Poisson
for(i in 1:nSim) # Convolution with Monte Carlo method
sj[i] = sum(rlnorm.gpd(n=freq[i], theta=c(theta1,theta2), theta.gpd=c(theta1.tail, theta2.tail, theta3.tail), u=H))
但是我得到了这个我无法解决的错误:
Error: min(p, na.rm = TRUE) >= 0 is not TRUE
附加问题
非常感谢影子。
我不知道如何更改函数引用。 qgpd.fExtremes 到 qgpd.evir 就这么简单吗?
再次感谢Shadow 指出这一点。 对于希望从不同包更改对函数的引用的任何人(在上面的示例中,从 fExtremes 到 evir 就像添加 evir:::(function) 一样简单。
例子:
evir:::qgpd( p=(p - Fu) / (1 - Fu) , xi=theta.gpd[1], mu=theta.gpd[2], beta=theta.gpd[3]) )
【问题讨论】:
-
theta[1] 或 theta[2] 是否永远小于零?您将两者都作为需要为正的参数发送。
标签: r