Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.Use tq_index from tidyquant to get all the stocks in the sp500 index. 2.Choose 3 stocks from the index at random(use sample or some other

1.Use tq_index from tidyquant to get all the stocks in the sp500 index. 2.Choose 3 stocks from the index at random(use sample or some other function to do this). 3.Create a dataframe called df containing the downloaded data about the adjusted closing price for the 3 stocks for the period 01Jan2018-01Jan2021. The dataframe should have only 3 columns - ticker name, date and adjusted price. 4.As we know this dataframe does not contain all the dates, due to holidays. Add all the dates and impute the missing values for the adjusted price. You must impute the values using the following logic: A missing value is equal to the mean value of the 3 adjusted prices before it, stripping any NA values. This means that the mean of 1,5,NA is (1+ 5) / 2 = 3 If you use mean, keep in mind that it will return NA if there is a single NA. In the above case it will calculate (1+5+NA) / 3 = NA. Look at the help for mean and specifically the na.rm option to fix this. If there are 3 NA, just impute NA and then do whatever imputation you like for those NAs - downup from dplyr/na_interpolation from imputeTS/some average/random number- doesnt matter. Id say the logic you should use is - check if we have a missing value. If we dont have, dont do anything, if we have - substitute it with the mean of the previous 3 observations. If you cant figure out how to do this, just impute any way you want or just keep the dataframe as is and move on to the other tasks. You can always return to this latter. 5.From now on start creating dataframes named df1, df2, df3 etc. for each of the subtasks. 5.1.Create a column which shows how much the price has changed day to day in absolute terms. So if the price has moved from 12 to 14 then it is equal to 2. If it moved from 20 to 14 then it is -6. Calculate the standard deviation and mean for each ticker of this new column. You can use base r functions to find the sd and mean, you dont have to write your own.:) 5.2.Create a dataframe which has only observations from the first ticker, from May2019. 5.3.For each of the tickers and for each month, calculate the minimum, maximum, mean value of adjusted and the number of observations in each month. April 2019 is different from April 2020. So dont just lump all the Aprils in one row, they are different months! 5.4. Create a dataframe containing all the observations where adjusted is at least 10% higher than the mean adjusted of the ticker. This means that you should calculate the mean for each stock and for each stock, only keep observations where the adjusted is at least 10% higher than this mean. 5.5. Create a new column, named dailyReturns, which has the daily returns of each of the stocks. Returns are calculated as price today - price yesterday and the result is divided by the price yesterday. 5.6.Create a column which shows if the price has dropped the last two days and then has increased the next two days, using 1 and 0. It should catch the following pattern: 9,8,3,5,11. It drops from 9 to 8 then to 3 - two days dropping in a row and then increases from 3 to 5 and to 11. For the day the price was 3, the new column should have value = 1. 6. Choose one stock and start with the price from 01Jan2019. Simulate 365 steps where the price changes each step with a random number coming from a normal distribution with the sd and mean, you have calculated for the stock in 5.1. Do 100 such simulations and calculate the average of the adjusted price. I start with price = 10 and each step it can go up 1 or down 1. If I simulate this for 5 steps I get something like: 10, 9, 8, 9, 8, - I only want you to keep the final number 8, so that it is easier for you. I do it again and I get 11, again 10 etc. My final output should be something, maybe a vector, but you can also do a list or whatever you want, which contains those final numbers - final_vector = 8, 11, 10 etc. 7.Choose one of the stocks and make a regression on its daily returns on the daily returns of the two other stocks. You should have 3 regressions : Stock1~Stock2, Stock1~Stock3, Stock1 ~ Stock2+Stock3. Compare the regressions. Which is the best regression and why? At any point if there is a task which required a previous task to be done and you havent done it, you can always use some random numbers and return to it later. For example on the simulation task, it doesnt really matter what distribution you are going to use to get your random steps.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Value Investing

Authors: Mike Hartley

1st Edition

979-8864443309

More Books

Students also viewed these Finance questions