# Configuracion inicial y carga de librerias
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(MASS) 
library(knitr)
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Parametros iniciales
trm_spot <- 3950
monto_maquina_cop <- 350000000
monto_maquina_usd <- monto_maquina_cop / trm_spot

cuota_inicial_usd <- monto_maquina_usd * 0.10
credito_usd <- monto_maquina_usd - cuota_inicial_usd

# Condiciones del credito (EE.UU.)
tasa_ea_usd <- 0.065
plazo_anos <- 10
n_cuotas <- plazo_anos * 4
tasa_trim <- (1 + tasa_ea_usd)^(1/4) - 1

# Cuota Francesa Trimestral
cuota_trim_usd <- credito_usd * (tasa_trim / (1 - (1 + tasa_trim)^-n_cuotas))

# Tabla de Amortizacion
amort <- data.frame(Periodo = 1:n_cuotas)
saldo <- credito_usd
for(i in 1:n_cuotas){
  interes <- saldo * tasa_trim
  capital <- cuota_trim_usd - interes
  amort$Saldo_Inicial[i] <- saldo
  amort$Cuota_USD[i] <- cuota_trim_usd
  amort$Interes[i] <- interes
  amort$Capital[i] <- capital
  saldo <- saldo - capital
  amort$Saldo_Final[i] <- saldo
}

kable(head(amort, 8), digits = 2, caption = "Amortizacion en Dolares (Primeras 8 cuotas)")
Amortizacion en Dolares (Primeras 8 cuotas)
Periodo Saldo_Inicial Cuota_USD Interes Capital Saldo_Final
1 79746.84 2708.14 1265.45 1442.70 78304.14
2 78304.14 2708.14 1242.55 1465.59 76838.54
3 76838.54 2708.14 1219.30 1488.85 75349.70
4 75349.70 2708.14 1195.67 1512.47 73837.22
5 73837.22 2708.14 1171.67 1536.47 72300.75
6 72300.75 2708.14 1147.29 1560.86 70739.89
7 70739.89 2708.14 1122.52 1585.62 69154.27
8 69154.27 2708.14 1097.36 1610.79 67543.48
amort_cop <- amort %>% 
  mutate(Cuota_COP = Cuota_USD * trm_spot,
         Saldo_COP = Saldo_Final * trm_spot)

kable(head(amort_cop[, c("Periodo", "Cuota_COP", "Saldo_COP")], 8), 
      digits = 0, caption = "Amortizacion Proyectada en COP (TRM fija)")
Amortizacion Proyectada en COP (TRM fija)
Periodo Cuota_COP Saldo_COP
1 10697171 309301338
2 10697171 303512249
3 10697171 297631296
4 10697171 291657023
5 10697171 285587948
6 10697171 279422567
7 10697171 273159353
8 10697171 266796752
set.seed(123)
mu_mensual <- 0.002 # Retorno esperado mensual
sigma_mensual <- 0.04 # Volatilidad mensual
n_meses <- 12

# BMG Normal
sim_normal <- trm_spot * exp(cumsum((mu_mensual - 0.5 * sigma_mensual^2) + sigma_mensual * rnorm(n_meses)))

# BMG T-Student (df = 3 para colas muy pesadas)
sim_t <- trm_spot * exp(cumsum((mu_mensual - 0.5 * sigma_mensual^2) + sigma_mensual * rt(n_meses, df=3)))

df_bmg <- data.frame(Mes = 1:n_meses, Normal = sim_normal, T_Student = sim_t)

ggplot(df_bmg, aes(x=Mes)) +
  geom_line(aes(y=Normal, color="Normal")) +
  geom_line(aes(y=T_Student, color="T-Student")) +
  labs(title="Simulacion BMG de la TRM", y="Precio USD/COP", color="Distribucion") +
  theme_minimal()

# Tasas de interes para Forward (emuladas)
tasa_col <- 0.105
tasa_usa <- 0.0525

# Calculo de Forwards de 1 año para los años 6 a 9
anos_fwd <- 6:9
precios_fwd <- trm_spot * ((1 + tasa_col) / (1 + tasa_usa))^anos_fwd


tabla_fwd <- data.frame(
  Anio = anos_fwd,
  Precio_Forward = round(precios_fwd, 2),
  USD_Asegurados_Anual = round(cuota_trim_usd * 4 * 0.75, 2)
)

kable(tabla_fwd, caption = "Estructura de Cobertura Forward")
Estructura de Cobertura Forward
Anio Precio_Forward USD_Asegurados_Anual
6 5289.79 8124.43
7 5553.65 8124.43
8 5830.67 8124.43
9 6121.51 8124.43