You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to estimate a 4-state Gaussian Hidden Markov Model. Two things are happening:
Every time I run the same code, I am getting different estimates for the parameters, even when setting a random seed. Is this the behavior?
Even though the transition matrix edges is different every time, the computed steady state distribution always ends up in uniform distribution.
It may very well be the case that I am doing something wrong, but I went deep into the documentation and could not find something to help. Due to the sample size, I believe there is no identification problem.
importpandasaspdimportmatplotlib.pyplotaspltimportnumpyasnpfrompomegranate.distributionsimportNormalfrompomegranate.hmmimportDenseHMMn_states=4# READ DATAfile_path=r"path/to/file/NAVs.xlsx"df=pd.read_excel(file_path, index_col=0)
df.index=pd.to_datetime(df.index)
rets=df.resample("M").last().pct_change(1).dropna() # about 330 lines and 5 columns# THE MODELhmm=DenseHMM(
distributions=[Normal() for_inrange(n_states)],
verbose=True,
)
hmm=hmm.fit(X=np.array([rets.values])) # to make sure that X is 3D# Transition Probability (changes every time I run)trans_prob=pd.DataFrame(np.exp(np.array(hmm.edges)))
trans_prob=trans_prob.div(trans_prob.sum(axis=1), axis=0) # Reduce numerical error# Stationary distribution (always outputs a uniform distribution)vals, vecs=np.linalg.eig(trans_prob)
stat_dist=pd.Series(vecs[:, np.argmax(vals)])
stat_dist=stat_dist*np.sign(stat_dist)
stat_dist=stat_dist/stat_dist.sum()
stat_dist.plot(kind="bar")
plt.show()
# States probabilities (changes every time I run)state_probs=pd.DataFrame(data=hmm.predict_proba(np.array([rets.values]))[0], index=rets.index)
state_probs.plot()
plt.show()
# Predicted / Most likely State (changes every time I run)state_pred=pd.Series(data=hmm.predict(np.array([rets.values]))[0], index=rets.index)
state_pred.plot()
plt.show()
The text was updated successfully, but these errors were encountered:
gusamarante
changed the title
Different results every run
[Question] Different results every run
Jan 13, 2025
The only source of randomness in pomegranate's HMMs should be in the initial clustering. The predictions do not involve randomness at all. How are you setting a seed? You might need to set both the numpy and torch random seeds. Unfortunately, torch is a bit challenging to ensure randomness for. Maybe you could try the first-k initialization and see? Can you ost your code where you set the random seed?
Description
I am trying to estimate a 4-state Gaussian Hidden Markov Model. Two things are happening:
edges
is different every time, the computed steady state distribution always ends up in uniform distribution.It may very well be the case that I am doing something wrong, but I went deep into the documentation and could not find something to help. Due to the sample size, I believe there is no identification problem.
Reproduction Code
I am reading data from this excel worksheet.
The text was updated successfully, but these errors were encountered: