predicted_rates = [NelsonSiegel(maturity, *params) for maturity in maturities]
# Calcul des statistiques du modèle
y = np.array([NelsonSiegel(maturity, *params) for maturity in maturities]) sse = np.sum((y - rates) ** 2) sst = np.sum((rates - np.mean(rates)) ** 2) r2 = 1 - sse/sst print("R² = {:.4f}".format(r2))
# Affichage des taux prédits
for maturity, predicted_rate in zip(maturities, predicted_rates): print("Taux prédit pour maturité {:.2f} ans : {:.4f}".format(maturity, predicted_rate))
# Tracé de la courbe des taux avec le modèle de Nelson-Siegel
fig, ax = plt.subplots(figsize=(12, 6))
, rates, 'o', label='Taux observés')
# Tracé des taux prédits
ax.plot(maturities, predicted_rates, label='Taux prédits') # Personnalisation du graphique ax.legend() ax.set_xlabel('Maturité') ax.set_ylabel('Taux') ax.set_title('Modèle de Nelson-Siegel')
# Ajout de la courbe de la pente
t = np.linspace(0, max(maturities), 100) y = params[0] + params[1] * ((1 - np.exp(-t/params[3])) / (t/params[3])) + params[2] * ((1 - np.exp(-t/params[3])) / (t/params[3]) - np.exp(-t/params[3])) ax.plot(t, y, 'r--', label='Courbe de la pente')