标题: Create Nelson-Siegel function for yield curve [打印本页] 作者: shiyiming 时间: 2011-10-8 03:10 标题: 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 _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(
&a1 + (&a2+&a3)*(&beta/years)*(1-exp(-years/&beta))
- &a3*exp(-years/&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"/>