Challenge: 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.
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
)
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.76
Challenge: Trend Analysis of Unemployment
Swipe to show 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.
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
)
Solution
Thanks for your feedback!
single