The following code:
- scrapes current U.S. Treasury yield data from the Treasury's website;
- plots the available data points (from the 1, 2, 3, 5, 7, 10, 20, and 30 year instruments); and
- interpolates & plots a full yield curve from those data points.
The code additionally shows what WolframAlpha outputs for comparison.
years = {1, 2, 3, 5, 7, 10, 20, 30};
StringSplit[
Import[
"https://www.treasury.gov/resource-center/data-chart-center/\
interest-rates/pages/XmlView.aspx?data=yield"
]][[-17 ;;]][[{1, 3, 5, 7, 9, 11, 13, 15}]];
StringDelete[#,
"xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"\
>"] & /@ %;
StringSplit[%, "</d:BC_"];
ToExpression[%[[All, 1]]]*0.01;
yields = Transpose[{years, %}];
curve = ListInterpolation[yields[[All, 2]], years];
Show[{
Plot[curve[x], {x, 1, 30}, PlotRange -> Full,
AxesOrigin -> {1, 0.01}, ImageSize -> 250] ,
ListPlot[yields, PlotStyle -> Red]}]
WolframAlpha["Treasury yields", {{"TreasuryYieldCurve:EconomicData",
1}, "Content"}]
Note: WolframAlpha does not appear to have access to the most current available daily data presented on the Treasury site. As of this posting on 8 Feb 2018, it only returns data from 1 Feb 2018.
I doubt that WolframAlpha has applied ListInterpolation
. It looks like just connects the data points.
But, the WolframAlpha plot does look more realistic, which got me to wondering.
The Treasury doesn't publish data for every year's yield so one could use them in an interpolation.
I need to make reasonable estimates of yield values at any point along the x axis.
In the plot of the interpolation, neither the peak of the curve between points 5 and 6 nor the dip in the curve between points 6 or 7 correspond to the real world yield curve. Yield curves can go very strange under "Black Swan" market events, but normally (and I use normally guardedly), longer maturity yields will have higher values than shorter maturity yields.
Under that assumption/observation, the WolframAlpha plot looks closer to the real world yield curve but its hardly a curve. Additionally, as I stated above, it doesn't use the most currently available data.
Can I configure the interpolation to more realistically model the yield curve? If not, I'd appreciate other suggestions to do so.
InterpolationOrder -> 1
? $\endgroup$InterpolationOrder
. Many thanks. $\endgroup$