Files
strategy-lab/to_explore/pyquantnews/27_Decomposition.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

195 lines
4.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "f265ef79",
"metadata": {},
"source": [
"<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
]
},
{
"cell_type": "markdown",
"id": "d7770571",
"metadata": {},
"source": [
"This code retrieves and analyzes unemployment data, focusing on trends and seasonality. It uses the OpenBB SDK to fetch unemployment data from 2010 to 2019. The data is then processed to calculate rolling statistics and visualized. Seasonal decomposition and STL decomposition are applied to understand the seasonal and trend components. Additionally, the Hodrick-Prescott filter is used to separate the cyclical and trend components of the data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "60656fb6",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1436ce1d",
"metadata": {},
"outputs": [],
"source": [
"from statsmodels.tsa.seasonal import seasonal_decompose, STL\n",
"from statsmodels.tsa.filters.hp_filter import hpfilter\n",
"from openbb_terminal.sdk import openbb"
]
},
{
"cell_type": "markdown",
"id": "566d605d",
"metadata": {},
"source": [
"Retrieve unemployment data from OpenBB SDK for the period starting 2010"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df30c6a4",
"metadata": {},
"outputs": [],
"source": [
"df = openbb.economy.unemp(2010)"
]
},
{
"cell_type": "markdown",
"id": "e7a42626",
"metadata": {},
"source": [
"Set the index to the 'date' column, filter up to 2019-12-31, and sort by date"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cbcf68cf",
"metadata": {},
"outputs": [],
"source": [
"df = df.set_index(\"date\")[:\"2019-12-31\"].sort_index()"
]
},
{
"cell_type": "markdown",
"id": "9b4b91cd",
"metadata": {},
"source": [
"Calculate rolling mean and standard deviation with a 12-month window"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e28f9215",
"metadata": {},
"outputs": [],
"source": [
"df[\"rolling_mean\"] = df[\"unemp\"].rolling(window=12).mean()\n",
"df[\"rolling_std\"] = df[\"unemp\"].rolling(window=12).std()"
]
},
{
"cell_type": "markdown",
"id": "e5863458",
"metadata": {},
"source": [
"Plot the unemployment rate with rolling mean and standard deviation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c10e8025",
"metadata": {},
"outputs": [],
"source": [
"df.plot(title=\"Unemployment rate\")"
]
},
{
"cell_type": "markdown",
"id": "17630ce4",
"metadata": {},
"source": [
"Perform seasonal decomposition of the unemployment data using an additive model and plot results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52f48f1d",
"metadata": {},
"outputs": [],
"source": [
"decomposition_results = seasonal_decompose(\n",
" df[\"unemp\"], \n",
" model=\"additive\"\n",
").plot()"
]
},
{
"cell_type": "markdown",
"id": "47f1d516",
"metadata": {},
"source": [
"Apply STL decomposition to the unemployment data and plot the results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a75ed137",
"metadata": {},
"outputs": [],
"source": [
"stl_decomposition = STL(df[[\"unemp\"]]).fit()\n",
"stl_decomposition.plot().suptitle(\"STL Decomposition\");"
]
},
{
"cell_type": "markdown",
"id": "b61552be",
"metadata": {},
"source": [
"Apply Hodrick-Prescott filter to decompose the unemployment data into cycle and trend components and plot results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36c7a50e",
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [],
"source": [
"hp_df = df[[\"unemp\"]].copy()\n",
"hp_df[\"cycle\"], hp_df[\"trend\"] = hpfilter(hp_df[\"unemp\"], 129600)\n",
"hp_df.plot(subplots=True, title=\"Hodrick-Prescott filter\");"
]
},
{
"cell_type": "markdown",
"id": "c508c532",
"metadata": {},
"source": [
"<a href=\"https://pyquantnews.com/\">PyQuant News</a> is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to <a href=\"https://gettingstartedwithpythonforquantfinance.com/\">get started with Python for quant finance</a>. For educational purposes. Not investment advise. Use at your own risk."
]
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all",
"main_language": "python",
"notebook_metadata_filter": "-all"
}
},
"nbformat": 4,
"nbformat_minor": 5
}