L6 Equity

In this section we are going through some ETFs and well know companies, to establish what was their equity through the period.

Stock Analysis

S&P 500

First we are going to see the performance of the S&P 500. to establish a baseline. This needs to be done by getting the data from 2024-05-01, to 2024-09-30 and then plotting in order to see the performance. But, first we need to download the necessary libraries.

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
# Load S&P 500 data
getSymbols("^GSPC", src = "yahoo", from = "2024-05-01", to = "2024-09-30")
## [1] "GSPC"
# Plot
plot(GSPC$GSPC.Adjusted, main = "S&P 500 (May 2024 - September 2024)", 
     ylab = "S&P 500 Adjusted Close", xlab = "Date", col = "red", lwd = 2)
grid()

# Summary
summary(GSPC$GSPC.Adjusted)
##      Index            GSPC.Adjusted 
##  Min.   :2024-05-01   Min.   :5018  
##  1st Qu.:2024-06-06   1st Qu.:5317  
##  Median :2024-07-16   Median :5470  
##  Mean   :2024-07-15   Mean   :5453  
##  3rd Qu.:2024-08-21   3rd Qu.:5585  
##  Max.   :2024-09-27   Max.   :5745

Tesla

Now we are going to analyze the Tesla company. Doing the same steps that we have done for S&P500, for the next companies we also do the same steps with their own ticker symbol.

# Load Tesla data
getSymbols("TSLA", src = "yahoo", from = "2024-05-01", to = "2024-09-30")
## [1] "TSLA"
# Plot
plot(TSLA$TSLA.Adjusted, main = "Tesla (TSLA) Adjusted Close (May 2024 - September 2024)", 
     ylab = "Adjusted Close Price (USD)", xlab = "Date", col = "blue", lwd = 2)
grid()

# Summary
summary(TSLA$TSLA.Adjusted)
##      Index            TSLA.Adjusted  
##  Min.   :2024-05-01   Min.   :168.5  
##  1st Qu.:2024-06-06   1st Qu.:180.0  
##  Median :2024-07-16   Median :208.5  
##  Mean   :2024-07-15   Mean   :208.6  
##  3rd Qu.:2024-08-21   3rd Qu.:229.9  
##  Max.   :2024-09-27   Max.   :263.3

Microsoft

# Load Microsoft data
getSymbols("MSFT", src = "yahoo", from = "2024-05-01", to = "2024-09-30")
## [1] "MSFT"
# Plot
plot(MSFT$MSFT.Adjusted, main = "Microsoft (MSFT) Adjusted Close (May 2024 - September 2024)", 
     ylab = "Adjusted Close Price (USD)", xlab = "Date", col = "orange", lwd = 2)
grid()

# Summary
summary(MSFT$MSFT.Adjusted)
##      Index            MSFT.Adjusted  
##  Min.   :2024-05-01   Min.   :393.5  
##  1st Qu.:2024-06-06   1st Qu.:413.8  
##  Median :2024-07-16   Median :424.5  
##  Mean   :2024-07-15   Mean   :426.7  
##  3rd Qu.:2024-08-21   3rd Qu.:439.7  
##  Max.   :2024-09-27   Max.   :466.7

IBM

# Load IBM data
getSymbols("IBM", src = "yahoo", from = "2024-05-01", to = "2024-09-30")
## [1] "IBM"
# Plot
plot(IBM$IBM.Adjusted, main = "IBM (IBM) Adjusted Close (May 2024 - September 2024)", 
     ylab = "Adjusted Close Price (USD)", xlab = "Date", col = "purple", lwd = 2)
grid()

# Summary
summary(IBM$IBM.Adjusted)
##      Index             IBM.Adjusted  
##  Min.   :2024-05-01   Min.   :161.4  
##  1st Qu.:2024-06-06   1st Qu.:168.4  
##  Median :2024-07-16   Median :181.7  
##  Mean   :2024-07-15   Mean   :184.1  
##  3rd Qu.:2024-08-21   3rd Qu.:196.0  
##  Max.   :2024-09-27   Max.   :223.4

NVIDIA

# Load NVIDIA data
getSymbols("NVDA", src = "yahoo", from = "2024-05-01", to = "2024-09-30")
## [1] "NVDA"
# Plot
plot(NVDA$NVDA.Adjusted, main = "NVIDIA (NVDA) Adjusted Close (May 2024 - September 2024)", 
     ylab = "Adjusted Close Price (USD)", xlab = "Date", col = "green", lwd = 2)
grid()

# Summary
summary(NVDA$NVDA.Adjusted)
##      Index            NVDA.Adjusted   
##  Min.   :2024-05-01   Min.   : 83.03  
##  1st Qu.:2024-06-06   1st Qu.:106.46  
##  Median :2024-07-16   Median :117.72  
##  Mean   :2024-07-15   Mean   :114.63  
##  3rd Qu.:2024-08-21   3rd Qu.:124.36  
##  Max.   :2024-09-27   Max.   :135.57

Fixed Income Analysis

Comparing Yields

L7.1 Difference between corporate high quality bonds and yield on Treasury bonds at the end of the school term?Explain why the difference exists?: The corporate Bond Yield has a 6.16% and the long-term bond yield is 4.38% so the difference between the yield is 1.78%. The difference exists because corporate bonds are riskier than treasury bonds. Treasury bonds are considered risk free since they are backed by the US government. While corporate bonds have a risk of default.

L7.2 What is the difference between the yield on long term treasury bonds and the yield on long term municipal bonds at the end of the school term? Explain why the difference exists The long term treasury bonds typically differ from the long term bonds because of tax advantage and credit risk differences that are associated with municipal bonds. Interest incomes from municipal bonds are often exempt from federal income taxes, sometimes state and local as well. Municipal bods have been backed by the federal government. Demand from tax-exempt income can lower municipal bond yields.

Yield Curve Analysis

L7.3 Difference between 26-week T-bill and 13-week T-bill The 26 week T-bill yield is 4.91% while the 13-week T-bill yield is 5.1%. Making the difference between them 0.19%

L7.4 Slope Direction: Downward at the term start, indicating lower future interest rates due to economic slowdown expectations.

L7.5 Assuming that this slope can be primarily attributed to expectations theory, did the direction of the slope indicate that the market expected higher or lower interest rate futures? Taking into account the expectation theory, the market expects lower interest rates. In the scenario, the higher yield reflects the markets belief that interest rates will decline over the next 26 weeks.

L7.6 Interest Rate Movement: Rates did not align fully with expectations, showing divergence between prediction and actual outcome.

Explaining shifts in the yield curve over time

L7.7 What was the difference between the long-term Treasury bond yield and the 13-week T-bill yield at the beginning of the school term?

At the begining of the term the 13-week treasury bill rate was 5.1 and the treasury long term bond yield 3.57. The difference is -1.5- The T-bill is higher than the long term treasury bond yield at the start of the term.

L7.8 What is the difference between long term treasury bond yield and the 13-week T-bill yield at the end of the term?

The difference at the end is 13-week T-bill 5.32 and the treasury long term bond yield 4.38 the difference being -0.94. the 13 week T-bill being higher.

L7.9 Given your answers to the two previous questions, describe how the yield curve changed over the school term. Explain changes in the expectations about future interest rates that are implied by the shift in the yield curve over the school term.

The yield curve is an inverted yield curve, which reflects market expectations of declining future interest rates and the yield curve remains inverted, but less pronounced inversion indicates a shift in market expectations.

L7.10 Did the Fed change the federal funds rate over the school term?

library(quantmod)
#Get the federal fund rate data from FRED
getSymbols("FEDFUNDS", src = "FRED")
## [1] "FEDFUNDS"
# Subset data of the desired date range
fed_subset<-window(FEDFUNDS, start = as.Date("2024-05-01"),end = as.Date("2024-09-30"))

#Plot the federal funds rate over the specified time period
plot(fed_subset, main = "Federal Funds Rate (May 2024- September 2024)",ylab = "Federal Funds Rate (%)", xlab = "Date", col="blue", lwd=2)

Yes, federal fund rates dropped

L7.11 Do you think the movements in interest rates over the school term were caused by the Feds monetary policy? Explain While we can say that monetary policy played a significant role in shaping interest rate movements. Other factors like inflation trends and global financial dynamics also have contributed to this.

##Measuring and explaining premiums on money market securities

L7.12 What is the difference between the yield on 90-day commercial paper and the yield on 13-week T-bills at the end of the school term? Explain why this premium exists. At the end of the school term 90-day comercial paper yield was 5.34% and 13-week T-bill yield was 5.32% the difference is 0.02%. The premium exists because the differencees in credit risks and liquidity between them.

L7.13 Compare the premium on the 90-day commercial paper yield that exists at the end of the school term to the premium that existed at the beginning of the term. Explain why the premium may have changed over the school term. 90-day commercial paper tield begining 5.1% and 90-day commercial paper yield 5.34% at the end. 13-week Treasury bill yield at the beginning 5.1% and 13-week Treasury bill yield at the end 5.32%. The difference at the end was 0.02%. This reflects a slighlty increased credit risk perception, tighter liquidity conditions, and changes in corporate borrowing dynamics.

##Explaining bond premiums and price movements

L7.14 What is the difference between the yield on high-yield corporate bonds at the end of the school term versus the yield on high quality corporate bonds at the begining of the school term? At the end of the school term, the yield on corporate bonds was 3.03 percentage points higher than the yield on high quality corporate bonds at the begining of the term. This reflects the higher risk premium demanded by investors for lower rated, higher risk debt instruments.

L7.15 Compare the long term Treasury bond yield at the end of the school term to the long term treasury bond yield that existed at the beginning of the school term. Given the direction of this change, did prices of long-term bonds rise or fall over the school term? Since the increase in the long term treasury bond yield was positive from 3.57% to 4.38% this causes the prices of long term bonds to decline.

L7.16 Compare the change in yields of treasury, municipal, and corporate bonds over the school term. Did the yields of all three types of securities move in the same direction and by about the same degree? Explain why yields of different types of bonds move together. While the yields of treasury and corporate bonds moved in the same direction, the degree of the change varied, with treasury bonds rising more sharply. This is because the treasury bond yields are more sensitive to changes in interest rates, while corporate bond yields are influenced by a combination of interest rates and credit risk perceptions.

L7.17 Compare the premium on high-yield corporate bonds at the beginning of the school term to premium that existed at the end of the school term. Did the premium increase or decrease? Why this premium changed over the school term. The premium on high yield corporate bonds compared to treasury bonds decreased from 4.68% to 4.42%. Suggested that investors became slightly more optimistic about the credit worthiness of high yield issuers.

L8 Portfolio - Markovitz

This code posits the idea that investors should construct portfolios based on expected risk, or minimizing risk for a given level of return.

In this case we will calculate the Apple, Microsoft, Amazon, Google and Meta for the given period.

library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
# Symbols and data
symbols <- c("AAPL", "MSFT", "AMZN", "GOOG", "META")
getSymbols(symbols, from = "2024-05-01", to = "2024-09-30")
## [1] "AAPL" "MSFT" "AMZN" "GOOG" "META"
prices <- merge(AAPL[,6], MSFT[,6], AMZN[,6], GOOG[,6], META[,6])
returns <- na.omit(ROC(prices))

# Portfolio statistics
mu <- colMeans(returns)
cov_mat <- cov(returns)
weights <- rep(1/length(symbols), length(symbols))
portfolio_return <- sum(weights * mu)
portfolio_variance <- t(weights) %*% cov_mat %*% weights

# Results
cat("Portfolio expected return (annualized):", round(portfolio_return * 252 * 100, 2), "%\n")
## Portfolio expected return (annualized): 33.81 %
cat("Portfolio annualized volatility:", round(sqrt(portfolio_variance * 252) * 100, 2), "%\n")
## Portfolio annualized volatility: 19.96 %

L9 Portfolio - CAMP

The Capital Asset Pricing Model is a model that describes the relationship between the expected return and risk of investing in a security. It shows that the expected return on a security is equal to the risk-free plus a risk premium, which is based on the beta of that security.

#Define the stock symbols and market index

symbols <- c("AMZN", "MSFT", "AAPL", "GOOG", "META", "^GSPC")

#Get the data

getSymbols(symbols, from = "2024-05-01", to = "2024-09-30")
## [1] "AMZN" "MSFT" "AAPL" "GOOG" "META" "GSPC"
#Extract adjusted prices for stock and market index

prices <- merge(AMZN[,6], MSFT[,6], AAPL[,6], GOOG[,6], META[,6], GSPC[,6])
colnames(prices) <- c("AMZN", "MSFT", "AAPL", "GOOG", "META", "GSPC")

#Calculate daily returns for stock and market index 

returns <- na.omit(ROC(prices))
stock_returns <- returns[, -6]  # Exclude GSPC (market index)
market_returns <- returns[, "GSPC"]

# Set risk-free rate to 5.59% annualized and convert to daily (risk free rate of 10 year Polish Treasuries)

risk_free_rate <- 0.0559 / 252  # Convert to daily rate

#Calculate beta and expected return for each stock

betas <- apply(stock_returns, 2, function(stock) CAPM.beta(Ra = stock, Rb = market_returns))
expected_returns <- risk_free_rate + betas * (mean(market_returns) - risk_free_rate)

#Print the results 

cat("Betas:\n")
## Betas:
print(betas)
##     AMZN     MSFT     AAPL     GOOG     META 
## 1.416118 1.073078 1.077254 1.210220 1.375356
cat("\nExpected Returns (annualized):\n")
## 
## Expected Returns (annualized):
print(round(expected_returns * 252 * 100, 2))  # Annualize and convert to percentage
##  AMZN  MSFT  AAPL  GOOG  META 
## 44.11 34.78 34.89 38.51 43.00

The difference between CAMP and Markowitz is that CAMP is a pricing model for individual assets based on market risk, while Markowitz is a portfolio optomozation method that focuses on diversification to archieve the best risk-return tradeoff