fBonds/0000755000176000001440000000000012254146512011517 5ustar ripleyusersfBonds/NAMESPACE0000644000176000001440000000134312157313046012737 0ustar ripleyusers ################################################ ## Note this file has been automatically ## generated --- Do not edit it. ################################################ ################################################ ## import name space ################################################ import("timeDate") import("timeSeries") import("fBasics") ################################################ ## S4 classes ################################################ ################################################ ## S3 classes ################################################ ################################################ ## functions ################################################ export( "NelsonSiegel", "Svensson" ) fBonds/R/0000755000176000001440000000000012251677434011731 5ustar ripleyusersfBonds/R/TermStructure.R0000644000176000001440000002356711370220776014713 0ustar ripleyusers # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU Library General # Public License along with this library; if not, write to the # Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, # MA 02111-1307 USA # Copyrights (C) # for this R-port: # 1999 - 2006, Diethelm Wuertz, GPL # Diethelm Wuertz # info@rmetrics.org # www.rmetrics.org # for the code accessed (or partly included) from other R-ports: # see R's copyright and license files # for the code accessed (or partly included) from contributed R-ports # and other sources # see Rmetrics's copyright file ################################################################################ # FUNCTION: DESCRIPTION: # NelsonSiegel Nelson Siegel Term Structure # Svensson Nelson-Siegel-Svensson Term Structure ################################################################################ NelsonSiegel = function(rate, maturity, doplot = TRUE) { # A function written by Diethelm Wuertz # Description: # Fit the Yield Curve by the Nelson-Siegel Method # Details: # This function finds a global solution. The betas are solved # exactly as a function of tau. # Notes: # Prelimiary Status # FUNCTION: # Settings: # Start function minimum (fmin), global minimum (gmin), tau # values, and delta tau (dtau) n = length(maturity) fmin = matrix(rep(NA, times = n), byrow = TRUE, nrow = n) gmin = 1.0e99 tau = rep(NA, times = n) # Run the loop over the grid - This gives the start solution for (i in 1:n) { tau[i] = maturity[i] x = maturity/tau[i] a = matrix(rep(NA, times = 9), byrow = TRUE, 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 ( rate ), mean ( rate * ((1-exp(-x))/x) ), mean ( rate * (((1-exp(-x))/x - exp(-x))) ) ) beta = solve(a, b) yfit = beta[1] + beta[2]*exp(-x) + beta[3]*x*exp(-x) fmin[i] = sum( (rate - yfit)^2 ) if (fmin[i] < gmin) { gmin = fmin[i] gvec = c(beta, tau[i]) } } # If desired plot OLS(tau1, tau2): if (doplot) { plot(tau, log(fmin), type = "b", main = "OLS: Nelson-Siegel") } # Internal Functions: 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, rate, maturity) { sum( (rate - 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]) ) )^2) } # Optimization: # DW: gvec = c(0.0840, -0.0063, 0.0044, 1.7603) fit = nlminb(start = gvec, objective = func, rate = rate, maturity = maturity, control = list(eval.max = 2000, iter.max = 2000)) names(fit$par) = c("beta0", "beta1", "beta2", "tau1") # Plot Curve if desired: if (doplot) { yfit = fx(maturity, fit$par) plot(maturity, rate, ylim = c(min(c(rate, yfit)), max(c(rate, yfit)) ), main = "Nelson-Siegel" ) lines(maturity, yfit, col = "steelblue") grid() } # Return Value: fit } # ------------------------------------------------------------------------------ Svensson = function(rate, maturity, doplot = TRUE) { # A function written by Diethelm Wuertz # Description: # Fits the Yield Curve by the Nelson-Siegel-Svensson Method # Details: # This function finds a global solution. The betas are solved # exactly as a function of the taus. # Notes: # Prelimiary Status # FUNCTION: # Settings # Start function minimum (fmin), global minimum (gmin), tau # values, and delta tau (dtau) n = length(maturity) gmin = 1e99 fmin = matrix(rep(gmin, times = n*n), byrow = TRUE, nrow = n) beta0 = beta1 = beta2 = beta3 = fmin2 = fmin tau1 = tau2 = rep(0, times = n) # Run the loops over the grid - This gives the start solution for (i in 1:n) { tau1[i] = maturity[i] x1 = maturity/tau1[i] for (j in 1:n) { tau2[j] = maturity[j] x2 = maturity/tau2[j] if (i == j) { a = matrix(rep(NA, times = 9), byrow = TRUE, nrow = 3) a[1,1] = 1 a[1,2] = a[2,1] = mean(exp(-x1)) a[1,3] = a[3,1] = mean(2*x1*exp(-x1)) a[2,2] = mean(exp(-2*x1)) a[2,3] = a[3,2] = mean(2*x1*exp(-2*x1)) a[3,3] = mean(4*x1*x1*exp(-2*x1)) b = c(mean(rate), mean(rate*exp(-x1)), mean(2*rate*x1*exp(-x1))) beta = solve(a, b) yfit = beta[1] + beta[2]*exp(-x1) + 2*beta[3]*x1*exp(-x1) # fmin[i,j] = sum((rate-yfit)^2) fmin[i,j] = sum(abs((rate-yfit))) beta0[i,j] = beta[1] beta1[i,j] = beta[2] ## DW: beta2[i,j] = beta[3] ## DW: beta3[i,j] = beta[3] beta2[i,j] = beta[3]/2 beta3[i,j] = beta[3]/2 if (fmin[i,j] < gmin) { gmin = fmin[i,j] gvec = c(beta, beta[length(beta)], tau1[i], tau2[j]) gindex = c(i, j) } } else { a = matrix(rep(NA, times = 16), byrow = TRUE, nrow = 4) a[1,1] = 1 a[1,2] = a[2,1] = mean(exp(-x1)) a[1,3] = a[3,1] = mean(x1*exp(-x1)) a[1,4] = a[4,1] = mean(x2*exp(-x2)) a[2,2] = mean(exp(-2*x1)) a[2,3] = a[3,2] = mean(x1*exp(-2*x1)) a[2,4] = a[4,2] = mean(x2*exp(-x1-x2)) a[3,3] = mean(x1*x1*exp(-2*x1)) a[3,4] = a[4,3] = mean(x1*x2*exp(-x1-x2)) a[4,4] = mean(x2*x2*exp(-2*x2)) b = c(mean(rate), mean(rate*exp(-x1)), mean(rate*x1*exp(-x1)), mean(rate*x2*exp(-x2))) beta = solve(a, b) yfit = beta[1] + beta[2]*exp(-x1) + beta[3]*x1*exp(-x1) + beta[4]*x2*exp(-x2) # fmin[i,j] = sum((rate-yfit)^2) fmin[i,j] = sum(abs((rate-yfit))) beta0[i,j] = beta[1] beta1[i,j] = beta[2] beta2[i,j] = beta[3] beta3[i,j] = beta[4] if (fmin[i,j] < gmin) { gmin = fmin[i,j] gvec = c(beta, tau1[i], tau2[j]) gindex = c(i, j) } } } } # If desired plot "smoothed" OLS(tau1, tau2): ntau = length(tau1)-1 fmin2 = fmin # Smoothing: for (i in 2:ntau) for (j in 2:ntau) fmin2[i,j] = ( 4 * fmin[i,j] + 2 * (fmin[i+1,j] + fmin[i-1,j] + fmin[i,j+1] + fmin[i,j-1] ) + (fmin[i+1,j+1] + fmin[i-1,j-1] + fmin[i-1,j+1] + fmin[i+1,j-1] ) ) / 16 fmin = fmin2 if (doplot) { tau = 1:length(tau1) # tau = tau1 Z = log(fmin) # for (i in tau) Z[i, i] = NA # Perspective Plot: persp(tau, tau, Z, theta = -70, phi = 50, xlab = "tau 1", ylab = "tau 2", zlab = "log(fmin)", col = "steelblue") title(main = "OLS: Nelson-Siegel-Svensson") # Image/Contour Plot: image(tau, tau, Z, # xlim = c(15, 30), ylim = c(30, 48), xlab = "tau 1", ylab = "tau 2") contour(tau, tau, log(fmin), nlevels = 50, xlab = "tau 1", ylab = "tau 2", add = TRUE) points(tau[gindex[1]], tau[gindex[2]], pch = 19, cex = 2, col = "steelblue") title(main = "OLS: Nelson-Siegel-Svensson") } # Function to optimize: fx = function(maturity, x) { x[1] + x[2]*exp(-maturity/x[5]) + x[3]*(maturity/x[5])*exp(-maturity/x[5]) + x[4]*(maturity/x[6])*exp(-maturity/x[6]) } func = function(x, rate, maturity) { sum((rate - x[1] - x[2]*exp(-maturity/x[5]) - x[3]*(maturity/x[5])*exp(-maturity/x[5]) - x[4]*(maturity/x[6])*exp(-maturity/x[6]))^2) sum(abs((rate - x[1] - x[2]*exp(-maturity/x[5]) - x[3]*(maturity/x[5])*exp(-maturity/x[5]) - x[4]*(maturity/x[6])*exp(-maturity/x[6])))) } # Optimization: fit = nlminb(start = gvec, objective = func, rate = rate, maturity = maturity, control = list(eval.max = 2000, iter.max = 2000)) names(fit$par) = c("beta0", "beta1", "beta2", "beta3", "tau1", "tau2") # Plot Curve if desired: if (doplot) { yfit = fx(maturity, fit$par) plot(maturity, rate, ylim = c(min(c(rate, yfit)), max(c(rate, yfit))), main = "Nelson-Siegel-Svensson" ) grid() lines(maturity, yfit, col = "steelblue") } # Return Value: fit } ################################################################################ fBonds/R/zzz.R0000644000176000001440000000346112251677434012715 0ustar ripleyusers # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU Library General # Public License along with this library; if not, write to the # Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, # MA 02111-1307 USA # Copyrights (C) # for this R-port: # 1999 - 2008, Diethelm Wuertz, Rmetrics Foundation, GPL # Diethelm Wuertz # info@rmetrics.org # www.rmetrics.org # for the code accessed (or partly included) from other R-ports: # see R's copyright and license files # for the code accessed (or partly included) from contributed R-ports # and other sources # see Rmetrics's copyright file ################################################################################ ## .First.lib = ## function(lib, pkg) ## { ## # Startup Mesage and Desription: ## MSG <- if(getRversion() >= "2.5") packageStartupMessage else message ## dsc <- packageDescription(pkg) ## if(interactive() || getOption("verbose")) { ## # not in test scripts ## MSG(sprintf("Rmetrics Package %s (%s) loaded.", pkg, dsc$Version)) ## } ## # Load dll: ## # library.dynam("fBonds", pkg, lib) ## } if(!exists("Sys.setenv", mode = "function")) # pre R-2.5.0, use "old form" Sys.setenv <- Sys.putenv ################################################################################ fBonds/MD50000644000176000001440000000043312254146512012027 0ustar ripleyusersd62f2ecccf19c00add4fe4325069afb3 *ChangeLog 025fc262d8beb351abe8ce1eb2bcbafa *DESCRIPTION c03fbe537c72d3d2b1eba46e2993c16f *NAMESPACE 2275bb12d8b7855c89f2a0a2147cd002 *R/TermStructure.R 7e7218f9c5459662b503077ccbcc7a16 *R/zzz.R 23dd8a238b2413d36a3c734fa872a00c *man/TermStructure.Rd fBonds/DESCRIPTION0000644000176000001440000000137212254146512013230 0ustar ripleyusersPackage: fBonds Version: 3010.77 Revision: 5531 Date: 2013-12-17 Title: Bonds and Interest Rate Models Author: Diethelm Wuertz and many others, see the SOURCE file Depends: R (>= 2.4.0), timeDate, timeSeries, fBasics Suggests: RUnit Maintainer: Yohan Chalabi Description: Environment for teaching "Financial Engineering and Computational Finance" Note: Several parts are still preliminary and may be changed in the future. this typically includes function and argument names, as well as defaults for arguments and return values. LazyData: yes License: GPL (>= 2) URL: http://www.rmetrics.org Packaged: 2013-12-17 21:27:46 UTC; yohan NeedsCompilation: no Repository: CRAN Date/Publication: 2013-12-17 23:16:42 fBonds/ChangeLog0000644000176000001440000000174012254140722013270 0ustar ripleyusers2013-12-10 chalabi * DESCRIPTION, R/zzz.R: 2012-12-10 chalabi * ChangeLog, DESCRIPTION: Updated ChangeLog and DESCRIPTION files * DESCRIPTION: Updated version number * DESCRIPTION: Updated maintainer field and 'Note' field in lower * NAMESPACE: Added NAMESPACE 2011-09-23 mmaechler * DESCRIPTION: remove deprecated "LazyLoad" entry 2010-07-23 chalabi * inst/DocCopying.pdf: removed DocCopying.pdf license is already specified in DESCRIPTION file 2009-09-30 chalabi * DESCRIPTION: updated version number 2009-09-29 chalabi * ChangeLog, DESCRIPTION: updated DESC and ChangeLog 2009-09-22 chalabi * R/TermStructure.R: fixed plot as suggested by Sergio Guirreri. Thanks ! 2009-04-02 chalabi * DESCRIPTION: more explicit depends and suggests field in DESC file. 2009-04-01 chalabi * DESCRIPTION: updated DESC file 2009-02-24 chalabi * R/TermStructure.R: thanks to Dott. Sergio S. Guirreri - wrong name assigned to fitted parameter in Svesson fBonds/man/0000755000176000001440000000000012254140467012275 5ustar ripleyusersfBonds/man/TermStructure.Rd0000644000176000001440000000427412254140467015423 0ustar ripleyusers\name{TermStructure} \alias{TermStructure} \alias{NelsonSiegel} \alias{Svensson} \title{Term Structure Modelling} \description{ A collection and description of functions for term structure modelling. \cr The functions are: \tabular{ll}{ \code{NelsonSiegel} \tab Nelson-Siegel Term Structure, \cr \code{Svensson} \tab Nelson-Siegel-Svensson Term Structure. } } \usage{ NelsonSiegel(rate, maturity, doplot = TRUE) Svensson(rate, maturity, doplot = TRUE) } \arguments{ \item{doplot}{ a logical. Should a plot be displayed? } \item{maturity}{ a numeric vector of maturities on an annual scale. } \item{rate}{ a numeric vector of forward rates. } } \value{ a list object with entries returned from the optimization function \code{nlminb}. } \references{ McCulloch J. H. (1990); \emph{US Term Structure Data: 1946-87}, Handbook of Monetary Economics, Friedman B.M. and Hahn F.H. (eds.), Elsevier Science. McCulloch J. H. and Kwon, H.C. (1993); \emph{US Term Structure Data: 1947-1991}, Working Paper No. 93-6, Department of Economics, Ohio State University. Zivot E., Wang J.; \emph{Modeling Financial Time Series with S-Plus}. } \examples{ 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(mfrow = c(2, 2)) Svensson(Yield, Maturity) } \keyword{datasets}