# -*- coding: utf-8 -*-
"""
Created on Sun Jun 23 13:26:11 2024

@author: Stéphane Pasquet
"""

import matplotlib.pyplot as plt
import numpy as np

# Données de la population mondiale (en milliards)
"""annees = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020, 2023]
population = [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])



# Création du graphique
plt.figure(figsize=(10, 6))
plt.plot(annees, population, 'bo-', label='Population mondiale')
plt.xlabel('Année')
plt.ylabel('Population (en milliards)')
plt.title('Évolution de la population mondiale au fil des décennies')
plt.grid(True)
plt.legend()
plt.show()

# Ajustement du modèle de régression linéaire avec numpy
coefficients = np.polyfit(annees, population, 1)
poly1d_fn = np.poly1d(coefficients)

# Création du graphique avec la régression linéaire utilisant numpy
plt.figure(figsize=(10, 6))
plt.plot(annees, population, 'bo-', label='Population mondiale')
# Ajout du coefficient de corrélation sur le graphique
plt.text(1950, 7.2, f'Coefficient de corrélation: {round(np.corrcoef(annees,population)[0,1], 3)}', fontsize=10, color='green')

if coefficients[1] > 0:
    plt.plot(annees, poly1d_fn(annees), 'r--', label=f'Régression linéaire: y = {round(coefficients[0],3)}x + {round(coefficients[1],3)}')
else:
    plt.plot(annees, poly1d_fn(annees), 'r--', label=f'Régression linéaire: y = {round(coefficients[0],3)}x {round(coefficients[1],3)}')
plt.xlabel('Année')
plt.ylabel('Population (en milliards)')
plt.title('Évolution de la population mondiale avec régression linéaire')
plt.grid(True)
plt.legend()
plt.show()

# Coefficients de la régression linéaire
print(np.corrcoef(annees,population)[0,1])

