Homework 9

So the order of the screenshots and explanation make absolutely no sense. I lost my orginal questions on github so I ss’d what I could after i re asked my questions but it give me new code that is exactly 100% aligned with my code in the qmd files. Nonetheless, my explanations of logic remain for the code.


Screenshot 1: (.R)

stock_data <- tq_get. tq_get() pulls daily price data from Yahoo Finance.Open, high, low, close, volume, and adjusted prices for each ticker.

Groups by ticker, selects adjusted prices (to account for splits/dividends).

tq_transmute() calculates monthly arithmetic returns (simple percentage change).

ungroup() flattens the result for later processing.

annual_rf_rate converts the annual risk-free rate to monthly using compounding.

monthly_rf_rate outputs the monthly risk-free rate used for Sharpe Ratio later.


Screenshot 2: (.R)

leveraged_returns <- portfolio_returns Leverages the portfolio 2x by borrowing at the risk-free rate.

leveraged_stats <- leveraged_returns %>% summarise Measures how leverage affects returns, risk, and Sharpe Ratio.

ggplot visualization


Screenshot 3: (.R)

sharpe_ratios. Measures return above risk-free rate per unit of risk

correlation <- monthly_returns %>% filter Filters only TSLA and NVDA.Converts from long to wide format

portfolio_returns <- monthly_returns %>% filter 50/50 equal-weighted portfolio of TSLA and NVDA. Averages their returns for each month

portfolio_stats <- portfolio_returns %>% summarise same but portfolio


Screenshot 4: (.R)

cumulative_returns Calculates cumulative return for each stock. cumprod(1 + return) grows the investment month-by-month.

ggplot Plots cumulative returns over time by stock. scales::percent makes the y-axis easier to interpret

summary_stats Calculates basic return stats for each stock: Mean, SD:volatility, Min/Max, Annualizes the return and volatility using compounding and square root of 12

kable formats the summary stats into a table