Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Time Series Analysis with Polynomial Detrending and Fourier Filtering

Overview

This Python script performs time series analysis on financial data (specifically EUR/USD closing prices) using polynomial detrending, Fourier transformation, and forecasting techniques. The code includes methods for removing trends, applying spectral analysis, and making future price predictions.

Features

  • Data Loading: Reads EUR/USD price data from CSV files
  • Detrending: Implements two detrending methods (difference and linear)
  • Polynomial Fitting: Fits polynomial curves to price data for trend extraction
  • Fourier Analysis: Applies FFT for frequency domain analysis and filtering
  • Forecasting: Generates future price predictions using Fourier components
  • Visualization: Creates plots to compare original data, trends, and predictions

Requirements

pandas
numpy
matplotlib
scikit-learn (for LinearRegression - note: currently missing import)

Installation

pip install pandas numpy matplotlib scikit-learn

Usage

Basic Usage

  1. Place your EUR/USD price data in a CSV file at the path 'Prices/EURUSD_.csv'
  2. Run the script:
python timeseries_analysis.py

Detrending Function

The script includes a detrend() function with two methods:

# Difference method
detrended = detrend(prices, method='difference')

# Linear regression method
detrended = detrend(prices, method='linear')

Key Parameters

  • training: Number of data points used for training (default: 226)
  • foresight: Number of future points to predict (default: 20)
  • deg: Polynomial degree for fitting (default: 10)

Code Structure

Main Components

  1. Data Preparation (lines 36-55):

    • Loads and preprocesses EUR/USD price data
    • Sets up training and testing datasets
  2. Polynomial Fitting (lines 57-67):

    • Fits a 10th-degree polynomial to training data
    • Calculates percentage error of the fit
  3. Residual Analysis (lines 70-75):

    • Computes differences between actual prices and polynomial fit
  4. Fourier Analysis (lines 78-112):

    • Applies FFT to residuals
    • Filters frequency components
    • Reconstructs signal using inverse FFT
  5. Forecasting (lines 114-138):

    • Extends Fourier components for future predictions
    • Combines polynomial trend and Fourier components
    • Calculates prediction weights
  6. Visualization (lines 140-145):

    • Plots original data, polynomial fit, and predictions

Note on Imports

The code currently uses LinearRegression without importing it. Add this import at the top:

from sklearn.linear_model import LinearRegression

Output

  1. Console Output:

    • Percentage error of polynomial fit
    • Fourier component values
    • Prediction weight
    • Combined trend and cyclical components
  2. Visual Output:

    • Plot 1: Polynomial fit vs. actual closing prices
    • Plot 2: Final predictions vs. original data (training and full dataset)

Example Output Interpretation

Percentage error: 0.XXX %
The prediction weight is: Y.YYY

The prediction weight represents the average of forecasted Fourier components and can be used as an indicator of future price movements.

Important Notes

  • The code uses a fixed training size of 226 points
  • Fourier filtering thresholds are hard-coded and may need adjustment
  • The polynomial degree (10) might lead to overfitting
  • Error handling for the detrending function is basic
  • File paths are hard-coded and may need modification

Customization

To adapt this script for your needs:

  1. Modify df=pd.read_csv('Prices/EURUSD_.csv') to point to your data file
  2. Adjust training and foresight parameters based on your data size
  3. Change polynomial degree (deg=10) as needed
  4. Modify Fourier filtering thresholds for different frequency cutoffs

Limitations

  • Missing error handling for file I/O
  • LinearRegression import is missing
  • Hard-coded parameters may not generalize well
  • Limited comments in complex sections
  • No validation of forecast accuracy

Future Improvements

  • Add cross-validation for parameter selection
  • Implement more robust error handling
  • Create configuration file for parameters
  • Add performance metrics for predictions
  • Implement multiple forecasting methods for comparison

Author

Code for time series analysis using polynomial and Fourier methods

#Fourier analysis for Forex predictions https://medium.com/@mnyandenilunga/fourier-analysis-for-forex-predictions-c2313adc6379

About

A Python script implementing polynomial detrending and Fourier transform filtering for financial time series predictions. Uses frequency-domain analysis to isolate cyclical patterns and generate forward price forecasts through signal decomposition and reconstruction techniques.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages