9494>>> correlation(x, y) #doctest: +ELLIPSIS
95950.31622776601...
9696>>> linear_regression(x, y) #doctest:
97- LinearRegression(intercept=1.5, slope=0.1 )
97+ LinearRegression(slope=0.1, intercept=1.5 )
9898
9999
100100Exceptions
@@ -932,18 +932,18 @@ def correlation(x, y, /):
932932 raise StatisticsError ('at least one of the inputs is constant' )
933933
934934
935- LinearRegression = namedtuple ('LinearRegression' , [ 'intercept ' , 'slope' ] )
935+ LinearRegression = namedtuple ('LinearRegression' , ( 'slope ' , 'intercept' ) )
936936
937937
938- def linear_regression (regressor , dependent_variable , / ):
938+ def linear_regression (x , y , / ):
939939 """Intercept and slope for simple linear regression
940940
941941 Return the intercept and slope of simple linear regression
942942 parameters estimated using ordinary least squares. Simple linear
943- regression describes relationship between *regressor * and
944- *dependent variable * in terms of linear function:
943+ regression describes relationship between *x * and
944+ *y * in terms of linear function:
945945
946- dependent_variable = intercept + slope * regressor + noise
946+ y = intercept + slope * x + noise
947947
948948 where *intercept* and *slope* are the regression parameters that are
949949 estimated, and noise represents the variability of the data that was
@@ -953,29 +953,28 @@ def linear_regression(regressor, dependent_variable, /):
953953
954954 The parameters are returned as a named tuple.
955955
956- >>> regressor = [1, 2, 3, 4, 5]
956+ >>> x = [1, 2, 3, 4, 5]
957957 >>> noise = NormalDist().samples(5, seed=42)
958- >>> dependent_variable = [2 + 3 * regressor [i] + noise[i] for i in range(5)]
959- >>> linear_regression(regressor, dependent_variable ) #doctest: +ELLIPSIS
960- LinearRegression(intercept=1.75684970486 ..., slope=3.09078914170 ...)
958+ >>> y = [2 + 3 * x [i] + noise[i] for i in range(5)]
959+ >>> linear_regression(x, y ) #doctest: +ELLIPSIS
960+ LinearRegression(slope=3.09078914170 ..., intercept=1.75684970486 ...)
961961
962962 """
963- n = len (regressor )
964- if len (dependent_variable ) != n :
963+ n = len (x )
964+ if len (y ) != n :
965965 raise StatisticsError ('linear regression requires that both inputs have same number of data points' )
966966 if n < 2 :
967967 raise StatisticsError ('linear regression requires at least two data points' )
968- x , y = regressor , dependent_variable
969968 xbar = fsum (x ) / n
970969 ybar = fsum (y ) / n
971970 sxy = fsum ((xi - xbar ) * (yi - ybar ) for xi , yi in zip (x , y ))
972971 s2x = fsum ((xi - xbar ) ** 2.0 for xi in x )
973972 try :
974973 slope = sxy / s2x # equivalent to: covariance(x, y) / variance(x)
975974 except ZeroDivisionError :
976- raise StatisticsError ('regressor is constant' )
975+ raise StatisticsError ('x is constant' )
977976 intercept = ybar - slope * xbar
978- return LinearRegression (intercept = intercept , slope = slope )
977+ return LinearRegression (slope = slope , intercept = intercept )
979978
980979
981980## Normal Distribution #####################################################
0 commit comments