library(ggplot2)
financial_data <- read.csv("financial_data_2022.csv")
financial_data$AAPL_Return <- c(NA, diff(log(financial_data$AAPL_Price)))
financial_data$SP500_Return <- c(NA, diff(log(financial_data$SP500_Price)))
financial_data$AAPL_Excess <- financial_data$AAPL_Return - financial_data$Risk_Free_Rate
financial_data$SP500_Excess <- financial_data$SP500_Return - financial_data$Risk_Free_Rate
capm_model <- lm(AAPL_Excess ~ SP500_Excess, data = financial_data)
summary(capm_model)
##
## Call:
## lm(formula = AAPL_Excess ~ SP500_Excess, data = financial_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.09148 -0.03323 0.01152 0.02985 0.07545
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0002281 0.0186833 0.012 0.990524
## SP500_Excess 1.1524040 0.2271125 5.074 0.000668 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.05321 on 9 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.741, Adjusted R-squared: 0.7122
## F-statistic: 25.75 on 1 and 9 DF, p-value: 0.0006681
alpha <- coef(capm_model)[1]
beta <- coef(capm_model)[2]
mean_market_return <- mean(financial_data$SP500_Return, na.rm = TRUE)
expected_return <- mean(financial_data$Risk_Free_Rate, na.rm = TRUE) + beta * (mean_market_return - mean(financial_data$Risk_Free_Rate, na.rm = TRUE))
cat("Alpha:", alpha, "\n")
## Alpha: 0.0002281282
cat("Beta:", beta, "\n")
## Beta: 1.152404
cat("Expected Return (CAPM):", expected_return, "\n")
## Expected Return (CAPM): -0.02639705
excess_return_aapl <- financial_data$AAPL_Excess
volatility_aapl <- sd(excess_return_aapl, na.rm = TRUE)
sharpe_ratio_aapl <- mean(excess_return_aapl, na.rm = TRUE) / volatility_aapl
cat("Sharpe Ratio for AAPL:", sharpe_ratio_aapl, "\n")
## Sharpe Ratio for AAPL: -0.4873968
cat("Jensen's Alpha (same as Alpha from CAPM):", alpha, "\n")
## Jensen's Alpha (same as Alpha from CAPM): 0.0002281282
treynor_ratio_aapl <- (mean(financial_data$AAPL_Excess, na.rm = TRUE)) / beta
cat("Treynor Ratio for AAPL:", treynor_ratio_aapl, "\n")
## Treynor Ratio for AAPL: -0.04195266
tracking_error <- sd(financial_data$AAPL_Excess - financial_data$SP500_Excess, na.rm = TRUE)
information_ratio <- mean(financial_data$AAPL_Excess, na.rm = TRUE) / tracking_error
cat("Information Ratio for AAPL:", information_ratio, "\n")
## Information Ratio for AAPL: -0.9345846
ggplot(financial_data, aes(x = Month)) +
geom_line(aes(y = AAPL_Return, color = "AAPL"), size = 1, group = 1) + # Group=1 to connect the points
geom_line(aes(y = SP500_Return, color = "S&P 500"), size = 1, group = 1) + # Group=1 to connect the points
labs(title = "Monthly Returns: AAPL vs S&P 500",
x = "Month", y = "Returns") +
scale_color_manual(values = c("AAPL" = "blue", "S&P 500" = "red")) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## 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.
ggplot(financial_data, aes(x = Month)) +
geom_line(aes(y = AAPL_Excess, color = "AAPL Excess"), size = 1, group = 1) + # Group=1 to connect the points
geom_line(aes(y = Risk_Free_Rate, color = "Risk-Free Rate"), size = 1, group = 1) + # Group=1 to connect the points
labs(title = "Excess Returns: AAPL vs Risk-Free Rate",
x = "Month", y = "Excess Returns") +
scale_color_manual(values = c("AAPL Excess" = "blue", "Risk-Free Rate" = "green")) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Conclusion : If alpha is positive, AAPL has generated returns in excess of what would be expected from its market exposure, which suggests good stock performance in the context of CAPM. Beta greater than 1 would indicate AAPL is more volatile than the market, making it riskier but potentially more rewarding in bull markets. The Sharpe ratio and Treynor ratio both help assess how well AAPL performs relative to the risk it carries. If these ratios are high, AAPL can be considered a good investment based on risk-adjusted returns. Jensen’s alpha and Information ratio further provide insights into how well AAPL is managed in comparison to the market and whether it adds value above and beyond what is expected based on market risk. Overall, the analysis provides a detailed picture of AAPL’s risk-adjusted performance in the market relative to broader benchmarks like the S&P 500, allowing you to gauge whether it is a good investment choice based on past performance.