0% found this document useful (0 votes)
7 views1 page

Code

The document outlines a Python script that analyzes historical natural gas prices using pandas and numpy. It includes data loading, feature engineering for trend and seasonality, visualization of the price trends, and a function to estimate future gas prices based on a linear trend and seasonal adjustments. Finally, it generates a one-year extrapolation of estimated prices and prints the results.

Uploaded by

amisharishabh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Code

The document outlines a Python script that analyzes historical natural gas prices using pandas and numpy. It includes data loading, feature engineering for trend and seasonality, visualization of the price trends, and a function to estimate future gas prices based on a linear trend and seasonal adjustments. Finally, it generates a one-year extrapolation of estimated prices and prints the results.

Uploaded by

amisharishabh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import pandas as pd

import numpy as np
import [Link] as plt

# ----------------------------
# Load data (from your table)
# ----------------------------
data = {
"Date": [
"10/31/20","11/30/20","12/31/20","1/31/21","2/28/21","3/31/21",
"4/30/21","5/31/21","6/30/21","7/31/21","8/31/21","9/30/21",
"10/31/21","11/30/21","12/31/21","1/31/22","2/28/22","3/31/22",
"4/30/22","5/31/22","6/30/22","7/31/22","8/31/22","9/30/22",
"10/31/22","11/30/22","12/31/22","1/31/23","2/28/23","3/31/23",
"4/30/23","5/31/23","6/30/23","7/31/23","8/31/23","9/30/23",
"10/31/23","11/30/23","12/31/23","1/31/24","2/29/24","3/31/24",
"4/30/24","5/31/24","6/30/24","7/31/24","8/31/24","9/30/24"
],
"Price": [
1.01E+01,1.03E+01,1.10E+01,1.09E+01,1.09E+01,1.09E+01,
1.04E+01,9.84E+00,1.00E+01,1.01E+01,1.03E+01,1.02E+01,
1.01E+01,1.12E+01,1.14E+01,1.15E+01,1.18E+01,1.15E+01,
1.07E+01,1.07E+01,1.04E+01,1.05E+01,1.04E+01,1.08E+01,
1.10E+01,1.16E+01,1.16E+01,1.21E+01,1.17E+01,1.20E+01,
1.15E+01,1.12E+01,1.09E+01,1.14E+01,1.11E+01,1.15E+01,
1.18E+01,1.22E+01,1.28E+01,1.26E+01,1.24E+01,1.27E+01,
1.21E+01,1.14E+01,1.15E+01,1.16E+01,1.15E+01,1.18E+01
]
}

df = [Link](data)
df["Date"] = pd.to_datetime(df["Date"])
df = df.sort_values("Date")

# ----------------------------
# Feature engineering
# ----------------------------
df["t"] = [Link](len(df)) # time index
df["month"] = df["Date"].[Link] # month for seasonality

# ----------------------------
# Trend model (linear)
# ----------------------------
trend_coeffs = [Link](df["t"], df["Price"], 1)
trend_model = np.poly1d(trend_coeffs)
df["trend"] = trend_model(df["t"])

# ----------------------------
# Seasonality (monthly)
# ----------------------------
seasonality = (df["Price"] - df["trend"]).groupby(df["month"]).mean()

# ----------------------------
# Visualization
# ----------------------------
[Link]()
[Link](df["Date"], df["Price"], label="Actual Price")
[Link](df["Date"], df["trend"], linestyle="--", label="Trend")
[Link]("Date")
[Link]("Natural Gas Price")
[Link]("Monthly Natural Gas Prices")
[Link]()
[Link]()

# ----------------------------
# Price estimation function
# ----------------------------
def estimate_gas_price(date):
date = pd.to_datetime(date)

t = ([Link] - df["Date"].iloc[0].year) * 12 + \
([Link] - df["Date"].iloc[0].month)

trend_price = trend_model(t)
seasonal_adj = [Link]([Link], 0)

return round(float(trend_price + seasonal_adj), 2)

# ----------------------------
# One-year extrapolation
# ----------------------------
future_dates = pd.date_range(
start=df["Date"].iloc[-1] + [Link](1),
periods=12,
freq="M"
)

future_prices = [estimate_gas_price(d) for d in future_dates]

future_df = [Link]({
"Date": future_dates,
"Estimated Price": future_prices
})

print(future_df)

You might also like