SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1804|回复: 0
打印 上一主题 下一主题

Create Nelson-Siegel function for yield curve

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-10-8 03:10:56 | 只看该作者

Create Nelson-Siegel function for yield curve

From Dapangmao's blog on sas-analysis

<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-FHm9z_3nyX0/To9J5Bk2N7I/AAAAAAAAAxQ/Q27DbhLMj7w/s1600/SGPlot17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="http://4.bp.blogspot.com/-FHm9z_3nyX0/To9J5Bk2N7I/AAAAAAAAAxQ/Q27DbhLMj7w/s400/SGPlot17.png" width="400" /></a></div>U.S. Treasury bonds with maturity ranging from 1 year to 30 years are daily updated on the <a href="http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield">Treasury’s website</a>. However, some yields, such as from 4 years maturity bond, have to be inferred. The <a href="http://en.wikipedia.org/wiki/Nelson-Siegel#Modeling_the_yield_curve">Nelson-Siegel function</a> is probably one of the most important formulas to construct the yield curve. Many people use <a href="http://www.soa.org/library/newsletters/compact/2011/january/com-2011-iss38-chan.aspx">EXCEL’s Solver </a>to find the nonlinear estimates of the four parameters (a1, a2, a3 and beta) for the Nelson-Siegel function. SAS’s PROC NLIN can also do this job smoothly. I copied today’s yield data and imported into SAS. Then I assigned some initial values to the unknown parameters and solved those parameters. With the PROC FCMP, I was able to build a function to predict the yields at any given year. Finally I plotted the yield curve (I also fitted the real yields with a quadratic regression line). The results showed that the nonlinear Nelson-Siegel function behaves better at longer maturities than linear regression.<br />
<br />
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
**********************(1) IMPORT STEP******************************************;
data _1;
   input ytm : $4. @@;
   cards;
1mo   3mo   6mo   1yr   2yr   3yr   5yr   7yr   10yr 20yr   30yr
;;;

data _2;
   input yield : 3.2 @@;
   cards;
0.01 0.01   0.03   0.09 0.29 0.46   1.01   1.52   2.01   2.71   2.96
;;;

data _3(keep=yield years);
   set _1;
   set _2;
   if find(ytm, 'yr') gt 0;
   years = input(compress(ytm, 'yr'),3.);
   yield = yield / 100;
run;

**********************(2) SOLVE PARAMETERS************************************;
proc nlin data=_3 method=newton;
   parms a1 = 0.05 a2 = 0.05
      a3 = 0.05 beta = 1;
   model yield=a1+(a2+a3)*(beta/years)*(1-exp(-years/beta))-a3*exp(-years/beta);
   ods output parameterestimates = parmds;
run;

data _null_;
   set parmds;
   call symput(parameter, estimate);
run;

**********************(3) BUILD FUNCTION AND APPLY IT****************************;
proc fcmp outlib = work.func.finance;
   function nelson_siegel(years);
      return(  
         &amp;a1 + (&amp;a2+&amp;a3)*(&amp;beta/years)*(1-exp(-years/&amp;beta))
         - &amp;a3*exp(-years/&amp;beta)
   );
   endsub;
run;
options cmplib=work.func;
data _4;
   do years = 1 to 30;
      predyield = nelson_siegel(years);
      output;
   end;
run;

data _5;
   merge _3(rename=(yield=realyield)) _4(in=a);
   by years;
   if a;
run;

ods html style = money;
proc sgplot data = _5;
   scatter x = years y = predyield;
   series x = years y = realyield ;
   reg x = years y = realyield / degree = 2;
   xaxis grid; yaxis grid label = ' ';
   format realyield percent8.2;
run;
ods html style = htmlbluecml;
********************END OF ALL CODING*****************************************;
</code></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3256159328630041416-7123540634538969558?l=www.sasanalysis.com' alt='' /></div><img src="http://feeds.feedburner.com/~r/SasAnalysis/~4/zFgtQoJTko0" height="1" width="1"/>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-5-7 21:03 , Processed in 0.067058 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表