今週も特にありません

進捗どうですか?

Rの金利期間構造パッケージ termstrc

ファイナンス系パッケージ探訪。fBonds、YieldCurveパッケージに続きtermstrcパッケージを使ってみたメモ

これを活用して、何かしているのかがあまり見つけられないパッケージ。こちらGitHub - datarob/termstrc: The R package offers a wide range of functions for term structure estimation based on static and dynamic coupon bond and yield data sets. The implementation focuses on the cubic splines approach of McCulloch (1971, 1975) and the Nelson and Siegel (1987) method with extensions by Svensson (1994), Diebold and Li (2006) and De Pooter (2007). We propose a weighted constrained optimization procedure with analytical gradients and a globally optimal start parameter search algorithm. Extensive summary statistics and plots are provided to compare the results of the different estimation methods. Several demos are available using data from European government bonds and yields.も非常に寂しい限り

Journal of Statistical SoftwareからZero-Coupon Yield Curve Estimation with the Package termstrcが出ており、パッケージの詳しい説明が書かれているので、本気で使いたい場合は一読が必要

fBonds、YieldCurveと比較して、クーポン債にも適用できる、Cubicスプラインによる推定ができる、adjusted Svenssonモデルも適用できる、Rcpp使っている、その他、有用な関数が入っている、ということがtermstrcの特徴になっている

まずは、パッケージを読み込んで、中身を確認

library(termstrc)

パッケージマニュアルには、関数がかなり細かく説明されているが、逆に何を使ったらよいかわからなくなっている

基本的な使い方としては、データがゼロクーポン債(zeroyields)なのかクーポン債(couponbonds)なのかを指定して、estim_nssでモデルのパラメータを推定する

> data(gobbonds)
> class(govbonds)
[1] "couponbonds"

> str(govbonds$GERMANY)
List of 8
 $ ISIN        : chr [1:52] "DE0001141414" "DE0001137131" "DE0001141422" "DE0001137149" ...
 $ MATURITYDATE: Date[1:52], format: "2008-02-15" "2008-03-14" ...
 $ ISSUEDATE   : Date[1:52], format: "2002-08-14" "2006-03-08" ...
 $ COUPONRATE  : num [1:52] 0.0425 0.03 0.03 0.0325 0.0413 ...
 $ PRICE       : num [1:52] 100 99.9 99.8 99.8 100.1 ...
 $ ACCRUED     : num [1:52] 4.09 2.66 2.43 2.07 2.39 ...
 $ CASHFLOWS   :List of 3
  ..$ ISIN: chr [1:384] "DE0001141414" "DE0001137131" "DE0001141422" "DE0001137149" ...
  ..$ CF  : num [1:384] 104 103 103 103 104 ...
  ..$ DATE: Date[1:384], format: "2008-02-15" "2008-03-14" ...
 $ TODAY       : Date[1:1], format: "2008-01-30"

> ns_res <- estim_nss(govbonds, c("GERMANY", "AUSTRIA", "FRANCE"),
+ matrange = c(0, 30), method = "ns", tauconstr = list(c(0.2, 5, 0.1), c(0.2, 5, 0.1), c(0.2, 5, 0.1)))
[1] "Searching startparameters for  GERMANY"
    beta0     beta1     beta2      tau1 
 5.132836 -1.274357 -3.208435  2.700100 
[1] "Searching startparameters for  AUSTRIA"
    beta0     beta1     beta2      tau1 
 5.050193 -1.327244 -2.629411  2.500100 
[1] "Searching startparameters for  FRANCE"
    beta0     beta1     beta2      tau1 
 5.108886 -1.217795 -3.068065  2.500100 

> summary(ns_res)
---------------------------------------------------
Goodness of fit:
---------------------------------------------------

                    GERMANY   AUSTRIA   FRANCE   
RMSE-Prices         0.3582276 0.1801092 0.2214637
AABSE-Prices        0.1992019 0.1224709 0.1182047
RMSE-Yields (in %)  0.0847062 0.0185987 0.0392355
AABSE-Yields (in %) 0.0498615 0.0155659 0.0275024


---------------------------------------------------
Startparameters:
---------------------------------------------------

        beta0    beta1    beta2    tau1    
GERMANY  5.13284 -1.27436 -3.20844  2.70010
AUSTRIA  5.05019 -1.32724 -2.62941  2.50010
FRANCE   5.10889 -1.21779 -3.06807  2.50010


---------------------------------------------------
Convergence information:
---------------------------------------------------

        optim() convergence info
GERMANY                        0
AUSTRIA                        0
FRANCE                         0

        optim() solver message
GERMANY NULL                  
AUSTRIA NULL                  
FRANCE  NULL               

クーポン債の場合には、データセットのように関数が適用できる形にデータを直したりするのが面倒かもしれない

パラメータを推定して、その時系列推移を見るという場合に結構使えるかもしれない。 ustycパッケージを使って、米国債イールドデータに適用してみる

library(ustyc)

maturities <- c(1/12, 1/4, 1/2, 1, 2, 3, 5, 7, 10, 20, 30)
yc <- getYieldCurve(year = "2014")
yields <- as.matrix(yc$df[-12])
dates <- rownames(yields)
datazeroyields <- zeroyields(maturities, yields, dates)

一応、これだけで、3Dプロットが出力できる

> datazeroyields
This is a data set of zero-coupon yields.
Maturities range from 0.0833333333333333 to 30 years.
There are 250 observations between 2014-01-02 and 2014-12-31 .
> class(datazeroyields)
[1] "zeroyields"
> plot(datazeroyields)

Nelson-Siegelモデルを当てはめる

ns_res <- estim_nss(datazeroyields, "ns", tauconstr = c(0.2, 6, 0.1))

まとめて、出力を見てみる

> ns_res
---------------------------------------------------
Estimated Nelson/Siegel parameters:
---------------------------------------------------

Number of oberservations: 250 

[[1]]
     beta_0          beta_1           beta_2           tau_1      
 Min.   :2.826   Min.   :-4.350   Min.   :-5.656   Min.   :1.024  
 1st Qu.:3.358   1st Qu.:-3.853   1st Qu.:-4.890   1st Qu.:1.284  
 Median :3.698   Median :-3.634   Median :-4.343   Median :1.381  
 Mean   :3.674   Mean   :-3.601   Mean   :-4.392   Mean   :1.388  
 3rd Qu.:3.953   3rd Qu.:-3.313   3rd Qu.:-3.988   3rd Qu.:1.485  
 Max.   :4.476   Max.   :-2.801   Max.   :-2.575   Max.   :1.668  

> summary(ns_res)
---------------------------------------------------
Goodness of fit:
---------------------------------------------------
                    [,1]     
RMSE-Yields (in %)  0.0334253
AABSE-Yields (in %) 0.0277030

---------------------------------------------------
Convergence information from optim ():
---------------------------------------------------
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    0    0    0    0    0    0     0
     [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
[1,]     0     0     0     0     0     0     0     0     0> head(param(ns_res)[[1]])
       beta_0    beta_1    beta_2    tau_1
[1,] 4.471780 -4.345616 -5.655850 1.545545
[2,] 4.476129 -4.349701 -5.596621 1.545772
[3,] 4.447632 -4.340263 -5.538249 1.561728
[4,] 4.421710 -4.323720 -5.432554 1.570774
[5,] 4.420168 -4.326042 -5.409182 1.511038
[6,] 4.380350 -4.294240 -5.321058 1.522402

Nelson-Siegelモデルならば、パッケージの使用例に書かれているtauconstrを指定すればよいが、Svensson、adjusted Svenssonだとoptimのところで収束しない、もしくは、パラメータの推定値がおかしな場合があるので、変えた方がよい

パラメータの時系列推移と各ファクターの寄与をプロット

> plot(param(ns_res))
> fcontrib(param(ns_res), index = 1, m = 1:30, method = "ns")

f:id:masaqol:20150118173205p:plain

f:id:masaqol:20150118181647p:plain

一年を通して、長期金利が低下傾向だったので、このような感じになる
満期が長くなるにつれて、長期水準以外のファクターの影響は小さくなっていく

関数が適用できる形にデータを持っていけば、いろいろなモデルに当てはめられる、良い感じの出力やプロットが得られるところがtermstrcのメリット。簡単に使えるシンプルさだったら、YieldCurveパッケージ

ggplot2を使いたい、もっと自由にいろいろデータを加工、適用したいという時にはtermstrcは少し面倒。このようなパッケージを参考に、自分で関数を作りながらやったほうが勉強にもなるし、分析はしやすい…