Help Converting Python Hidden Markov Model (HMM) into Matlab
조회 수: 8 (최근 30일)
이전 댓글 표시
I'm trying to convert a Python HMM code into Matlab (warning - I'm just learning about HMM models and probably missing something easy).
The Python code seeds itself uses HMMlearn as its model:
seed=42
import os
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
import random
random.seed(seed)
#Tweaking the fonts, etc.
import matplotlib.pyplot as plt
from matplotlib import rcParams
!pip install hmmlearn
I'm getting tripped up as the Python code simply uses a single column array of stock returns as its input:
from hmmlearn import hmm
model = hmm.GaussianHMM(n_components=2, covariance_type="diag")
X_train = df_train['returns'].to_numpy().reshape(-1, 1)
model.fit(X_train)
How can I do this in Matlab? The transition matrix is simply a 2x2.
Below is my sample attempt in Matlab:
% Example stock returns data (0.1% to 1.0%)
stock_returns = [0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01];
% Number of states
numStates = 2;
% Gaussian distribution for emissions
mu = mean(stock_returns); % Initial guess for means
sigma = std(stock_returns); % Initial guess for standard deviations
% Transition probability matrix (initial guess)
transMatGuess = [0.95, 0.05; 0.05, 0.95];
% Emission parameters (initial guess)
emissionGuess = struct('mu', mu, 'Sigma', sigma);
% Initialize the HMM
hmmModel = struct('StartProb', [0.5, 0.5], 'TransProb', transMatGuess, 'Emissions', emissionGuess);
% Train the HMM
[trained_hmm, logL] = hmmtrain(hmmModel, stock_returns);
I am having absolutely no luck getting this to run. Would someone please help me get my model on the right path? Thanks in advance!
댓글 수: 0
답변 (1개)
Venkat Siddarth Reddy
2024년 10월 19일
Hi Daulton_Benesuave,
It seems that the input arguments passed to the function "hmmtrain" in the above MATLAB script doesn't match with the structure and the data types of the input arguments mentioned in the MATLAB documentation.
Please use the "hmmtrain" function with the input arguments mentioned in the following documentation:
I hope it helps!
참고 항목
카테고리
Help Center 및 File Exchange에서 Markov Chain Models에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!