R script seems to be designed for conducting a financial analysis of a portfolio consisting of several assets. Here’s a breakdown of the different sections of your script and what they contribute to the analysis:
Setup and Libraries: We begin by loading necessary libraries such as dplyr, quantmod, and PerformanceAnalytics for data manipulation and financial analysis.
Data Retrieval: We define the tickers for the assets of The Procter & Gamble Company (PG), Verizon Communications Inc. (VZ), The Coca-Cola Company (KO), and General Electric Company (GE) form NYSE. Using a loop, that we retrieve historical price data for each asset from Yahoo Finance and store it in a dataframe portfolioPrices.
tickers <- c("PG", "VZ", "KO", "GE")
weights <- c(.30, .25, .25, .20)
# bench mark
benchmarkPrices <- getSymbols.yahoo("IWM", from="2012-01-01", periodicity = "daily", auto.assign=FALSE)[,6]
colSums(is.na(benchmarkPrices)) # is there any NA value
## IWM.Adjusted
## 0
benchmarkReturns <- na.omit(ROC(benchmarkPrices, type="discrete"))
#Rename Columns
colnames(portfolioPrices) <- tickers
#Get sum of NA per column
colSums(is.na(portfolioPrices))
## PG VZ KO GE
## 0 0 0 0
Plot the historical prices of assets in the portfolio using plot() function.
Calculate daily returns for each asset in the portfolio and handle missing values. Calculate the portfolio returns using Return.portfolio() function.
#Calculate Returns For DF
dailyReturns <- na.omit(ROC(portfolioPrices, type="discrete"))
#Calculate Portfolio Returns
portfolioReturn <- Return.portfolio(dailyReturns, weights=weights)
Plot the cumulative returns of the portfolio using chart.
Calculate CAPM beta of the portfolio against the benchmark, both in general and during bullish and bearish market conditions.
## [1] 0.3765233
## [1] 0.4037961
## [1] 0.4520633
Calculate CAPM Jensen’s alpha to measure the portfolio’s risk-adjusted returns. Compute the Sharpe Ratio to assess the portfolio’s risk-adjusted returns relative to the risk-free rate.
## [1] 0.04013656
Generating a table of annualized returns adjusted for risk-free rate. Provide a table of calendar returns.
## portfolio.returns
## StdDev Sharpe (Rf=0%, p=95%): 0.02652909
Finally, it involve interpreting the visualizations, risk metrics, and performance metrics to draw conclusions about the portfolio’s performance and risk characteristics.
## portfolio.returns
## Annualized Return 0.0784
## Annualized Std Dev 0.1448
## Annualized Sharpe (Rf=2.5%) 0.3576
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2012 -0.2 0.3 0.4 -0.6 0.1 1.9 -0.3 0.5 -0.3 -0.1 0.2 1.3
## 2013 -0.1 -0.1 0.3 0.0 -1.9 -0.8 -0.8 0.1 -1.3 -0.5 -0.3 0.0
## 2014 -0.4 0.3 0.2 0.3 0.4 -0.4 -1.6 0.3 0.3 0.7 0.8 -1.3
## 2015 -1.4 0.6 -0.8 -0.1 -0.8 -0.3 0.1 -0.6 0.8 -0.6 -0.8 -0.7
## 2016 2.1 -0.6 -0.2 0.0 0.0 1.8 0.4 -0.1 0.7 -0.1 -1.3 -0.4
## 2017 0.0 0.0 -0.4 -0.5 0.5 0.2 0.4 0.4 0.1 -0.1 0.9 -0.2
## 2018 0.2 -1.6 -0.1 -1.9 -1.4 -0.3 0.4 -0.5 -0.1 -0.7 1.5 1.0
## 2019 2.4 -0.2 0.4 1.5 -2.2 -0.1 -2.0 -0.1 0.0 -0.2 0.1 0.3
## 2020 -0.8 -0.9 -2.6 -0.6 0.4 1.1 -0.4 -0.5 1.0 0.0 -0.5 0.9
## 2021 -1.8 -2.3 -0.5 0.3 -0.2 0.6 0.4 0.0 -1.3 0.3 -3.0 0.3
## 2022 0.4 -1.1 -1.1 -2.6 -0.9 0.5 -2.2 -1.4 -1.6 -0.8 2.2 -0.4
## 2023 1.0 -0.6 0.7 0.3 0.0 1.1 -0.3 0.0 -0.3 0.5 1.6 0.3
## 2024 -0.5 -0.4 0.8 NA NA NA NA NA NA NA NA NA
## portfolio.returns
## 2012 3.1
## 2013 -5.3
## 2014 -0.6
## 2015 -4.7
## 2016 2.2
## 2017 1.2
## 2018 -3.5
## 2019 -0.2
## 2020 -2.7
## 2021 -7.0
## 2022 -8.8
## 2023 4.4
## 2024 -0.1