<jupyter_start><jupyter_text>Linear Regression- Attempts to establish how X causes Y  Correlation vs Linear Regression**Similarities**- Quantify the direction and strength of the relationship**Differences**- Correlation is a single statistic- Linear regression produces an equation<jupyter_code>import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
%matplotlib notebook
X = np.random.randn(5000)
Y = np.random.randn(5000)

fig, ax = plt.subplots()
ax.scatter(X, Y, alpha=.2)
tickers = ['AAPL', 'TWTR', 'IBM', 'MSFT', '^GSPC']
start = dt.datetime(2020, 1, 1)

data = pdr.get_data_yahoo(tickers, start)
data = data['Adj Close']
data.head()
log_returns = np.log(data/data.shift())
log_returns
def linear_regression(ticker_a, ticker_b):
    X = log_returns[ticker_a].iloc[1:].to_numpy().reshape(-1, 1)
    Y = log_returns[ticker_b].iloc[1:].to_numpy().reshape(-1, 1)

    lin_regr = LinearRegression()
    lin_regr.fit(X, Y)

    Y_pred = lin_regr.predict(X)

    alpha = lin_regr.intercept_[0]
    beta = lin_regr.coef_[0, 0]

    fig, ax = plt.subplots()
    ax.set_title("Alpha: " + str(round(alpha, 5)) + ", Beta: " + str(round(beta, 3)))
    ax.scatter(X, Y)
    ax.plot(X, Y_pred, c='r')
linear_regression("AAPL", "^GSPC")
linear_regression("AAPL", "MSFT")
linear_regression("AAPL", "TWTR")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14414414414414414, 'fraction_numerical': 0.018018018018018018, 'mean_word_length': 5.197424892703863, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '652057', 'license': 'MIT', 'n_tokens_mistral': 580, 'n_tokens_neox': 541, 'n_words': 126, 'path': 'Python For Finance Risk And Return/06 - Linear Regression.ipynb', 'repo_name': 'shreejitverma/Data-Scientist', 'size': '1443'}