Help Converting Python Hidden Markov Model (HMM) into Matlab

조회 수: 14 (최근 30일)
Daulton_Benesuave
Daulton_Benesuave 2024년 10월 18일
댓글: Daulton_Benesuave 2024년 10월 24일 12:16
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!

답변 (1개)

Venkat Siddarth Reddy
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!
  댓글 수: 1
Daulton_Benesuave
Daulton_Benesuave 2024년 10월 24일 12:16
Unfortunately, I've already read the hmmtrain info a few times and am still missing something basic as I cannot get the example to run. Any other ideas or tips?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Markov Chain Models에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by