<jupyter_start><jupyter_text>A Whale off the Port(folio) In this assignment, you'll get to use what you've learned this week to evaluate the performance among various algorithmic, hedge, and mutual fund portfolios and compare them against the S&P 500.<jupyter_code>import pandas as pd
import numpy as np
import datetime as dt
from pathlib import Path
%matplotlib inline
import matplotlib.pyplot as plt
import ctypes
#print(ctypes.sizeof(ctypes.c_voidp))<jupyter_output><empty_output><jupyter_text>Data CleaningIn this section, you will need to read the CSV files into DataFrames and perform any necessary data cleaning steps. After cleaning, combine all DataFrames into a single DataFrame.Files:1. whale_returns.csv2. algo_returns.csv3. sp500_history.csv  Whale ReturnsRead the Whale Portfolio daily returns and clean the data<jupyter_code># Reading whale, algo and SP500 returns
whale_returns = Path("Resources/whale_returns.csv")
# YOUR CODE HERE
whale_data = pd.read_csv(whale_returns,index_col = "Date", parse_dates = ['Date'] )
whale_data.head(5)
# Count nulls
# YOUR CODE HERE
#whale_null = 
whale_data.isna().sum()
# Drop nulls
# YOUR CODE HERE
whale_data = whale_data.dropna()
whale_data.isna().sum()<jupyter_output><empty_output><jupyter_text>Algorithmic Daily ReturnsRead the algorithmic daily returns and clean the data<jupyter_code># Reading algorithmic returns
algo_returns = Path("Resources/algo_returns.csv")
#algo_returns_csv = Path("Resources/algo_returns.csv")
# YOUR CODE HERE
algo_data = pd.read_csv(algo_returns, index_col= 'Date', parse_dates = ['Date'])
algo_data.head(5)
# Count nulls
# YOUR CODE HERE
algo_null = algo_data.isna().sum()
algo_null
# Drop nulls
# YOUR CODE HERE
algo_data = algo_data.dropna()
algo_data.isna().sum()<jupyter_output><empty_output><jupyter_text>S&P 500 ReturnsRead the S&P500 Historic Closing Prices and create a new daily returns DataFrame from the data.<jupyter_code># Reading S&P 500 Closing Prices
sp500_history = Path("Resources/sp500_history.csv")
#sp500_returns = pd.read_csv(sp500_history_csv)
# YOUR CODE HERE
sp500_data = pd.read_csv(sp500_history, index_col='Date', parse_dates = ['Date'] ).sort_values('Date') 
sp500_data.head(5)
# Check Data Types
# YOUR CODE HERE
sp500_data.dtypes
# Fix Data Types
# YOUR CODE HERE
sp500_data['Close'] = sp500_data['Close'].str.replace('$', '')
sp500_data
sp500_data['Close'] = sp500_data.Close.astype(float)
sp500_data.dtypes

# Calculate Daily Returns
# YOUR CODE HERE
sp500_daily = sp500_data.pct_change()
sp500_daily.head(5)
# Drop nulls
# YOUR CODE HERE
sp500_dropna = sp500_daily.dropna()
sp500_dropna.head(5)

#sp500_data.isna()
# Rename Column
# YOUR CODE HERE

#sp500_new = sp500_dropna.rename(columns={'Close': 'S&P500'})
#sp500_new.head(5)

sp500_new = sp500_dropna.rename(columns={'Close': 'S&P500'})
sp500_new.head(5)
#making the columns all caps
algo_data = algo_data.rename(columns={'Algo 1': 'ALGO 1', 'Algo 2': 'ALGO 2'})<jupyter_output><empty_output><jupyter_text>Combine Whale, Algorithmic, and S&P 500 Returns<jupyter_code># Concatenate all DataFrames into a single DataFrame
# YOUR CODE HERE
df = pd.merge(whale_data, algo_data, on = 'Date')
all_df = pd.merge(df,sp500_new, on = 'Date')
all_df.tail(5)<jupyter_output><empty_output><jupyter_text>---  Portfolio AnalysisIn this section, you will calculate and visualize performance and risk metrics for the portfolios.  PerformanceCalculate and Plot the daily returns and cumulative returns. Does any portfolio outperform the S&P 500?<jupyter_code># Plot daily returns
# YOUR CODE HERE
all_df.plot(title = "Daily Returns", figsize=(16,8),legend = True)
plt.legend(loc=3)
# Plot cumulative returns
all_cum = ((all_df + 1).cumprod() - 1)

# YOUR CODE HERE
all_cum.plot(title = "Cumulative Returns", figsize=(16,8),legend = True )
plt.legend(loc=2)<jupyter_output><empty_output><jupyter_text>RiskDetermine the _risk_ of each portfolio:1. Create a box plot for each portfolio. 2. Calculate the standard deviation for all portfolios4. Determine which portfolios are riskier than the S&P 5005. Calculate the Annualized Standard Deviation<jupyter_code># Box plot to visually show risk
# YOUR CODE HERE
all_df_box = all_df.boxplot(figsize=(16,6), grid = False, fontsize = 8)
plt.suptitle("Portfolio Risk")
# Daily Standard Deviations
# Calculate the standard deviation for each portfolio. 
# YOUR CODE HERE
all_df_std = all_df.std().round(6)
all_df_std<jupyter_output><empty_output><jupyter_text>Which portfolios are riskier than the S&P 500?TIGER GLOBAL MANAGEMENT LLC    0.010894BERKSHIRE HATHAWAY INC         0.012919<jupyter_code># Determine which portfolios are riskier than the S&P 500
# YOUR CODE HERE
def risk(x):
    for cols in all_df_std:
        if cols > all_df_std['S&P500']:
            print (f'True')
        else:
            print(f'False')
           
risk(all_df)
# Calculate the annualized standard deviation (252 trading days)
# YOUR CODE HERE
all_df_ann = all_df_std*(252**0.5)
all_df_ann<jupyter_output><empty_output><jupyter_text>---  Rolling StatisticsRisk changes over time. Analyze the rolling statistics for Risk and Beta. 1. Plot the rolling standard deviation of the various portfolios along with the rolling standard deviation of the S&P 500 (consider a 21 day window). Does the risk increase for each of the portfolios at the same time risk increases in the S&P?2. Construct a correlation table for the algorithmic, whale, and S&P 500 returns. Which returns most closely mimic the S&P?3. Choose one portfolio and plot a rolling beta between that portfolio's returns and S&P 500 returns. Does the portfolio seem sensitive to movements in the S&P 500?4. An alternative way to calculate a rolling window is to take the exponentially weighted moving average. This is like a moving window average, but it assigns greater importance to more recent observations. Try calculating the ewm with a 21 day half-life.<jupyter_code># Calculate and plot the rolling standard deviation for
# the S&P 500 and whale portfolios using a 21 trading day window 
# YOUR CODE HERE
all_roll = all_df.rolling(21).std()
all_roll.plot(title = "21 days Rolling Standard Deviation", figsize=(16,8), legend= True)
plt.legend(loc=2, fontsize=8)
# Construct a correlation table
# YOUR CODE HERE
     
all_df.corr(method = 'spearman')
# Calculate Beta for a single portfolio compared to the total market (S&P 500)
# (Your graph may differ, dependent upon which portfolio you are comparing)
# YOUR CODE HERE
all_df_cov = np.var(all_df) 
all_df_cov
all_algo1_var = all_df_cov / berks_var
all_algo1_var.plot(title = "Algo 1", figsize=(16,8))
# Calculate a rolling window using the exponentially weighted moving average. 
# YOUR CODE HERE

exp_data=all_df.ewm(span=230,adjust=False).mean()
exp_data.plot(title = "Exponentially Weighted Average", figsize = (18,6),legend=True)
plt.legend(loc=2, fontsize=7)<jupyter_output><empty_output><jupyter_text>---  Sharpe RatiosIn reality, investment managers and thier institutional investors look at the ratio of return-to-risk, and not just returns alone. (After all, if you could invest in one of two portfolios, each offered the same 10% return, yet one offered lower risk, you'd take that one, right?)1. Using the daily returns, calculate and visualize the Sharpe ratios using a bar plot.2. Determine whether the algorithmic strategies outperform both the market (S&P 500) and the whales portfolios.<jupyter_code># Calculate annualized Sharpe Ratios
# YOUR CODE HERE
all_daily_rtns  = all_df.diff()
all_daily_rtns
sharpe_df = all_daily_rtns.mean() / all_daily_rtns.std()
annual_sharpe_df = sharpe_df*(252**0.05)
annual_sharpe_df
all_df.count()
# Visualize the sharpe ratios as a bar plot
# YOUR CODE HERE
annual_sharpe_df.plot.bar(title = "Sharpe Ratio", )<jupyter_output><empty_output><jupyter_text>On the basis of this performance metric, do our algo strategies outperform both 'the market' and the whales? Type your answer here: ---  Portfolio ReturnsIn this section, you will build your own portfolio of stocks, calculate the returns, and compare the results to the Whale Portfolios and the S&P 500. 1. Visit [Google Sheets](https://docs.google.com/spreadsheets/) and use the in-built Google Finance function to choose 3-5 stocks for your own portfolio.2. Download the data as CSV files and calculate the portfolio returns.3. Calculate the returns for each stock.4. Using those returns, calculate the weighted returns for your entire portfolio assuming an equal number of shares for each stock.5. Add your portfolio returns to the DataFrame with the other portfolios and rerun the analysis. How does your portfolio fair? Your analysis should include the following:- Using all portfolios: - The annualized standard deviation (252 trading days) for all portfolios. - The plotted rolling standard deviation using a 21 trading day window for all portfolios. - The calculated annualized Sharpe Ratios and the accompanying bar plot visualization. - A correlation table.- Using your custom portfolio and one other of your choosing: - The plotted beta.   Choose 3-5 custom stocks with at last 1 year's worth of historic prices and create a DataFrame of the closing prices and dates for each stock.<jupyter_code># Read the first stock
# YOUR CODE HERE
file_path_cola = Path("Resources/cocacola1_hist.csv", )
cola_data = pd.read_csv(file_path_cola, index_col = "Date",  parse_dates = ['Date'], header = 0)
cola_data.head(5)
# Read the second stock
# YOUR CODE HERE
file_path_nke = Path("Resources/nke1_hist.csv")
nke_data = pd.read_csv(file_path_nke, index_col = "Date", parse_dates = ['Date'],header = 0)
nke_data.head(5)
# Read the third stock
# YOUR CODE HERE
file_path_twtr = Path("Resources/twtr1_hist.csv")
twtr_data = pd.read_csv(file_path_twtr, index_col = "Date",  parse_dates = ['Date'],  header = 0)
twtr_data.head(5)
# Read the fourth stock
# YOUR CODE HERE
file_path_dell = Path("Resources/dell1_hist.csv")
dell_data = pd.read_csv(file_path_dell, index_col = "Date", parse_dates = ['Date'],  header = 0)

dell_data.head(5)
#all = pd.DataFrame(cola_data, nke_data,dell_data,twtr_data)
# Concatenate all stocks into a single DataFrame
# YOUR CODE HERE
all_new_data = pd.concat([cola_data, nke_data,dell_data,twtr_data])
all_new_data.head(5)
# Reset the index
# YOUR CODE HERE
new_data_reset = all_new_data.reset_index()
new_data_reset.head(5)
# Pivot so that each column of prices represents a unique symbol _table , columns=['DELL', 'TWTR', 'COLA', 'NKE'], unstack()
# YOUR CODE HERE, 
#new_data_cols = new_data_reset.pivot()
new_data_cols =pd.pivot_table(new_data_reset, "Close", index='Date',  aggfunc=np.mean)
new_data_cols.head(5)

# Drop Nulls
# YOUR CODE HERE
new_data_dropna = new_data_cols.dropna()
new_data_dropna.head(5)<jupyter_output><empty_output><jupyter_text>Calculate the weighted returns for the portfolio assuming an equal number of shares for each stock<jupyter_code># Calculate weighted portfolio returns - .product()
weights = [1/4, 1/4, 1/4, 1/4]
# YOUR CODE HERE
data_ewm= new_data_dropna.ewm(com=0.25).mean()
data_ewm.head(5)<jupyter_output><empty_output><jupyter_text>Join your portfolio returns to the DataFrame that contains all of the portfolio returns<jupyter_code># Add your "Custom" portfolio to the larger dataframe of fund returns
# YOUR CODE HERE
data_ewm = data_ewm.rename(columns={'Close':'Custom'})
#combined_data = pd.concat([all_df, data_ewm], sort =True)
combined_data = pd.merge(all_df, data_ewm, on = "Date")
combined_data.head(5)

# Only compare dates where return data exists for all the stocks (drop NaNs)
# YOUR CODE HERE
combined_dropna = combined_data.dropna()<jupyter_output><empty_output><jupyter_text>Re-run the performance and risk analysis with your portfolio to see how it compares to the others<jupyter_code># Risk
# YOUR CODE HERE
combined_std = combined_dropna.std()*(252**0.5)
combined_std
# Rolling
# YOUR CODE HERE
combined_roll = combined_std.rolling(21)
# Annualized Sharpe Ratios
# YOUR CODE HERE
# Visualize the sharpe ratios as a bar plot
# YOUR CODE HERE
# Create a correlation analysis
# YOUR CODE HERE
# Beta
# YOUR CODE HERE<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup