Help Converting Python Hidden Markov Model (HMM) into Matlab

14 vues (au cours des 30 derniers jours)
Daulton_Benesuave
Daulton_Benesuave le 18 Oct 2024
Commenté : Daulton_Benesuave le 24 Oct 2024 à 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!

Réponses (1)

Venkat Siddarth Reddy
Venkat Siddarth Reddy le 19 Oct 2024
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 commentaire
Daulton_Benesuave
Daulton_Benesuave le 24 Oct 2024 à 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?

Connectez-vous pour commenter.

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by