1a.
curve(1.5*x+4, -5, 5, ylab="y", main="Graph 1: Y= 1.5X + 4");
1b.
curve(log(x), 0, 5, ylab="y",main="Graph 2: Y = Ln(X)");
1c.
curve(exp(x), 0, 5, ylab="y",main="Graph 3: Y = e^x");
1d.
curve(5 / (x - 1), -5, 5, ylab = "y", main = "Graph 4: Y=5/(x-1)")
1e.
curve(x^2 -2*x + 20, -5, 5, ylab = "y", main = "Graph 5: Y = X^2 – 2X + 20")
1f.
curve(x^3 - x^2 + 12*x + 20, -5, 5, ylab = "y", main = "Graph 6: Y = X^3 - X2 + 12X + 20")
2a.
curve(.5^x, 0, 5, ylab = "y", xlab = "T (Time)", main = "Graph 7: Y = .5^T")
2b.
curve(2^x, 0, 5, ylab = "y", xlab = "T (Time)", main = "Graph 8: Y = 2^T")
2c.
# Define the range of T values (integers only to avoid complex numbers)
T <- seq(-10, 10, by=1)
# Calculate the corresponding Y values
Y <- (-0.5)^T
# Create a plot
plot(T, Y, type="o", col="blue", xlab="T", ylab="Y", main="Graph 9 = (-0.5)^T")
# 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
library(xts)
library(ggplot2)
# Download S&P 500 index data from Yahoo Finance
getSymbols("^GSPC", from = "2000-01-01", to = Sys.Date())
## [1] "GSPC"
# Convert daily data to monthly data by using the monthly closing price
sp500_monthly <- to.monthly(GSPC, indexAt = "lastof", OHLC = FALSE)
# Convert the monthly index to returns
sp500_returns <- diff(log(Cl(sp500_monthly))) * 100 # Calculate log returns and multiply by 100 for percentage
# Remove the first row with NA
sp500_returns <- sp500_returns[-1]
# Plot the monthly index
ggplot(data = as.data.frame(sp500_monthly), aes(x = index(sp500_monthly), y = Cl(sp500_monthly))) +
geom_line(color = "blue") +
labs(title = "Monthly S&P 500 Index", x = "Date", y = "Index Level") +
theme_minimal()
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.
# Plot the monthly returns
ggplot(data = as.data.frame(sp500_returns), aes(x = index(sp500_returns), y = sp500_returns)) +
geom_line(color = "red") +
labs(title = "Monthly Returns of S&P 500", x = "Date", y = "Return (%)") +
theme_minimal()
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.
# Load necessary packages
library(quantmod)
library(ggplot2)
# Get quarterly real GDP data from FRED (Federal Reserve Economic Data)
getSymbols("GDPC1", src = "FRED")
## [1] "GDPC1"
# Convert the real GDP data to a time series object and visualize
real_gdp <- GDPC1
# Plot the real GDP
ggplot(data = as.data.frame(real_gdp), aes(x = index(real_gdp), y = GDPC1)) +
geom_line(color = "blue") +
labs(title = "Quarterly Real GDP ",
x = "Year",
y = "Real GDP") +
theme_minimal()
# Calculate the GDP growth rate (percentage change from previous quarter)
gdp_growth <- diff(log(GDPC1)) * 100 # Log difference multiplied by 100 for percentage
# Remove the first row with NA
gdp_growth <- gdp_growth[-1]
# Plot the real GDP growth
ggplot(data = as.data.frame(gdp_growth), aes(x = index(gdp_growth), y = gdp_growth)) +
geom_line(color = "red") +
labs(title = "Quarterly Real GDP Growth (%)",
x = "Year",
y = "Growth Rate (%)") +
theme_minimal()
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.
# Load necessary packages
library(quantmod)
library(ggplot2)
# Download AAPL stock prices from Yahoo Finance
getSymbols("AAPL", src = "yahoo", from = "2000-01-01")
## [1] "AAPL"
# Convert daily AAPL data to monthly data using the monthly closing price
aapl_monthly <- to.monthly(AAPL, indexAt = "lastof", OHLC = FALSE)
# Plot the monthly AAPL price
ggplot(data = as.data.frame(aapl_monthly), aes(x = index(aapl_monthly), y = AAPL.Close)) +
geom_line(color = "blue") +
labs(title = "Monthly AAPL Price", x = "Year", y = "Price (USD)") +
theme_minimal()
# Convert the monthly prices to returns (percentage change)
aapl_returns <- diff(log(Cl(aapl_monthly))) * 100 # Log returns multiplied by 100 for percentage
# Remove the first row with NA (due to diff())
aapl_returns <- aapl_returns[-1]
# Plot the monthly returns
ggplot(data = as.data.frame(aapl_returns), aes(x = index(aapl_returns), y = aapl_returns)) +
geom_line(color = "red") +
labs(title = "Monthly AAPL Returns", x = "Year", y = "Return (%)") +
theme_minimal()
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.
# Load necessary packages
library(quantmod)
library(ggplot2)
# Create data frames for AAPL and S&P 500 (assuming these are already downloaded)
aapl_df <- as.data.frame(aapl_monthly)
sp500_df <- as.data.frame(sp500_monthly)
# Add Date columns
aapl_df$Date <- index(aapl_monthly)
sp500_df$Date <- index(sp500_monthly)
# Merge the two data frames based on the Date
merged_df <- merge(aapl_df[, c("Date", "AAPL.Close")], sp500_df[, c("Date", "GSPC.Close")], by = "Date")
# Calculate correlation between AAPL and S&P 500 prices
correlation <- cor(merged_df$AAPL.Close, merged_df$GSPC.Close, use = "complete.obs")
# Print the correlation value
print(paste("The correlation between AAPL and S&P 500 prices is:", round(correlation, 4)))
## [1] "The correlation between AAPL and S&P 500 prices is: 0.9666"
# Plot with two y-axes: AAPL on the left y-axis and S&P 500 on the right y-axis
ggplot(merged_df, aes(x = Date)) +
geom_line(aes(y = AAPL.Close, color = "AAPL Price"), size = 1) + # AAPL Price on primary y-axis
geom_line(aes(y = GSPC.Close / 50, color = "S&P 500 Index"), size = 1) + # S&P 500 Index on secondary y-axis (scaled)
scale_y_continuous(
name = "AAPL Price (USD)", # Primary y-axis label
sec.axis = sec_axis(~ . * 50, name = "S&P 500 Index Level") # Secondary y-axis label and rescale
) +
scale_color_manual(name = "Legend", values = c("AAPL Price" = "blue", "S&P 500 Index" = "red")) +
labs(title = "Monthly AAPL Price vs. S&P 500 Index", x = "Date") +
theme_minimal() +
theme(
axis.title.y = element_text(color = "blue"), # Color primary y-axis labels blue
axis.title.y.right = element_text(color = "red") # Color secondary y-axis labels red
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
The relationship between the two variables is highly correlated, as indicated by the .97 correlation value.
# Load necessary packages
library(quantmod)
library(ggplot2)
# Download AAPL and S&P 500 (^GSPC) stock prices from Yahoo Finance
getSymbols(c("AAPL", "^GSPC"), from = "2000-01-01", to = Sys.Date(), src = "yahoo")
## [1] "AAPL" "GSPC"
# Convert daily data to monthly data using the monthly closing price
aapl_monthly <- to.monthly(AAPL, indexAt = "lastof", OHLC = FALSE)
sp500_monthly <- to.monthly(`GSPC`, indexAt = "lastof", OHLC = FALSE)
# Calculate the log returns (percentage change) for both AAPL and S&P 500
aapl_returns <- diff(log(Cl(aapl_monthly))) * 100 # Log returns for AAPL, in %
sp500_returns <- diff(log(Cl(sp500_monthly))) * 100 # Log returns for S&P 500, in %
# Remove the first row with NA (due to diff())
aapl_returns <- aapl_returns[-1]
sp500_returns <- sp500_returns[-1]
# Merge AAPL and S&P 500 returns into one data frame
returns_data <- merge(aapl_returns, sp500_returns)
colnames(returns_data) <- c("AAPL", "SP500")
# Convert xts object to data frame for ggplot
returns_df <- data.frame(Date = index(returns_data), coredata(returns_data))
# Plot the monthly returns using ggplot2
ggplot(returns_df, aes(x = Date)) +
geom_line(aes(y = AAPL, color = "AAPL")) +
geom_line(aes(y = SP500, color = "S&P 500")) +
labs(title = "Monthly Returns: AAPL vs S&P 500",
x = "Year",
y = "Return (%)") +
scale_color_manual(values = c("AAPL" = "red", "S&P 500" = "blue")) +
theme_minimal()
Direction of Movement: The monthly returns of AAPL and the S&P 500 generally move in the same direction, especially during periods of broad market rallies or sell-offs. This reflects AAPL’s strong connection with broader market trends.
Volatility: AAPL’s returns tend to be more volatile compared to the S&P 500. This is expected since AAPL is a single stock, and individual stocks are usually more volatile than a diversified market index like the S&P 500.
Correlation: Though AAPL follows market trends, there will be periods where it deviates from the S&P 500 due to company-specific news or factors impacting technology stocks disproportionately.