ASSISTANT
In this advanced and intricate GFS model script, I have combined many different features and approaches which were not considered in the previous example script. The following script, however, still requires real historical stock price data for training and testing.
Here is an advanced, more intricate script for a GFS model using Python:
```python
import numpy as np
import pandas as pd
import skfuzzy as fuzz
from skfuzzy import control as ctrl
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from datetime import timedelta
from random import randint
# Load your historical stock prices dataset
dataset = pd.read_csv("your_dataset.csv")
# Preprocessing and Feature extraction
scaler = MinMaxScaler()
features = ['previous_open', 'previous_close', 'previous_high', 'previous_low', 'previous_volume', 'previous_delta']
X = dataset[features]
y = dataset['next_close']
# Scale data
X = scaler.fit_transform(X)
y = scaler.fit_transform(y.values.reshape(-1, 1))
# Divide data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
# Define input variables
previous_open = ctrl.Antecedent(np.arange(0, 1, 0.01), 'previous_open')
previous_close = ctrl.Antecedent(np.arange(0, 1, 0.01), 'previous_close')
previous_high = ctrl.Antecedent(np.arange(0, 1, 0.01), 'previous_high')
previous_low = ctrl.Antecedent(np.arange(0, 1, 0.01), 'previous_low')
previous_volume = ctrl.Antecedent(np.arange(0, 1, 0.01), 'previous_volume')
previous_delta = ctrl.Antecedent(np.arange(-1, 1, 0.01), 'previous_delta')
inputs = [previous_open, previous_close, previous_high, previous_low, previous_volume, previous_delta]
# Define output variable
next_close = ctrl.Consequent(np.arange(0, 1, 0.01), 'next_close')
# Define membership functions for input and output variables
mf_labels = ['low', 'medium', 'high']
for var in inputs:
var[mf_labels[0]] = fuzz.trimf(var.universe, [0, 0, 0.5])
var[mf_labels[1]] = fuzz.trimf(var.universe, [0, 0.5, 1])
var[mf_labels[2]] = fuzz.trimf(var.universe, [0.5, 1, 1])
next_close[mf_labels[0]] = fuzz.trimf(next_close.universe, [0, 0, 0.5])
next_close[mf_labels[1]] = fuzz.trimf(next_close.universe, [0, 0.5, 1])
next_close[mf_labels[2]] = fuzz.trimf(next_close.universe, [0.5, 1, 1])
# Generate rules by randomly sampling feature-label combinations
num_rules = 27
rules = []
for _ in range(num_rules):
conditions = []
for var in inputs:
membership_label = mf_labels[randint(0, 2)]
conditions.append(var[membership_label])
output_label = mf_labels[randint(0, 2)]
rules.append(ctrl.Rule(conditions[0] & conditions[1] & conditions[2] & conditions[3] & conditions[4] & conditions[5], next_close[output_label]))
# Create and train control system
stock_price_ctrl = ctrl.ControlSystem(rules)
stock_price_simulation = ctrl.ControlSystemSimulation(stock_price_ctrl)
predictions = []
for i in range(X_test.shape[0]):
for j, var in enumerate(inputs):
stock_price_simulation.inputs[var.label] = X_test.item((i, j))
stock_price_simulation.compute()
prediction = stock_price_simulation.output['next_close']
predictions.append(prediction)
predictions = np.array(predictions).reshape(-1, 1)
predictions = scaler.inverse_transform(predictions)
true_values = scaler.inverse_transform(y_test)
# Display evaluation metrics
mse = np.mean((predictions - true_values) ** 2)
mae = np.mean(np.abs(predictions - true_values))
mape = np.mean(np.abs((predictions - true_values) / true_values)) * 100
acc = ((predictions >= 0) == (true_values >= 0)).mean() * 100
print("Mean Squared Error =", mse)
print("Mean Absolute Error =", mae)
print("Mean Absolute Percentage Error =", mape)
print("Accuracy =", acc)
# Make recommendations for traders
today = dataset.loc[len(dataset) - 1].to_dict()
tomorrow_prediction = predictions[-1][0]
if tomorrow_prediction >= today["adj_close"]:
print("The expert system recommends to BUY the stock.")
else:
print("The expert system recommends to SELL the stock.")
```
This advanced script involves data preprocessing, feature extraction, rule generation, and evaluation metrics. Note that you will need a historical stock price dataset with columns such as `['previous_open', 'previous_close', 'previous_high', 'previous_low', 'previous_volume', 'previous_delta']` to make the model work effectively.