Will the Recession Ever Arrive? A Data-Driven Look at the Yield Curve and Jobless Rate

I’m always monitoring economic data for clues about where the broader market and sentiment might be headed. Since I’m a visual thinker, I like to create charts—like my egg price and median house price charts. In this post, I’ll share my Python code for generating a chart that overlays the 2-year/10-year Treasury yield spread with the current jobless rate, highlighting recession periods for context. I track this chart closely because the 2-year/10-year spread has historically predicted recessions whenever it turns negative. However, the timing of a recession remains uncertain. This is where jobless rates become a crucial indicator—when employers start shedding jobs, the likelihood of a recession increases. ...

Generate Chart of Median House Prices With Mortgage Rates And Recessions in Python

In case you want to generate a beautiful chart in Python of median house prices with mortgage rates and recessions overlaid, here’s the code. It’s all self explanatory but if you need help, drop me a comment below. import pandas as pd import matplotlib.pyplot as plt from pandas_datareader.data import DataReader from datetime import datetime save_path = "./results/median-house-price-with-mortgage-rates.png" # Fetch median sales price data from FRED def fetch_median_sales_price(): """ Fetches Median Sales Price of Houses in the US from FRED. Returns: DataFrame with date index and median sales price. """ start_date = "1988-01-01" end_date = datetime.now() median_sales_price = DataReader("MSPNHSUS", "fred", start_date, end_date) median_sales_price.rename(columns={"MSPNHSUS": "Median Sales Price"}, inplace=True) return median_sales_price # Fetch mortgage rates from FRED def fetch_mortgage_rates(): """ Fetches 30-Year Fixed Mortgage Rates in the US from FRED. Returns: DataFrame with date index and mortgage rates. """ start_date = "1988-01-01" end_date = datetime.now() mortgage_rates = DataReader("MORTGAGE30US", "fred", start_date, end_date) mortgage_rates.rename(columns={"MORTGAGE30US": "Mortgage Rates"}, inplace=True) return mortgage_rates # Fetch recession data from FRED def fetch_recession_data(): """ Fetches US recession data from FRED. Returns: DataFrame with date index and recession indicators. """ start_date = "1988-01-01" end_date = datetime.now() recession_data = DataReader("USREC", "fred", start_date, end_date) return recession_data def get_recession_periods(recessions): """ Extracts periods of recessions. Args: recessions: DataFrame containing recession indicators (1 for recession, 0 otherwise). Returns: List of tuples representing start and end dates of recessions. """ recession_periods = [] in_recession = False start_date = None for date, value in recessions["USREC"].items(): if value == 1 and not in_recession: in_recession = True start_date = date elif value == 0 and in_recession: in_recession = False end_date = date recession_periods.append((start_date, end_date)) return recession_periods def plot_median_sales_price_and_mortgage_rates(median_sales_price, mortgage_rates, recession_periods): """ Plots the Median Sales Price of Houses and Mortgage Rates with recession periods. Args: median_sales_price: DataFrame with median sales price data. mortgage_rates: DataFrame with mortgage rate data. recession_periods: List of recession periods (start and end dates). """ fig, ax1 = plt.subplots(figsize=(14, 8)) # Plot median sales price ax1.plot(median_sales_price.index, median_sales_price["Median Sales Price"], color="blue", linewidth=1.5, label="Median Sales Price") ax1.set_xlabel("Year", fontsize=14, labelpad=10) ax1.set_ylabel("Median Sales Price (USD)", fontsize=14, labelpad=10, color="blue") ax1.tick_params(axis="y", labelcolor="blue") ax1.grid(visible=True, which="major", linestyle="--", linewidth=0.5, alpha=0.7) # Add recession periods for start_date, end_date in recession_periods: ax1.axvspan(start_date, end_date, color="gray", alpha=0.3, label="Recession" if start_date == recession_periods[0][0] else "") # Plot mortgage rates ax2 = ax1.twinx() ax2.plot(mortgage_rates.index, mortgage_rates["Mortgage Rates"], color="green", linewidth=1.5, linestyle="--", label="Mortgage Rates") ax2.set_ylabel("Mortgage Rates (%)", fontsize=14, labelpad=10, color="green") ax2.tick_params(axis="y", labelcolor="green") # Combine legends lines, labels = ax1.get_legend_handles_labels() lines2, labels2 = ax2.get_legend_handles_labels() ax1.legend(lines + lines2, labels + labels2, fontsize=12, loc="upper left") # Title and layout plt.title("Median Sales Price of Houses and Mortgage Rates with Recession Periods", fontsize=18, fontweight="bold", pad=20) plt.tight_layout() plt.savefig(save_path, dpi=300, bbox_inches="tight") plt.show() if __name__ == "__main__": try: median_sales_price = fetch_median_sales_price() mortgage_rates = fetch_mortgage_rates() recession_data = fetch_recession_data() recession_periods = get_recession_periods(recession_data) plot_median_sales_price_and_mortgage_rates(median_sales_price, mortgage_rates, recession_periods) except Exception as e: print(f"An error occurred: {e}")

LIBSVM - A Library For Support Vector Machines

It looks like the old LibSVM library is still kicking around and being updated. Last release was a bug fix on July 2023. Version 3.32 released on July 9, 2023. We fix some minor bugs. Plus changes how to use in Python: > pip install -U libsvm-official The python directory is re-organized so >>> from libsvm.svmutil import * instead of >>> from svmutil import * Via Webpage

Regular Expressions for Data Scientists in Python

An oldie but goodie. Regular Expressions are a must for anybody using Python, doing Data Science, or just ETL’ing data. Regular expressions (regex) are essentially text patterns that you can use to automate searching through and replacing elements within strings of text. This can make cleaning and working with text-based data sets much easier, saving you the trouble of having to search through mountains of text by hand. Via Dataquest ...

Stock Seasonality in Python - A Tutorial

A Python script that evaluates seasonal investing

Rebasing Time Series With Python

Rebasing a time series using Pandas and plotting stock price performance. Then extracting the month and year values.

Sentiment Analysis of Tweets With Python, NLTK, Word2vec & Scikit-learn - Marcin Zabłocki blog

A good tutorial on how do advanced sentiment analysis. The goal of this project was to predict sentiment for the given Twitter post using Python. Sentiment analysis can predict many different emotions attached to the text, but in this report only 3 major were considered: positive, negative and neutral. The training dataset was small (just over 5900 examples) and the data within it was highly skewed, which greatly impacted on the difficulty of building good classifier. After creating a lot of custom features, utilizing both bag-of-words and word2vec representations and applying the Extreme Gradient Boosting algorithm, the classification accuracy at level of 58% was achieved. - via Zablo.net ...

FB Prophet Forecasting

This tutorial will show you how many followers you should expect in the coming days and weeks using the Facebook Prophet (FBProphet) python package. This is a perfect example of what python can be used for. You can use it to forecast your medium followers or sales revenues. It can be any time series related problem, but first you must install FBProphet. Install FBProphet My code is simple, and the FB Prophet documentation is written very well. In fact, most of the code I share below has been cobbled together from the examples the documentation provides. ...

Python to the Rescue

I’m evaluating whether or not I should move this blog to another CMS platform so I can start building a community around it like it was before. Right now this blog runs on Hugo and AWS Amplify and it’s freaking awesome. I push new posts to GitHub, AWS pulls the changes and rebuilds the site, and then I can look at make sure it looks fine before I merge into master. ...

H2O Wave App Tutorials

Just like what I did with my general H2O-3 Tutorials, I’m starting a seperate H2O Wave Tutorial post for you all here. I will add to this as I go along and build more apps. Stock Chart Generator Wave App This is a simple candlestick chart generator that defaults to JNJ when you load it. All you need to do is add your Alphavantage API key where it says "apikey": "XXXXXXX" } #ENTER YOUR ALPHAVANTAGE KEY HERE ...