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

Compito

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
)

Soluzione

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 7
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

close

bookChallenge: Trend Analysis of Unemployment

Scorri per mostrare il menu

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.

Compito

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
)

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 7
single

single

some-alt