Skip to main content

Tổng hợp các kỹ thuật dự đoán giá cổ phiếu

· 3 min read

Dự đoán giá cổ phiếu là một trong những bài toán quan trọng trong tài chính. Dưới đây là các phương pháp phổ biến giúp dự đoán xu hướng giá cổ phiếu:

1. Hồi quy tuyến tính (Linear Regression)

Hồi quy tuyến tính là phương pháp cơ bản giúp dự đoán giá cổ phiếu dựa trên mối quan hệ tuyến tính giữa các biến đầu vào.

📌 Ưu điểm: Đơn giản, dễ hiểu, dễ triển khai.
⚠️ Nhược điểm: Không hiệu quả với dữ liệu phi tuyến tính.

import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Lấy dữ liệu cổ phiếu
df = yf.download("AAPL", start="2020-01-01", end="2024-01-01")

# Chuẩn bị dữ liệu
df['Date'] = np.arange(len(df)) # Chuyển ngày thành số
X = df[['Date']]
y = df['Close']

# Huấn luyện mô hình hồi quy tuyến tính
model = LinearRegression()
model.fit(X, y)

# Dự đoán giá
df['Predicted'] = model.predict(X)

# Vẽ biểu đồ
plt.figure(figsize=(10,5))
plt.plot(df.index, df['Close'], label="Giá thực tế")
plt.plot(df.index, df['Predicted'], label="Dự đoán", linestyle='dashed')
plt.legend()
plt.title("Dự đoán giá cổ phiếu AAPL bằng Linear Regression")
plt.show()

2. ARIMA (AutoRegressive Integrated Moving Average)

ARIMA là mô hình dự đoán thời gian dựa trên dữ liệu lịch sử.

📌 Ưu điểm: Hiệu quả với dữ liệu có xu hướng và mùa vụ.
⚠️ Nhược điểm: Yêu cầu kiểm tra tính dừng của dữ liệu.

from statsmodels.tsa.arima.model import ARIMA

# Huấn luyện mô hình ARIMA
model = ARIMA(df['Close'], order=(5,1,0))
model_fit = model.fit()

# Dự đoán giá
df['ARIMA_Predicted'] = model_fit.predict(start=0, end=len(df)-1)

# Vẽ biểu đồ
plt.figure(figsize=(10,5))
plt.plot(df.index, df['Close'], label="Giá thực tế")
plt.plot(df.index, df['ARIMA_Predicted'], label="Dự đoán ARIMA", linestyle='dashed')
plt.legend()
plt.title("Dự đoán giá cổ phiếu AAPL bằng ARIMA")
plt.show()

3. LSTM (Long Short-Term Memory)

LSTM là mô hình mạng nơ-ron giúp xử lý dữ liệu chuỗi thời gian phức tạp.

📌 Ưu điểm: Xử lý dữ liệu dài hạn tốt.
⚠️ Nhược điểm: Cần nhiều dữ liệu và thời gian huấn luyện dài.

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

# Chuẩn bị dữ liệu cho LSTM
data = df['Close'].values.reshape(-1,1)
data = data / np.max(data) # Chuẩn hóa dữ liệu

# Tạo mô hình LSTM
model = Sequential([
LSTM(50, return_sequences=True, input_shape=(10,1)),
LSTM(50, return_sequences=False),
Dense(1)
])

model.compile(optimizer='adam', loss='mse')
# Huấn luyện mô hình (đơn giản hóa, không đầy đủ)
# model.fit(X_train, y_train, epochs=50, batch_size=16)

# Dự đoán (giả lập)
df['LSTM_Predicted'] = df['Close'].shift(-1)

# Vẽ biểu đồ
plt.figure(figsize=(10,5))
plt.plot(df.index, df['Close'], label="Giá thực tế")
plt.plot(df.index, df['LSTM_Predicted'], label="Dự đoán LSTM", linestyle='dashed')
plt.legend()
plt.title("Dự đoán giá cổ phiếu AAPL bằng LSTM")
plt.show()

Kết luận

  • Hồi quy tuyến tính: Phù hợp với dữ liệu đơn giản, có quan hệ tuyến tính.
  • ARIMA: Hiệu quả với dữ liệu có xu hướng hoặc mùa vụ.
  • LSTM: Mạnh mẽ, phù hợp với dữ liệu chuỗi thời gian dài hạn.

👉 Mỗi phương pháp có ưu và nhược điểm riêng, tùy vào đặc điểm dữ liệu mà chọn mô hình phù hợp.

📌 Nếu bạn quan tâm đến Machine Learning và Phân tích dữ liệu tài chính, hãy tiếp tục theo dõi các bài viết tiếp theo! 🚀