<jupyter_start><jupyter_text>Correlation- Correlation measures association, but doesn't show if x causes y or vice versa- Correlation is a statistic that measures the degree to which two variables move in relation to each other.- In finance, the correlation can measure the movement of a stock with that of a benchmark index, such as the S&P 500.  Formula- $r = \frac{\sum(X - \bar{X})(Y - \bar{Y})}{\sqrt{\sum(X - \bar{X})^2 \sum(Y - \bar{Y})^2}}$- $r$: the correlation factor- $\bar{X}$: the average observations of $X$- $\bar{Y}$: the average observations of $Y$ What does it mean?- $r$ ranges between -1 and 1 (both inclusive)- $r = 1$: Perfect positive correlation- $r = -1$: Perfect negative correlation- $r = 0$: No correlation at all  Resources- Correlation https://www.investopedia.com/terms/c/correlation.asp- SP500 by Market Cap https://www.slickcharts.com/sp500<jupyter_code>import pandas as pd
import pandas_datareader as pdr
import datetime as dt
import numpy as np
tickers = ['AAPL', 'TWTR', 'IBM', 'MSFT']
start = dt.datetime(2020, 1, 1)

data = pdr.get_data_yahoo(tickers, start)
data.head()
data = data['Adj Close']
data.head()
log_returns = np.log(data/data.shift())
log_returns
log_returns.corr()
sp500 = pdr.get_data_yahoo("^GSPC", start)
log_returns['SP500'] = np.log(sp500['Adj Close']/sp500['Adj Close'].shift())
log_returns.corr()
def test_correlation(ticker):
    df = pdr.get_data_yahoo(ticker, start)
    lr = log_returns.copy()
    lr[ticker] = np.log(df['Adj Close']/df['Adj Close'].shift())
    return lr.corr()
test_correlation("LQD")
test_correlation("TLT")
import matplotlib.pyplot as plt
%matplotlib notebook
def visualize_correlation(ticker1, ticker2):
    df = pdr.get_data_yahoo([ticker1, ticker2], start)
    df = df['Adj Close']
    df = df/df.iloc[0]
    fig, ax = plt.subplots()
    df.plot(ax=ax)
visualize_correlation("AAPL", "TLT")
visualize_correlation("^GSPC", "TLT")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14917695473251028, 'fraction_numerical': 0.020061728395061727, 'mean_word_length': 4.893939393939394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '345012', 'license': 'MIT', 'n_tokens_mistral': 723, 'n_tokens_neox': 661, 'n_words': 199, 'path': 'Python For Finance Risk And Return/05 - Correlation.ipynb', 'repo_name': 'shreejitverma/Data-Scientist', 'size': '1944'}