43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
|
|
def rolling_polyfit_curves(series, degree=3, window=50, step=10):
|
|
curves = [] # Liste des courbes
|
|
positions = [] # Index de départ pour chaque courbe
|
|
|
|
for i in range(window, len(series), step):
|
|
y = series[i - window:i].values
|
|
x = np.arange(window)
|
|
coeffs = np.polyfit(x, y, degree)
|
|
poly = np.poly1d(coeffs)
|
|
curve = poly(x)
|
|
curves.append(curve)
|
|
positions.append(i)
|
|
|
|
return curves, positions
|
|
|
|
# Exemple d'utilisation
|
|
|
|
# Exemple avec une série synthétique
|
|
np.random.seed(0)
|
|
x = np.linspace(0, 20, 250)
|
|
y = 0.05 * x**3 - x**2 + 2 * x + 5 + np.random.normal(0, 15, size=len(x))
|
|
series = pd.Series(y)
|
|
|
|
# series = pd.Series(np.sin(np.linspace(0, 10*np.pi, 300)) + np.random.normal(0, 0.2, 300))
|
|
curves, positions = rolling_polyfit_curves(series, degree=3, window=50, step=10)
|
|
|
|
# Visualisation
|
|
plt.figure(figsize=(12, 6))
|
|
plt.plot(series.values, label="Original")
|
|
|
|
for curve, pos in zip(curves, positions):
|
|
x_global = np.arange(pos - 50, pos)
|
|
plt.plot(x_global, curve, alpha=0.6)
|
|
|
|
plt.title("Régressions polynomiales glissantes (chaque 10 bougies sur 50 précédentes)")
|
|
plt.legend()
|
|
plt.grid(True)
|
|
plt.show()
|