electricity-price-forecast-tft
Forecast of the SPOT Price in the Spanish Electricity Market with Neural Networks
Science Score: 67.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 3 DOI reference(s) in README -
✓Academic publication links
Links to: zenodo.org -
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.0%) to scientific vocabulary
Repository
Forecast of the SPOT Price in the Spanish Electricity Market with Neural Networks
Basic Info
- Host: GitHub
- Owner: MrGG14
- License: apache-2.0
- Language: Jupyter Notebook
- Default Branch: main
- Size: 257 MB
Statistics
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 1
Metadata Files
README.md
Forecasting SPOT Price in the Spanish Electricity Market using Neural Networks
Overview
This repository contains the code used for the Bachelor's Thesis titled "Forecasting SPOT Price in the Spanish Electricity Market using Neural Networks" by Nicolás Vega Muñoz. The thesis was submitted to the Escuela Técnica Superior de Ingenieros Informáticos at the Universidad Politécnica de Madrid in June 2024. The full thesis is available here.
The primary objective of this work is to develop a model capable of predicting the SPOT price (price per MWh) in the Spanish electricity market for each hour of the following day. This model aims to optimize market offers by providing accurate price forecasts, which are crucial for making informed decisions in a highly volatile energy market.
Note: The dataset used in this project is not published due to a confidentiality agreement.
Objectives
- Accurate Prediction: Develop a model to predict the SPOT price with high accuracy.
- Model Comparison: Evaluate and compare various models, including classical methods and state-of-the-art deep learning models like Temporal Fusion Transformers (TFT).
- Strategic Advantage: Provide a strategic advantage for operating in the daily electricity market through improved prediction accuracy and optimized energy offer management.
- Model Interpretability: Highlight the importance of model interpretability for strategic decision-making.
Methodology
The methodology applied in this work includes the following steps:
- Data Acquisition: Collect and preprocess data relevant to the Spanish electricity market.
- Modeling: Develop various models, including both classical and deep learning approaches.
- Model Comparison: Compare the performance of different models based on metrics such as Mean Absolute Error (MAE).
- Results Analysis: Analyze the results to identify the best-performing model and its potential impact on the market.
Data Acquisition
Data was acquired from public sources by doing some Web Scrapping and using the ESIOS API.
The variables used are: - Gas price (€) - European Union Allowances (EUA) price: CO2 emissions allowances. (€) - Demand (MwH) - Resiudal demand (MwH) - Solar production (MwH) - Eolic production (MwH)
And were obtained from:
# Web Scraping: - MIBGAS: Gas price and forecast - Sendeco2: Historical CO2 prices - ICE: CO2 price and forecast
API
- ESIOS: SPOT price, production and forecast of renewables (solar and wind), demand (and forecast), and residual demand.
Modelling
In this section, we present the various models employed to forecast electricity prices in the wholesale market. We explored both classical and deep learning approaches to identify the most accurate and robust model for our predictions.
Classical Models
SARIMA and SARIMAX
Seasonal Autoregressive Integrated Moving Average (SARIMA) and its extension with exogenous variables (SARIMAX) were among the classical models we tested. These models are well-suited for capturing seasonality and trends in time series data. We trained a SARIMAX model with the configuration SARIMAX(1,1,4)(1,0,0)[24], which provided a good fit to our data with a mean absolute error (MAE) of 7.13 on the validation set. However, the model exhibited limitations in capturing complex patterns beyond repetitive seasonal components.
Neural Network Models
Long Short-Term Memory (LSTM)
LSTM networks, a type of recurrent neural network (RNN), are effective for sequential data due to their ability to retain information over long periods. We implemented several LSTM architectures, starting with a basic model and progressing to more complex configurations with multiple LSTM layers and dense layers. The most effective LSTM model included dropout and regularization, achieving a promising MAE of 4.63 over a 7-day prediction horizon.
Convolutional Neural Networks (CNN)
We also experimented with 1D Convolutional Neural Networks (CNNs) to capture local patterns in the time series data. Starting with a simple model comprising a single convolutional layer with 16 filters followed by max pooling, we gradually increased the complexity. The best CNN model demonstrated decent performance, with a MAE of 4.98, indicating its potential for capturing significant features in the time series data.
State of the Art model: Temporal Fusion Transformers (TFT)
To leverage the power of attention mechanisms, we employed Temporal Fusion Transformers (TFT). This state-of-the-art model excels in handling temporal dynamics and multi-horizon forecasting. Our TFT model achieved the lowest MAE of 1.1, making it the most accurate model among those evaluated. The TFT model's interpretability also provided valuable insights into the contributing factors of the forecasts, aiding strategic decision-making.
Model Comparison
The following table summarizes the performance of the four models evaluated:
| Model | MAE | |-----------------------------|------| | Temporal Fusion Transformer | 1.26 | | LSTM | 4.63 | | CNN + LSTM | 4.98 | | ARIMA | 10.8 |
The study found that the Temporal Fusion Transformer (TFT) provided the most accurate predictions with a Mean Absolute Error (MAE) of 1.26. The implementation of this model is intended to improve prediction accuracy and optimize energy offer management, helping companies to bid more competitively and efficiently.
Results
The study found that state-of-the-art models, particularly the Temporal Fusion Transformers (TFT), provided the most accurate predictions with a Mean Absolute Error (MAE) of 1.26. The implementation of this model is intended to improve prediction accuracy and optimize energy offer management, helping companies to bid more competitively and efficiently.
TFT Model Results
)
Interpretability of the TFT Model
The Temporal Fusion Transformer (TFT) model includes several mechanisms for enhancing interpretability, making it easier to understand the factors driving the SPOT price predictions. These mechanisms are crucial for strategic decision-making in the energy market.
Key Mechanisms for Interpretability
Variable Selection Networks:
- The TFT model uses variable selection networks at the instance level, selecting the most relevant variables for each prediction. This helps eliminate noisy inputs and highlights the most significant variables for making predictions.
Interpretable Multi-Head Attention:
- The model incorporates a modified multi-head attention mechanism to learn long-term dependencies across different time points. This attention mechanism is designed to be interpretable, allowing each attention head to learn different temporal patterns while analyzing a common set of input features.
Static Covariate Encoders:
- These encoders transform static variables into contextual representations used throughout the prediction process. Incorporating static information allows the TFT to make more accurate and contextually relevant predictions, enhancing interpretability.
Temporal Fusion Decoder:
- The decoder includes layers that focus on learning temporal relationships within the data. For instance, the locality enhancement with a sequence-to-sequence layer helps identify significant values in relation to their neighboring values, useful for detecting anomalies, inflection points, and cyclical patterns.
Importance of Variables
Key insights from the TFT model include the identification of the most relevant variables influencing the SPOT price predictions:
- Demanda Residual: The most significant feature, indicating a strong correlation between residual demand and electricity prices.
- Precio SPOT Lagged by 72 Hours: Highly influential, showing that past prices from three days prior are a strong predictor of future prices.
- Relative Time Index: Important for capturing temporal patterns and trends.
- CO2 Levels: Reflects the impact of carbon emissions on price fluctuations.
- Renewable Energy Production: Includes solar and wind energy production, highlighting their roles in influencing price predictions.
Visual Insights
Below are visual representations of the attention mechanism and the importance of various decoder variables, which provide deeper insights into the interpretability of the TFT model:
Attention Mechanism

Encoder Variable Importance

Decoder Variable Importance

Static Variable Importance

Conclusions on Interpretability
Understanding Key Drivers:
- The TFT model's interpretability mechanisms allow for a detailed understanding of the key drivers behind SPOT price predictions. This is particularly important for stakeholders who need to make informed strategic decisions based on these predictions.
Enhanced Decision-Making:
- By highlighting the most important features and their relationships, the model provides actionable insights that can lead to more effective and strategic energy offer management.
Transparency and Trust:
- The transparent nature of the TFT model's predictions fosters trust among users, as they can see and understand the factors influencing the model
Conclusions
- Temporal Fusion Transformer (TFT): The TFT model outperformed other models with the lowest MAE, demonstrating its effectiveness in handling time series data for SPOT price prediction.
- LSTM: The LSTM model, while effective, had a higher MAE compared to TFT, indicating less accuracy in predicting SPOT prices.
- CNN + LSTM: Combining CNN with LSTM did not significantly improve accuracy over standalone LSTM, showing that more complexity doesn't always lead to better performance.
- ARIMA: The ARIMA model had the highest MAE, indicating that traditional statistical methods are less effective for this type of prediction task compared to modern deep learning approaches.
Repository Structure
old/: Old, unused files.src: Main folder where EDA, modelling and experiments are implemented.src/models/: Contains the implementation of the ARIMA, LSTM and CNN models evaluated in this study.plots/: Contains plots generated in the project. Contains both the results of the best TFT models and its interpretability plots.README.md: This README file.tfg_etsiinf_NicolasVega: Complete thesis.requirements.txt: Necessary libraries to execute code.
How to Use
- Clone the repository:
sh git clone https://github.com/MrGG14/Electricity-Price-Forecast-TFT - Install requirements
pip install -r requirements.txt## Acknowledgements This work was supervised by Bojan Mihaljevic at the Department of Artificial Intelligence, Escuela Técnica Superior de Ingenieros Informáticos, Universidad Politécnica de Madrid.
Owner
- Name: Nico Vega
- Login: MrGG14
- Kind: user
- Repositories: 2
- Profile: https://github.com/MrGG14
Citation (CITATION.cff)
# This CITATION.cff file was generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!
cff-version: 1.2.0
title: Forecasting SPOT Price in the Spanish Electricity Market using Neural Networks
message: >-
If you use this software, please cite it using the
metadata from this file.
type: software
authors:
- given-names: Nicolas
family-names: Vega Muñoz
email: nicovegamunoz1@gmail.com
identifiers:
- type: doi
value: 10.5281/zenodo.12207478
- type: url
value: 'https://github.com/MrGG14/OpenScience'
repository-code: 'https://github.com/MrGG14/Electricity-Price-Forecast-NN'
abstract: The objective of this Final Degree Project is to develop a model capable of predicting the price of MWh (SPOT price of the energy market) for each hour of the following day, thus optimizing market offers. Given the volatility of the energy market and the influence of numerous external factors, accuracy in predicting the SPOT price is crucial for companies to make informed decisions on how to price their offers to maximize profits without the risk of exclusion from the market. To address this challenge, various models were studied and compared, including both classical and Deep Learning models. Among the evaluated models, state-of-the-art models such as Temporal Fusion Transformers (TFT) stand out, achieving predictions with a mean absolute error (MAE) of C1.26. The applied methodology ranges from data acquisition and preprocessing to modeling, model comparison, and results analysis, thus providing a strategic advantage for operating in the daily electricity market. The implementation of this model aims not only to improve prediction accuracy but also to optimize energy offer management, helping companies to bid more competitively and efficiently. Additionally, the importance of advanced technologies and model interpretability is highlighted, enabling companies (generators, marketers...) to understand how and why predictions are generated, which is essential for strategic decision-making in a dynamic electricity market.
keywords:
- Temporal Fusion Transformers
- Electricity Price Forecast
- Deep Learning
- LSTM
- CNN 1D
- Forecasting
- Spanish electricity market
- Mercado energetico español
license: Apache-2.0
version: '3.0.0'
date-released: '2024-06-03'
CodeMeta (Codemeta.json)
{
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
"@type": "SoftwareSourceCode",
"license": "https://spdx.org/licenses/Apache-2.0",
"codeRepository": "https://github.com/MrGG14/OpenScience",
"dateCreated": "2024-04-03",
"datePublished": "2024-06-03",
"name": "Forecasting SPOT Price in the Spanish Electricity Market using Neural Networks",
"version": "3.0.0",
"identifier": "10.5281/zenodo.12207478",
"description": "This repository contains the code used for the Bachelor's Thesis titled 'Forecasting SPOT Price in the Spanish Electricity Market using Neural Networks' by Nicolás Vega Muñoz. The thesis was submitted to the Escuela Técnica Superior de Ingenieros Informáticos at the Universidad Politécnica de Madrid in June 2024. The primary objective of this work is to develop a model capable of predicting the SPOT price (price per MWh) in the Spanish electricity market for each hour of the following day. This model aims to optimize market offers by providing accurate price forecasts, which are crucial for making informed decisions in a highly volatile energy market.",
"applicationCategory": "Business",
"keywords": [
"Temporal Fusion Transformers",
"Electricity Price Forecast",
"LSTM",
"ARIMA",
"CNN 1D",
"Forecasting",
"Spanish electricity market"
],
"programmingLanguage": [
"Python"
],
"author": [
{
"@type": "Person",
"givenName": "Nicolas",
"familyName": "Vega"
}
]
}
GitHub Events
Total
- Watch event: 1
- Push event: 1
Last Year
- Watch event: 1
- Push event: 1
Dependencies
- Jinja2 ==3.1.4
- Mako ==1.3.5
- Markdown ==3.6
- MarkupSafe ==2.1.5
- PyYAML ==6.0.1
- SQLAlchemy ==2.0.30
- Werkzeug ==3.0.3
- absl-py ==2.1.0
- aiohttp ==3.9.5
- aiosignal ==1.3.1
- alembic ==1.13.1
- annotated-types ==0.7.0
- anyio ==4.4.0
- astunparse ==1.6.3
- async-timeout ==4.0.3
- attrs ==23.2.0
- cachetools ==5.3.3
- certifi ==2024.2.2
- charset-normalizer ==3.3.2
- click ==8.1.7
- colorlog ==6.8.2
- contourpy ==1.1.1
- cycler ==0.12.1
- dnspython ==2.6.1
- email_validator ==2.1.1
- et-xmlfile ==1.1.0
- exceptiongroup ==1.2.1
- fastapi ==0.111.0
- fastapi-cli ==0.0.4
- filelock ==3.14.0
- flatbuffers ==24.3.25
- fonttools ==4.52.4
- frozenlist ==1.4.1
- fsspec ==2024.5.0
- gast ==0.4.0
- google-auth ==2.29.0
- google-auth-oauthlib ==1.0.0
- google-pasta ==0.2.0
- greenlet ==3.0.3
- grpcio ==1.64.0
- h11 ==0.14.0
- h5py ==3.11.0
- httpcore ==1.0.5
- httptools ==0.6.1
- httpx ==0.27.0
- idna ==3.7
- importlib_resources ==6.4.0
- intel-openmp ==2021.4.0
- joblib ==1.4.2
- keras ==2.13.1
- kiwisolver ==1.4.5
- libclang ==18.1.1
- lightning ==2.2.5
- lightning-utilities ==0.11.2
- markdown-it-py ==3.0.0
- matplotlib ==3.7.5
- mdurl ==0.1.2
- mkl ==2021.4.0
- mpmath ==1.3.0
- multidict ==6.0.5
- networkx ==3.1
- numpy ==1.24.3
- oauthlib ==3.2.2
- openpyxl ==3.1.2
- opt-einsum ==3.3.0
- optuna ==3.4.0
- optuna-integration ==3.6.0
- orjson ==3.10.3
- pandas ==2.0.3
- patsy ==0.5.6
- pillow ==10.3.0
- protobuf ==4.25.3
- pyasn1 ==0.6.0
- pyasn1_modules ==0.4.0
- pydantic ==2.7.2
- pydantic_core ==2.18.3
- pyparsing ==3.1.2
- python-dotenv ==1.0.1
- python-multipart ==0.0.9
- pytorch-forecasting ==1.0.0
- pytorch-lightning ==1.9.0
- pytorch_optimizer ==2.12.0
- pytz ==2024.1
- pywin32 ==306
- requests ==2.32.3
- requests-oauthlib ==2.0.0
- rich ==13.7.1
- rsa ==4.9
- scikit-learn ==1.3.2
- scipy ==1.10.1
- shellingham ==1.5.4
- sniffio ==1.3.1
- starlette ==0.37.2
- statsmodels ==0.14.1
- sympy ==1.12
- tbb ==2021.12.0
- tensorboard ==2.13.0
- tensorboard-data-server ==0.7.2
- tensorflow ==2.13.0
- tensorflow-estimator ==2.13.0
- tensorflow-intel ==2.13.0
- tensorflow-io-gcs-filesystem ==0.31.0
- termcolor ==2.4.0
- threadpoolctl ==3.5.0
- torch ==2.0.1
- torchaudio ==2.0.2
- torchdata ==0.6.1
- torchmetrics ==1.4.0.post0
- torchtext ==0.15.2
- torchvision ==0.15.2
- tqdm ==4.66.4
- typer ==0.12.3
- typing_extensions ==4.5.0
- tzdata ==2024.1
- ujson ==5.10.0
- urllib3 ==2.2.1
- uvicorn ==0.30.0
- watchfiles ==0.22.0
- websockets ==12.0
- wrapt ==1.16.0
- yarl ==1.9.4