#### QUANDL API
# Step to create your key
# 1. Go to https://www.quandl.com/
# 2. Create an account
# 3. Get your key
# 4. Add your key below (I can't give you mine)
library(yfR)
library(ggplot2)
library(GetQuandlData)
library(PerformanceAnalytics)
library(ggthemes)
library(roll)
library(readxl)
library(tidyr)
library(dplyr)
<- "Your QUANDL API KEY here"
api_KEY #Ibov
<-'^BVSP'
stock<-'1995-01-01'
start<-Sys.Date()
end<- yf_get(tickers = stock,
ibov first_date = start,
last_date = end)
<- ibov[order(as.numeric(ibov$ref_date)),]
ibov# Cumulative return Ibov
$Ibov_return <- ibov$cumret_adjusted_prices -1
ibov#Selic
<- get_Quandl_series(id_in = c('Selic' = 'BCB/11'),
selic api_key = api_key,
first_date = start,
last_date = end)
<- selic[order(as.numeric(selic$ref_date)),]
selic$value <- selic$value / 100
selic# Cumulative return Selic
<- data.frame(nrow(selic):1)
return_selic colnames(return_selic)<- "selic_return"
for(i in (2:nrow(selic))) {
1] <- Return.cumulative( selic$value[1:i] )
return_selic[i,
}# CDI
<- get_Quandl_series(id_in = c('CDI' = 'BCB/12'),
cdi api_key = api_key ,
first_date = start ,
last_date = end)
<- cdi[order(as.numeric(cdi$ref_date)),]
cdi$value <- cdi$value / 100
cdi# Cumulative return CDI
<- data.frame(nrow(cdi):1)
return_CDI colnames(return_CDI)<- "CDI_return"
for(i in (2:nrow(cdi))) {
1] <- Return.cumulative( cdi$value[1:i] )
return_CDI[i,
}#Inflation Brazil
<- get_Quandl_series(id_in = c('Inflation' = 'BCB/433'),
inf api_key = api_key,
first_date = start,
last_date = end)
<- inf[order(as.numeric(inf$ref_date)),]
inf$value <- inf$value / 100
inf
# Cumulative return Inflation
<- data.frame(nrow(inf):1)
return_Inf colnames(return_Inf)<- "Inflation_return"
for(i in (2:nrow(inf))) {
1] <- Return.cumulative( inf$value[1:i] )
return_Inf[i,
}# Merging dataframes
<- cbind(selic, return_selic)
selic <- cbind(cdi, return_CDI)
cdi <- cbind(inf, return_Inf)
inf
<- merge(cdi ,selic, by=c("ref_date"))
df <- merge(df ,ibov, by=c("ref_date"))
df <- merge(df ,inf, by=c("ref_date"))
df2
$selic_return[1] <- NA
df$CDI_return[1] <- NA
df$Ibov_return[1] <- NA
df$Inflation_return[1] <- NA
df2
# Graph cumulated return CDI and IBOV
ggplot(df, aes(ref_date)) +
geom_line(aes(y = CDI_return, color = "CDI")) +
geom_line(aes(y = Ibov_return, colour = "Ibov")) +
geom_line(aes(y = selic_return, colour = "Selic")) +
labs(y='Cumulative return (daily)') + theme_solarized()
3 Risk and Return
This is a book containing all class notes.
Here are some graphs about the evolution of prices through time.
3.1 Insights Ibov
3.1.1 Ibov vs CDI vs Selic in Brazil
3.1.2 Ibov vs CDI Vs. Selic vs. Inflation in Brazil
# Graph cumulated return CDI, IBOV, and Inflation
ggplot(df2, aes(ref_date)) +
geom_line(aes(y = CDI_return, colour = "CDI")) +
geom_line(aes(y = selic_return, colour = "Selic")) +
geom_line(aes(y = Inflation_return, colour = "Inflation")) +
geom_line(aes(y = Ibov_return, colour = "Ibov")) +
labs( y='Cumulative return (monthly)') + theme_solarized()
3.1.3 Ibov vs CDI vs Selic in Brazil 2002-2007
# Graph cumulated return CDI and IBOV
#| label: insights_4
ggplot(df, aes(ref_date)) +
geom_line(aes(y = CDI_return, colour = "CDI")) +
geom_line(aes(y = Ibov_return, colour = "Ibov")) +
geom_line(aes(y = selic_return, colour = "Selic")) +
labs(y='Cumulative return (daily)') + theme_solarized()
3.1.4 Ibov vs CDI vs Selic in Brazil 2010-2019
3.1.5 Closing prices
ggplot(ibov,aes(ibov$ref_date, ibov$price_close))+
geom_line(color='#801e00') +
labs(x = "",
y='Closing prices',
title="IBOV") + theme_solarized()
3.1.6 Daily returns
Daily returns vary a lot.
3.1.7 Histogram
The returns seems to follow a normal distribution.
ggplot(ibov,aes(ibov$ret_closing_prices))+
geom_histogram(color='#006600',
bins = 100) +
labs(x = "",
y='Daily return',
title="IBOV") + theme_solarized()
3.1.8 Standard deviation through time
$sd <- roll_sd(ibov$ret_closing_prices,
ibovwidth = 100,
min_obs = 100 ) * sqrt(250)
ggplot(ibov,aes(ref_date,sd))+
geom_line(color='#006600') +
labs(x = "",
y='Standard deviation using 100 daily returns',
title="IBOV") + theme_solarized()
3.2 Insights stocks
3.2.1 Histogram two assets
See below that some stocks have heavier tails than others. The stock with the heavier tail is riskier.
<-c('GOLL4.SA', 'ITUB3.SA')
stocks <-'2012-01-01'
start <-Sys.Date()
end
<- yf_get(tickers = stocks,
data first_date = start,
last_date = end)
<-data[complete.cases(data),]
data
ggplot(data, aes(ret_closing_prices, fill = ticker)) +
geom_histogram(bins = 100, alpha = 0.35, position='identity') +
labs(x = "",
y='Daily returns',
title="Gol and Itub",
subtitle = start)+
xlim(-0.05,0.05) + theme_solarized()
3.2.2 Tradeoff risk & return
There are always outliers for a particular time horizon. Also, Brazilian stocks have higher average standard deviation than Berk & Demarzo’s Figure 10.7.
<-c('^BVSP', 'ITUB3.SA', 'WEGE3.SA', 'PETR3.SA', 'VALE3.SA', 'ITSA3.SA' , 'BBDC3.SA', 'BBAS3.SA', 'ABEV3.SA', 'GGBR4.SA', 'LREN3.SA', 'B3SA3.SA', 'CSNA3.SA', 'EMBR3.SA', 'VIVO4.SA', 'CCRO3.SA', 'UGPA3.SA', 'CSMG3.SA', 'HYPE3.SA')
stocks <-'2010-01-01'
start <-'2023-01-01'
end <- yf_get(tickers = stocks,
data first_date = start,
last_date = end,
freq_data = "yearly",)
<-data[complete.cases(data),]
data<- data %>%
mean_sd group_by(ticker) %>%
summarise_at(vars(ret_adjusted_prices),
list(mean = mean,
sd = sd)) %>%
as.data.frame()
ggplot(mean_sd, aes(x=sd, y=mean, color = ticker)) +
geom_point(size = 4) +
labs(x = "Annualized average standard deviation",
y='Annualized average return',
title="Historical return vs. Historical volatility (selected stocks)") + theme_solarized()
3.2.3 Tradeoff risk & return (Ibov)
<-'2010-01-01'
start <-'2023-01-01'
end <- yf_collection_get("IBOV",
data first_date = start,
last_date = end,
freq_data = "yearly",)
<-data[complete.cases(data),]
data# Get mean & standard deviation by ticker
<- data %>%
mean_sd group_by(ticker) %>%
summarise_at(vars(ret_adjusted_prices),
list(mean = mean,
sd = sd)) %>%
as.data.frame()
ggplot(mean_sd, aes(x=sd, y=mean)) +
geom_point(size = 2) +
labs(x = "Average standard deviation of annual returns",
y='Annualized average return',
title="Historical return vs. Sd (All Ibov 2010-2022)") + theme_solarized()
3.3 Insights ESG
3.3.1 Tradeoff risk & return (by ESG level)
The graph below shows the trade-off risk and return by ESG score levels. I can’t see a clear pattern here.
Let’s exclude the intermediate group to see if there is a more clear pattern.
Let’s now create the same graph using only the E and the S pillars. Here you have the E Pillar.
Now you have the S Pillar.