def REGRESSION(x_list, y_list): """ Regression Returns: dict of lists of floats = jhta.REGRESSION(x_list, y_list) Source: https://www.wallstreetmojo.com/regression-formula/ """ xy_list = [] x2_list = [] for i in range(len(x_list)): x = x_list[i] y = y_list[i] xy = x * y x2 = x * x xy_list.append(xy) x2_list.append(x2) # set n: n = len(x_list) # sum it: x_sum = jhta.SUM({'x_list': x_list}, n, 'x_list')[-1] y_sum = jhta.SUM({'y_list': y_list}, n, 'y_list')[-1] xy_sum = jhta.SUM({'xy_list': xy_list}, n, 'xy_list')[-1] x2_sum = jhta.SUM({'x2_list': x2_list}, n, 'x2_list')[-1] # calculate intercept: a = ((y_sum * x2_sum) - (x_sum * xy_sum)) / (n * x2_sum - x_sum**2) # calculate slope: b = ((n * xy_sum) - (x_sum * y_sum)) / (n * x2_sum - x_sum**2) estimate_list = [] err_list = [] err2_list = [] for i in range(len(x_list)): x = x_list[i] y = y_list[i] # calculate estimate: yes = a + b * x estimate_list.append(yes) # calculate error: err = y - yes err_list.append(err) # calculate square error: err2 = err**2 err2_list.append(err2) return {'estimate': estimate_list, 'err': err_list, 'err2': err2_list}
def LSR(df, price='Close', predictions_int=0): """ Least Squares Regression """ x_list = [] y_list = [] x2_list = [] xy_list = [] i = 0 while i < len(df[price]): # For each (x,y) calculate x2 and xy: x = i y = df[price][i] x2 = x * x xy = x * y x_list.append(x) y_list.append(y) x2_list.append(x2) xy_list.append(xy) i += 1 # Sum all x, y, x2 and xy, which gives us Σx, Σy, Σx2 and Σxy: x_sum = jhta.SUM({'x_list': x_list}, len(x_list), 'x_list')[-1] y_sum = jhta.SUM({'y_list': y_list}, len(y_list), 'y_list')[-1] x2_sum = jhta.SUM({'x2_list': x2_list}, len(x2_list), 'x2_list')[-1] xy_sum = jhta.SUM({'xy_list': xy_list}, len(xy_list), 'xy_list')[-1] # set n: n = len(df[price]) # Calculate Slope: m = (n * xy_sum - x_sum * y_sum) / (n * x2_sum - x_sum * x_sum) # Calculate Intercept: b = (y_sum - m * x_sum) / n lsr_list = [] i = 0 while i < len(df[price]) + predictions_int: # Assemble the equation of a line: lsr = m * i + b lsr_list.append(lsr) i += 1 return lsr_list
def LSR(df, price='Close', predictions_int=0): """ Least Squares Regression Returns: list of floats = jhta.LSR(df, price='Close', predictions_int=0) Source: https://www.mathsisfun.com/data/least-squares-regression.html """ x_list = [] y_list = [] x2_list = [] xy_list = [] for i in range(len(df[price]) - predictions_int): # For each (x,y) calculate x2 and xy: x = i y = df[price][i] x2 = x * x xy = x * y x_list.append(x) y_list.append(y) x2_list.append(x2) xy_list.append(xy) # Sum all x, y, x2 and xy, which gives us Σx, Σy, Σx2 and Σxy: x_sum = jhta.SUM({'x_list': x_list}, len(x_list), 'x_list')[-1] y_sum = jhta.SUM({'y_list': y_list}, len(y_list), 'y_list')[-1] x2_sum = jhta.SUM({'x2_list': x2_list}, len(x2_list), 'x2_list')[-1] xy_sum = jhta.SUM({'xy_list': xy_list}, len(xy_list), 'xy_list')[-1] # set n: n = len(df[price]) - predictions_int # Calculate Slope: m = (n * xy_sum - x_sum * y_sum) / (n * x2_sum - x_sum * x_sum) # Calculate Intercept: b = (y_sum - m * x_sum) / n # Assemble the equation of a line: return [m * i + b for i in range(len(df[price]))]
def INFO(df, price='Close'): """ Print df Information """ print('{:_<28}:{:_>22}'.format('DF PRICE COLUMN', price)) print('{:_<28}:{:_>22d}'.format('LEN', len(df[price]))) print('{:_<28}:{:_>28.5f}'.format('MIN', jhta.MIN(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format('MAX', jhta.MAX(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format('SUM', jhta.SUM(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format('MEAN', jhta.MEAN(df, len(df[price]), price)[-1])) # print ('{:_<28}:{:_>28.5f}'.format('HARMONIC_MEAN', jhta.HARMONIC_MEAN(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN', jhta.MEDIAN(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN_LOW', jhta.MEDIAN_LOW(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN_HIGH', jhta.MEDIAN_HIGH(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN_GROUPED', jhta.MEDIAN_GROUPED(df, len(df[price]), price)[-1])) # print ('{:_<28}:{:_>28.5f}'.format('MODE', jhta.MODE(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'PSTDEV', jhta.PSTDEV(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'PVARIANCE', jhta.PVARIANCE(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'STDEV', jhta.STDEV(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'VARIANCE', jhta.VARIANCE(df, len(df[price]), price)[-1]))