64 lines
1.7 KiB
R
Executable file
64 lines
1.7 KiB
R
Executable file
library("scatterplot3d")
|
|
|
|
##
|
|
## implémentation de la courbe de Nelson-Siegel
|
|
##
|
|
|
|
## Lecture des taux spot
|
|
taux <- t(read.table("usgg.csv",header=T, sep=";",na.strings = "#NA")
|
|
[seq(from=1,to=5758,by=30),2:6])
|
|
length(taux)
|
|
temps <- c(.25,2,3,5,10)
|
|
|
|
## fonction Nelson-Siegel originale
|
|
f0t <- function(PAR,t)
|
|
{
|
|
PAR[1] + (PAR[2]+PAR[3]*t)*exp(-PAR[4]*t)
|
|
}
|
|
|
|
## fonction objectif
|
|
f0obj <- function(PAR,temps,taux)
|
|
{
|
|
sum((taux-f0t(PAR,temps))^2)
|
|
}
|
|
|
|
## fonction qui retourne les paramètres
|
|
NelsonSiegel <- function(taux,temps)
|
|
{
|
|
m1 <- optim(c(1,1,1,1),f0obj,gr=NULL,temps,taux)
|
|
m1$par
|
|
}
|
|
|
|
## dimensions pour le graphique
|
|
duration <- seq(from=0,to=12,by=0.1)
|
|
timeline <- 1:dim(taux)[2]
|
|
|
|
## calcul des paramètres pour chaque courbe
|
|
parametres <- apply(taux,2,NelsonSiegel,temps)
|
|
|
|
## calcul des points du graphique
|
|
pointsgraph <- cbind(rep(timeline,each=length(duration)),duration,
|
|
as.vector(apply(parametres,2,f0t,duration)))
|
|
|
|
## tracer le graphique
|
|
pdf("nelsonsiegel-plots.pdf")
|
|
scatterplot3d(pointsgraph,type="l", pch=20,
|
|
xlab="Observations",
|
|
ylab="Duration(t)", zlab="r(t)")
|
|
plot(pointsgraph[pointsgraph[,1]==1,][,-1],type="l",main="Temps 1",
|
|
xlab="Duration (t)",
|
|
ylab="r(t)",ylim=c(0,10))
|
|
points(temps,taux[,1])
|
|
plot(pointsgraph[pointsgraph[,1]==61,][,-1],type="l",main="Temps 61",
|
|
xlab="Duration (t)",
|
|
ylab="r(t)",ylim=c(0,10))
|
|
points(temps,taux[,61])
|
|
plot(pointsgraph[pointsgraph[,1]==121,][,-1],type="l",main="Temps 121",
|
|
xlab="Duration (t)",
|
|
ylab="r(t)",ylim=c(0,10))
|
|
points(temps,taux[,121])
|
|
plot(pointsgraph[pointsgraph[,1]==181,][,-1],type="l",main="Temps 181",
|
|
xlab="Duration (t)",
|
|
ylab="r(t)",ylim=c(0,10))
|
|
points(temps,taux[,181])
|
|
dev.off()
|