HW5: Investment Simulator Shiny AppHW5: Investment Simulator Shiny AppStat 133, Fall 2022MotivationThe purpose of this assignment is to create an investment simulator using a shiny app.In...

1 answer below »
Statistics assignment using R and shiny app


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://bcourses.berkeley.edu/courses/1516876/files/folder/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://bcourses.berkeley.edu/courses/1516876/files/folder/cheatsheets?preview=83674918 Shiny examples discussed in lecture (see bCourses Files/shiny-apps): https://bcourses.berkeley.edu/courses/1516876/files/folder/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://bcourses.berkeley.edu/courses/1516876/files/folder/readings?preview=84322485 https://bcourses.berkeley.edu/courses/1516876/files/folder/readings?preview=84322485 https://bcourses.berkeley.edu/courses/1516876/files/folder/shiny-apps/shiny-investment https://shiny.rstudio.com/gallery/widget-gallery.html https://bcourses.berkeley.edu/courses/1516876/files/folder/cheatsheets?preview=83674918 https://bcourses.berkeley.edu/courses/1516876/files/folder/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 or “volatility” (σ) for various portfolio combinations of stocks and bonds, based on historical data involving the time period 1987-2021. Source: 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-year 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 bring 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 1000(1 + r1) + 360 = amt1 year = 1 Annual return year 1 r1 <- rnorm(1,="" μ,="" σ)="" initial="" amount="" amt0="" amount="" at="" the="" end="" of="" 1st="" year="" amt1="" (amt1)="" (1="" +="" r2)="" +="" 360="amt2" amt2="" annual="" return="" year="" 2="" r2=""><- rnorm(1,="" μ,="" σ)="" 1="" 2="" year="2" 2="" 3="" year="3" (amt2)="" (1="" +="" r3)="" +="" 360="amt3" annual="" return="" year="" 3="" r3=""><- rnorm(1, μ, σ) at the end of every year you contribute 360 amount at the end of 2nd year amount at the end of 3rd year … 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 0.0 2.5 5.0 7.5 10.0 x−axis label y− ax is la be 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 your 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 greater than or equal to 5000, • at the end of year 2, what is the proportion of simulations that reach a balance greater than or equal to 5000, • at the end of year 3, what is the proportion of simulations that reach a balance greater 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 0.0 2.5 5.0 7.5 10.0 year pr ob ab 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 other 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 arranged 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 garret grolemund’s video on how to do this: https://vimeo.com/rs tudioinc/review/131218530/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 your 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/rstudioinc/review/131218530/212d8a5a7a/#t=30m35s https://vimeo.com/rstudioinc/review/131218530/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 rnorm(1,="" μ,="" σ)="" at="" the="" end="" of="" every="" year="" you="" contribute="" 360="" amount="" at="" the="" end="" of="" 2nd="" year="" amount="" at="" the="" end="" of="" 3rd="" year="" …="" 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="" 0.0="" 2.5="" 5.0="" 7.5="" 10.0="" x−axis="" label="" y−="" ax="" is="" la="" be="" 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="" your="" 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="" greater="" than="" or="" equal="" to="" 5000,="" •="" at="" the="" end="" of="" year="" 2,="" what="" is="" the="" proportion="" of="" simulations="" that="" reach="" a="" balance="" greater="" than="" or="" equal="" to="" 5000,="" •="" at="" the="" end="" of="" year="" 3,="" what="" is="" the="" proportion="" of="" simulations="" that="" reach="" a="" balance="" greater="" 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="" 0.0="" 2.5="" 5.0="" 7.5="" 10.0="" year="" pr="" ob="" ab="" 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="" other="" 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="" arranged="" 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="" garret="" grolemund’s="" video="" on="" how="" to="" do="" this:="" https://vimeo.com/rs="" tudioinc/review/131218530/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="" your="" 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/rstudioinc/review/131218530/212d8a5a7a/#t="30m35s" https://vimeo.com/rstudioinc/review/131218530/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)="">
Answered 4 days AfterNov 08, 2022

Answer To: HW5: Investment Simulator Shiny AppHW5: Investment Simulator Shiny AppStat 133, Fall...

Subhanbasha answered on Nov 12 2022
43 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