# Load packages
# Core
library(tidyverse)
library(tidyquant)
Revise the code below.
symbols <- c("TSLA", "NVDA")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2022-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
Revise the code for weights.
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "NVDA" "TSLA"
# weights
weights <- c(.5, .5)
weights
## [1] 0.5 0.5
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 2 × 2
## symbols weights
## <chr> <dbl>
## 1 NVDA 0.5
## 2 TSLA 0.5
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
portfolio_returns_tbl
## # A tibble: 120 × 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0510
## 2 2013-02-28 -0.0180
## 3 2013-03-28 0.0488
## 4 2013-04-30 0.212
## 5 2013-05-31 0.324
## 6 2013-06-28 0.0318
## 7 2013-07-31 0.126
## 8 2013-08-30 0.128
## 9 2013-09-30 0.0941
## 10 2013-10-31 -0.107
## # ℹ 110 more rows
# Get mean portfolio return
mean_port_return <- mean(portfolio_returns_tbl$returns)
mean_port_return
## [1] 0.03309617
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.1197002
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
## [1] 0.0121009486 0.0385052280 -0.0486138033 0.1023147909 0.0926811014
## [6] -0.1531046117 -0.0769370244 0.1580504046 -0.1360817985 -0.0155435225
## [11] 0.1028239077 0.0190930871 -0.0417803847 -0.0423639926 -0.0741876420
## [16] 0.0499900714 0.2397571491 -0.0766777459 -0.0744361013 -0.0733198332
## [21] -0.0108040126 0.0204537374 0.1105996571 0.0151384377 0.0228164095
## [26] 0.3852834782 0.1030624747 0.0395193864 0.0454272566 0.2207674497
## [31] -0.0570617917 -0.0391132609 0.0333568377 0.1280347387 0.1122126289
## [36] -0.0138809237 0.2486426202 0.1932262431 -0.1052431132 0.2087547904
## [41] -0.1180380647 0.0225728318 0.1617028108 0.0093054089 -0.0529227688
## [46] 0.1481675405 0.0888987450 0.0702070286 0.0430424134 0.1556633751
## [51] -0.0349931976 0.0354986087 0.0607220873 0.0147957911 0.1613912483
## [56] -0.1980656443 0.1241887672 0.0048531457 -0.1419174163 -0.0409615168
## [61] -0.2050350009 0.0236318132 -0.1801024470 -0.0875895165 0.0007395882
## [66] 0.0445593239 0.0295619310 0.0999100468 -0.0205118207 -0.2121014105
## [71] 0.1505974382 0.0168722487 0.0360228933 -0.2545378955 0.0043182492
## [76] 0.0787473766 0.0015650362 0.2112089765 0.1149850907 -0.0307229845
## [81] -0.2399386884 0.1281786213 0.0085327060 0.0548769779 -0.1137583059
## [86] 0.1049855547 0.0437969731 0.0060684593 -0.0357322507 0.1449763015
## [91] -0.0095987846 -0.1633927881 -0.0145057500 0.0973578176 0.1169910837
## [96] -0.0646393338 0.1370102529 0.1801869921 0.1420217946 -0.0508087352
## [101] 0.0059829773 0.1787724862 0.1183882838 -0.0882382557 0.1969303888
## [106] 0.2360270832 -0.0634996410 -0.1223286710 0.0766138938 0.0367563837
## [111] -0.0657707116 0.1958253452 0.0738978903 0.2732319385 0.0329605070
## [116] 0.1698826785 0.0081022582 0.2560584815 0.1319714192 -0.0041689927
# Add a dollar
simulated_returns_add_1 <- tibble(returns = c(1, 1 + simulated_monthly_returns))
simulated_returns_add_1
## # A tibble: 121 × 1
## returns
## <dbl>
## 1 1
## 2 1.01
## 3 1.04
## 4 0.951
## 5 1.10
## 6 1.09
## 7 0.847
## 8 0.923
## 9 1.16
## 10 0.864
## # ℹ 111 more rows
# Calculate the cumulative growth of a dollar
simulated_growth <- simulated_returns_add_1 %>%
mutate(growth = accumulate(returns, function(x, y) x*y)) %>%
select(growth)
simulated_growth
## # A tibble: 121 × 1
## growth
## <dbl>
## 1 1
## 2 1.01
## 3 1.05
## 4 1.00
## 5 1.10
## 6 1.20
## 7 1.02
## 8 0.942
## 9 1.09
## 10 0.942
## # ℹ 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 38.69011
No need
Based on the Monte Carlo simulation results, how much should you expect from your $100 investment after 20 years? What is the best-case scenario? What is the worst-case scenario? What are limitations of this simulation analysis?
The best case scenario according to the graph would be that and investment of $100 can increase in 20 years. The worst case scenario shown in the graph could be the investment of 100 dollars could decrease using outside resources you can tell this depends on the job market.