knitr::opts_chunk$set(echo = TRUE)
##install.packages("rmarkdown")
library(readxl)
ZeroPrices <- read_excel("C:\\Users\\Mercedes\\Downloads//ZeroPrices.xlsx", sheet="zero_prices")
View(ZeroPrices)
attach(ZeroPrices)
names(ZeroPrices)
## [1] "periodo" "maturity" "price" "maturity1"
##install.packages("nls2")
##install.packages("proto")
library(nls2)
## Warning: package 'nls2' was built under R version 4.0.2
## Loading required package: proto
## Warning: package 'proto' was built under R version 4.0.2
library(proto)
c.0<- min(ZeroPrices$maturity1)*0.5
modelo1 <- lm(log(maturity1 - c.0) ~ periodo, data=ZeroPrices)
pred1<-predict(modelo1)
start <- list(a=0.0001, d=-0.0001)
nonlin_mod=
nonlin_mod1<-nls2(maturity1~a+(pred1)*exp(-d*periodo),start=start, data=ZeroPrices, trace = FALSE, algorithm="port")
summary(modelo1)
##
## Call:
## lm(formula = log(maturity1 - c.0) ~ periodo, data = ZeroPrices)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.2374 -0.1185 0.4252 0.5392 0.7223
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.0416 3.5304 1.145 0.259
## periodo -1.1422 0.6239 -1.831 0.075 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.053 on 38 degrees of freedom
## Multiple R-squared: 0.08105, Adjusted R-squared: 0.05687
## F-statistic: 3.352 on 1 and 38 DF, p-value: 0.07498
summary(nonlin_mod1)
##
## Formula: maturity1 ~ a + (pred1) * exp(-d * periodo)
##
## Parameters:
## Estimate Std. Error t value Pr(>|t|)
## a 0.78785 0.08786 8.967 6.45e-11 ***
## d 0.22799 0.02327 9.799 5.99e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.06274 on 38 degrees of freedom
##
## Algorithm "port", convergence message: relative convergence (4)
Los valores a=0.78785, b=4.0416, c=-1.1422, d=0.22799 Aplicando las tecnicas de aproximacion quedaria asi: maturity1~0.78785+(4.0416-1.1422#periodo)#exp(-0.22799#periodo)
Seria la ecuacion de regresion no-lineal de mejor ajuste.
##install.packages("ggplot2")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.2
p1<-ggplot(data.frame(x=c(5, 7)), aes(x)) +stat_function(fun=function(x) 0.78785+(4.0416-1.1422*x)*exp(-0.22799*x))
p1 + geom_point(data=ZeroPrices, aes(x = periodo, y = maturity1))
En la grafica se puede apreciar que la curva tiene un buen ajuste de los datos y es convexa al origen.
Vf=1000
Vp=828
n=5
r_2a=((Vf/Vp) ^ (1/n) )-1
r_2a
## [1] 0.03846995
El valor de r es 3.85%
Vf=1000
r_2b=0.0425
n=5
P_2b= Vf / ((1+r_2a) * (1+r_2b) ^ (n-1))
P_2b
## [1] 815.2707
El precio del bono es $815.2707
P_2b=815.2707
Vp=828
ValorInv = P_2b - Vp
ValorInv
## [1] -12.7293
El valor de la inversion es $ -12.7293
P_2b=815.2707
Vp=828
Retor_neto = (P_2b - Vp) / Vp
Retor_neto
## [1] -0.01537355
se obtiene una tasa de rendimiento anual del -1,53%
n=20
r_3a=0.028 + 0.00042*n
r_3a
## [1] 0.0364
El rendimiento es de 3.64%.
t=15
r=0.028 + 0.00042*t
r
## [1] 0.0343
Vf=1000
P=Vf/(1+r)^t
P
## [1] 602.9789
La tasa de madurez es 3.43% El precio del bono con cupon cero es 602.98.
t=8
r_4a=0.028 + 0.0002*t - 0.0003*t^2
r_4a
## [1] 0.0104
El valor del rendimeinto es 1.04%
t=5
r_4b=0.028 + 0.0002*t - 0.0003*t^2
r
## [1] 0.0343
Vf=1000
Pb_4b <-Vf/((1+r_4b)^(t))
Pb_4b
## [1] 899.1003
La tasa de madurez es 2.15% El precio del bono con cupon cero es $899.10.
## Warning: package 'ggplot2' is in use and will not be installed
La grafica de la parbola es decreciente y es concava al origen.
t=10
r_4d=0.028+0.0002*t - 0.0003*t^2
r_4d
## [1] 0
t=11
r_4d= 0.028+0.0002*t - 0.0003*t^2
r_4d
## [1] -0.0061
El valor de la tasa de retorno es cero. Para el siguiente año es negativa.
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
}
Utilice la funcion uniroot() para resolver las siguiente preguntas.
c=40
T=30
r=0
par=1000
bv=1200
uniroot(function(r) bv-(c/r+(par-c/r)*(1+r)^(-2*T)), interval= c(0.0001, 1), tol = 1e-9)$root
## [1] 0.03239813
El valor de la madurez es 3.24%
c=280
T=8
r=0
par=10000
bv=9800
uniroot(function(r) bv-(c/r+(par-c/r)*(1+r)^(-2*T)), interval= c(0.0001, 1), tol = 1e-9)$root
## [1] 0.0295872
El valor de la madurez es 2.96%
c=35
T=20
r=0
par=1000
bv=1050
uniroot(function(r) bv-(c/r+(par-c/r)*(1+r)^(-2*T)), interval= c(0.0001, 1), tol = 1e-9)$root
## [1] 0.03274004
El valor de la madurez es 3.27%
c=0
T=5
r=0.035
par=1000
bv=950.10
uniroot(function(c) bv-(c/r+(par-c/r)*(1+r)^(-2*T)), interval= c(0, 100), tol = 1e-9)$root
## [1] 28.99996
El valor del cupon es $29.97