psihum / predict.py
Hlt58's picture
Update predict.py
82277fd verified
Raw
History Blame Contribute Delete
851 Bytes
import numpy as np
def compute_slope(series):
if len(series) < 2:
return 0.0
x = np.arange(len(series))
y = np.array(series)
return float(np.polyfit(x, y, 1)[0])
def classify_trend(slope, threshold=0.001):
if slope > threshold:
return "hausse"
elif slope < -threshold:
return "baisse"
return "stable"
def project_series(series, horizon):
if len(series) < 2:
return series
x = np.arange(len(series))
slope, intercept = np.polyfit(x, series, 1)
future_x = np.arange(len(series) + horizon)
return list(slope * future_x + intercept)
def time_to_threshold(series, threshold):
slope = compute_slope(series)
current = series[-1]
if slope <= 0:
return None
t = (threshold - current) / slope
if t < 0:
return None
return int(t)