我正在尝试使用 XGBoost 对不等长曝光期生成的数据的索赔频率进行建模,但无法让模型正确处理曝光。我通常会通过将 log(exposure) 设置为偏移量来做到这一点——你能在 XGBoost 中做到这一点吗?
(这里发布了一个类似的问题:xgboost,偏移曝光?)
为了说明这个问题,下面的 R 代码生成了一些带有字段的数据:
- x1, x2 - 因子(0 或 1)
- 暴露 - 观察数据的政策期限长度
- 频率 - 每单位风险的平均索赔数量
- 索赔 - 观察到的索赔数量〜泊松(频率*曝光)
目标是使用 x1 和 x2 预测频率 - 真实模型是:如果 x1 = x2 = 1,频率 = 2,否则频率 = 1。
曝光不能用于预测频率,因为它在政策开始时是未知的。我们可以使用它的唯一方法是:预期索赔数量 = 频率 * 曝光。
该代码尝试通过以下方式使用 XGBoost 进行预测:
- 将曝光设置为模型矩阵中的权重
- 将 log(exposure) 设置为偏移量
在这些下面,我展示了如何处理树 (rpart) 或 gbm 的情况。
set.seed(1)
size<-10000
d <- data.frame(
x1 = sample(c(0,1),size,replace=T,prob=c(0.5,0.5)),
x2 = sample(c(0,1),size,replace=T,prob=c(0.5,0.5)),
exposure = runif(size, 1, 10)*0.3
)
d$frequency <- 2^(d$x1==1 & d$x2==1)
d$claims <- rpois(size, lambda = d$frequency * d$exposure)
#### Try to fit using XGBoost
require(xgboost)
param0 <- list(
"objective" = "count:poisson"
, "eval_metric" = "logloss"
, "eta" = 1
, "subsample" = 1
, "colsample_bytree" = 1
, "min_child_weight" = 1
, "max_depth" = 2
)
## 1 - set weight in xgb.Matrix
xgtrain = xgb.DMatrix(as.matrix(d[,c("x1","x2")]), label = d$claims, weight = d$exposure)
xgb = xgb.train(
nrounds = 1
, params = param0
, data = xgtrain
)
d$XGB_P_1 <- predict(xgb, xgtrain)
## 2 - set as offset in xgb.Matrix
xgtrain.mf <- model.frame(as.formula("claims~x1+x2+offset(log(exposure))"),d)
xgtrain.m <- model.matrix(attr(xgtrain.mf,"terms"),data = d)
xgtrain <- xgb.DMatrix(xgtrain.m,label = d$claims)
xgb = xgb.train(
nrounds = 1
, params = param0
, data = xgtrain
)
d$XGB_P_2 <- predict(model, xgtrain)
#### Fit a tree
require(rpart)
d[,"tree_response"] <- cbind(d$exposure,d$claims)
tree <- rpart(tree_response ~ x1 + x2,
data = d,
method = "poisson")
d$Tree_F <- predict(tree, newdata = d)
#### Fit a GBM
gbm <- gbm(claims~x1+x2+offset(log(exposure)),
data = d,
distribution = "poisson",
n.trees = 1,
shrinkage=1,
interaction.depth=2,
bag.fraction = 0.5)
d$GBM_F <- predict(gbm, newdata = d, n.trees = 1, type="response")