#############################################################
## NOMBRE FABIAN ARLEY CHACON GONZALEZ ##
#############################################################
# Historico del mercado COLCAP
library(xts)
## Cargando paquete requerido: zoo
##
## Adjuntando el paquete: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(readxl)
### ELiminar serie
rm(DATOS)
## Warning in rm(DATOS): objeto 'DATOS' no encontrado
DATOS <- read_excel("C:/Users/fabia/OneDrive/Escritorio/2024-1/RIESGOS FINANCIEROS/DATOS.xlsx",
sheet = "DATOS")
ECOPETROL <- read_excel("C:/Users/fabia/OneDrive/Escritorio/2024-1/RIESGOS FINANCIEROS/DATOS.xlsx",
sheet = "ECOPETROL")
## New names:
## • `` -> `...11`
BANCOLOMBIA <- read_excel("C:/Users/fabia/OneDrive/Escritorio/2024-1/RIESGOS FINANCIEROS/DATOS.xlsx",
sheet = "BANCOLOMBIA")
## New names:
## • `` -> `...11`
####
attach(BANCOLOMBIA)
attach(DATOS)
## The following object is masked from BANCOLOMBIA:
##
## Fecha
names(DATOS)
## [1] "Fecha" "Valor_hoy" "Valor_ayer"
## [4] "Variación_absoluta" "Variación_porcentual" "Variación12meses"
## [7] "Variaciónaño"
attach(ECOPETROL)
## The following objects are masked from BANCOLOMBIA:
##
## ...11, Cantidad, Nemotécnico, Precio cierre, Precio máximo, Precio
## mínimo, Precio promedio ponderado, Variación absoluta, Variación
## porcentual, Volumen
###################################################################
########### PUNTO 1 #########
###################################################################
### Grafica Indice MSI
plot(DATOS$Fecha,DATOS$Valor_hoy,type = "l",
ylab = "Indice COLCAP"
,xlab = "Fecha"
,main = "Valor hoy MSI COLCAP 2021-2023")
grid(col = "gray")

###################################################################
########### PUNTO 2 #########
###################################################################
### Grafica Ecopetrol
plot(ECOPETROL$EC,ECOPETROL$`Precio cierre`,type = "l",
ylab = "Precio de cierre"
,xlab = "Fecha"
,main = "Precio de cierre Ecopetrol 2021-2023")
lines(ECOPETROL$EC, ECOPETROL$`Precio mínimo`, col = "red")
lines(ECOPETROL$EC, ECOPETROL$`Precio máximo`, col = "blue")
legend("topright", legend = c("Precio cierre", "Precio mínimo", "Precio máximo"),
col = c("black", "red", "blue"), lty = 1, cex = 0.6)
grid(col = "gray")

### Grafica Bancolombia
plot(BANCOLOMBIA$Fecha,BANCOLOMBIA$`Precio cierre`,type = "l",
ylab = "Precio de cierre"
,xlab = "Fecha"
,main = "Precio de cierre Bancolombia 2021-2023")
lines(BANCOLOMBIA$Fecha, BANCOLOMBIA$`Precio mínimo`, col = "red")
lines(BANCOLOMBIA$Fecha, BANCOLOMBIA$`Precio máximo`, col = "blue")
legend("topright", legend = c("Precio cierre", "Precio mínimo", "Precio máximo"),
col = c("black", "red", "blue"), lty = 1, cex = 0.6)
grid(col = "gray")

###################################################################
########### PUNTO 3 #########
###################################################################
######### CAPM ######
CAMPMECO=lm(DATOS$Variación_porcentual~ECOPETROL$`Variación porcentual`)
summary(CAMPMECO)
##
## Call:
## lm(formula = DATOS$Variación_porcentual ~ ECOPETROL$`Variación porcentual`)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.3433 -0.4632 -0.0586 0.4623 5.0585
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.02815 0.03290 -0.856 0.392
## ECOPETROL$`Variación porcentual` 0.32933 0.01366 24.109 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.89 on 730 degrees of freedom
## Multiple R-squared: 0.4433, Adjusted R-squared: 0.4425
## F-statistic: 581.3 on 1 and 730 DF, p-value: < 2.2e-16
plot(DATOS$Variación_porcentual,ECOPETROL$`Variación porcentual`
,main = "COLCAP - BANCOLOMBIA",
xlab = "Rendimientos del COLCAP",
ylab="Rendimeintos de ECOPETROL")
grid(col="gray")
abline(CAMPMECO , col="red")
abline(h=0 ,lwd=0.4)
abline(v=0 ,lwd=0.4)

####################################################################
CAMPBANCO=lm(DATOS$Variación_porcentual~BANCOLOMBIA$`Variación porcentual`)
summary(CAMPBANCO)
##
## Call:
## lm(formula = DATOS$Variación_porcentual ~ BANCOLOMBIA$`Variación porcentual`)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.7158 -0.4576 0.0069 0.4813 4.6099
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.02367 0.03228 -0.733 0.464
## BANCOLOMBIA$`Variación porcentual` 0.34367 0.01368 25.131 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8734 on 730 degrees of freedom
## Multiple R-squared: 0.4639, Adjusted R-squared: 0.4631
## F-statistic: 631.6 on 1 and 730 DF, p-value: < 2.2e-16
plot(DATOS$Variación_porcentual,BANCOLOMBIA$`Variación porcentual`
,main = "COLCAP - BANCOLOMBIA",
xlab = "Rendimientos del COLCAP",
ylab="Rendimeintos de BANCOLOMBIA")
grid(col="gray")
abline(CAMPBANCO , col="red")
abline(h=0 ,lwd=0.4)
abline(v=0 ,lwd=0.4)

###################################################################
########### PUNTO 4 #########
###################################################################
######### GRAFICAS DE PRECIOS NORMALIZADOS ######
ECOPETROL$`Precio cierre normalizado` <- ECOPETROL$`Precio cierre` / ECOPETROL$`Precio cierre`[1]
BANCOLOMBIA$`Precio cierre normalizado` <- BANCOLOMBIA$`Precio cierre` / BANCOLOMBIA$`Precio cierre`[1]
DATOS$`Valor_hoy_normalizado` <- DATOS$Valor_hoy / DATOS$Valor_hoy[1]
plot(DATOS$Fecha, ECOPETROL$`Precio cierre normalizado`, type = "l",
ylab = "Precios normalizados",
xlab = "Fecha",
main = "Valor del COLCAP y precios normalizados 2021-2023",
col="red",
ylim =c(0.7,1.7) )
grid(col = "gray")
lines(DATOS$Fecha, DATOS$Valor_hoy_normalizado, col = "black")
lines(BANCOLOMBIA$Fecha, BANCOLOMBIA$`Precio cierre normalizado`, col = "blue")
legend("topright", legend = c("COLCAP", "Ecopetrol", "Bancolombia"),
col = c("black", "red", "blue"), lty = 1, cex = 0.6)

## Solamente para las dos acciones
plot(DATOS$Fecha, ECOPETROL$`Precio cierre normalizado`, type = "l",
ylab = "Precios normalizados",
xlab = "Fecha",
main = "Precios normalizados 2021-2023",
col="red",
ylim =c(0.7,1.7) )
grid(col = "gray")
lines(BANCOLOMBIA$Fecha, BANCOLOMBIA$`Precio cierre normalizado`, col = "blue")
legend("topright", legend = c( "Ecopetrol", "Bancolombia"),
col = c("red", "blue"), lty = 1, cex = 0.6)

####################################################################
################## OPCIONAL #####################
####################################################################
# otra manera de normalizar el precio con max y minimos de acda vairbale
normalize_min_max <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
normalizadaCOLCAP=normalize_min_max(DATOS$Valor_hoy)
normalizadaECOP=normalize_min_max(ECOPETROL$`Precio cierre`)
normalizadaBANC=normalize_min_max(BANCOLOMBIA$`Precio cierre`)
plot(BANCOLOMBIA$Fecha,normalizadaBANC,type = "l",col="red",
main = "Precios Normalizados entre 0 - 1",
ylab = "Precios normalizados",
xlab="Fecha")
lines(BANCOLOMBIA$Fecha,normalizadaECOP,type = "l",col="blue")
lines(BANCOLOMBIA$Fecha,normalizadaCOLCAP,type = "l")

###################################################################
########### PUNTO 5 #########
###################################################################
riesgo_total_bancolombia<-sd(BANCOLOMBIA$`Precio cierre normalizado`)
riesgo_total_bancolombia
## [1] 0.1377766
riesgo_total_bancolombia=riesgo_total_bancolombia*100
riesgo_total_bancolombia
## [1] 13.77766
beta1=0.34367
riesgo_de_mercado<-sd(DATOS$Valor_hoy_normalizado)
riesgo_de_mercado
## [1] 0.09827678
#riesgo sistemico
riesgo_sistemico_bancolombia<-beta1*riesgo_de_mercado
riesgo_sistemico_bancolombia
## [1] 0.03377478
riesgo_sistemico_bancolombia=riesgo_sistemico_bancolombia*100
riesgo_sistemico_bancolombia
## [1] 3.377478
# El riesgo sistemico es de 3.37 % precio normalizado
#riesgo ideosincratico
riesgo_ideosincratico_des_banco<-riesgo_total_bancolombia**2-riesgo_sistemico_bancolombia**2
riesgo_ideosincratico<-sqrt(riesgo_ideosincratico_des_banco)
riesgo_ideosincratico
## [1] 13.35726
#Bancolombia tiene un riesgo ideosincratico de 13.35%
##########################################################################
riesgo_total_ecopetrol<-sd(ECOPETROL$`Precio cierre normalizado`)
riesgo_total_ecopetrol
## [1] 0.1505839
riesgo_total_ecopetrol=riesgo_total_ecopetrol*100
riesgo_total_ecopetrol
## [1] 15.05839
beta2=0.32933
#riesgo sistemico
riesgo_sistemico_ecopetrol<-beta2*riesgo_de_mercado
riesgo_sistemico_ecopetrol
## [1] 0.03236549
riesgo_sistemico_ecopetrol=riesgo_sistemico_ecopetrol*100
riesgo_sistemico_ecopetrol
## [1] 3.236549
# El riesgo sistemico es de 3.236549% precio normalizado
#riesgo ideosincratico
riesgo_ideosincratico_des_ecopetrol<-riesgo_total_ecopetrol**2-riesgo_sistemico_ecopetrol**2
riesgo_ideosincratico_ecopetrol<-sqrt(riesgo_ideosincratico_des_ecopetrol)
riesgo_ideosincratico_ecopetrol
## [1] 14.70646
#Ecopetrol tiene un riesgo ideosincratico de 14.706468%
#Entre 2021 y 2023, las acciones de ECOPETROL mostraron
#mayor riesgo idiosincrático debido a la volatilidad de
#los precios del petróleo y factores globales. En contraste,
#Bancolombia presentó mayor estabilidad, influenciada principalmente
#por la economía local y las tasas de interés, destacándose frente al COLCAP.
# install.packages("ggplot2")
library(ggplot2)
riesgos1 <- data.frame( tipo_riesgo = c("Riesgo Idiosincrático", "Riesgo Sistémico", "Riesgo Total"),
valor = c(riesgo_ideosincratico,riesgo_sistemico_bancolombia,riesgo_total_bancolombia))
# Crear el gráfico de barras
ggplot(data = riesgos1, aes(x = tipo_riesgo, y = valor, fill = tipo_riesgo)) +
geom_bar(stat = "identity") +
labs(title = "Riesgos de BANCOLOMBIA", x = "Tipo de Riesgo", y = "Valor") +
theme_minimal() +
scale_fill_manual(values = c("Riesgo Idiosincrático" = "blue", "Riesgo Sistémico" = "green", "Riesgo Total" = "red"))

riesgos <- data.frame( tipo_riesgo = c("Riesgo Idiosincrático", "Riesgo Sistémico", "Riesgo Total"),
valor = c(riesgo_ideosincratico_ecopetrol, riesgo_sistemico_ecopetrol, riesgo_total_ecopetrol))
# Crear el gráfico de barras
ggplot(data = riesgos, aes(x = tipo_riesgo, y = valor, fill = tipo_riesgo)) +
geom_bar(stat = "identity") +
labs(title = "Riesgos de ECOPETROL", x = "Tipo de Riesgo", y = "Valor") +
theme_minimal() +
scale_fill_manual(values = c("Riesgo Idiosincrático" = "blue", "Riesgo Sistémico" = "green", "Riesgo Total" = "red"))
