Demonstrating the Capital Asset Pricing Model (CAPM) in R Understanding CAPM
The Capital Asset Pricing Model (CAPM) is a model that describes the relationship between systematic risk and expected return for a financial asset. It’s often used to price assets and evaluate investment performance.
R Code Implementation
library(quantmod)
## Ładowanie wymaganego pakietu: xts
## Ładowanie wymaganego pakietu: zoo
##
## Dołączanie pakietu: 'zoo'
## Następujące obiekty zostały zakryte z 'package:base':
##
## as.Date, as.Date.numeric
## Ładowanie wymaganego pakietu: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(PerformanceAnalytics)
##
## Dołączanie pakietu: 'PerformanceAnalytics'
## Następujący obiekt został zakryty z 'package:graphics':
##
## legend
getSymbols(c("AAPL", "^GSPC"), from = "2018-01-01", to = "2023-12-31")
## [1] "AAPL" "GSPC"
stock_returns <- na.omit(ROC(AAPL[,6]))
market_returns <- na.omit(ROC(GSPC[,6]))
beta <- CAPM.beta(Ra = stock_returns, Rb = market_returns)
risk_free_rate <- 0.03
expected_return <- risk_free_rate + beta * (mean(market_returns) - risk_free_rate)
cat("Beta:", beta, "\n")
## Beta: 1.217629
cat("Expected Return:", expected_return * 100, "%")
## Expected Return: -0.6068119 %
Explanation:
Data Acquisition: - getSymbols: Fetches historical price data for the stock (AAPL) and the market index (S&P 500).
Return Calculation:
Beta Calculation:
Expected Return Calculation:
Result Printing:
Note:
The risk-free rate can be obtained from various sources, such as Treasury yields or other risk-free assets. The CAPM is a simplified model and may not perfectly predict future returns. It’s important to consider other factors, such as company-specific risks and market conditions, when making investment decisions. (more advanced techniques like time-varying beta and factor models.)