This project leverages machine learning to predict the price of Gold (GLD) based on a dataset containing historical financial indicators. Utilizing a Random Forest Regressor, the model aims to provide accurate predictions, achieving a high R-squared score, and demonstrating strong performance in tracking actual gold price movements.
The analysis is performed on the gld_price_data.csv dataset, which comprises the following key features:
Date: The date of the recorded observation.SPX: The S&P 500 Index, representing a broad market indicator.GLD: The target variable, representing the Gold Price (USD).USO: The United States Oil Fund ETF, reflecting oil market trends.SLV: The iShares Silver Trust ETF, indicating silver prices.EUR/USD: The Euro to US Dollar exchange rate, a currency market indicator.
This project is developed using Python and relies on the following libraries:
numpypandasmatplotlibseabornscikit-learn(sklearn)
To run this project, follow these steps:
- Clone the repository (if applicable):
git clone <your-repo-link> cd <your-repo-name>
- Ensure you have the dataset: Place the
gld_price_data.csvfile in the same directory as the Jupyter/Colab notebook, or update the path in the code. - Install dependencies: Make sure you have all the required Python libraries installed. You can install them using pip:
pip install numpy pandas matplotlib seaborn scikit-learn
- Open and run the notebook: Open
gold_price_prediction.ipynbin a Jupyter environment or Google Colab and execute the cells sequentially.
The project systematically approaches the prediction task through the following stages:
- The
gld_price_data.csvis loaded into a pandas DataFrame. - Initial data inspection includes
head(),tail(),shape,info(), anddescribe()to understand its structure and statistical properties. - Missing values are identified using
isnull().sum(). - The 'Date' column is converted to a datetime object for proper time-series analysis capabilities.
- Correlation Matrix: A heatmap visualization of the correlation matrix is generated using
seabornto illustrate relationships between features and the target variable (GLD). - Target Variable Distribution: The distribution of the
GLDprice is visualized using adistplotto understand its spread and characteristics.
- Feature Engineering: The 'Date' column is dropped from the features (
X) as it's not directly used in the model training after being used for correlation analysis, andGLDis separated as the target variable (y). - Train-Test Split: The dataset is divided into training (80%) and testing (20%) sets using
train_test_splitwithrandom_state=2for reproducibility.
- A
RandomForestRegressormodel is initialized withn_estimators=100. - The model is trained on the prepared training data (
x_train,y_train).
- Prediction: The trained model generates predictions on the unseen test set (
x_test). - R-squared Score: The model's performance is quantified using the R-squared metric, comparing predicted (
test_data_prediction) against actual (y_test) values. - Visual Comparison: A plot is generated to visually compare the actual GLD prices with the predicted GLD prices, showcasing the model's ability to capture trends.
The Random Forest Regressor model achieved an impressive R-squared score of 0.9891 on the test set. This indicates a very strong predictive capability, with the model explaining approximately 98.91% of the variance in gold prices. The visual comparison between actual and predicted prices further confirms the model's high accuracy and close tracking of gold price movements.
- Hyperparameter Tuning: Optimize the
RandomForestRegressorparameters (e.g.,max_depth,min_samples_split) for potentially better performance. - Time Series Cross-Validation: Implement more robust validation strategies tailored for time-series data.
- Additional Features: Explore incorporating other financial indicators, macroeconomic data, or news sentiment as features.
- Deep Learning Models: Experiment with advanced models like LSTMs or Transformers for time-series forecasting.
- Deployment: Develop an API or a simple web application to deploy the trained model for real-time predictions.