The purpose of this lab is to get more practice creating and manipulating functions in RStudio and to introduce the idea of a regression model.
v1 = c(4, 9.8, 2, -10, 39, 987)
d1 = c(8, 7, 20, 4, 29, 382)
Write a function called “proj” that computes the projection of a vector v onto a vector d. Use the “dot” and “mag” functions that we wrote in class in your “proj” function. To receive credit for this exercise enter your code in Canvas.
Use the function you wrote in the previous exercise to find the projection of v1 onto d1. Test your function to make sure its working correctly by asking doing a problem that we did by hand in class. Report your answer by entering the last element of your projection vector in Canvas.
Write a function called “dist” that takes two vectors as inputs and computes the distance between the tips of these vectors. When writing this function use the “mag” function we wrote in class. To receive credit for this exercise enter your code in Canvas.
Test your “dist” function by verifying our answer to Example 2f from Lecture 2. The compute and report the distance between v1 and d1 (above).
What is the distance between the tips of a vector whose elements are integers 1 through 50 and a ones vector of the same length?
Read section 2.3 in “Introduction to Applied Linear Algebra” by Boyd and Vandenberghe (a pdf of this text can be found in the Textbook(s) module in Canvas). In the second half of the course we will use matrices and projections to build these models from scratch (we will compute the 𝛽 vector of coefficients ourselves). The following exercises involve using the model after the coefficients have been computed.
Suppose that the 𝛽 vector holds the predicted prices for 8 different assets (in order): 𝛽 = <100.39, 293.02, 32.23, 83.20, 54.29, 609.32, 1223.32, 2.98>. You have a portfolio that holds 100 shares of asset 1, 288 shares of asset 2, -233 shares of asset 3, 439.5 shares of asset 4, 22 shares of asset 5, 89 shares of asset 6, 90 shares of asset 7, and 440.25 shares of asset 8. 𝜈 is 10,020, which represents cash-on-hand in the portfolio.
Find the predicted total value \(\hat{y}\) for this portfolio given by this model.
If you’re unfamiliar with the term, read a little bit on the internet about “short selling” stocks. Asset # ___ is being “shorted” in this portfolio.