Time Series forecasting of natural gas prices with Python

A common task in finance is forecasting values. There are several methods for creating forecasts such as ARIMA, Bayesian Structural Time Series, and simulation. Monte Carlo is a simulation technique based on generating random walks over a period of time.

Kyle Jones
6 min readOct 26, 2023

In this tutorial, we will:

  1. fetch Natural Gas prices
  2. Simulate future price movements using Monte Carlo
  3. Plot the simulated future paths
  4. Calculate future values using ARIMA and Bayesian Timeseries

We start by importing the modules we will need. We are going to access the price of natural gas from Financial Modeling Prep, a web API. You need an API key from them to pull the data.

# getting historical data for US Natural Gas prices. 
# This code calls the API and transforms the result into a DataFrame.

import numpy as np
np.random.seed(3363)
import pandas as pd

from scipy.stats import norm
import datetime

import matplotlib.pyplot as plt
%matplotlib inline
ticker = "NGUSD" #US natural gas
base = 'https://financialmodelingprep.com/api/v3/'
key = YOUR_KEY
target = "{}historical-price-full/{}?apikey={}".format(base, ticker, key)

df = pd.read_json(target)
df = pd.json_normalize(df['historical'])
df['date'] =…

--

--

Kyle Jones

I’m a cloud architect, project manager, and analytics enthusiast. Opinions are my own.