Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

HW5: Investment Simulator Shiny App HW5: Investment Simulator Shiny App Stat 133, Fall 2022 Motivation The purpose of this assignment is to create an investment simulator using a shiny app. In...

1 answer below »
HW5: Investment Simulator Shiny App
HW5: Investment Simulator Shiny App
Stat 133, Fall 2022
Motivation
The purpose of this assignment is to create an investment simulator using a shiny app.
In this assignment—which builds on top of the previous HW—you will have to create a shiny
app, publish it on RStudio shinyapps.io, and record a short video (of 3 mins max) showing
how your app works and how to make sense of the displayed information.
We are assuming that you have gone through the learning materials covered in weeks 8, 9,
and 10.
Please read this document in its entirety before writing any code.
Resources
While working on this HW, you may want to take a look at:
Sample app-investment-simulator.R (companion shiny app for the tutorial investing-
simulations.html):
https:
courses.berkeley.edu/courses/1516876/files/folde
shiny-apps/shiny-investment
Shiny widgets gallery (to see sample code of input widgets):
https:
shiny.rstudio.com/gallery/widget-gallery.html
Shiny cheatsheet:
https:
courses.berkeley.edu/courses/1516876/files/folde
cheatsheets?preview= XXXXXXXXXX
Shiny examples discussed in lecture (see bCourses Files/shiny-apps):
https:
courses.berkeley.edu/courses/1516876/files/folde
shiny-apps
More examples in Shiny gallery (full of sample shiny apps to get inspiration from):
https:
shiny.rstudio.com/gallery
Instructions in the next page
1
https:
courses.berkeley.edu/courses/1516876/files/folde
eadings?preview= XXXXXXXXXX
https:
courses.berkeley.edu/courses/1516876/files/folde
eadings?preview= XXXXXXXXXX
https:
courses.berkeley.edu/courses/1516876/files/folde
shiny-apps/shiny-investment
https:
shiny.rstudio.com/gallery/widget-gallery.html
https:
courses.berkeley.edu/courses/1516876/files/folde
cheatsheets?preview= XXXXXXXXXX
https:
courses.berkeley.edu/courses/1516876/files/folde
shiny-apps
https:
shiny.rstudio.com/gallery
1) Investing in a Total Stock Market index fund
This section aims to provide context about the assignment and therefore is purely informative.
We are going to consider investing in a portfolio consisting of an x% proportion of stocks,
and a y% = 100% − x% proportion of bonds. For instance, a conservative portfolio may have
x = 30% stocks and y = 70% bonds. In turn, an aggressive portfolio may have x = 80%
stocks and y = 20% bonds.
The return and volatility of a given portfolio will depend on its stock-bond composition. The
following table shows the average annual return (µ), and the annual standard deviation o
“volatility” (σ) for various portfolio combinations of stocks and bonds, based on historical
data involving the time period XXXXXXXXXXSource: Portfolio Visualizer.
Stocks Bonds Avg (µ) SD (σ)
0% 100% 5.09% 4.03%
5% 95% 5.40% 3.99%
10% 90% 5.70% 4.11%
15% 85% 6.00% 4.37%
20% 80% 6.29% 4.74%
25% 75% 6.57% 5.21%
30% 70% 6.84% 5.74%
35% 65% 7.11% 6.32%
40% 60% 7.37% 6.94%
45% 55% 7.62% 7.59%
50% 50% 7.87% 8.25%
55% 45% 8.11% 8.94%
60% 40% 8.34% 9.64%
65% 45% 8.56% 10.35%
70% 30% 8.78% 11.07%
75% 25% 8.99% 11.80%
80% 20% 9.19% 12.54%
85% 15% 9.38% 13.28%
90% 10% 9.57% 14.03%
95% 5% 9.74% 14.79%
100% 0% 9.91% 15.55%
1.1) Investing a Lump-sum with periodic contributions
Say you are going to start your investment with a lump-sum of $1000 in a x%-stock and
y%-bond portfolio. Also, suppose you decide to make fixed periodic contributions, e.g. $360
at the end of each year. How much money would you expect to get at the end of a 10-yea
period? Or after 15 years? Or after 20 years? Or after a longer period?
2
1.2) Variable rates of return and volatility
For simulation purposes, we are going to assume that annual rates of return rt follow a
normal distribution with mean µ and standard deviation σ based on the table given in
the preceding page.
For example, annual rates of return for a 60%-40% portfolio have a Normal distribution:
60-40 portfolio =⇒ rt ∼ N(µ = 0.0834, σ = 0.0964)
In contrast, annual rates of return for a 80%-20% portfolio have a Normal distribution:
80-20 portfolio =⇒ rt ∼ N(µ = 0.0919, σ = 0.1254)
2) Simulation with variable rates of return
This section describes contextual information that will guide you in coding your shiny app.
Let’s
ing back the investing scenario with an initial amount of $1000, making contributions
of $360 at the end of every year, during a 10-year period, for a 60%-40% portfolio, which
has annual rates of return rt normally distributed as N(µ = 0.0834, σ = 0.0964)
Here’s a conceptual diagram illustrating the flow of the investment during the first three
years, assuming annual contributions of $360:
1000
0 1
XXXXXXXXXXr XXXXXXXXXX = amt1
year = 1
Annual return year 1
1 <- rnorm(1, μ, σ) Initial amount
amt0 Amount at the end of 1st yea
amt1 (amt XXXXXXXXXXr XXXXXXXXXX = amt2
amt2
Annual return year 2
2 <- rnorm(1, μ, σ)
1 2
year = 2
2 3
year = 3
(amt XXXXXXXXXXr XXXXXXXXXX = amt3
Annual return year 3
3 <- rnorm(1, μ, σ)
At the end of every year
you contribute 360
Amount at the end of 2nd yea
Amount at the end of 3rd yea
… ETC
3
3) Reaching a Target Amount
To make things more interesting, say you have a goal of reaching a target amount of $5000.
One important question to consider is:
How much time (on average) would it take you to reach this target?
To answer this question, we could run various simulations—say 100 simulations—and then
visualize their timelines (see graphic below). Moreover, doing some calculations on the set
of 100 simulations, it turns out that the typical amount of time—in terms of the average
number of years—to reach the target would be 6.84 years.
annotation
6.840
5000
10000
15000
20000
XXXXXXXXXX
x−axis label
y−
ax
is
la
e
l
Descriptive subtitle
Catchy Title
BTW: This graph is just for illustration purposes, and there is plenty of room to be visually
improved. We expect your graphic to look much better.
4
4) Probability of Reaching Target
In addition to computing the average amount of years that it would take you to reach you
target, it would also be nice to take advantage of all the obtained simulations and find the
empirical proportion of reaching the target, for every possible year.
In other words, the idea is to obtain:
• at the end of year 1, what is the proportion of simulations that reach a balance greate
than or equal to 5000,
• at the end of year 2, what is the proportion of simulations that reach a balance greate
than or equal to 5000,
• at the end of year 3, what is the proportion of simulations that reach a balance greate
than or equal to 5000,
• and so on, and so for . . .
With these proportions (i.e. empirical probabilities), we could obtain the following figure:
0.00
0.25
0.50
0.75
1.00
XXXXXXXXXX
yea
p
o
a
ili
ty
Probability of reaching target
BTW: This graph is just for illustration purposes, and there is plenty of room to be visually
improved. We expect your graphic to look much better.
5) Shiny App
The main goal of this assignment is to create a shiny app to run simulations, and to display
the output of what could happen when investing in a total market index fund (or some othe
financial tool).
5
Your app should have a layout like the following diagram (see specifications below). You can
find a template R script file app-template.R in the folder containing this pdf of instructions
(bCourses).
Title of your App
1) Initial Amount
2) Periodic
Contributions
3) Target Amount
4) Number of Years
5) Portfolio
Composition (e.g.
proportion of stocks)
Suggested Columns for input widgets
6) Number of simulations
7) Random seed
You can include additional widgets if you want
Plot: Simulated Timelines
Here goes plot 1
Plot: Probability of Reaching Target
Here goes plot 2
Summary statistics to help in the
interpretation of the simulated results
Stats
As you can tell from the above diagram, the layout of the app involves five distinctive
sections—see template file app.R:
• title: main title for your app (give it a meaningful name)
• input widgets: widgets a
anged in four columns
• plot1: an output graph to display the timelines of yearly balances
• plot2: an output graph to display the probability of reaching the target
• stats: an output area (e.g. for a table, text, etc) to display numeric summaries.
6) Submission
1) R file: You will have to submit your app.R file (do NOT confuse with an Rmd file)
containing the code of your app.
2) Link of published app: You will also have to submit the link of your published app
in shinyapps.io (the free version). This publication process is fairly straightforward,
6
https:
www.shinyapps.io
and you can watch Ga
et Grolemund’s video on how to do this: https:
vimeo.com
s
tudioinc
eview/ XXXXXXXXXX/212d8a5a7a/#t=30m35s. Share the link with us in the
comments section of the submission in bCourses.
3) Video: In addition to the app.R file and the link of your published app, you will also
have to record a video—maximum length of 3 minutes—in which you show us you
published shiny app, how to use it, and a description of its outputs. Share the public
link with us in the comments section of the submission in bCourses.
Make sure that the video does not exceed 3-minutes, that its resolution is okay, without too
much background noise, avoiding very low volume or inaudible audio. Above all, record a
video in which both your screen and your face are captured. We want to see your app,
and we also want to see your face.
3a) Ideas for your video. You can tell us:
• How changing the composition of the portfolio (say from 60-40% to 80%-20%) affects
the number of years needed to reach a target amount.
• How increasing or decreasing the annual contribution affects the number of years needed
to reach a target amount.
• What could be some of the (main) limitations when trying to use this calculator in
“real” life?
4) Important: You do NOT have to submit any Rmd or html files this time. Also,
we will not accept any content sent by email. We will only grade the app.R
file submitted to bCourses, the public link of the video, and the link of your app in
shinyapps.io.
7
https:
vimeo.com
studioinc
eview/ XXXXXXXXXX/212d8a5a7a/#t=30m35s
https:
vimeo.com
studioinc
eview/ XXXXXXXXXX/212d8a5a7a/#t=30m35s
    Motivation
    Resources
    1) Investing in a Total Stock Market index fund
    1.1) Investing a Lump-sum with periodic contributions
    1.2) Variable rates of return and volatility
    2) Simulation with variable rates of return
    3) Reaching a Target Amount
    4) Probability of Reaching Target
    5) Shiny App
    6) Submission
Answered 4 days After Nov 08, 2022

Solution

Subhanbasha answered on Nov 12 2022
55 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here