# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize expected returns and risk to make it easier to compare the performance of multiple assets and portfolios.
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbols <- c("NXPI", "IFX.DE", "AMD")
prices <- tq_get(
x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31"
)
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(
select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
type = "arithmetic",
col_rename = "returns"
) %>%
slice(-1) %>%
ungroup() %>%
rename(asset = symbol)
w_tbl <- tibble(
symbols = c("NXPI", "IFX.DE", "AMD"),
weights = c(0.40, 0.40, 0.20)
)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 NXPI 0.4
## 2 IFX.DE 0.4
## 3 AMD 0.2
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(
assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters"
)
portfolio_returns_tbl
## # A tibble: 23 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0728
## 2 2013-06-28 0.147
## 3 2013-09-30 0.127
## 4 2013-12-30 0.0197
## 5 2013-12-31 0.0950
## 6 2014-03-31 0.173
## 7 2014-06-30 0.0807
## 8 2014-09-30 -0.0646
## 9 2014-12-30 0.0318
## 10 2014-12-31 0.00309
## # ℹ 13 more rows
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round(Stdev, 4)) %>%
pull(tq_sd)
portfolio_sd_tidyquant_builtin_percent
## [1] 0.1002
# mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <-
mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.07564489
# expected returns vs risk
sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
# add portfolio sd
add_row(tibble(asset = "portfolio",
Mean = portfolio_mean_tidyquant_builtin_percent,
Stdev = portfolio_sd_tidyquant_builtin_percent))
sd_mean_tbl
## # A tibble: 4 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 AMD 0.116 0.327
## 2 IFX.DE 0.0812 0.120
## 3 NXPI 0.0843 0.126
## 4 portfolio 0.0756 0.100
How should you expect your portfolio to perform relative to its assets in the portfolio? Would you invest all your money in any of the individual stocks instead of the portfolio? Discuss both in terms of expected return and risk.
–My portfolio’s average return is 7.56%, with a standard deviation of 10.02%. It has a lower average return than all three individual stocks, but also lower risk. I would choose the portfolio because diversification reduces risk. AMD has the highest average return at 11.6%, but its 32.7% standard deviation shows much greater volatility.