Deadline: 2018-11-05 at 4pm
To submit your work, add samorso
and irudnyts
as collaborators to your private GitHub repo.
We will grade only the latest files prior to the deadline. Any ulterior modifications are pointless.
The objectives of this homework assignment are the followings:
To begin with, create a (preferably private) GitHub repository for your group, and name it ptds2018hw2
. Once again, make sure to add samorso
and irudnyts
as collaborators. This project must be done using GitHub and respect the following requirements:
You can create one or several RMarkdown files to answer the following problems:
Write a program that prints the numbers from 1 to 1000, but with the following specific requirement:
An example of the output would be:
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, ...
Using the same tools we used in class, create a simple map to represent the volume of the real estate market in Switzerland. More specifically, the goal of this problem is to reproduce as closely as possible the map below:
Note that the code below was used to scrap the data needed for this graph:
# Please do not forget to update ptds2018
library("rworldmap")
library("rworldxtra")
library("ggmap")
library("tidyverse")
library("magrittr")
library("ptds2018")
cities <- data.frame(
name = c("zurich", "bern", "lausanne", "geneva", "basel"),
language = c("german", "german", "french", "french", "german"),
stringsAsFactors = FALSE
)
# Scrap prices from comparis.ch
#-------------------------------------------------------------------------------
volumes <- sapply(cities$name, get_volume)
cities <- cbind(
cities,
data.frame(volume = volumes)
)
# ...or use dplyr
# cities <- cities %>%
# dplyr::mutate(volume = volumes)
# Define cities' coordinates
#-------------------------------------------------------------------------------
cities <- cbind(
cities,
geocode(location = cities$name, source = "dsk")
)
# Draw the map
#-------------------------------------------------------------------------------
world_map <- getMap(resolution = "high")
which(sapply(1:243, function(x) world_map@polygons[[x]]@ID) == "Switzerland")
switzerland <- world_map@polygons[[40]]@Polygons[[1]]@coords %>% as_tibble()
# your code goes here
In this problem you will program a three-dimensional random walk. For this purpose we will consider a three-dimensional space where we let X0=[000]T denote the starting point of our process. Suppose that there exists a sequence of (univariate) random variables U1,⋯,UB such Utiid∼U(0,1). Then, we let the position at time t (where 1≤t≤B) be given by Xt=Xs+f(Ut),
For example, let B=2, ai=i6, U1=0.12 and U2=0.81 then we have, at the first step, X1=X0+f(U1)=[000]T+[100]T=[100]T,
B <- 10^4
set.seed(1982)
Ut <- runif(B)
Notice that Ut corresponds to the t-th element of Ut
. With this configuration, show that a the last step you obtain XB=[26−4426]T,
segments3D
from the plot3D
package. Note that the red and blue points indicate, respectively the starting and end points of the random walk.
B <- 10^5
set.seed(2000)
Ut <- runif(B)
Verify that you obtain XB=[142−133−899]T
animation
to create a video that shows how a random walk evolves over time.Suppose that you are working in an investment firm company as a quantitative analyst. Your boss gives you the task of creating a portfolio for one of your clients. The client wants to find the portfolio with the smallest variance that satisfies the following constraints:
Your execution fees (i.e. the cost of buying shares) are given by Ci=max($40,$0.0001⋅Xi) for each transaction where Xi represent the amount of money you wish to invest in stock i. For example, if you want to invest 30% and 70% in stocks A and B your total cost would be Cost=C1+C2=max($40,$0.0001⋅0.3⋅106)+max($40,$0.0001⋅0.7⋅106)=$40+$70=$110.
Note that ∑iXi=$1,000,000. Therefore, your boss requires that you compute all possible portfolios that satify the client’s constraints, represent them graphically as (for example) in the graph below and find the weight of the best (i.e. minimum variance) portfolio.
In order to complete this task, your boss tells you to use 3 years of historical data and gives you this code to download the data you will need (how kind of him/her):
library(quantmod)
library(rvest)
sp500 <- read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
sp500 %>%
html_nodes(".text") %>%
html_text() -> ticker_sp500
SP500_symbol <- ticker_sp500[(1:499)*2+1]
SP500_symbol[SP500_symbol == "BRK.B"] <- "BRK-B"
SP500_symbol[SP500_symbol == "BF.B"] <- "BF-B"
Your boss also mentions that the function get()
could be useful for this project and provides you with the example below (what a really nice boss!):
library(quantmod)
today <- Sys.Date()
three_year_ago <- seq(today, length = 2, by = "-3 year")[2]
stocks_tickers <- c("AAPL", "MSFT")
getSymbols(stocks_tickers, from = three_year_ago, to = today)
nb_ticker <- length(stocks_tickers)
var_stocks <- rep(NA, nb_ticker)
names(var_stocks) <- stocks_tickers
for (i in 1:nb_ticker){
Xt = na.omit(ClCl(get(stocks_tickers[i])))
stocks_tickers[i] = var(Xt)
}
stocks_tickers