# 加载中...

• 博客等级：
• 博客积分：0
• 博客访问：568,107
• 关注人气：32
• 获赠金笔：0支
• 赠出金笔：0支
• 荣誉徽章：

## R语言使用随机技术差分进化算法优化的Nelson-Siegel-Svensson模型

(2020-04-12 18:50:11)

# 1引言

``````

> set.seed(112233)```
```

# 2将NS模型拟合到给定的零利率

### NS模型

``````> tm <- c(c(1, 3, 6, 9)/12, 1:10)

> betaTRUE <- c(6, 3, 8, 1)

> yM <- NS(betaTRUE, tm)

> par(ps = 11, bty = "n", las = 1, tck = 0.01, mgp = c(3, 0.2, 0), mar = c(4, 4, 1, 1))

> plot(tm, yM, xlab = "maturities in years", ylab = "yields in %")```
```

``````> data <- list(yM = yM, tm = tm, model = NS, ww = 0.1, min = c( 0,-15,-30, 0), max = c(15, 30, 30,10))

```
```

OF将采用候选解决方案参数，通过data \$ model将此解决方案转换为收益，并将这些收益与yM进行比较，这意味着要计算最大绝对差。

``````

> OF(param2, data) ## ... gives a postive number

[1] 0.97686```
```

``````> par(ps = 11, bty = "n", las = 1, tck = 0.01, mgp = c(3, 0.2, 0), mar = c(4, 4, 1, 1))

> plot(tm, yM, xlab = "maturities in years", ylab = "yields in %")

> lines(tm, NS(param1, tm), col = "blue")

> lines(tm, NS(param2, tm), col = "red")

> legend(x = "topright", legend = c("true yields", "param1", "param2"), col = c("black", "blue", "red"), pch = c(1, NA, NA), lty = c(0, 1, 1))```
```

``````> param1 <- c( 6, 3, 8, -1)

> param2 <- c( 6, 3, 8, 1)

> param3 <- c(-1, 3, 8, 1)

> mP <- cbind(param1,param2,param3)

> rownames(mP) <- c("b1","b2","b3","lambda")

> mP

param1 param2 param3

b1 6 6 -1

b2 3 3 3

b3 8 8 8

lambda -1 1 1```
```

``````> penalty(mP,data)

param1 param2 param3

0.2 0.0 0.2```
```

``````> data\$ww <- 0.5
> penalty(mP,data)
param1 param2 param3
1 0 1```
```

``````
> penalty(mP, data)
param1 param2 param3
0 0 0```
```

``````> sol <- DEopt(OF = OF, algo = algo, data = data)
```
```

``````> max( abs(data\$model(sol\$xbest, tm) - data\$model(betaTRUE, tm)) )
[1] 0
> sol\$OFvalue
[1] 0 ```
```

``````> s0 <- algo\$min + (algo\$max - algo\$min) * runif(length(algo\$min))
> sol2 <- nlminb(s0, OF, data = data,
lower = data\$min,
upper = data\$max,
control = list(eval.max = 50000L,
iter.max = 50000L))```
```

``````> max( abs(data\$model(sol2\$par, tm) - data\$model(betaTRUE,tm)) )
[1] 1.5787e-07
> sol2\$objective
[1] 1.5787e-07```
```

``````
> plot(tm, yM, xlab = "maturities in years",
ylab = "yields in %")
> algo\$printDetail <- FALSE
> for (i in seq_len(nRuns)) {
sol <- DEopt(OF = OF, algo = algo, data = data)
lines(tm, data\$model(sol\$xbest,tm), col = "blue")
s0 <- algo\$min + (algo\$max-algo\$min) * runif(length(algo\$min))
sol2 <- nlminb(s0, OF, data = data,
lower = data\$min,
upper = data\$max,
control = list(eval.max = 50000L,
iter.max = 50000L))
lines(tm,data\$model(sol2\$par,tm), col = "darkgreen", lty = 2)
}
> legend(x = "topright", legend = c("true yields", "DE", "nlminb"),
col = c("black","blue","darkgreen"),
pch = c(1, NA, NA), lty = c(0, 1, 2))```
```

### 其他约束

NS（和NSS）模型的参数约束是要确保所得的零利率为非负数。但实际上，它们不能保证正利率。

``````
> plot(tm, yM, xlab = "maturities in years", ylab = "yields in %")
> abline(h = 0)```
```

``````> penalty2(c(3, -2, -8, 1.5),data)
[1] 0.86343```
```

``````> algo\$pen <- NULL; data\$yM <- yM; data\$tm <- tm
> par(ps = 11, bty = "n", las = 1, tck = 0.01,
mgp = c(3, 0.2, 0), mar = c(4, 4, 1, 1))
> plot(tm, yM, xlab = "maturities in years", ylab = "yields in %")
> abline(h = 0)
> sol <- DEopt(OF = OFa, algo = algo, data = data)
> lines(tm,data\$model(sol\$xbest,tm), col = "blue")
> legend(x = "topleft", legend = c("true yields", "DE (constrained)"),
col = c("black", "blue"),
pch = c(1, NA, NA), lty = c(0, 1, 2))```
```

### 3将NSS模型拟合到给定的零利率

``````> sol <- DEopt(OF = OF, algo = algo, data = data)
> max( abs(data\$model(sol\$xbest, tm) - data\$model(betaTRUE, tm)) )
[1] 7.9936e-15
> sol\$OFvalue
[1] 7.9936e-15```
```

``````> par(ps = 11, bty = "n", las = 1, tck = 0.01,
mgp = c(3, 0.2, 0), mar = c(4, 4, 1, 1))
> plot(tm, yM, xlab = "maturities in years", ylab = "yields in %")
> for (i in seq_len(nRuns)) {
sol <- DEopt(OF = OF, algo = algo, data = data)
lines(tm, data\$model(sol\$xbest,tm), col = "blue")
s0 <- algo\$min + (algo\$max - algo\$min) * runif(length(algo\$min))
sol2 <- nlminb(s0, OF, data = data,
lower = data\$min,
upper = data\$max,
control = list(eval.max = 50000L,
iter.max = 50000L))
lines(tm, data\$model(sol2\$par,tm), col = "darkgreen", lty = 2)
}
> legend(x = "topright", legend = c("true yields", "DE", "nlminb"),
col = c("black","blue","darkgreen"),
pch = c(1,NA,NA), lty = c(0,1,2), bg = "white")```
```

### 参考文献

QQ：3025393450

【服务场景】

【大数据部落】提供定制化的一站式数据挖掘和统计分析咨询服务

QQ交流群：186388004

0

• 评论加载中，请稍候...

发评论

以上网友发言只代表其个人观点，不代表新浪网的观点或立场。

新浪BLOG意见反馈留言板　电话：4000520066 提示音后按1键（按当地市话标准计费）　欢迎批评指正

新浪公司 版权所有