Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Trend Analysis of Unemployment | Advanced Economic Analysis and Visualization
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Economists

bookChallenge: Trend Analysis of Unemployment

In your journey through economic data analysis, you have encountered various techniques for handling and visualizing time series. One particularly insightful approach for understanding labor market trends is analyzing unemployment rates over time. Suppose you are given a pandas Series containing monthly unemployment rates for a country spanning five years. Your goal is to create a function that calculates the 12-month moving average of the unemployment rate, visualizes the data, and highlights periods where unemployment is consistently rising or falling.

To achieve this, you will first need to smooth the original data using a moving average. This helps you see the underlying trend without being distracted by short-term fluctuations. Next, by examining the differences in the moving average, you can pinpoint stretches of time where unemployment is increasing or decreasing. Annotating these periods directly on your plot will make the visualization much more informative for economists and policy analysts.

Uppgift

Swipe to start coding

Write a function named plot_unemployment_trend that takes a pandas Series of monthly unemployment rates and:

  • Calculates the 12-month moving average.
  • Identifies periods where the moving average is consistently increasing or decreasing.
  • Plots both the original data and the moving average using matplotlib.
  • Annotates increasing periods with "Increasing" (in red) and decreasing periods with "Decreasing" (in green) on the plot.

The function should display the plot. Use the following hardcoded Series for testing:

import pandas as pd

dates = pd.date_range(start="2018-01-01", periods=60, freq="M")
unemployment = pd.Series(
    [5.2, 5.3, 5.1, 5.0, 5.0, 5.2, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9,
     6.0, 6.1, 6.2, 6.1, 6.0, 5.9, 5.8, 5.7, 5.6, 5.5, 5.4, 5.3,
     5.2, 5.1, 5.0, 4.9, 4.8, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3,
     5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3, 6.4, 6.5,
     6.6, 6.7, 6.6, 6.5, 6.4, 6.3, 6.2, 6.1, 6.0, 5.9, 5.8, 5.7],
    index=dates
)

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 7
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you provide an example of the unemployment rate data format?

How do I highlight the rising and falling periods on the plot?

What libraries should I use for visualization?

close

bookChallenge: Trend Analysis of Unemployment

Svep för att visa menyn

In your journey through economic data analysis, you have encountered various techniques for handling and visualizing time series. One particularly insightful approach for understanding labor market trends is analyzing unemployment rates over time. Suppose you are given a pandas Series containing monthly unemployment rates for a country spanning five years. Your goal is to create a function that calculates the 12-month moving average of the unemployment rate, visualizes the data, and highlights periods where unemployment is consistently rising or falling.

To achieve this, you will first need to smooth the original data using a moving average. This helps you see the underlying trend without being distracted by short-term fluctuations. Next, by examining the differences in the moving average, you can pinpoint stretches of time where unemployment is increasing or decreasing. Annotating these periods directly on your plot will make the visualization much more informative for economists and policy analysts.

Uppgift

Swipe to start coding

Write a function named plot_unemployment_trend that takes a pandas Series of monthly unemployment rates and:

  • Calculates the 12-month moving average.
  • Identifies periods where the moving average is consistently increasing or decreasing.
  • Plots both the original data and the moving average using matplotlib.
  • Annotates increasing periods with "Increasing" (in red) and decreasing periods with "Decreasing" (in green) on the plot.

The function should display the plot. Use the following hardcoded Series for testing:

import pandas as pd

dates = pd.date_range(start="2018-01-01", periods=60, freq="M")
unemployment = pd.Series(
    [5.2, 5.3, 5.1, 5.0, 5.0, 5.2, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9,
     6.0, 6.1, 6.2, 6.1, 6.0, 5.9, 5.8, 5.7, 5.6, 5.5, 5.4, 5.3,
     5.2, 5.1, 5.0, 4.9, 4.8, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3,
     5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3, 6.4, 6.5,
     6.6, 6.7, 6.6, 6.5, 6.4, 6.3, 6.2, 6.1, 6.0, 5.9, 5.8, 5.7],
    index=dates
)

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 7
single

single

some-alt