renderPlot({
# Captura os valores dos inputs
<- input$mu1
mu1 <- input$mu2
mu2 <- input$rf
rf <- input$sigma1
sigma1 <- input$sigma2
sigma2 <- input$rho
rho <- input$incluir_rf
incluir_rf
if (!incluir_rf) {
# Caso: apenas ativos de risco
<- seq(0, 1, length.out = 200)
w1 <- 1 - w1
w2 <- w1 * mu1 + w2 * mu2
retorno_port <- sqrt( w1^2 * sigma1^2 +
desvio_port ^2 * sigma2^2 +
w22 * w1 * w2 * rho * sigma1 * sigma2 )
<- data.frame(Desvio = desvio_port, Retorno = retorno_port)
df ggplot(df, aes(x = Desvio, y = Retorno)) +
geom_line(color = "blue", size = 1) +
labs(title = "Fronteira Eficiente - Apenas Ações",
x = "Desvio Padrão do Portfólio",
y = "Retorno Esperado do Portfólio") +
theme_minimal()
else {
} # Caso: incluindo a taxa livre de risco
<- matrix(c(sigma1^2, rho * sigma1 * sigma2,
Sigma * sigma1 * sigma2, sigma2^2), nrow = 2)
rho <- c(mu1, mu2)
mu_risky <- mu_risky - rf
A <- solve(Sigma)
invSigma <- invSigma %*% A
w_t <- as.numeric(w_t / sum(w_t))
w_t
<- sum(w_t * mu_risky)
mu_t <- sqrt( t(w_t) %*% Sigma %*% w_t )
sigma_t
<- seq(0, 2, length.out = 200) # permitindo alavancagem
lambda <- lambda * sigma_t
desvio_port <- rf + lambda * (mu_t - rf)
retorno_port
<- data.frame(Desvio = desvio_port, Retorno = retorno_port)
df ggplot(df, aes(x = Desvio, y = Retorno)) +
geom_line(color = "red", size = 1) +
labs(title = "Fronteira Eficiente com Taxa Livre de Risco (CML)",
x = "Desvio Padrão do Portfólio",
y = "Retorno Esperado do Portfólio") +
theme_minimal() +
geom_point(aes(x = sigma_t, y = mu_t), color = "blue", size = 3) +
annotate("text", x = sigma_t, y = mu_t,
label = "Portfólio Tangência", vjust = -1)
} })
Fronteira Eficiente com 3 Ativos
Fronteira Eficiente
A seguir, definimos o gráfico que reage aos inputs definidos acima: