<jupyter_start><jupyter_text>Calculating Market RiskIn this activity, you’ll calculate the beta for three stocks to identify the most conservative investment. Instructions:1. Import the required libraries and dependencies.2. Use the `read_csv` function and the `Path` module to import `tech_data.csv` into a Pandas DataFrame.3. Use the Pandas `pct_change` function together with the `dropna` function to calculate the daily returns for each stock in the DataFrame.4. Use the Pandas `var` function to calculate the variance for all the stocks in the DataFrame.5. Use the Pandas `var` function to calculate the variance for just the S&P 500. (You’ll use this in the beta calculation.)6. Use the Pandas `cov` function to calculate the covariance between each social media stock (FB, TWTR, and SNAP) and the market (S&P 500).7. Calculate the beta value for each of the three social media stocks by dividing the covariance of the stock by the variance of the market.8. Answer the following question: Based on your calculations of the betas, which of the three social media stocks presents the most conservative investment?References:[Pandas var function](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.var.html)[Pandas cov function](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.var.html)  Step 1: Import the required libraries and dependencies.<jupyter_code># Import Pandas library 
import pandas as pd

# Import the Path module from the pathlib library
from pathlib import Path

# Import the matplotlib inline dependency
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Step 2: Use the `read_csv` function and the `Path` module to import `tech_data.csv` into a Pandas DataFrame.<jupyter_code># Using the read_csv function and the Path module, read in the "tech_data.csv" file from the Resources folder
# Set the "Date" as the index column 
# Be sure to set the DatetimeIndex using the parse_dates and infer_datetime_format parameters
tech_data_csvpath = Path('./Resources/tech_data.csv')
tech_df = pd.read_csv(tech_data_csvpath, index_col='Date', parse_dates=True, infer_datetime_format=True)

# Review the 'tech_df' DataFrame with both the 'head' & 'tail' functions
display(tech_df.head())
display(tech_df.tail())<jupyter_output><empty_output><jupyter_text>Step 3: Use the Pandas `pct_change` function together with the `dropna` function to calculate the daily returns for each stock in the DataFrame.<jupyter_code># Using the Pandas pct_change function in conjunction with the dropna function, create the tech daily returns DataFrame
tech_daily_returns = tech_df.pct_change().dropna()

# Review the first 5 rows of the tech daily returns DataFrame
tech_daily_returns.head()<jupyter_output><empty_output><jupyter_text>Step 4: Use the Pandas `var` function to calculate the variance for all the stocks in the DataFrame.<jupyter_code># Using the Pandas var function, calculate the variance of all stocks in the tech daily returns DataFrame
tech_daily_returns.var()<jupyter_output><empty_output><jupyter_text>Step 5: Use the Pandas `var` function to calculate the variance for just the S&P 500. (You’ll use this in the beta calculation.)<jupyter_code># Using the Pandas var function, generate the variance for just the S&P 500
# The S&P 500 is being used as the proxy for the market throughout this activity
variance_market = tech_daily_returns['S&P 500'].var()

# View the variance of the market
variance_market<jupyter_output><empty_output><jupyter_text>Step 6: Use the Pandas `cov` function to calculate the covariance between each social media stock (FB, TWTR, and SNAP) and the market (S&P 500).<jupyter_code># Using the Pandas cov function, calculate the covariance between the daily returns of FB and the daily returns of the S&P 500
fb_covariance = tech_daily_returns['FB'].cov(tech_daily_returns['S&P 500'])

# View the FB covariance
fb_covariance

# Using the Pandas cov function, calculate the covariance between the daily returns of TWTR and the daily returns of the S&P 500
twtr_covariance = tech_daily_returns['TWTR'].cov(tech_daily_returns['S&P 500'])

# View the TWTR covariance
twtr_covariance

# Using the Pandas cov function, calculate the covariance between the daily returns of SNAP and the daily returns of the S&P 500
snap_covariance = tech_daily_returns['SNAP'].cov(tech_daily_returns['S&P 500'])

# View the SNAP covariance
snap_covariance<jupyter_output><empty_output><jupyter_text>Step 7: Calculate the beta value for each of the three social media stocks by dividing the covariance of the stock by the variance of the market.<jupyter_code># Calculate beta of FB by dividing the covariance of FB by the variance of the S&P 500
fb_beta = fb_covariance / variance_market

# Print the beta of FB
print(f"FB Beta = {fb_beta}")

# Calculate beta of TWTR by dividing the covariance of TWTR by the variance of the S&P 500
twtr_beta = twtr_covariance / variance_market

# Print the beta of TWTR
print(f"TWTR Beta = {twtr_beta}")

# Calculate beta of SNAP by dividing the covariance of SNAP by the variance of the S&P 500
snap_beta = snap_covariance / variance_market

# Print the beta of SNAP
print(f"SNAP Beta = {snap_beta}")<jupyter_output>SNAP Beta = 1.1097434117740557
{'fraction_non_alphanumeric': 0.06865728413845569, 'fraction_numerical': 0.015405096995055155, 'mean_word_length': 5.7509627727856225, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '411911', 'license': 'MIT', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1501, 'n_words': 652, 'path': '01_Calculating_Market_Risk/Calculating_Market_Risk.ipynb', 'repo_name': 'jpweldon/Module_4_Practice', 'size': '5258'}