Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Inflation Rate Forecast | Economic Modeling and Regression
Python for Economists

bookChallenge: Inflation Rate Forecast

In this challenge, you will apply your knowledge of linear regression and data visualization to forecast future inflation rates. You will work with a hardcoded pandas DataFrame containing annual inflation rates for a country over the past 15 years. Your goal is to write a function that fits a linear regression model to this data and then uses it to predict the inflation rate for the next two years. Additionally, you will visualize both the historical and forecasted inflation rates using matplotlib to clearly communicate the results.

To begin, consider how economic forecasting often relies on historical trends to estimate future values. By fitting a linear regression model to past inflation data, you can capture the underlying trend and use it to make informed predictions. Plotting the results allows you to compare the actual historical data with your model's forecasts, which is a common practice in economic reporting and analysis.

You will need to use pandas to manage the data, scikit-learn's LinearRegression to fit the model, and matplotlib to create the visualization. The function you write should take no arguments, operate on the provided DataFrame, and return the two forecasted inflation rates as a tuple. The plot should show the historical inflation rates as well as the regression line extended to include the two forecasted years.

Tâche

Swipe to start coding

Write a function named forecast_inflation that does the following:

  • Creates a pandas DataFrame with two columns: "Year" (integers for the last 15 years, ending with 2023) and "InflationRate" (15 realistic annual inflation rates, hardcoded as floats).
  • Fits a linear regression model using scikit-learn to predict InflationRate based on Year.
  • Forecasts the inflation rate for the next two years (2024 and 2025) and returns these values as a tuple.
  • Plots the historical inflation rates as blue dots, the regression line for all years (2009–2025) as a red line, and highlights the forecasted points (2024, 2025) as green dots.
  • Labels the axes and adds a legend.

Your function should not take any arguments and should return a tuple of two predicted inflation rates for 2024 and 2025.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 7
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookChallenge: Inflation Rate Forecast

Glissez pour afficher le menu

In this challenge, you will apply your knowledge of linear regression and data visualization to forecast future inflation rates. You will work with a hardcoded pandas DataFrame containing annual inflation rates for a country over the past 15 years. Your goal is to write a function that fits a linear regression model to this data and then uses it to predict the inflation rate for the next two years. Additionally, you will visualize both the historical and forecasted inflation rates using matplotlib to clearly communicate the results.

To begin, consider how economic forecasting often relies on historical trends to estimate future values. By fitting a linear regression model to past inflation data, you can capture the underlying trend and use it to make informed predictions. Plotting the results allows you to compare the actual historical data with your model's forecasts, which is a common practice in economic reporting and analysis.

You will need to use pandas to manage the data, scikit-learn's LinearRegression to fit the model, and matplotlib to create the visualization. The function you write should take no arguments, operate on the provided DataFrame, and return the two forecasted inflation rates as a tuple. The plot should show the historical inflation rates as well as the regression line extended to include the two forecasted years.

Tâche

Swipe to start coding

Write a function named forecast_inflation that does the following:

  • Creates a pandas DataFrame with two columns: "Year" (integers for the last 15 years, ending with 2023) and "InflationRate" (15 realistic annual inflation rates, hardcoded as floats).
  • Fits a linear regression model using scikit-learn to predict InflationRate based on Year.
  • Forecasts the inflation rate for the next two years (2024 and 2025) and returns these values as a tuple.
  • Plots the historical inflation rates as blue dots, the regression line for all years (2009–2025) as a red line, and highlights the forecasted points (2024, 2025) as green dots.
  • Labels the axes and adds a legend.

Your function should not take any arguments and should return a tuple of two predicted inflation rates for 2024 and 2025.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 7
single

single

some-alt