midterm_13-19

.docx

School

Georgia Institute Of Technology *

*We aren’t endorsed by this school

Course

OMSA

Subject

Finance

Date

May 3, 2024

Type

docx

Pages

3

Uploaded by DukeSummer7561 on coursehero.com

# Load the required packages library(tidyquant) library(dplyr) # Define the list of stock symbols stocks <- c('TSLA', 'JNJ', 'GOOGL', 'GE') # Download adjusted prices for the specified stocks from Yahoo Finance price_data <- stocks %>% tq_get(get = "stock.prices", from = "2018-01-01", to = "2022-12-31") %>% select(symbol, date, adjusted) # Calculate monthly returns for each stock returns_data <- price_data %>% group_by(symbol) %>% tq_transmute(select = adjusted, mutate_fun = periodReturn, col_rename = "monthly_return", period = "monthly") head(returns_data) # Create an equally weighted portfolio portfolio_returns <- returns_data %>% group_by(date) %>% tq_portfolio(assets_col = symbol, returns_col = monthly_return, rebalance_on = "months") summary(portfolio_returns) head(portfolio_returns) # Join the portfolio returns with the market return and risk-free rate data # Replace "market_returns.csv" and "risk_free_rate.csv" with your data file paths return_riskfree <- read.csv("/Users/srimukiviswanathan/Documents/GeorgiaTech_OMSA/MGT6203_DataAnalytic sBusiness/Exam/Market_and_RiskFree_Returns_2018_to_2022.csv") head(return_riskfree) #class(return_riskfree$) return_riskfree$date <- ymd(return_riskfree$date) head(return_riskfree) market_data <- read.csv("/Users/srimukiviswanathan/Documents/GeorgiaTech_OMSA/MGT6203_DataAnalytic sBusiness/Exam/MarketRet.csv") risk_free_data <- read.csv("/Users/srimukiviswanathan/Documents/GeorgiaTech_OMSA/MGT6203_DataAnalytic sBusiness/Exam/Risk_free.csv") # Assuming that the market_data and risk_free_data have columns "date", "market_return", and "risk_free_rate" portfolio_with_market_data <- portfolio_returns %>% left_join(return_riskfree, by = c("date"="date")) retruns_with_market_data <- returns_data %>% left_join(return_riskfree, by = c("date"="date")) # Now you have a dataset with the portfolio returns, market return, and risk-free rate for further analysis. head(portfolio_with_market_data) head(portfolio_returns) head(retruns_with_market_data)
# Assuming you've already loaded the required packages and have the necessary data in 'portfolio_with_market_data' dataframe # Calculate Portfolio Standard Deviation portfolio_std_dev <- portfolio_with_market_data %>% summarise(portfolio_std_dev = sd(portfolio.returns)) %>% pull(portfolio_std_dev) # Calculate GE Standard Deviation ge_std_dev <- retruns_with_market_data %>% filter(symbol == "GE") %>% summarise(ge_std_dev = sd(monthly_return)) %>% pull(ge_std_dev) # Calculate Portfolio Arithmetic Average portfolio_avg <- portfolio_with_market_data %>% summarise(portfolio_avg = mean(portfolio.returns)) %>% pull(portfolio_avg) # Calculate GE Arithmetic Average ge_avg <- retruns_with_market_data %>% filter(symbol == "GE") %>% summarise(ge_avg = mean(monthly_return)) %>% pull(ge_avg) # Round the results to four decimal places portfolio_std_dev <- round(portfolio_std_dev, 4) ge_std_dev <- round(ge_std_dev, 4) portfolio_avg <- round(portfolio_avg, 4) ge_avg <- round(ge_avg, 4) # Print the results cat("Portfolio Standard Deviation:", portfolio_std_dev, "%\n") cat("GE Standard Deviation:", ge_std_dev, "%\n") cat("Portfolio Arithmetic Average:", portfolio_avg, "%\n") cat("GE Arithmetic Average:", ge_avg, "%\n") # Assuming you've already loaded the required packages and have the necessary data in 'portfolio_with_market_data' dataframe # Calculate Cumulative Return of the Portfolio cumulative_return <- portfolio_with_market_data %>% summarise(cumulative_return = prod(1 + portfolio.returns) - 1) %>% pull(cumulative_return) # Convert to percentage and round to the nearest percent cumulative_return cumulative_return_percent <- round(cumulative_return * 100) # Print the cumulative return rounded to the nearest percent cat("Cumulative Return of the Portfolio:", cumulative_return_percent, "%\n") stocks_monthly_returns %>% filter(portfolio.returns > 0.25 * stocks$mon_return)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help