Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
2
Indexable
Never
import yfinance as yf
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM

# Download historical data for OMX and Dow Jones
omx = yf.download('^OMX', start='2012-01-01', end=pd.Timestamp.today())
dj = yf.download('^DJI', start='2012-01-01', end=pd.Timestamp.today())

# Create lagged features for OMX and DJ
omx['OMX_lag'] = omx['Close'].shift(1)
dj['DJ_lag'] = dj['Close'].shift(1)
omx['OMX_diff'] = omx['Close'] - omx['OMX_lag']
dj['DJ_diff'] = dj['Close'] - dj['DJ_lag']

# Merge OMX and DJ data
data = pd.concat([omx['OMX_diff'], dj['DJ_diff']], axis=1).dropna()

# Scale data
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data)

# Split into train and test sets
train_size = int(len(scaled_data) * 0.8)
train_data = scaled_data[:train_size, :]
test_data = scaled_data[train_size:, :]

# Function to create input-output pairs for LSTM
def create_dataset(dataset, look_back=1):
    X, Y = [], []
    for i in range(len(dataset) - look_back):
        X.append(dataset[i:(i + look_back), :])
        Y.append(dataset[i + look_back, 0])
    return np.array(X), np.array(Y)

# Prepare input-output pairs for LSTM
look_back = 10
X_train, y_train = create_dataset(train_data, look_back)
X_test, y_test = create_dataset(test_data, look_back)

# Create and train LSTM model
model = Sequential()
model.add(LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(1, activation='sigmoid'))  # Add sigmoid activation function
model.compile(loss='binary_crossentropy', optimizer='adam')  # Use binary_crossentropy loss
model.fit(X_train, y_train, epochs=100, batch_size=1, verbose=2)

# Create and train LSTM model
model = Sequential()
model.add(LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, y_train, epochs=100, batch_size=1, verbose=2)

# Make predictions and evaluate the model
train_pred = model.predict(X_train)
test_pred = model.predict(X_test)

# Inverse transform the predictions
train_pred_transformed = np.zeros((train_pred.shape[0], 2))
train_pred_transformed[:, 0] = train_pred[:, 0]
train_pred_transformed = scaler.inverse_transform(train_pred_transformed)
train_pred = train_pred_transformed[:, 0]
test_pred_transformed = np.zeros((test_pred.shape[0], 2))
test_pred_transformed[:, 0] = test_pred[:, 0]
test_pred_transformed = scaler.inverse_transform(test_pred_transformed)
test_pred = test_pred_transformed[:, 0]

# Provide a simple Yes or No answer based on the prediction along with probability
next_day_pred = test_pred[-1]
next_day_pred_probability = next_day_pred * 100
if next_day_pred > 0.5:
    print(f"Yes (probability: {next_day_pred_probability:.2f}%)")
else:
    print(f"No (probability: {100 - next_day_pred_probability:.2f}%)")