Downloads · 30 days
21
11% of all-time downloads
Shoriful025/finance-ts-lstm-autoencoder-anomaly
finance-ts-lstm-autoencoder-anomaly is a machine learning model from Shoriful025. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This model is a specialized LSTM Autoencoder designed for Unsupervised Anomaly Detection in multivariate financial time series data. It learns the "normal" day-to-day patterns and correlations between key financial as…
Downloads · 30 days
21
11% of all-time downloads
All-time downloads
183
Public
Repo size
—
Likes
0
Public
Click a slice to open those files.
.md3.8 KB · 66%
From the Hugging Face model README
This model is a specialized LSTM Autoencoder designed for Unsupervised Anomaly Detection in multivariate financial time series data. It learns the "normal" day-to-day patterns and correlations between key financial assets (e.g., Bitcoin, Ethereum, Nasdaq, VIX). An anomaly is identified when the model's reconstruction error for a given time window exceeds a predetermined threshold, indicating a significant deviation from expected behavior.
num_layers=2), mapping the input sequence down to a fixed-size latent_dimension (32). The latent vector captures the compressed "essence" of the sequence's normal pattern.anomaly_threshold (e.g., 0.05 MSE). Choosing this threshold is subjective and requires careful validation, as a low threshold causes many false alarms (false positives), and a high one misses real crises (false negatives).This example shows how the model is conceptually used to calculate an anomaly score.
import torch
import torch.nn as nn
import numpy as np
# Model parameters (matches config.json)
INPUT_SIZE = 4
SEQ_LENGTH = 30
ENCODER_HIDDEN = 64
LATENT_DIM = 32
# Conceptual Model Class (not runnable without full implementation)
class LSTMAutoencoder(nn.Module):
# (Encoder and Decoder setup omitted for brevity)
def forward(self, x):
# x is (Batch, SEQ_LENGTH, INPUT_SIZE)
# encoder_output = self.encoder(x)
# reconstructed_output = self.decoder(encoder_output)
# return reconstructed_output
return torch.randn_like(x) # Placeholder for reconstruction
# Load weights and instantiate (Conceptual)
# model = LSTMAutoencoder(...)
# model.load_state_dict(torch.load("pytorch_model.bin"))
# model.eval()
# Dummy input data: 30 days of the 4 features
# In a real scenario, this would be standardized/normalized.
current_window = torch.randn(1, SEQ_LENGTH, INPUT_SIZE)
# with torch.no_grad():
# reconstruction = model(current_window)
# Calculate Anomaly Score (Reconstruction Error)
mse_loss = nn.MSELoss(reduction='mean')
anomaly_score = mse_loss(current_window, reconstruction).item()
# ANOMALY_THRESHOLD = 0.05 (from config.json)
# if anomaly_score > ANOMALY_THRESHOLD:
# print(f"ANOMALY DETECTED! Score: {anomaly_score:.4f}")
# else:
# print(f"Normal. Score: {anomaly_score:.4f}")