1. Graph the following functions:

a. Y = 1.5X + 4

y = function(x) 
1.5*x + 4
plot(y, -10, 10, main="Y = 1.5X + 4")

y <- function(x) 1.5*x + 4
x <- seq(-10, 10, .01)
plot(x, y(x), type="l", main="Y = 1.5X + 4")

b. Y = Ln(X)

y = function(x)
log(x)
plot(y, 0, 10, main="log(x)")

y <- function(x) log(x)
x <- seq(0, 10, .01)
plot(x, y(x), type="l", main="log(x)")

c.Y = e^X

y = function(x) 
exp(x)
plot(y, 0, 10, main="e^x")

y <- function(x) exp(x)
x <- seq(0, 10, .01)
plot(x, y(x), type="l", main="e^x")

d. Y = 5/(X – 1)

y = function(x) 
5/(x - 1)
plot(y, -10, 10, main="5/(x - 1)")

y <- function(x) 5/(x - 1)
x <- seq(-10, 10, .01)
plot(x, y(x), type="l", main="5/(x - 1)")

e. Y = X^2 – 2X + 20

y = function(x) 
x^2 - 2*x + 20
plot(y, -10, 10, main="x^2 - 2*x + 20")

y <- function(x) x^2 - 2*x + 20
x <- seq(-10, 10, .01)
plot(x, y(x), type="l", main="x^2 - 2*x + 20")

f. Y = X^3 - X^2 + 12X + 20

y = function(x) 
x^3 - x^2 + 12*x + 20
plot(y, -10, 10, main="x^3 - x^2 + 12*x + 20")

y <- function(x) x^3 - x^2 + 12*x + 20
x <- seq(-10, 10, .01)
plot(x, y(x), type="l", main="x^3 - x^2 + 12*x + 20")

2. Graph the following functions where T is time:

a. Y = 0.5^T

y = function(T) 
0.5^T
plot(y, -10, 10, main="0.5^T")

y <- function(T) 0.5^T
x <- seq(-10, 10, .01)
plot(x, y(x), type="l", main="0.5^T")

b. Y = 2^T

y = function(T) 
2^T
plot(y, -10, 10, main="2^T")

y <- function(T) 2^T
x <- seq(-10, 10, .01)
plot(x, y(x), type="l", main="2^T")

c. Y = (-0.5)T

y = function(T) 
(-.5)^T
T = -10:0
Y = y(T)
plot(T, Y, type = 'l', main="(-0.5)T")

3. Download S&P500 index. Convert the index from daily to monthly. Convert the monthly index to monthly return to S&P500. Graph the monthly index. Graph the monthly return to S&P500

install.packages("quantmod", repos="http://cloud.r-project.org/")
## 
## The downloaded binary packages are in
##  /var/folders/__/s0rmstmj4ys1fh0w3g4mhvp40000gn/T//Rtmp27yFFl/downloaded_packages
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
getSymbols("^GSPC", src = "yahoo", from = "2021-09-03", to = Sys.Date())
## [1] "GSPC"
monthlyData = to.monthly(GSPC)
mDGSPC = monthlyData[,6]

plot (mDGSPC, xlab = "Date", ylab = "adj Price", type ="l", main = "Monthly index of S&P 500")

mRGSPC = monthlyReturn(mDGSPC)

plot (mRGSPC, xlab = "Date", ylab = "adj monthly return", type ="l", main = "Monthly return to S&P500")

4. Download the quarterly real GDP from Fred and graph the GDP. Convert the real GDP to real GDP growth and graph the real GDP growth.

library(quantmod)

getSymbols("GDPC1", src="FRED", from= "2021-04-01", to = Sys.Date())
## [1] "GDPC1"
plot (GDPC1, xlab = "Date", ylab = "Real GDP", type ="l", main = "Real GDP growth")

realGdpGrowth = periodReturn(GDPC1, period = 'quarterly')
plot (realGdpGrowth, xlab = "Date", ylab = "Real GDP growth", type ="l", main = "Real GDP growth")

5. Download AAPL (Apple stock prices). Convert AAPL to monthly AAPL. Graph the monthly price data. Convert monthly AAPL price to return to monthly AAPL and graph the return to monthly AAPL.

library(quantmod)

getSymbols("AAPL", src = "yahoo", from = "2021-09-03", to = Sys.Date())
## [1] "AAPL"
monthlyData = to.monthly(AAPL)
mDAAPL = monthlyData[,6]

plot (mDAAPL, xlab = "Date", ylab = "adj AAPL Price", type ="l", main = "Monthly index of AAPL")

mRAAPL = monthlyReturn(mDAAPL)

plot (mRAAPL, ylab = "adj monthly AAPL return", type ="l", main = "Monthly return to AAPL")

6. Graph monthly AAPL and monthly S&P500 on one coordinate system and comment on the relation between the two variables.

getSymbols("^GSPC", src = "yahoo", from = "2021-09-03", to = Sys.Date())
## [1] "GSPC"
getSymbols("AAPL", src = "yahoo", from = "2021-09-03", to = Sys.Date())
## [1] "AAPL"
mDAAPL = to.monthly(AAPL) 
mDGSPC = to.monthly(GSPC)

Apple = mDAAPL [,6]
sP = mDGSPC[,6]


plot (index(sP), coredata(sP), xlab = "Date", ylab = "adj Monthly Price", type ="l", main = "Monthly AAPL and monthly S&P500", col = "red", ylim = range(10 * Apple, sP))
lines(index(Apple), 10 * coredata(Apple), col = "blue")

legend("topleft", legend = c("AAPL", "S&P 500"), col = c("blue", "red"), lty = 1, lwd = 1)

# As a major technology company in the market, Apple tends to show a strong correlation with the overall trend of the S&P 500. When the S&P 500 is on the rise, Apple also tends to follow an upward trend. Conversely, when the S&P 500 experiences turbulence, Apple is also likely to be in a turbulent phase. The main reasons for this are as follows:  
# 1. As one of the largest companies by market capitalization, Apple's direction often has an impact on the S&P 500 (e.g., through the development of new technologies or the launch of innovative products). 
# 2.Conversely, the broader market environment also affects Apple's stock value (macro influences on the micro level).

7. Graph returns to monthly AAPL and monthly S&P500 on one coordinate system and comment on their relation.

getSymbols("^GSPC", src = "yahoo", from = "2021-09-03", to = Sys.Date())
## [1] "GSPC"
getSymbols("AAPL", src = "yahoo", from = "2021-09-03", to = Sys.Date())
## [1] "AAPL"
RApple = monthlyReturn(AAPL) 
RsP = monthlyReturn(GSPC)



plot (RsP, xlab = "Date", ylab = "adj Monthly return", type ="l", main = "Monthly AAPL and monthly S&P500", col = "red")

lines(RApple, type ="l", col = "blue")

legend("topleft", legend = c("AAPL", "S&P 500"), col = c("blue", "red"), lty = 1, lwd = 1)

# In this chart, Apple is represented by the blue line and the S&P 500 by the red line. From this chart, we can observe two things:

# 1.Their correlation remains strong. When the S&P 500 reaches a peak, Apple also tends to peak, and when the S&P 500 is at a trough, Apple is also at a trough. The reasons for this are similar to those explained in the previous question.

# 2.The volatility of Apple is significantly greater than that of the S&P 500. This is because the S&P 500, as a market index, includes many stocks, which provides diversification and thereby reduces risk. As a result, the S&P 500 experiences less volatility compared to Apple.