【问题标题】:How to forecast the parameters of Nelson Siegel with a constant lambda in R?如何用 R 中的常数 lambda 预测 Nelson Siegel 的参数?
【发布时间】:2019-09-30 12:20:59
【问题描述】:

我想用固定的 lambda 计算 Nelson Siegel 模型的 beta 参数。 lambda 应设置为 0.609。我有一个包含 54 条收益率曲线的 excel 文件。对于每条收益率曲线,我想根据 Nelson Siegel 模型估计 beta 参数。

起初,我尝试使用“YieldCurve”包中的 Nelson.Siegel 函数。但是,该函数只有两个参数,因此我无法获取 lambda 常量。

其次,我自己创建了一个函数来计算 Nelson Siegel 模型。我想通过更改函数的 beta 参数来最小化 Nelson Siegel 收益率与实际收益率之间的平方差。我通过使用求解器在 excel 中做到了这一点。

我目前的代码:

RTS54_list <- read_xlsx("YieldCurves.xlsx")

nelson_siegel_calculate<-function(theta,lambda,beta0,beta1,beta2){
  beta0 + beta1*(1-exp(-lambda * theta))/(lambda * theta) + beta2*((1-exp(-lambda * theta))/(lambda * theta) - exp(-lambda * theta))
}

ns_data <-
  data.frame(maturity=1:100) %>%
  mutate(NSS=nelson_siegel_calculate(theta=maturity,lambda=0.0609,beta0=0.02,beta1=-0.02,beta2=0.01))

如何在 R 中做同样的事情?

或者还有其他方法可以得到我的结果吗?

【问题讨论】:

    标签: r lambda


    【解决方案1】:

    您可以使用 R 中的 optim() 函数来最小化实际收益率和 nelson_siegel 预测之间的平方差之和。

    我抓住了US Treasury yield curve for 2 Jan 2019;将月份成熟度转换为年份分数,我最终得到了以下数据框:

        yldmat <- structure(list(maturity = c(0.0833333333333333, 0.166666666666667, 
                  0.25, 0.5, 1, 2, 3, 5, 7, 10, 20, 30), yield = c(0.024, 0.024, 
                  0.0242, 0.0251, 0.026, 0.025, 0.0247, 0.0249, 0.0256, 0.0266, 
                  0.0283, 0.0297)), class = "data.frame", row.names = c(NA, -12L))
    

    然后我创建了以下最小化函数,lambda 默认为 0.609,theta 与每个曲线点的“成熟度”值相同:

    min.ns <- function(data, param) {
        with(data, sum((yield - nelson_siegel_calculate(maturity, 0.609, param[1], param[2], param[3]))^2))
    }
    

    现在我可以使用初始参数集 0.02、-0.02、0.01 为 beta 调用 optim() 例程,根据您的示例:

    optim(par = c(0.02,-0.02,0.01), fn = min.ns, data = yldmat)
    $par
    [1]  0.029152661 -0.004403284 -0.007526309
    
    $value
    [1] 7.983234e-06
    
    $counts
    function gradient 
         132       NA 
    
    $convergence
    [1] 0
    
    $message
    NULL
    

    $par 值是您想要的 beta0、beta1、beta2 的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2020-02-04
      • 1970-01-01
      • 2014-02-12
      • 2019-02-15
      相关资源
      最近更新 更多