Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Trend Analysis of Unemployment | Advanced Economic Analysis and Visualization
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.

Opgave

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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Trend Analysis of Unemployment

Stryg for at vise menuen

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.

Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 7
single

single

some-alt