Covariance, Correlation, and OLS

Covariance

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import linregress

First, we define two random variables with sample size ($n$) 500.

$$ \begin{gathered} X \sim Exp(\lambda=0.5) \\\\ \epsilon \sim \mathcal{N}(\mu=9, \sigma^2=4) \\\\ Y \sim 3X + \epsilon \end{gathered} $$

In [2]:
X = np.random.exponential(scale=2.0, size=500)
Y = 3.0 * X + np.random.normal(loc=9.0, scale=2.0, size=500)
In [3]:
mean_X = np.mean(X)
mean_Y = np.mean(Y)
In [4]:
print(f"E[X] = {mean_X}")
print(f"E[Y] = {mean_Y}")
E[X] = 1.8984106745902172
E[Y] = 14.685288972603761

Since we are interested in calculating the covariance, let’s calculate the deviations for both X and Y first.

$$ \begin{gathered} \text{dev_X} = X_i - \bar{X} \\\\ \text{dev_Y} = Y_i - \bar{Y} \end{gathered} $$

In [5]:
dev_X = X - mean_X
dev_X
Out[5]:
array([ 1.78624827e+00,  2.65166724e+00, -1.67047772e+00,  1.69001939e+00,
       -8.70852415e-01, -1.02360376e+00, -2.18182965e-01, -6.87690533e-01,
       -1.01640883e+00, -9.21678917e-01, -1.58181387e+00, -1.71118984e+00,
        3.43572682e-01, -4.90520225e-02,  8.10384581e-01, -9.72808424e-01,
        8.40114950e-02,  3.44900266e+00, -1.53121454e+00,  9.06588349e-01,
        1.44918546e+00, -4.49992529e-01, -8.10604508e-01, -1.39619293e+00,
       -7.70811924e-01,  1.97932089e-02,  5.48480911e+00, -1.66134959e-01,
        7.45713332e-01,  1.90773328e-01,  2.16070025e+00,  2.24537671e+00,
        3.45019467e+00, -1.35228065e-01,  3.69221815e-02,  1.01365934e+00,
       -1.12862265e+00,  1.93274005e+00, -1.50356845e+00, -7.58567451e-02,
        3.17483134e+00, -1.63487792e+00, -1.83394514e+00, -1.68344727e+00,
       -6.32828607e-01, -3.57694353e-01,  7.55791354e-01,  2.55198117e+00,
       -1.04679227e+00, -4.90993871e-01, -1.06743756e+00,  3.42205347e+00,
        8.10373600e-01, -1.32641455e-01, -6.04640948e-01,  1.76560303e+00,
        3.77407199e+00, -1.60049750e+00,  3.27212672e+00,  3.78271835e-01,
       -1.73502493e+00,  1.95021516e+00,  5.60578591e-01,  7.85698602e-01,
       -1.59747867e+00,  1.75377525e-01,  5.48688150e+00,  2.04778031e+00,
        9.72176318e-01, -2.89503227e-01, -1.30140952e+00, -1.45442113e+00,
       -1.33155339e+00, -1.05602432e+00,  3.98511036e+00,  1.76457058e+00,
        6.10089537e-02, -8.74394818e-01, -3.17101479e-02, -9.30449066e-01,
       -9.75490967e-01, -1.77503620e+00,  1.13437113e-01,  1.03141267e+00,
       -1.68149392e+00, -8.87691326e-01, -1.30262997e+00,  3.46611017e+00,
       -1.62403509e+00, -2.18987307e-01,  3.26504998e+00, -3.18336021e-01,
       -1.13750175e+00, -1.41011382e+00,  1.33214396e-02, -7.43325130e-01,
       -9.28199266e-01, -1.78753481e+00, -1.21396714e+00, -1.09674400e+00,
        5.84867358e-01, -7.79238494e-01, -9.63907156e-01,  7.15200613e-01,
       -1.29712473e+00, -1.61379575e+00, -1.41706199e-01,  6.66407920e-01,
       -8.48040507e-01,  1.42032371e-01, -1.73504996e+00, -1.60784195e+00,
       -1.80798107e+00, -2.66755677e-01, -1.76313126e+00, -3.08900667e-01,
       -8.95805301e-01,  1.67560316e+00, -1.06793087e+00, -3.29281357e-01,
       -1.64315758e+00,  2.26379308e-01, -1.03379619e+00,  2.08836117e+00,
       -7.48416816e-01, -6.78793299e-01, -1.88350680e+00, -1.22532828e+00,
       -1.60168813e+00,  9.91635414e-01,  1.20023269e+00, -7.26111377e-01,
       -8.77318746e-01, -3.16084429e-01, -1.10588056e+00, -1.63175968e+00,
       -6.79264113e-01,  2.26888277e+00,  2.96103782e+00, -1.35579714e+00,
       -8.51154061e-01, -1.14778731e+00, -1.26244875e+00, -1.13336968e+00,
       -2.80790648e-01, -1.43822885e+00, -1.73973333e+00, -1.73519224e+00,
       -1.68031789e+00, -1.61434460e-01,  3.20189227e+00,  1.12642373e+00,
       -7.99711376e-01, -1.70999222e+00, -1.20471261e+00, -1.14682419e+00,
       -1.32021013e+00, -1.03782209e+00, -3.21070904e-01, -1.69506030e+00,
       -1.80826073e+00,  8.37940754e-01, -1.24324062e+00,  1.16824030e+00,
        6.72058845e+00, -8.06542122e-01, -7.85562384e-01, -1.28745721e+00,
       -3.46235755e-01,  2.81164776e+00, -9.93078460e-01,  7.01597818e-01,
       -1.73633177e+00, -1.30254764e+00,  7.79643234e-01, -6.33624357e-01,
        5.10976583e-01,  6.72061009e-01,  4.18536741e+00,  1.86711174e+00,
       -1.50010624e+00, -1.86277397e+00, -1.03988762e+00, -1.87961636e+00,
       -6.28437328e-01, -1.49851118e+00, -9.21000735e-01, -2.80538147e-01,
       -1.85132115e-01,  2.49495865e+00, -1.03211994e+00, -1.46062251e+00,
       -8.23995962e-01, -7.65825790e-01, -1.66635255e+00, -9.02226136e-02,
        4.58737455e-01,  9.95233549e-01,  3.43890308e+00,  1.45221128e+00,
       -1.72933939e+00, -1.32277634e+00, -9.62660498e-01, -1.25269381e+00,
       -1.21694700e+00,  4.58670398e+00, -4.88588312e-01,  1.20386762e+00,
       -3.08391766e-01, -8.75732573e-01, -2.12037161e-01, -8.26869386e-01,
        4.17663649e-01,  7.25090645e-01,  1.50803604e+00, -1.40559760e+00,
       -1.12311777e+00, -1.09689918e+00, -8.32587400e-01, -1.89774630e+00,
       -3.28208858e-01,  6.96667916e-01, -4.98227925e-01,  6.76810373e-01,
        1.37682125e+00,  6.33874439e-01, -7.81379724e-01,  3.41915168e+00,
       -1.83523749e+00,  4.83655732e+00,  4.07754809e-01, -1.33584434e+00,
       -1.33479217e-01, -6.43572217e-01, -1.76311005e+00, -1.24732595e+00,
       -1.55487693e+00, -4.49701283e-01, -1.62732283e+00, -1.64858701e+00,
       -7.94476086e-01, -5.47042059e-01,  4.00515610e+00, -7.75561181e-01,
       -3.33324357e-01,  1.00645644e-01,  1.19552303e+00,  5.06962829e+00,
        3.42950518e+00, -1.12684150e+00, -6.20015186e-01,  8.44697302e-01,
       -9.52614750e-01, -1.52310172e+00, -1.24406080e+00, -1.36285658e+00,
       -7.72258788e-01, -7.88642864e-02,  4.09231090e-01, -1.18386024e+00,
       -1.72655458e+00, -7.56162056e-01, -1.72571948e+00, -1.58726776e+00,
       -1.10452546e+00,  4.23091585e-01, -1.78535165e+00, -1.79821759e+00,
        1.29593362e+00, -1.75150117e+00, -1.80792853e+00, -1.29140773e+00,
        5.13146316e-01,  2.16355617e+00,  5.63055940e+00,  1.45150010e+00,
       -1.29103479e+00,  4.86492729e-01, -8.33543406e-01, -1.10709040e+00,
       -1.13555320e-01,  4.10812548e-02, -8.89925507e-01, -1.57718626e+00,
        8.84474166e-02, -1.64156462e+00, -1.34692759e+00,  2.41738713e+00,
       -1.07037752e+00,  1.35455901e+00,  7.56402712e+00, -1.02283425e+00,
       -2.49022909e-01,  8.73136356e-01,  5.24255951e-01,  3.18807648e+00,
       -5.29159949e-01, -3.59427930e-01,  1.13377718e-01, -5.41800711e-01,
       -1.78404764e+00,  5.21587383e+00, -1.37733350e+00,  1.16317707e-01,
       -4.21393132e-01, -1.01208752e+00,  8.09406879e-01, -5.00223180e-01,
       -1.49521182e+00, -1.16080902e+00, -7.95448077e-01,  3.51851814e-01,
       -1.89076865e+00,  3.93950281e-01, -1.07024129e+00, -1.54132157e+00,
       -7.61047538e-01,  2.09435425e+00, -5.30214916e-02,  8.94159263e-01,
       -1.07847345e-01, -1.42032732e+00, -1.66476019e+00,  1.07217784e+00,
       -1.84961832e+00,  6.46747461e-01, -1.71418564e+00, -1.34410636e+00,
        1.31703505e+00,  5.37257477e+00, -1.54437302e+00, -6.82224876e-01,
        4.61704501e-01, -1.55085717e+00,  1.30011908e+00,  2.11693253e+00,
       -2.26622270e-01, -1.48971751e+00, -1.69719959e-01,  4.31567267e+00,
        2.13654005e+00, -1.67217534e-02, -1.74184313e+00, -7.24047151e-01,
       -1.69967711e+00, -5.87510520e-01, -1.60748861e+00,  1.11145054e+00,
       -9.22699345e-01, -7.46261389e-01,  1.07995115e+00,  1.68535051e+00,
        2.55305905e+00, -8.34818078e-01, -8.82228615e-01,  7.19409069e-01,
       -5.93971077e-01, -3.79031842e-03,  1.75290118e+00,  5.90673297e+00,
        1.57182535e+00, -4.36264640e-01, -1.63188970e+00,  9.56102791e-02,
        1.74870817e+00,  2.46505596e+00, -7.45690216e-01, -3.40713719e-01,
       -1.15642785e+00,  5.65524718e-01, -9.72914529e-01,  5.37924667e+00,
        1.05359672e+00,  3.32302375e+00, -1.11567815e+00,  1.15141289e-02,
       -6.63666252e-01, -1.28494907e+00, -1.46415738e+00, -1.18770538e+00,
       -1.37590286e+00,  7.13547343e-02, -6.97215330e-01, -1.61985342e+00,
        6.67723936e-01,  8.42613984e-01,  5.78323035e-01,  7.44182368e-01,
        2.13592097e+00,  1.57622716e+00,  6.81803510e-01,  6.74504336e+00,
       -1.15260763e+00, -1.46150189e+00, -1.35004268e-01, -1.57950310e+00,
        1.53417321e+00,  1.82312532e+00, -1.32906676e+00, -1.88425690e+00,
       -7.76273970e-01, -1.80050991e+00, -1.82823655e+00, -8.66135540e-01,
        3.60533380e+00, -1.59338849e+00, -1.62978564e+00,  1.15584809e+00,
        1.28961059e+00, -3.82040747e-01, -1.18571391e+00, -7.69051311e-02,
       -1.55092714e+00,  5.97968632e-01,  1.59836064e-01, -1.51386770e+00,
       -5.97079630e-01, -2.19252564e-01,  1.38862020e+00, -1.09979461e+00,
        5.42257918e+00,  3.81433016e+00, -1.24562742e-01, -1.85982897e-01,
       -8.91922601e-01,  3.37583898e-01,  1.77285440e+00, -7.05284229e-01,
        1.18554049e+00,  3.25372748e+00, -6.89301901e-01,  3.37627682e+00,
       -1.63676981e+00, -6.78468392e-01, -1.08276189e+00,  2.40133865e+00,
       -5.19989586e-01,  2.00628458e-02, -7.82060011e-01, -1.18591384e+00,
       -1.04345008e+00,  9.45375567e-01, -1.26465364e+00, -1.26557024e+00,
       -1.36018684e+00, -8.98702643e-01, -1.74680435e+00, -1.76946013e+00,
        7.24791628e-01, -6.68496569e-01, -1.86857905e+00, -3.37841944e-01,
        2.93531035e+00,  4.32382132e+00, -1.47213205e+00,  4.75605098e+00,
        3.15616282e+00,  1.42101253e-01, -7.60785246e-02, -6.40147945e-01,
       -1.89501731e+00, -6.90446145e-01,  4.52446484e-01, -1.39184858e+00,
        3.87211585e+00, -7.82354942e-01, -1.83118250e+00, -1.88603365e+00,
       -1.77128185e+00, -1.76740790e+00,  2.51455755e+00,  9.92009075e-01,
       -1.67675363e+00, -1.51398301e+00, -1.72979098e+00,  2.51543371e+00,
       -7.12120703e-01,  1.12824047e+00, -1.30402939e+00, -8.79508256e-01,
        1.72551304e+00,  6.62674338e-01,  5.32882230e+00,  2.16782602e+00,
        3.59284087e-01,  3.50138380e+00,  1.33948724e+00, -1.76908806e+00,
       -1.10225378e+00, -7.77204887e-01, -7.41348298e-01, -1.51585011e+00,
        1.45869949e-01, -3.10599838e-01, -1.18105646e+00, -1.17342066e+00,
       -1.46635282e+00, -1.44975990e+00, -1.84523255e+00, -1.47880348e+00])
In [6]:
dev_Y = Y - mean_Y
dev_Y
Out[6]:
array([ 2.96850027,  7.00894162, -3.32921744,  2.76629173, -0.36796349,
       -5.52379108, -2.56882634, -2.22140406, -5.86420246, -6.40695002,
       -7.42788772, -3.49164642, -1.76802741, -2.06273946,  3.44157704,
       -3.43927339,  1.86395855,  9.4663647 , -0.19762551,  5.84145788,
        6.05380006, -2.69502997, -1.59927559, -4.02693164, -3.08404525,
        1.67223025, 18.31969195, -5.18863438,  2.25512504, -1.70198158,
        5.58892132,  5.17119832,  9.49055   , -4.19620028, -0.08599203,
        0.05849675, -0.27298481,  8.92696302, -3.34294649,  1.87301969,
        9.4627851 , -7.58432717, -7.1723129 , -3.03116949,  0.53479634,
        2.48860827, -0.29349983,  6.81730291, -6.13075294, -1.98054368,
       -2.11982358, 10.5155648 ,  2.80779448,  0.04593297, -0.63462079,
        6.60505345, 11.47575695, -4.28665516, 11.16941297,  3.67605821,
       -6.79632198,  5.28288605,  6.02700567, -0.85634614, -5.82127142,
        1.43527738, 16.0690148 ,  7.95476868,  4.02604096, -2.55891191,
       -2.84906076, -3.08359263,  0.73319477,  1.6288984 , 15.53694032,
        7.77581802, -1.22253136, -3.45021291, -1.05826441, -2.41844332,
       -2.69166774, -4.06782416,  2.7519385 ,  4.80093616, -8.24782915,
       -1.35710363, -2.0723541 ,  9.34368886, -2.86153087,  1.77844436,
       11.5184079 ,  0.20351248, -4.73258457, -5.92233628,  0.02826463,
       -2.19550448, -2.15027175, -3.87540589, -0.45274987, -4.90538064,
        3.00305316, -2.85151581, -5.59433021,  5.28655781, -2.59581735,
       -6.1864691 , -0.21741905,  4.18343223, -2.84687154,  0.19963231,
       -5.7946133 , -4.42890928, -2.74143411,  1.57971266, -4.45756855,
       -1.72014467, -2.64836387,  7.10814255, -2.99389334, -3.31757349,
       -7.74721979,  2.56691315, -6.25917736,  7.16422883, -3.76762448,
       -1.60292562, -3.47117867, -5.61286267, -3.75209784,  0.56544895,
        0.803808  , -0.290593  , -7.13653202,  2.25808586, -2.17169821,
       -6.18105793, -1.73851665,  6.09108969,  7.3366906 , -2.33388937,
       -2.80566842, -5.01537093, -2.72607558, -5.15239128, -0.67686377,
       -3.44910306, -6.66554088, -7.33492944, -4.32787948, -1.71526047,
        6.27645786,  1.48256791, -2.40343751, -5.59141547,  0.23206078,
       -4.13738312, -4.12895815,  1.20126877,  1.12161035, -6.4868771 ,
       -4.46093341,  0.88413304, -6.01712828,  3.67140982, 22.28817171,
       -2.9297988 , -3.66782076, -5.18351616, -2.18106463,  5.8850656 ,
       -2.81868944,  1.21394229, -6.17484184, -4.87571823, -0.58818355,
       -4.69074656,  0.55267968,  1.48663703, 10.4695471 ,  4.46296024,
       -2.03697232, -6.12232168, -4.4382197 , -4.79641374,  2.89057213,
       -4.94783146, -2.81363013,  1.26501169, -0.69556309,  4.82380871,
       -4.52209943, -1.52837731, -3.80313825,  1.87042376, -2.65654834,
       -1.04370061,  0.91860657, -0.74844929,  9.79179691,  4.54095221,
       -8.23352946, -3.04679466, -5.16362467, -1.85842217, -3.02726469,
       12.82147302, -2.08184433,  7.00874337, -0.07702215, -1.47222365,
       -2.00945676, -5.00564885,  2.67576529,  1.61903368,  1.23104459,
       -4.5562179 , -0.46263349, -1.77240358, -0.46573998, -6.93990952,
       -1.93830506,  3.6998051 , -1.20354938,  1.45678347,  3.67695861,
        1.68803075, -2.30542571,  7.87851002, -7.58434464, 15.68384859,
       -0.33125842, -6.29841542,  1.48714255, -0.37554352, -6.31716871,
       -6.61912868, -4.23365557, -0.8572073 , -6.68713017, -6.2299596 ,
        0.74858112, -4.46292235, 13.23861815, -4.24926997,  0.2301625 ,
        1.09634315,  4.08194028, 17.33656028, 11.23592776, -5.2837214 ,
        1.04518452,  3.8568176 , -0.07629605, -5.32031546, -5.98872562,
       -8.09227457, -0.24370868, -0.62614843,  2.53630149, -2.8968815 ,
       -3.43030233, -2.51128465, -6.76488521, -1.26447694, -6.42961004,
       -3.04054953, -7.22447031, -3.87023409,  2.89882928, -5.04382112,
       -2.78015596, -5.52797814,  0.7267885 ,  2.91202656, 17.91791159,
        6.75067996, -3.34824304,  0.8539714 ,  0.23319061, -3.90157606,
       -3.65621752,  0.75278241, -5.17962101, -2.91604926, -1.10956154,
       -4.30405286, -4.98050429,  7.3699792 , -3.61672447,  3.15060282,
       25.32461187, -2.90793316, -1.14755372, -2.82620539, -0.12537359,
        8.45652361,  0.64296282, -3.03834939, -0.50133277, -3.42130204,
       -7.61466683, 14.51765048, -0.48543862, -4.35996693,  1.92698451,
       -1.17697683,  1.60023449, -1.85187557, -5.90532948, -6.91543056,
       -2.54765611, -0.90625957, -5.53208558, -1.40749928, -0.60650377,
       -2.58277099, -1.50684666,  7.41423987, -1.76516909,  1.36781738,
       -0.99626823, -4.92933129, -3.73089754,  1.43853004, -3.78863832,
       -1.16578311, -5.26901057, -1.42815793,  1.91016958, 13.52891633,
       -4.97036304,  0.54903989,  2.79456727, -5.05271446,  7.13002126,
        6.64431311,  1.10620351, -5.43048078,  1.20739817, 13.18661383,
        6.00201795, -2.53483159, -0.31833811, -2.13966121, -7.25996564,
       -2.01659409, -5.16274917,  4.49309907, -5.37732553, -2.60035083,
        3.59201359,  7.06459388,  9.58530849, -2.32436921, -3.89928195,
        2.10189538, -0.27762348, -3.87768906,  4.65293787, 17.99696985,
        6.93693962,  0.21940655, -2.29195539, -2.0433798 ,  5.99735414,
        7.8515871 , -0.88167192, -0.84751356, -0.15261145,  3.07743381,
       -1.33759406, 16.09321342,  5.21079143,  9.96826505, -4.81679393,
        1.49597699,  3.21483954, -3.13872555, -3.35211866, -4.3729908 ,
       -4.29431072,  1.119112  , -3.11935593, -6.20923578,  1.39346329,
        2.48762642,  4.72166444,  0.76434548,  2.35233953,  4.32171258,
        5.48224607, 23.10170031, -3.30102879, -4.73869189, -0.76957954,
       -0.35986597,  4.52517568,  5.51264273, -8.07130851, -4.42070337,
       -1.07134321, -5.88897846, -7.78364473, -2.93667585, 10.4268986 ,
       -3.02055297, -3.18226719,  5.87178104,  4.13763515, -0.77202156,
       -2.30452312,  1.23604627, -4.08822769,  0.61234702,  0.69482639,
       -1.32338693, -4.00836575,  0.94272319,  6.11570965, -7.98394663,
       18.36969886,  9.55764995,  0.29763824, -0.08703282, -3.64866625,
        2.50749977,  3.69599327, -1.30944243,  5.04462229,  8.0943844 ,
        0.09451445, 11.6013012 , -1.73825825, -5.69766346,  1.51283334,
        7.22371731, -1.67225946,  0.77131961, -2.53250988, -0.94418459,
       -4.59800608,  5.14104816, -2.63790727, -2.10795237, -5.03692249,
       -4.77364385, -7.83156776, -5.288063  , -0.4135523 ,  0.97159043,
       -4.89506189,  0.13499182, 10.16932919, 12.5536291 , -6.68589047,
       12.98070965, 10.6896977 , -4.01573202, -2.29095917, -1.38588455,
       -3.93952027, -3.14018955,  0.07965645, -3.37404296,  8.09383251,
       -1.99370436, -7.04261268, -7.30422375,  0.26309262, -6.97840166,
       10.55580908,  2.69839923, -4.84516851, -3.9218612 , -7.26239848,
        5.1112359 , -0.75259324,  3.7609487 , -6.47379771, -5.02434028,
        4.15040311,  6.41100587, 10.87732479,  5.09283035, -1.3297419 ,
       10.37117153,  7.14320883, -4.82336129, -5.24385935, -4.67315965,
       -1.97832469, -5.55950417, -1.04803105,  0.29249116, -4.12211196,
       -5.08629404, -5.48055947, -3.01957757, -4.0805628 , -2.55925179])

To find the covariance of X and Y, we calculate the mean of the product of the deviations of the two random variables that is the expected value.

$$ Cov(X,Y) = E[\text{dev_X}\cdot\text{dev_Y}] $$

In [7]:
cov_XY = np.mean(dev_X * dev_Y)
cov_XY
Out[7]:
np.float64(9.155666431830372)
In [8]:
cov_matrix = np.cov(X, Y, bias=True)
cov_matrix
Out[8]:
array([[ 3.08682904,  9.15566643],
       [ 9.15566643, 31.07502308]])

As we can see below, NumPy’s built-in method for calculating covariance also yields the same result as the one we derived.

In [9]:
np.isclose(cov_matrix[0][0], np.mean(dev_X * dev_X))
Out[9]:
np.True_

Correlation

The only difference between correlation and covariance is that correlation is normalized.

$$ \begin{gathered} z_X = \frac{X_i - E[X]}{\sigma_X} \\\\ z_Y = \frac{Y_i - E[Y]}{\sigma_Y} \end{gathered} $$

In [10]:
z_X = dev_X / np.std(X, ddof=1)
z_Y = dev_Y / np.std(Y, ddof=1)

While in covariance we directly calculate the mean of the deviations, here we calculate the mean of the Z-scores, which is actually the mean of the standardized deviations.

$$ \rho_{X,Y} = E[z_X z_Y] $$

In [11]:
corr_XY = np.mean(z_X * z_Y)
corr_XY
Out[11]:
np.float64(0.9329502476880944)

As we can see, NumPy’s built-in function also returns very similar values.

In [12]:
print(np.corrcoef(X, Y)[0][1], corr_XY)
0.9348198874630202 0.9329502476880944

Ordinary Least Squares

When creating an OLS model, we actually build it entirely using covariances and variances. Let's remember the model first:

$$ Y = \beta_0 + \beta_1 X $$

First, let’s define $\beta_1$:

$$ \beta_1 = \frac{Cov(X,Y)}{Var(X)} $$

The result of this calculation will tell us how much Y changes for every unit change in X. This makes perfect sense when we consider that covariance measures the “rate of joint change.”

In [13]:
slope = cov_XY / np.var(X)
slope
Out[13]:
np.float64(2.966042600287344)

However, to determine the y-intercept, it is not enough to know the slope; we also need to know the value of Y when X is 0.

To do this, we first swap the variables in our model function:

$$ \beta_0 = Y - \beta_1 X $$

Then we substitute $\bar{X}$ and $\bar{Y}$ for X and Y.

In [14]:
intercept = mean_Y - (slope * mean_X)
intercept
Out[14]:
np.float64(9.054522038928942)
In [15]:
model = linregress(X, Y)
model.__getattribute__("slope")
Out[15]:
np.float64(2.966042600287344)

We can also see that the values we actually found are correct when we check them using the linregress function from scipy.stats.

In [16]:
print(
    "Is manuall found intercept and scipy's interecept close?",
    np.isclose(intercept, model.__getattribute__("intercept")),
)
print(
    "Is manuall found slope and scipy's slope close?",
    np.isclose(slope, model.__getattribute__("slope")),
)
Is manuall found intercept and scipy's interecept close? True
Is manuall found slope and scipy's slope close? True

Our goal in linear regression was to minimize the sum of squared errors; below, we can see the sum of squared errors we obtained.

In [17]:
Y_pred = intercept + (slope * X)
residuals = Y - Y_pred
print("Sum of squared errors:", np.sum(residuals**2))
Sum of squared errors: 1959.463206274273

Here, too, we can see how our data points are distributed in the scatter plot. The line passing through them is the one that minimizes the sum of the squared errors, in other words, our linear regression line.

In [18]:
fig, ax = plt.subplots()
ax.scatter(X, Y, alpha=0.1, marker=".", color="red")
ax.plot(X, Y_pred, label="model")
ax.axvline(
    mean_X, ymax=mean_Y, linestyle="--", alpha=0.5, label=f"$\\bar{{X}}$: {mean_X:.2f}"
)
ax.axhline(
    mean_Y, xmax=mean_X, linestyle="--", alpha=0.5, label=f"$\\bar{{Y}}$: {mean_Y:.2f}"
)
ax.legend()
plt.show()
No description has been provided for this image

From our graph, we can also see that we have aligned the point where the line intersects the Y-axis ($\beta_0$) with the point where the line passes through the means of both X and Y.

In [19]:
np.isclose((intercept + (slope * mean_X)), mean_Y)
Out[19]:
np.True_
>