← Back to list

Modeling Stock Prices Using Fourier Neural Operators

Introduction

shashank Jain in GoPenAI · 2024-08-28 08:47 · 45 claps · 5.0 min read paywalled
#fno #fourier-neural-operators #time-series-forecasting #time-series-analysis #fourier-time-series
Open on Medium ↗

Modeling Stock Prices Using Fourier Neural Operators

Introduction

In the world of finance, predicting stock prices is a complex and challenging task. Traditional methods like time series analysis or simple machine learning models often struggle to capture the intricacies of stock movements, especially when dealing with vast datasets. Enter Fourier Neural Operators (FNOs), an innovative deep learning approach that leverages the power of frequency-domain analysis to model complex patterns and interactions in data.

In this blog, we’ll explore what Fourier Neural Operators are, delve into the concept of function spaces in infinitesimal dimensions, and demonstrate how we can model stock prices using FNOs. We’ll also provide a detailed architecture of our solution, accompanied by a block-by-block explanation of the code.

Understanding Fourier Neural Operators (FNOs) and Function Spaces

What Are Fourier Neural Operators (FNOs)?

Fourier Neural Operators are a class of neural networks designed to learn operators that map functions to functions in a resolution-invariant manner. Unlike traditional neural networks that learn mappings between finite-dimensional vectors, FNOs operate in the frequency domain, learning patterns and dependencies across all frequencies present in the data.

Why Use FNOs?

  • Global Pattern Recognition: FNOs analyze the entire dataset in the frequency domain, capturing both local (short-term) and global (long-term) patterns.
  • Resolution Invariance: They can generalize across different resolutions, making them particularly useful for datasets that vary in granularity.
  • Efficiency: The use of Fourier transforms allows FNOs to efficiently compute convolutions in the frequency domain, often leading to faster computations compared to traditional methods.

What Are Function Spaces in Infinitesimal Dimensions?

To understand FNOs, we need to understand the concept of function spaces.

  • Function Spaces: Think of a function space as a collection of functions that share certain properties, such as continuity or differentiability. For example, in stock price modeling, the prices over time can be considered as a continuous function defined over the time domain.
  • Infinitesimal Spaces: When we talk about infinitesimal spaces, we’re referring to spaces where every point can represent an infinitesimally small segment of a function. This allows us to consider not just the values at discrete points but the behavior of the function in between those points.

In the context of FNOs, we’re working in these continuous function spaces. By transforming stock prices into the frequency domain, we treat the entire dataset as a continuous function that can be decomposed into its constituent frequencies. This allows us to capture the full “story” of the stock movements, from slow, overarching trends to rapid, day-to-day fluctuations.

Modeling Stock Prices Using Fourier Neural Operators

Why Model Stock Prices with FNOs?

Stock prices are inherently complex and influenced by numerous factors. Modeling them as functions in a continuous space enables us to capture both short-term fluctuations and long-term trends. By using Fourier Neural Operators, we can analyze the frequency components of stock price data and predict future movements based on patterns identified in the past.

Architecture of Our Solution

  1. Data Preparation: Fetch historical stock price data from Yahoo Finance for multiple Indian IT stocks over a year.
  2. Fourier Transform: Convert the price data into the frequency domain using Fourier transforms. This step decomposes the time series data into its sine and cosine components.
  3. Neural Network Model: Use a convolutional neural network in the frequency domain to learn patterns and interactions between different frequencies.
  4. Inverse Fourier Transform: Convert the processed data back into the time domain to obtain predicted stock prices.
  5. Prediction and Visualization: Predict future stock prices based on historical data and visualize the results to evaluate model performance.

Code Explanation: Block by Block

  1. Data Preparation: Fetching and Cleaning Stock Data

We begin by downloading historical stock price data for selected Indian IT stocks using the yfinance library. This data is preprocessed to remove any missing values.

import yfinance as yf import pandas as pd import torch

Download one year of daily closing prices for selected Indian IT stocks

tickers = [‘TCS.NS’, ‘INFY.NS’, ‘WIPRO.NS’, ‘HCLTECH.NS’, ‘TECHM.NS’] data = yf.download(tickers, period=’1y’, interval=’1d’)[‘Close’] data = data.dropna() # Drop missing values

Convert to numpy and then to torch tensors

prices = data.values # Shape: (number of days, number of stocks) prices_tensor = torch.tensor(prices, dtype=torch.float32) # Convert to a PyTorch tensor

This block initializes the data required for our model, transforming it into a format suitable for processing by PyTorch.

2. Defining the Fourier Neural Operator Model

Our model consists of three main components: Fourier Transform Layer: Converts the input data into the frequency domain.

  • Convolution Layer: Applies convolution operations in the frequency domain to learn complex patterns.
  • Inverse Fourier Transform: Converts the data back to the time domain.

class FourierConvolutionModel(nn.Module): def init(self): super(FourierConvolutionModel, self).init() self.conv1 = nn.Conv1d(in_channels=10, out_channels=10, kernel_size=3, padding=1) self.relu = nn.ReLU() self.fc = nn.Linear(10 * 60, 5) # Fully connected layer to predict prices for 5 stocks

def forward(self, x):

Combine real and imaginary parts along the channel dimension

x = torch.cat([x.real, x.imag], dim=1) x = self.conv1(x) # Convolution in frequency domain x = fft.ifft(x, dim=2).real # Inverse Fourier Transform on the real part x = x.view(x.size(0), -1) x = self.relu(x) x = self.fc(x) return x This block defines our model’s architecture, leveraging both linear and non-linear transformations to predict future prices based on historical data.

  1. Training the Model

We train our model using a sliding window approach, where each window represents 60 days of data. The model learns to predict the stock prices for the next day based on this window.

Prepare sliding windows for training

window_size = 60 X_train, y_train = [], []

for i in range(len(prices_tensor) — window_size): X_train.append(prices_tensor[i:i+window_size]) y_train.append(prices_tensor[i+window_size])

X_train = torch.stack(X_train) y_train = torch.stack(y_train)

Training loop

epochs = 100 for epoch in range(epochs): model.train() optimizer.zero_grad() X_fft = fft.fft(X_train, dim=1) # Fourier Transform output = model(X_fft.permute(0, 2, 1)) loss = criterion(output, y_train) loss.backward() optimizer.step() if (epoch + 1) % 10 == 0: print(f’Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}’)

This block iteratively trains the model, applying the Fourier transform to the input data, passing it through the neural network, and optimizing the weights based on the loss function.

  1. Predicting and Plotting Prices

Finally, we use the trained model to predict stock prices over the entire historical period and plot the results to visualize its performance.

Predicting prices over entire history using a sliding window

model.eval() predicted_prices_history = []

with torch.no_grad(): for i in range(len(prices_tensor) — window_size): X_test_fft = fft.fft(prices_tensor[i:i+window_size].unsqueeze(0), dim=1) predicted_prices = model(X_test_fft.permute(0, 2, 1)).squeeze().numpy() predicted_prices_history.append(predicted_prices)

predicted_prices_history = np.array(predicted_prices_history)

Plotting actual vs predicted prices for each stock

plt.figure(figsize=(12, 8)) for i, ticker in enumerate(tickers): plt.plot(data.index[window_size:], predicted_prices_history[:, i], label=f’Predicted — {ticker}’) plt.plot(data.index, data.values[:, i], label=f’Actual — {ticker}’)

plt.xlabel(‘Date’) plt.ylabel(‘Stock Price’) plt.title(‘Actual vs. Predicted Stock Prices Over Time’) plt.legend() plt.show()

This block uses the model to generate predictions for each day, starting from the end of each sliding window. It then plots both actual and predicted prices, allowing us to visualize how closely the model’s predictions align with reality.

Conclusion

Fourier Neural Operators offer a powerful new approach to modeling complex data patterns, such as stock price movements, by leveraging the full potential of function spaces in the frequency domain. By treating stock prices as functions in a continuous space and applying Fourier transforms, FNOs capture both local and global trends, enabling more accurate predictions over time.

This exploration provides a foundation for further improvements, such as tuning model hyperparameters, experimenting with more advanced architectures, or incorporating additional features to enhance predictive performance.

Disclaimer : This is just an academic exercise and by no means an accurate model to predict stock prices


메타데이터
post_id
6db8adf4e2fb
slug
modeling-stock-prices-using-fourier-neural-operators-6db8adf4e2fb
url
https://blog.gopenai.com/modeling-stock-prices-using-fourier-neural-operators-6db8adf4e2fb
canonical_url
https://blog.gopenai.com/modeling-stock-prices-using-fourier-neural-operators-6db8adf4e2fb
author_url
https://medium.com/@jain.sm
status
ok
fetched_at
2026-09-03 21:08:38