Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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.

Taak

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
)

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 7
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

close

bookChallenge: Trend Analysis of Unemployment

Veeg om het menu te tonen

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.

Taak

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
)

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 7
single

single

some-alt