Se trabajará con un conjunto de datos, donde se rescatarán los precios y los vencimientos de los bono cupón cero, es decir, pagan el valor nominal al vencimiento.
zero_prices = read.table("C:/Users/DELL/Desktop/uni 2020/RIESGOS/data/ZeroPrices.txt",header=TRUE)
attach(zero_prices)Para resolver esta parte de la pregunta, empezaremos con establecer valores iniciales a los tethas: \(θ_1=0.01\), \(θ_2=0.025\), \(θ_3=0.01\), \(θ_4=0.075\). Cabe mencionar que los valores pueden ser cualquiera, siempre y cuando se encuentren entre 0 y 1; por ende son aleatorios.
Luego procedemos a plantear una función para obtener los valores de los parámetros tethas, mediante mínimos cuadrados.
NSYield = function(theta)
{theta[1] + (theta[2] + theta[3]/theta[4]) *
(1 - exp(-theta[4]*T))/(theta[4]*T)-
(theta[3]/theta[4])*exp(-theta[4]*T)}
attach(zero_prices)## The following objects are masked from zero_prices (pos = 3):
##
## maturity, price
T = maturity
SS = function(theta) sum((price-100*exp(-T*NSYield(theta)))^2)
fitnls = optim(start,SS)
thetahat = fitnls$par
round(thetahat,4)## [1] 0.0176 0.0416 0.0023 0.0696
Con lo cual se obtiene las siguientes estimaciones:
| \(θ_1= 0.0176\) | \(θ_2= 0.0416\) |
|---|---|
| \(θ_3= 0.0023\) | \(θ_4= 0.0696\) |
Ahora, la continuación del código R anterior, define una función para calcular el tipo de interés a plazo y traza el tipo de cambio a plazo y la curva de rendimiento:
NSForward = function(theta){
theta[1] + (theta[2] + theta[3]*T) * exp(-theta[4]*T)
}
plot(T,NSForward(thetahat),type="l",ylab="Rate",xlab="Maturity",lwd=2)
lines(T,NSYield(thetahat),lty=2,lwd=2,col="red")
legend("topright",c("Tasa Forward","Rendimiento"),lty=c(1,2),
lwd=2,col=c("black","red"))A modo de verificación de que los cálculos son correctos, el siguiente código grafica los precios y los precios estimados:
plot(maturity,price)
lines(T,100*exp(-T*NSYield(fitnls$par)),col="red",lwd=2 )
legend("topright",c("Precio","Precio Estimado"),lty=c(NA,1),
pch=c("o",NA),col=c("black","red"),lwd=2)Vemos que los precios se predicen con mucha exactitud por el Modelo Nelson-Siegel.
La fórmula general del bono es:
## [1] 3.774842
El valor de r es 3.7748442%.
cupon_cero=function(T,r){
price = 1000*exp(-T*r)
}
T=4
r=0.0425
options(digits = 5)
precio_bono=cupon_cero(T,r)
precio_bono## [1] 843.66
El precio del bono sería $843.66.
## [1] 15.665
## [1] 1.8919
El retorno neto sería 1.8919%.
Suponga que la tasa forward es: \(r(t)=0,028+0,00042t\)
El rendimiento al vencimiento de un bono cupón cero con fecha de vencimiento T es definido para ser:
rendimiento=function(x,m,t){
r=x+m*(t/2)
r
}
x = 0.028
m = 0.00042
t = 20
yield=rendimiento(x,m,t)
yield*100## [1] 3.22
El YTM es 3.22%.
precio_bono=function(t,y,z){
price=1000*exp(-((15*0.028)+(0.00042)*(15^2)/2))
}
t= 15
y= 0.028
z= 0.00042
P=precio_bono(15,0.028,0.00042)
P## [1] 626.72
El precio del bono es $626.72.
Suponga que la tasa forward es: \(r(t)=0,028+0,0002t−0,0003t^2\)
El rendimiento al vencimiento de un bono cupón cero con fecha de vencimiento T es definido para ser:
yield_to_maturity=function(p,b,m,t){
r=p+b*t/2-m*(t/3)^2
r
}
t = 8
p = 0.028
b = 0.0002
m = 0.0003
YTM=yield_to_maturity(p,b,m,t)
YTM*100## [1] 2.6667
El rendimiento es 2.667%.
p_b=function(t,a,b,c){
price=1000*exp(-((t*a)+(b)*(t^2)/2)-(c)*(t^2)/3)
}
t=5
a=0.028
b=0.0002
c=0.0003
price=p_b(t,a,b,c)
price## [1] 865.02
El precio del bono es $865.02.
Teniendo en cuenta el siguiente código:
bondvalue = function (c, T , r , par)
{
# Computes bv = bond values ( current prices ) corresponding
# to all values of yield to maturity in the
# input vector r
#
# INPUT
# c = coupon payment ( semiannual )
# T = time to maturity (in years )
# r = vector of yields to maturity ( semiannual rates )
# par = par value
#
bv = c / r + (par - c / r ) * (1 + r )^( -2 * T )
bv
}ur1<- uniroot( function(r) bondvalue(40, 30, r, 1000) - 1200, c(0.03,0.04) )
ur1_r <- ur1$root
ur1$root*100## [1] 3.2416
El retorno a la madurez es 3.2416%.
ur2 <- uniroot(function(r) bondvalue(280, 8, r, 10000) - 9800, c(0.01,0.04) )
ur2_r <- ur2$root
ur2$root*100## [1] 2.9564
El retorno a la madurez es 2.9564%.
ur3 <- uniroot(function(r) bondvalue(35, 20, r, 1000) - 1050, c(0.01,0.04) )
ur3_r <- ur3$root
ur3$root*100## [1] 3.2737
El retorno a la madurez es 3.2737%.
ur4 <- uniroot(function(c) bondvalue(c, 5, 0.035, 1000) - 950.10, c(0,1000) )
ur4_r <- ur4$root
ur4$root## [1] 29
El valor del cupón es $29.