Skip to content

Importing Data

import pandas as pd

# Load the CSV file containing Moroccan All Shares Historical Data
masi_prices = pd.read_csv("Moroccan All Shares Historical Data.csv")

# Display the first few rows of the dataframe
masi_prices.head()
import plotly.graph_objects as go

# Create a candlestick chart using masi_prices dataframe
fig = go.Figure(data=[go.Candlestick(x=masi_prices['Date'],
                                     open=masi_prices['Open'],
                                     high=masi_prices['High'],
                                     low=masi_prices['Low'],
                                     close=masi_prices['Price'])])

# Update layout for better visualization
fig.update_layout(title='Candlestick chart for MASI',
                  xaxis_title='Date',
                  yaxis_title='Price',
                  xaxis_rangeslider_visible=False)

# Show the figure
fig.show()

Data Pre-processing

import pandas as pd

# Ensure the 'Date' column is in datetime format
masi_prices['Date'] = pd.to_datetime(masi_prices['Date'])

# Set 'Date' as the index
masi_prices.set_index('Date', inplace=True)

# Resample the data to ensure a single observation per day, filling missing values with NaN
masi_prices = masi_prices.resample('D').asfreq()

# Interpolate to fill missing values, including weekends
masi_prices.interpolate(method='linear', inplace=True)

# Display the dataframe to check the changes
masi_prices.head()

# Display the data types of each column in the dataframe masi_prices
masi_prices.dtypes
masi_prices.head()

Predicting with TimeGpt

!pip install nixtla, zstandard
Hidden output
#!pip install zstandard
import zstandard
print(zstandard.__version__)

Defining APi

import os

api_key = os.environ["api_key"]
nixtla_client = NixtlaClient(api_key=api_key)
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error

def walk_forward_validation(data, initial_train_size, forecast_horizon=1, freq='D'):
    """
    Perform walk-forward validation on time series data.

    Parameters:
    - data: DataFrame with 'Date' as index, 'Price' as target column, and 'unique_id'.
    - initial_train_size: Number of initial training samples.
    - forecast_horizon: Number of periods to forecast (default is 1 for daily prediction).
    - freq: Frequency of the time series data (default is daily).

    Returns:
    - rmse_list: List of RMSE values for each prediction.
    - predictions_list: List of predictions for each forecast horizon.
    - data_with_predictions: DataFrame with predictions added to the original data.
    """
    rmse_list = []
    predictions_list = []
    data_with_predictions = data.copy()
    data_with_predictions['Prediction'] = np.nan

    # Loop through the data with a sliding window
    for start in range(initial_train_size, len(data)):
        # Define training and testing sets based on the index position
        train_data = data.iloc[:start]
        test_data = data.iloc[start:start + forecast_horizon]

        # Prepare data for forecasting
        train_data_ = pd.DataFrame({
            'unique_id': [1] * len(train_data),
            'ds': train_data.index,  # Use Date index for the time column
            'y': train_data['Price']
        })
        test_data_ = pd.DataFrame({
            'unique_id': [1] * len(test_data),
            'ds': test_data.index,  # Use Date index for the time column
            'y': test_data['Price']
        })

        # Forecasting with TimeGPT
        forecast_df = nixtla_client.forecast(df=train_data_, h=forecast_horizon, freq=freq, time_col='ds', target_col='y')

        # Calculate RMSE for the current prediction
        test_predictions = forecast_df.iloc[:forecast_horizon]  # Adjusted to get the correct forecast horizon
        rmse = np.sqrt(mean_squared_error(test_data['Price'], test_predictions['TimeGPT']))
        rmse_list.append(rmse)
        predictions_list.append(test_predictions['TimeGPT'].values)

        # Add predictions to the data_with_predictions DataFrame
        data_with_predictions.iloc[start:start + forecast_horizon, data_with_predictions.columns.get_loc('Prediction')] = test_predictions['TimeGPT'].values

    return rmse_list, predictions_list, data_with_predictions

# Define parameters
initial_train_size = int(len(masi_prices) * 0.9)  # 90% for training

# Perform walk-forward validation
rmse_values, predictions, masi_prices_with_predictions = walk_forward_validation(masi_prices, initial_train_size)

# Calculate average RMSE
avg_rmse = np.mean(rmse_values)

avg_rmse, masi_prices_with_predictions

Ploting Price vs Predictions

import pandas as pd

# Import masi_prices_with_predictions from masi_prices_with_predictions.csv
masi_prices_with_predictions = pd.read_csv('masi_prices_with_predictions.csv')