- GuruFinance Insights
- Posts
- Predicting Natural Gas Purchase Prices Using Linear Regression
Predicting Natural Gas Purchase Prices Using Linear Regression
100 Genius Side Hustle Ideas
Don't wait. Sign up for The Hustle to unlock our side hustle database. Unlike generic "start a blog" advice, we've curated 100 actual business ideas with real earning potential, startup costs, and time requirements. Join 1.5M professionals getting smarter about business daily and launch your next money-making venture.
Exciting News: Paid Subscriptions Have Launched! 🚀
On September 1, we officially rolled out our new paid subscription plans at GuruFinance Insights, offering you the chance to take your investing journey to the next level! Whether you're just starting or are a seasoned trader, these plans are packed with exclusive trading strategies, in-depth research paper analysis, ad-free content, monthly AMAsessions, coding tutorials for automating trading strategies, and much more.
Our three tailored plans—Starter Investor, Pro Trader, and Elite Investor—provide a range of valuable tools and personalized support to suit different needs and goals. Don’t miss this opportunity to get real-time trade alerts, access to masterclasses, one-on-one strategy consultations, and be part of our private community group. Click here to explore the plans and see how becoming a premium member can elevate your investment strategy!
Check Out Latest Premium Articles
Given the monthly gas prices from October 31, 2020, to September 30, 2024, the objective is to estimate the price on a specified date in the past and extrapolate it for one year into the future. The code will take a date as input and return a price estimate.
Strategy
Extrapolate Data Points
Utilize Linear Regression for Predictions
Modernize your marketing with AdQuick
AdQuick unlocks the benefits of Out Of Home (OOH) advertising in a way no one else has. Approaching the problem with eyes to performance, created for marketers with the engineering excellence you’ve come to expect for the internet.
Marketers agree OOH is one of the best ways for building brand awareness, reaching new customers, and reinforcing your brand message. It’s just been difficult to scale. But with AdQuick, you can easily plan, deploy and measure campaigns just as easily as digital ads, making them a no-brainer to add to your team’s toolbox.
Step 1: Understanding the Data
First, we need to convert the date format (MM/DD/YY) into a datetime format that can be utilized for analysis.
import pandas as pd
# Import data to a pandas DataFrame
df = pd.read_csv('input/Nat_Gas.csv')
df.head(5)
The DataFrame should look like this:

Next, we convert these date strings into datetime objects.
from datetime import datetime
# Convert dates to datetime format
df['Dates'] = pd.to_datetime(df['Dates'])
# Set index as datetime format
df.set_index('Dates', inplace=True)
df.head(5)
Output will be:

Step 2: Visualizing Historical Prices
To better understand the data, we can visualize the historical prices.
import matplotlib.pyplot as plt
# Plot the data
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Prices'], marker='o')
plt.title('Monthly Natural Gas Prices')
plt.xlabel('Date')
plt.ylabel('Price (in $)')
plt.grid()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Marketing ideas for marketers who hate boring
The best marketing ideas come from marketers who live it. That’s what The Marketing Millennials delivers: real insights, fresh takes, and no fluff. Written by Daniel Murray, a marketer who knows what works, this newsletter cuts through the noise so you can stop guessing and start winning. Subscribe and level up your marketing game.
Step 3: Predicting Future Prices
Now we apply linear regression to interpolate and extrapolate the data.
import numpy as np
from sklearn.linear_model import LinearRegression
def estimate_price(input_date):
input_date = pd.to_datetime(input_date)
# Extrapolate data for 12 months
future_dates = [input_date + timedelta(days=30 * i) for i in range(1, 13)]
# Prepare data for linear regression
df['Date_Ordinal'] = df.index.map(datetime.toordinal)
X = df['Date_Ordinal'].values.reshape(-1, 1)
y = df['Prices'].values
model = LinearRegression()
model.fit(X, y)
# Predict price for the input date
input_date_ordinal = np.array(input_date.toordinal()).reshape(-1, 1)
predicted_price = model.predict(input_date_ordinal)[0]
# Predict future prices
future_dates_ordinal = np.array([date.toordinal() for date in future_dates]).reshape(-1, 1)
future_prices = model.predict(future_dates_ordinal)
return predicted_price, future_dates, future_prices
input_date = "04/15/2023" # Change to the desired date
estimated_price, future_dates, future_prices = estimate_price(input_date)
print(f"Estimated price for {input_date}: ${estimated_price:.2f}")
for date, price in zip(future_dates, future_prices):
print(f"Estimated price for {date.date()}: ${price:.2f}")
Example Output
Estimated price for 2023-04-15: $11.44
Estimated price for 2023-05-15: $11.48
Estimated price for 2023-06-14: $11.52
...
Estimated price for 2024-04-09: $11.90
Step 4: Visualizing Predictions
Finally, let’s visualize both the historical data and the predictions.
plt.figure(figsize=(10, 6))
# Plot historical data
plt.plot(df.index, df['Prices'], label='Historical Prices', color='blue')
# Input date line
plt.axvline(pd.to_datetime(input_date), color='red', linestyle='--', label='Input Date')
# Future predictions
plt.plot(future_dates, future_prices, label='Future Predictions', color='orange', marker='o')
# Formatting the plot
plt.title('Price Estimation and Future Predictions')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid()
plt.xticks(rotation=45)
plt.tight_layout()
# Show plot
plt.show()

This comprehensive approach provides a clear understanding of how to predict natural gas prices using linear regression, allowing for both historical analysis and future estimations.Given the monthly gas prices from October 31, 2020, to September 30, 2024, the objective is to estimate the price on a specified date in the past and extrapolate it for one year into the future. The code will take a date as input and return a price estimate.
Strategy
Extrapolate Data Points
Utilize Linear Regression for Predictions
Step 1: Understanding the Data
First, we need to convert the date format (MM/DD/YY) into a datetime format that can be utilized for analysis.
import pandas as pd
# Import data to a pandas DataFrame
df = pd.read_csv('input/Nat_Gas.csv')
df.head(5)
The DataFrame should look like this:

Next, we convert these date strings into datetime objects.
from datetime import datetime
# Convert dates to datetime format
df['Dates'] = pd.to_datetime(df['Dates'])
# Set index as datetime format
df.set_index('Dates', inplace=True)
df.head(5)
Output will be:

Step 2: Visualizing Historical Prices
To better understand the data, we can visualize the historical prices.
import matplotlib.pyplot as plt
# Plot the data
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Prices'], marker='o')
plt.title('Monthly Natural Gas Prices')
plt.xlabel('Date')
plt.ylabel('Price (in $)')
plt.grid()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Step 3: Predicting Future Prices
Now we apply linear regression to interpolate and extrapolate the data.
import numpy as np
from sklearn.linear_model import LinearRegression
def estimate_price(input_date):
input_date = pd.to_datetime(input_date)
# Extrapolate data for 12 months
future_dates = [input_date + timedelta(days=30 * i) for i in range(1, 13)]
# Prepare data for linear regression
df['Date_Ordinal'] = df.index.map(datetime.toordinal)
X = df['Date_Ordinal'].values.reshape(-1, 1)
y = df['Prices'].values
model = LinearRegression()
model.fit(X, y)
# Predict price for the input date
input_date_ordinal = np.array(input_date.toordinal()).reshape(-1, 1)
predicted_price = model.predict(input_date_ordinal)[0]
# Predict future prices
future_dates_ordinal = np.array([date.toordinal() for date in future_dates]).reshape(-1, 1)
future_prices = model.predict(future_dates_ordinal)
return predicted_price, future_dates, future_prices
input_date = "04/15/2023" # Change to the desired date
estimated_price, future_dates, future_prices = estimate_price(input_date)
print(f"Estimated price for {input_date}: ${estimated_price:.2f}")
for date, price in zip(future_dates, future_prices):
print(f"Estimated price for {date.date()}: ${price:.2f}")
Example Output
Estimated price for 2023-04-15: $11.44
Estimated price for 2023-05-15: $11.48
Estimated price for 2023-06-14: $11.52
...
Estimated price for 2024-04-09: $11.90
Step 4: Visualizing Predictions
Finally, let’s visualize both the historical data and the predictions.
plt.figure(figsize=(10, 6))
# Plot historical data
plt.plot(df.index, df['Prices'], label='Historical Prices', color='blue')
# Input date line
plt.axvline(pd.to_datetime(input_date), color='red', linestyle='--', label='Input Date')
# Future predictions
plt.plot(future_dates, future_prices, label='Future Predictions', color='orange', marker='o')
# Formatting the plot
plt.title('Price Estimation and Future Predictions')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid()
plt.xticks(rotation=45)
plt.tight_layout()
# Show plot
plt.show()

This comprehensive approach provides a clear understanding of how to predict natural gas prices using linear regression, allowing for both historical analysis and future estimations.