# -*- coding: utf-8 -*-
"""
Created on Sun Jun 23 14:05:42 2024

@author: Stéphane Pasquet
"""

import numpy as np
import matplotlib.pyplot as plt

# Données
"""annees = np.array([1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020, 2023])
population = np.array([2.5, 3, 3.7, 4.4, 5.3, 6.1, 6.9, 7.8, 8.0])"""
annees = np.array([1850, 1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020, 2023])
population = np.array([1.25, 1.66, 1.75, 1.86, 2.07, 2.3, 2.5, 3, 3.7, 4.4, 5.3, 6.1, 6.9, 7.8, 8.0])


# Ajustement du modèle de régression quadratique
coefficients_quadratic = np.polyfit(annees, population, 2)
a, b, c = coefficients_quadratic[0], coefficients_quadratic[1], coefficients_quadratic[2]
poly2d_fn = np.poly1d(coefficients_quadratic)

# Calcul du coefficient de corrélation
predictions = poly2d_fn(annees)
correlation_coefficient = np.corrcoef(population, predictions)[0, 1]

# Création du graphique avec la régression quadratique
plt.figure(figsize=(10, 6))
plt.plot(annees, population, 'bo-', label='Population mondiale')
plt.plot(annees, poly2d_fn(annees), 'r--', label=f'Régression quadratique: y = {a:.4e}x² + {b:.4e}x + {c:.4e}')
plt.text(1950, 7.2, f'Coefficient de corrélation: {round(correlation_coefficient, 8)}', fontsize=10, color='green')
plt.xlabel('Année')
plt.ylabel('Population (en milliards)')
plt.title('Évolution de la population mondiale avec régression quadratique')
plt.grid(True)
plt.legend()
plt.show()

# Affichage des coefficients
print("Coefficients de la régression quadratique:", correlation_coefficient)
