fBonds

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

fBonds

Thomas Steiner
I'd like to calculate forward rates from swap rates and then fit these
fwd-rates to a Svensson family and see how parameters envolve over
time (consistency).
I've read of the R-package fBonds, which could help me a lot. It is
not availible at CRAN or www.metrics.org Does anybody know about its
current state? Is it available to the public somewhere? Are there any
other packages that could help me?
Thanks for suggestions, help and hints
Thomas

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds

Spencer Graves
          RSiteSearch("fBonds") produced 'No document matching your query.'  I
similarly got nothing relevant searching for "swap rates" and
"Svensson".  Googling for "fBonds" found a similar question to
[R-sig-finance] in Oct. 2004, with no recorded answre.  The developer
and maintainer of the Rmetrics project is Diethelm Wuertz
<[hidden email]>.  I don't know if S-Plus FinMetrics has this
or not.

          However, if you want to provide the math and some references with an
incomplete attempt to program what you want in R, someone might help you
get past any given obstacle.  (If you do this, you may want to know that
posts more consistent with the posting guide!
"www.R-project.org/posting-guide.html" may get quicker, more helpful
answers.)

          I'm sorry I couldn't give you a more helpful answer, but I hope you
will find this useful nevertheless.

          Best Wishes,
          spencer graves

Thomas Steiner wrote:

> I'd like to calculate forward rates from swap rates and then fit these
> fwd-rates to a Svensson family and see how parameters envolve over
> time (consistency).
> I've read of the R-package fBonds, which could help me a lot. It is
> not availible at CRAN or www.metrics.org Does anybody know about its
> current state? Is it available to the public somewhere? Are there any
> other packages that could help me?
> Thanks for suggestions, help and hints
> Thomas
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds

Eric Zivot
S+FinMetrics has a number of tools for term structure modeling. The
term.struct() function implements a number of interpolation and spline
techniques, as well as Nelson-Siegel and Svensson models. In S+FinMetrics
2.0, the state space functionality has been upgraded to handle a general
class of affine term structure models. You can also easily use the state
space tools to implement a nelson-siegel or svensson model with time varying
coefficients (see, e.g., Diebold and Lee 2002 "Forecasting the Term
Structure of Government Bonds"). This allows for better out of sample
prediction.  I currently have a Phd student working on these methods as well
as extensions. Anyone interested in some code examples please email me
directly.
ez

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Spencer Graves
Sent: Saturday, January 21, 2006 12:27 AM
To: Thomas Steiner
Cc: [hidden email]
Subject: Re: [R-sig-finance] fBonds

          RSiteSearch("fBonds") produced 'No document matching your query.'
I
similarly got nothing relevant searching for "swap rates" and
"Svensson".  Googling for "fBonds" found a similar question to
[R-sig-finance] in Oct. 2004, with no recorded answre.  The developer
and maintainer of the Rmetrics project is Diethelm Wuertz
<[hidden email]>.  I don't know if S-Plus FinMetrics has this
or not.

          However, if you want to provide the math and some references with
an
incomplete attempt to program what you want in R, someone might help you
get past any given obstacle.  (If you do this, you may want to know that
posts more consistent with the posting guide!
"www.R-project.org/posting-guide.html" may get quicker, more helpful
answers.)

          I'm sorry I couldn't give you a more helpful answer, but I hope
you
will find this useful nevertheless.

          Best Wishes,
          spencer graves

Thomas Steiner wrote:

> I'd like to calculate forward rates from swap rates and then fit these
> fwd-rates to a Svensson family and see how parameters envolve over
> time (consistency).
> I've read of the R-package fBonds, which could help me a lot. It is
> not availible at CRAN or www.metrics.org Does anybody know about its
> current state? Is it available to the public somewhere? Are there any
> other packages that could help me?
> Thanks for suggestions, help and hints
> Thomas
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds

Diethelm Wuertz
In reply to this post by Thomas Steiner

In the Rmetrics "Fact Sheet", accessiible on the Home page of the Rmetrics
Server www.rmetrics.org, you find that the fBonds package is not  yet
available.
The package is still uncomplete, untested, and  less documented.  Currently
are available the following chapters with R functions::

    Bond and Money Market Arithmetic
    Bootstrapping - Direct Calculation of the Yield Curve
    Yield Curve Estimation by Regression Analysis
    Parameterizing a Yield Curve by Forward Rates
    Building the Yield Curve from the BBA LIBOR
    Adding Swaps to the Yield Curve
    Term Structure Fitting: Nelson-Siegel and Svensson models
    Replicating Portfolios for Risk Management of Mortgages and Savings

Less is done with functions for the valuation of interest rate options.
Only a
few models are implemented.

Since other components of the Rmetrics Packages have higher priorities
it will
take some additional time to finish the R package fBonds. If somebody from
the R community is ready for cooperation and will contribute with
additional
functions for analysing and modelling Bond and Money Markets, or testing,
or writing documentation, please let me know.

Diethelm Wuertz

PS: Have also a look on SPlus/Finmetrics

Thomas Steiner wrote:

>I'd like to calculate forward rates from swap rates and then fit these
>fwd-rates to a Svensson family and see how parameters envolve over
>time (consistency).
>I've read of the R-package fBonds, which could help me a lot. It is
>not availible at CRAN or www.metrics.org Does anybody know about its
>current state? Is it available to the public somewhere? Are there any
>other packages that could help me?
>Thanks for suggestions, help and hints
>Thomas
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds svensson fitting

Krishna Kumar-2
In reply to this post by Thomas Steiner
Nelson-siegel and svensson are easy to fit (see
http://www.bankofcanada.ca/en/res/tr/1999/tr84.pdf)

I used the following test data

 > fwdcrv
   tenor     fwd
1      1 1.10000
2      2 1.11927
3      3 1.13754
4      4 1.15735
5      5 1.22402
6      6 1.26254
7      7 1.33145
8      8 1.40115
9      9 1.47255
10    10 1.62928
11    11 1.69269
12    12 1.81135


And now in R one could do the following:
-------------------------------
nelson<-function(x,y=x)
{
nelson<- sum((y[,2]-(x[1] + (x[2] + x[3]* y[,1])* exp(-x[4]* y[,1])))^2)
}

r = nlm(f=nelson,p=c(1,10,10,10),y=fwdcrv,steptol=1e-10,iterlim=500)

$minimum
[1] 0.6461518

$estimate
[1]  1.361599  8.683838  8.683784 36.325287

$gradient
[1] 1.431076e-08 0.000000e+00 0.000000e+00 0.000000e+00

$code
[1] 1

$iterations
[1] 139

Note svensson is double humped so you have an additional parameter (
x[5] ) and so modify the function suitably.
Hope this helps,

Kris






Thomas Steiner wrote:

>I'd like to calculate forward rates from swap rates and then fit these
>fwd-rates to a Svensson family and see how parameters envolve over
>time (consistency).
>I've read of the R-package fBonds, which could help me a lot. It is
>not availible at CRAN or www.metrics.org Does anybody know about its
>current state? Is it available to the public somewhere? Are there any
>other packages that could help me?
>Thanks for suggestions, help and hints
>Thomas
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds svensson fitting

Thomas Steiner
Dear Kris,
excellent, thank you.

> r = nlm(f=nelson,p=c(1,10,10,10),y=fwdcrv,steptol=1e-10,iterlim=500)

Where is the difference to nls? I tried something like

nls(formula=yc$yield~nelsonsiegel(yc$time,init), data = yc, start=init)

where yc is my dataframe with time and forward rates.
Anyway, even good starting estimates seem always to produce a singular
gradient matrix.

> Note svensson is double humped so you have an additional parameter (
> x[5] )

I think Svensson is 6-dimensional.
Some interpretations of the parameters of eg Nelson-Siegel can be used
to get better initial estimates. But this is as well mentioned in the
paper (pdf) you linked.

> Hope this helps,

Yes, a lot. BTW: How do you calculate forward rates out of SWAP (Libor) rates?

Thomas

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds svensson fitting

Krishna Kumar-2
Thomas Steiner wrote:

>  
>
>>r = nlm(f=nelson,p=c(1,10,10,10),y=fwdcrv,steptol=1e-10,iterlim=500)
>>    
>>
>
>  
>
 >>How do you calculate forward rates out of SWAP (Libor) rates?

Typically you have money-market/cash , futures and swap rates so it is a
little more involved and there are other considerations
such as smoothness of the forward and day counts etc. If you want an
ugly way of doing this (caveat emptor)

Let us say we have the swap crv object with the first row being a cash
rate (1 year) rate. The columns are
 tenor in years, day count between two tenor points and the swap/cash rates.

 > swapcrv
   Tenor days.360  Rates
1      1 1.022222 0.0359
2      2 1.011111 0.0390
3      3 1.011111 0.0403
4      4 1.013889 0.0409
5      5 1.016667 0.0414
6      6 1.013889 0.0418
7      7 1.019444 0.0423
8      8 1.011111 0.0427
9      9 1.013889 0.0430
10    10 1.013889 0.0434


---------------------------------------------
dfcurve<-function(swapcrv)
{
#First construct a discount curve!
##assumes the first point is cash and rest are swap rates
n<-dim(swapcrv)[1]
dfcrv<-matrix(0,n,2)
dfcrv[,1]<-swapcrv[,1]
dfcrv[,2]<-1/(1+swapcrv[,2]*swapcrv[,3])
dfcrv[-1,2]<-dfcrv[-1,2]*(1-(cumsum(swapcrv[-n,2]*dfcrv[-n,2]))*swapcrv[-1,3])
return(dfcrv)
}


df.to.fwd<-function(dfcurve,tenor,interp="linear")
{
#function to compute the forward curve at the tenor points given a dfcurve!
#do linear interpolation of the dfactors or cubic spline from akima package!
tenor<-c(0,tenor)
n<-length(tenor)
fwd.tenor<-array(0,n)
df.tenor<-array(0,n)
if(interp=="spline")
{
df.tenor<-aspline(dfcurve[,1],dfcurve[,2],tenor)$y

} else
{
df.tenor<-approx(dfcurve[,1],dfcurve[,2],tenor)$y

}
df.tenor[1]<-1
fwd.tenor<-(df.tenor[-n]/df.tenor[-1] -1)/diff(tenor)
return(list(fwd=fwd.tenor,df=df.tenor))

}
-----------------------------------------------


So now you can do

 >   dfcrv <- dfcurve(swapcrv)

And then construct the forward curve from the discount curve.

 > df.to.fwd(dfcrv,tenor=seq(0.5,10,by=0.5),interp="spline")

There are many ways to construct these curves and you will find this
exhaustive paper useful
(http://www.cam.wits.ac.za/mfinance/papers/interpolation.pdf )

Best,
Kris

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: fBonds svensson fitting

Diethelm Wuertz
In reply to this post by Thomas Steiner
# Here comes a program to compute the Nelson-Siegel approach for the
# Yield Curve. It can be modified for forward rates (straightforward
# but a little more lengthy), and to other approaches including the Svenson
# Approach and/or several Spline Interpolations.

# The data set used here are an extension of the McCulloch U.S. Treasury
# term structure data (appearing in the Handbook of Monetary Economics)
# to February 1991 as used by S-Plus.

# The results of my program are compared with those obtained from
# SPlus/Finmetrics. They agree to more than 6 digets!



NelsonSiegel =
function(Yield, Maturity)
{    # A function written by Diethelm Wuertz
   
    # Description:
    #    Fit the Yield Curve by the Nelson-Siegel Method
    #
    # Details:
    #    This function finds a global solution. The start values for the
    #    betas are solved exactly as a function of tau using OLS.
    #
    # Copyright:
    #    Diethelm Wuertz, (c) 2004 fBonds
    #
    # Source:
    #    Partial copy from 'fBonds' from 'Rmetrics' (unpublished).
   
    # FUNCTION:
   
    # Find Optimal Start Solution by OLS of beta's vs. Yields:
    n = length(Maturity)
    gmin = 1.0e99
    for (i in 1:n) {    
        tau = Maturity[i]
        x = Maturity/tau        
        a = matrix(rep(NA, times = 9), nrow = 3)
            a[1,1] = 1
            a[1,2] = a[2,1] = mean((1-exp(-x))/x)
            a[1,3] = a[3,1] = mean((1-exp(-x))/x - exp(-x))
            a[2,2] = mean( ((1-exp(-x))/x)^2 )
            a[2,3] = a[3,2] = mean(((1-exp(-x))/x)*((1-exp(-x))/x-exp(-x)))
            a[3,3] = mean(((1-exp(-x))/x - exp(-x))^2)
        b = c(
            mean ( Yield ),
            mean ( Yield *  ((1-exp(-x))/x)),
            mean ( Yield * (((1-exp(-x))/x - exp(-x)))))            
        beta = solve(a, b)
        yfit = beta[1] + beta[2]*exp(-x) + beta[3]*x*exp(-x)
        fmin = sum( (Yield-yfit)^2 )
        if (fmin < gmin) {
            gmin = fmin
            gvec = c(beta, tau)
        }
    }
                   
    # Function to be optimized:      
    fx <- function(Maturity, x) {
        x[1] + x[2] * (1-exp(-Maturity/x[4]))/(Maturity/x[4]) +
        x[3] *
((1-exp(-Maturity/x[4]))/(Maturity/x[4])-exp(-Maturity/x[4]))
    }
    func <- function(x) { sum( (Yield - fx(Maturity, x))^2 ) }

    # Optimize:
    fit = nlminb(objective = func, start = gvec)
    fit$start = gvec
    names(fit$par) = c("beta1", "beta2", "beta3", "tau")
       
    # Plot Curve:
    yfit = fx(Maturity, gvec)
    plot(Maturity, Yield, ylim = c(min(c(Yield, yfit)), max(c(Yield,
yfit))),
        pch = 19, cex = 0.5, main = "Nelson-Siegel" )
    lines(Maturity, yfit, col = "steelblue")
           
    # Return Value:
    fit
}


Yield = c(
0.04984, 0.05283, 0.05549, 0.05777, 0.05961, 0.06102, 0.06216, 0.06314,
0.06403,
0.06488, 0.06568, 0.06644, 0.06717, 0.06786, 0.06852, 0.06913, 0.06969,
0.07020,
0.07134, 0.07205, 0.07339, 0.07500, 0.07710, 0.07860, 0.08011, 0.08114,
0.08194,
0.08274, 0.08355, 0.08434, 0.08512, 0.08588, 0.08662, 0.08731, 0.08794,
0.08851,
0.08900, 0.08939, 0.08967, 0.08980, 0.08976, 0.08954, 0.08910, 0.08843,
0.08748,
0.08626, 0.08474, 0.08291)


Maturity = c(
  0.083,  0.167,  0.250,  0.333,  0.417,  0.500,  0.583,  0.667,  
0.750,  0.833,
  0.917,  1.000,  1.083,  1.167,  1.250,  1.333,  1.417,  1.500,  
1.750,  2.000,
  2.500,  3.000,  4.000,  5.000,  6.000,  7.000,  8.000,  9.000, 10.000,
11.000,
 12.000, 13.000, 14.000, 15.000, 16.000, 17.000, 18.000, 19.000, 20.000,
21.000,
 22.000, 23.000, 24.000, 25.000, 26.000, 27.000, 28.000, 29.000)



NelsonSiegel(Yield, Maturity)

# $par
#         beta1         beta2         beta3           tau
#  8.885514e-02 -3.671098e-02  1.398381e-11  1.025116e+00


# Compare with S-Plus - (only runs under SPlus):
# ans = term.struct(Yield, Maturity, na.rm = T, method = "ns", input =
"spot")
# c(ans$coefficients, tau = ans$tau)
#          b0          b1            b2      tau
#  0.08885513 -0.03671097 5.254415e-008  1.02511


RMSE =
function(Yield, Maturity, coef)
{
    sum(Yield - coef[1] +
        coef[2]*(1-exp(-Maturity/coef[4]))/(Maturity/coef[4]) +
        coef[3] * ( (1-exp(-Maturity/coef[4]))/(Maturity/coef[4]) -
        exp(-Maturity/coef[4]))    )^2
}

coef = c(8.885514e-02, -3.671098e-02,  1.398381e-11,  1.025116e+00)
RMSE(Yield, Maturity, coef)
# 1.492437

coef = c(0.08885513, -0.03671097, 5.254415e-008,  1.02511)
RMSE(Yield, Maturity, coef)
#  1.492431


DW



Thomas Steiner wrote:

>Dear Kris,
>excellent, thank you.
>
>  
>
>>r = nlm(f=nelson,p=c(1,10,10,10),y=fwdcrv,steptol=1e-10,iterlim=500)
>>    
>>
>
>Where is the difference to nls? I tried something like
>
>nls(formula=yc$yield~nelsonsiegel(yc$time,init), data = yc, start=init)
>
>where yc is my dataframe with time and forward rates.
>Anyway, even good starting estimates seem always to produce a singular
>gradient matrix.
>
>  
>
>>Note svensson is double humped so you have an additional parameter (
>>x[5] )
>>    
>>
>
>I think Svensson is 6-dimensional.
>Some interpretations of the parameters of eg Nelson-Siegel can be used
>to get better initial estimates. But this is as well mentioned in the
>paper (pdf) you linked.
>
>  
>
>>Hope this helps,
>>    
>>
>
>Yes, a lot. BTW: How do you calculate forward rates out of SWAP (Libor) rates?
>
>Thomas
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance