You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 KiB

None <html lang="en"> <head> </head>
In [17]:
# data for the OLS
import numpy as np
import matplotlib.pyplot as plt

theta0 = 2.6486
theta1 = 4.98
m = 500
disp = 1
x = np.linspace(0,5,m)
y = theta0+theta1*x+disp*np.random.randn(m)
In [18]:
plt.plot(x,y, '.k')
plt.show()
No description has been provided for this image
In [20]:
xMean = x.mean()
yMean = y.mean()

Sxx = np.sum((x - xMean)**2)
Syy = np.sum((y - yMean)**2)
Sxy = np.sum((x - xMean) * (y - yMean))

r = Sxy / np.sqrt(Sxx * Syy)
r
Out[20]:
np.float64(0.990800615658485)
In [22]:
t1 = Sxy / Sxx
t0 = yMean - t1 * xMean

yHat = t0 + t1 * x
residuals = y - yHat
residuals
Out[22]:
array([-0.95082413, -0.64573564,  0.87130595, -0.53988214, -0.56134601,
        0.718763  , -1.57062343,  0.39547852,  0.28642336,  0.67589762,
       -0.05256052,  0.52740588, -0.2622087 ,  0.95248882,  0.77106812,
        1.04923914, -0.56930707,  0.0336549 , -1.23845916,  1.34279223,
       -1.28317243,  0.23367606,  1.5810006 , -1.28925369, -0.91503262,
       -0.47238037, -0.51189083,  0.22029059,  0.1362538 ,  0.10890607,
        1.45135919,  1.07239237, -0.9002686 ,  1.64189709, -0.28342477,
       -1.09095379, -0.95598686, -0.40362419, -1.11918067,  0.20567161,
       -0.87438623, -1.84805568, -0.00444769, -0.11272938,  0.50923524,
       -0.89881567,  1.16206936, -1.9768896 , -0.05677879,  0.38329957,
        0.0615791 ,  0.8993067 , -0.24370477,  1.25224753,  0.60692638,
        0.95457789,  0.98691527, -0.1311992 ,  2.58603558,  1.0730014 ,
        0.17406217, -1.29052871,  0.20098696,  0.45699896, -0.52684263,
        0.10757726, -1.89143251, -0.49583779, -1.25280976, -0.59317229,
       -1.81655035, -0.07774643,  0.15291058, -0.75178735,  0.62247512,
       -0.32728714, -1.19090979, -0.43800424, -0.16657564, -0.06809202,
       -1.10897576,  0.96155511, -1.27332763, -1.69728706, -0.13244864,
        2.26993619, -1.1321696 ,  0.23118172, -0.72868948, -0.40760238,
        1.14781616,  0.89399912, -1.5778674 ,  1.72964767,  1.57281777,
        0.98959707, -0.25492353,  2.74815498,  0.84893292,  0.02476336,
       -0.14090556, -0.10655555,  0.16575435,  2.23144836, -1.3674825 ,
        1.60257431, -0.89391951, -0.21138826, -1.35590793,  0.24152162,
       -0.68848452,  0.98023832, -0.03728212,  1.03889927,  0.42250127,
        0.02780973, -0.33943696,  0.70674449, -0.31820218,  1.53218837,
        1.60772438, -0.52389461, -1.2920113 ,  1.24985278,  1.46437141,
        1.72886415,  0.12737415, -1.97940349,  0.13704773,  1.74651052,
        0.27692414, -0.69784684,  0.59447656, -1.6277447 ,  0.33408282,
        0.08283985, -0.42907066, -3.17350929,  1.11954369,  0.02343232,
       -0.14214897, -0.08838091,  0.1710834 , -0.52138624, -0.47162436,
        0.6054694 , -0.77336679, -1.04961157, -1.15736182,  1.38816005,
       -1.19569705, -0.95423653, -0.71998855, -0.44764349,  2.48909426,
        1.32604927, -0.87414051,  0.5652511 , -1.41119301, -1.17531097,
       -0.2006058 ,  0.68645401,  1.18821643,  1.10287223, -1.13623775,
       -0.44324516, -0.60034911, -1.71777341, -0.13731388, -1.19598731,
       -1.40460078,  2.18318474, -0.25665186,  0.13941005, -1.03257039,
       -0.14168248, -0.05353929,  0.9652615 ,  1.59328294, -0.43964003,
       -0.18848033, -0.42417106, -1.36980691, -0.26081353,  0.78554832,
       -1.42055201,  1.29653318, -0.39959893,  0.26578292,  0.43503535,
        1.12952839, -1.15640984, -1.86981674, -1.85299422,  0.58197496,
        0.33479713,  1.10426353,  0.65709598, -0.36545727, -0.94257773,
        0.32804686,  1.14686355, -0.40733814,  1.6815773 ,  0.55602572,
       -0.22397765,  0.09222097,  0.32542925, -0.64405536,  0.69482021,
        0.39390481, -0.42203615,  1.46687941, -0.54187665, -1.21430829,
        0.27910103,  1.39819159, -1.17537755, -0.65009484,  0.99475956,
        1.0573686 , -1.03661444,  0.1850461 ,  0.47221162,  0.30483808,
        0.44341025,  1.80746389, -0.08666078,  0.38689218, -0.79105298,
       -0.46934343, -2.98387474,  0.49558085, -0.82921583,  0.30384201,
       -0.41699037,  0.71682262,  0.08100853,  0.95911846,  0.1805543 ,
        1.24498695,  2.13075034, -0.79247779, -1.46850503, -1.45895857,
       -0.10525365, -0.24755927, -0.4308391 , -0.14923592,  0.65655332,
        0.4330343 , -1.14340091, -0.19470556, -1.27218357,  0.1085798 ,
        1.00076225,  1.064037  , -0.69196389, -2.42524639,  0.27019292,
       -0.73286207,  1.14572395,  0.51588267,  1.5363161 ,  0.85035987,
       -0.58268652,  0.15414688, -1.88700757, -0.09797611, -0.86406005,
        0.21557541,  0.29166805,  2.06165041,  0.54580793,  2.23401705,
        1.04566736, -1.40389533,  1.76667624, -0.83624078, -0.51967116,
        0.46900263, -0.28565591,  0.0056141 ,  1.7469312 , -0.02517737,
        0.10641521,  0.6377089 , -0.92879003, -1.98053636, -0.38344278,
       -0.80088618,  0.77383712,  0.9961228 ,  0.43158633,  2.09416231,
        0.81005138,  0.0776556 ,  1.65976834, -1.7955456 , -1.10733159,
       -1.54788739, -0.60808617, -0.83599693, -1.34597955, -0.40085066,
       -0.38056226,  0.62933008,  1.69891605,  0.85233287, -0.64892305,
        0.63648449,  0.025184  , -0.29230831, -0.97429273, -0.43512737,
        1.19596399, -0.46304232, -0.54587135,  0.58229878,  1.03146887,
       -0.81520763,  1.10749725, -0.92944773, -0.07445753,  0.76357247,
        0.69727264, -0.02613729,  1.10331085, -1.05104358,  0.67284364,
       -0.16857696,  0.77230299, -0.15154626, -0.00998967, -0.27331846,
        0.22032324,  0.4355563 , -1.11632126, -0.02649838, -0.86337924,
        0.28522106,  0.00560423, -0.36240625, -0.19437625, -1.27732657,
        0.68564858,  0.11559882, -0.3007616 ,  0.01091722, -1.01793123,
       -0.04668242,  0.136249  ,  0.27341311, -1.65484484,  0.09055898,
        1.48200569,  0.45785882,  0.4459455 ,  0.14846279,  1.84017203,
        1.05349984,  1.29204124,  0.10212956, -2.51882166,  1.17062174,
       -0.44771132, -0.93342153,  0.00570185,  2.40556188, -0.77453901,
        0.66017808,  0.64440044, -0.57974439, -0.70358591, -0.91256498,
        0.52750612,  0.2248227 , -0.50471824, -0.62510315, -0.25753937,
       -0.11933973,  0.9110346 ,  0.06135248, -0.02085503,  0.54536065,
        1.5261428 , -0.86068793,  0.26628651,  0.8725808 ,  0.10768281,
       -0.02945102,  1.08162492,  0.96757504,  0.8487286 ,  0.40946741,
       -0.02082662, -2.8380769 ,  1.00451544,  0.33697275, -0.99442708,
        1.06901056,  1.17072805, -0.5387951 ,  0.48246067, -0.39052061,
       -1.28477645, -1.04858003, -0.88628018, -0.47541591,  0.50383958,
       -0.70677023, -1.50609385, -0.60616037, -0.34665137, -1.8652387 ,
       -0.19390395, -0.66607777, -1.00763511,  0.01564608, -0.91465297,
       -2.83929432, -0.09720386, -0.07391387, -1.31838473,  0.75623635,
        0.85999741,  1.19046736, -0.54713802,  0.48442037,  0.14527973,
       -0.93618386,  0.20301457, -0.67223971,  0.80744661,  0.87137894,
       -0.73160779,  0.88953047, -0.76267675, -0.74691851, -0.88915534,
        1.40348052, -0.32213203,  0.85653684,  0.54061916, -0.06615145,
       -0.06541276, -0.1874332 ,  1.64653204,  0.27782626, -1.56029627,
        1.65082298,  0.67656205, -1.58084623, -0.43152429,  1.2077006 ,
       -0.01064432,  0.39992249,  0.7204933 , -0.97893129,  0.3783936 ,
        1.42397314,  2.2570722 ,  0.73192937, -0.6796615 ,  0.79733617,
       -1.22591839, -0.3439277 ,  0.04718119,  1.01248675, -0.25599286,
       -0.78884594,  0.40217221,  0.54593394,  0.77981857,  1.6034912 ,
       -0.58348904,  0.23082941, -0.84446779,  0.0897946 ,  0.32875383,
        0.5969846 ,  0.2950068 , -1.85252387,  0.20742835, -1.99840818,
        1.13742464,  0.32724843,  0.80450174,  0.25135255,  0.6623171 ,
       -0.92213262, -0.07474869, -1.32358274, -1.36633224, -0.17526807,
       -1.49026666, -0.04415546,  0.51904066, -0.10048653,  0.10796533])
In [23]:
R2 = r**2
R2
Out[23]:
np.float64(0.9816858599892329)
In [25]:
print("Correlation r:", r)
print("Intercept t0:", t0)
print("Slope t1:", t1)
print("R2:", R2)
Correlation r: 0.990800615658485
Intercept t0: 2.715728336655882
Slope t1: 4.987467248897177
R2: 0.9816858599892329
In [30]:
plt.scatter(x, y, color='black', alpha=0.3)
plt.plot(x, yHat, ':r')
plt.xlabel("x")
plt.ylabel("y")
plt.title("Simple Linear Regression")
plt.show()
No description has been provided for this image
In [38]:
plt.scatter(x, residuals, color="red", alpha=0.3)
plt.axhline(0, color='black')
plt.xlabel("x")
plt.ylabel("Residuals")
plt.title("Residual Plot")
plt.show()
No description has been provided for this image
In [1]:
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=float)
y = np.array([2.2, 4.1, 5.9, 8.3, 10.1, 12.0, 13.8, 16.2], dtype=float)

x_mean = x.mean()
y_mean = y.mean()

Sxx = np.sum((x - x_mean)**2)
Syy = np.sum((y - y_mean)**2)
Sxy = np.sum((x - x_mean) * (y - y_mean))

r = Sxy / np.sqrt(Sxx * Syy)

b1 = Sxy / Sxx
b0 = y_mean - b1 * x_mean

y_hat = b0 + b1 * x
residuals = y - y_hat

R2 = r**2

print("Correlation r:", r)
print("Intercept b0:", b0)
print("Slope b1:", b1)
print("R²:", R2)

plt.scatter(x, y)
plt.plot(x, y_hat)
plt.xlabel("x")
plt.ylabel("y")
plt.title("Simple Linear Regression")
plt.show()

plt.scatter(x, residuals)
plt.axhline(0)
plt.xlabel("x")
plt.ylabel("Residuals")
plt.title("Residual Plot")
plt.show()
Correlation r: 0.999445617564577
Intercept b0: 0.15000000000000036
Slope b1: 1.9833333333333332
R²: 0.9988915424690386
No description has been provided for this image
No description has been provided for this image
</html>