I may be missing something simple as usual but..
How do I calculate a slope of something? I would usually use 180/pi() * arctan((x)today - (x)yesterday) to get the current slope in degrees.
Cheers,
Pete
I may be missing something simple as usual but..
How do I calculate a slope of something? I would usually use 180/pi() * arctan((x)today - (x)yesterday) to get the current slope in degrees.
Cheers,
Pete
Maybe try one of these, possibly can be improved, the first is a little more dramatish.
import statsmodels.api as sm
def slope_calc2(in_list):
if len(in_list) == 1: return 0
n = len(in_list) # like 10
sum_x = 0; sum_y = 0; sum_x_y = 0; sum_x_2 = 0
# Collect sums
for i in range(n): # 0-based, 0-9 for list of length 10
x = i + 1 # 1-based
y = float(in_list[i])
sum_x += x; sum_y += y; sum_x_y += x * y; sum_x_2 += x**2
# Calculate slope
that = ( n * sum_x_2 - (sum_x**2) )
if that != 0: slope = ((n * sum_x_y) - (sum_x * sum_y)) / that
else: slope = 0
return slope # pretend these are like angles
def slope_calc(in_list):
time = sm.add_constant(range(-len(in_list) + 1, 1))
return sm.OLS(in_list, time).fit().params[-1] # slope
And if you want to know which way a set of points are curving. Positive is counterclockwise and again one offers more extreme values.
def curve_calc(in_list):
vals = []
for i in range(len(in_list) - 1):
vals.append(in_list[i+1] - in_list[i])
return np.mean(np.array(vals))
def curve_calc2(in_list):
vals = []
vals2 = []
for i in range(len(in_list) - 1):
vals.append(in_list[i+1] - in_list[i])
for i in range(len(vals) - 1):
vals2.append(vals[i+1] - vals[i])
return np.mean(np.array(vals2))