Warning The sklearn.hmm module has now been deprecated due to it no longer matching the scope and the API of the project. It is scheduled for removal in the 0.17 release of the project.html
The sklearn.hmm module has now been deprecated due to it no longer matching the scope and the API of the project. It is scheduled for removal in the 0.17 release of the project.git
Evaluation problem answers the question: what is the probability that a particular sequence of symbols is produced by a particular model?
For evaluation we use two algorithms: the forward algorithm or the backwards algorithm (DO NOT confuse them with the forward-backward algorithm).
Decoding problem - 解碼問題
Decoding problem answers the question: Given a sequence of symbols (your observations) and a model, what is the most likely sequence of states that produced the sequence.
For decoding we use the Viterbi algorithm.
Training problem - 學習問題
Training problem answers the question: Given a model structure and a set of sequences, find the model that best fits the data.
For this problem we can use the following 3 algorithms:
MLE (maximum likelihood estimation)
Viterbi training (DO NOT confuse with Viterbi decoding)
Note, since the EM algorithm is a gradient-based optimization method, it will generally get stuck in local optima. You should in general try to run fit with various initializations and select the highest scored model.
You can use the monitor_ attribute to diagnose convergence.
""" Sampling from HMM ----------------- This script shows how to sample points from a Hiden Markov Model (HMM): we use a 4-components with specified mean and covariance. The plot show the sequence of observations generated with the transitions between them. We can see that, as specified by our transition matrix, there are no transition between component 1 and 3. """print(__doc__) import numpy as np import matplotlib.pyplot as plt from hmmlearn import hmm ############################################################## # Prepare parameters for a 4-components HMM # Initial population probability
startprob = np.array([0.6, 0.3, 0.1, 0.0]) # The transition matrix, note that there are no transitions possible # between component 1 and 3
transmat = np.array([[0.7, 0.2, 0.0, 0.1], [0.3, 0.5, 0.2, 0.0], [0.0, 0.3, 0.5, 0.2], [0.2, 0.0, 0.2, 0.6]]) # The means of each component
means = np.array([[0.0, 0.0], [0.0, 11.0], [9.0, 10.0], [11.0, -1.0]]) # The covariance of each component
covars = .5 * np.tile(np.identity(2), (4, 1, 1)) # Build an HMM instance and set parametersmodel = hmm.GaussianHMM(n_components=4, covariance_type="full") # Instead of fitting it from the data, we directly set the estimated # parameters, the means and covariance of the componentsmodel.startprob_ = startprob model.transmat_ = transmat model.means_ = means model.covars_ = covars ################################################################ Generate samples
X, Z = model.sample(500) # Plot the sampled data
plt.plot(X[:, 0], X[:, 1], ".-", label="observations", ms=6, mfc="orange", alpha=0.7) # Indicate the component numbersfor i, m in enumerate(means): plt.text(m[0], m[1], 'Component %i' % (i + 1), size=17, horizontalalignment='center', bbox=dict(alpha=.7, facecolor='w')) plt.legend(loc='best') plt.show()
The finance module has been split out into its own package https://github.com/matplotlib/mpl_finance and deprecated in matplotlib. Any further development will take place in https://github.com/matplotlib/mpl_finance can you report the issue there? The finance module has been deprecated because non of the matplotlib developers are interested in maintaining it and mpl_finance is currently almost un maintained. It might also be worth checking out https://pypi.python.org/pypi/yahoo-finance as an alternative solution
""" Gaussian HMM of stock data -------------------------- This script shows how to use Gaussian HMM on stock price data from Yahoo! finance. For more information on how to visualize stock prices with matplotlib, please refer to ``date_demo1.py`` of matplotlib. """from__future__import print_function import datetime import numpy as np from matplotlib import cm, pyplot as plt from matplotlib.dates import YearLocator, MonthLocator try: from matplotlib.finance import quotes_historical_yahoo_ochl except ImportError: # For Matplotlib prior to 1.5.from matplotlib.finance import ( quotes_historical_yahoo as quotes_historical_yahoo_ochl ) from hmmlearn.hmm import GaussianHMM print(__doc__) ############################################################################### # Get quotes from Yahoo! finance
quotes = quotes_historical_yahoo_ochl("^GSPC", datetime.date(2012, 1, 1), datetime.date(2015, 1, 6)) sp = quotes_historical_yahoo_ochl("^GSPC", datetime.date(2012, 1, 1), datetime.date(2015, 1, 6), asobject=True, adjusted=True) # Unpack quotes
dates = np.array([q[0] for q in quotes], dtype=int) close_v = np.array([q[2] for q in quotes]) volume = np.array([q[5] for q in quotes])[1:] # Take diff of close value. Note that this makes # ``len(diff) = len(close_t) - 1``, therefore, other quantities also # need to be shifted by 1.
diff = np.diff(close_v) dates = dates[1:] close_v = close_v[1:] # Pack diff and volume for training.
X = np.column_stack([diff, volume]) ############################################################################### # Run Gaussian HMMprint("fitting to HMM and decoding ...", end="") # Make an HMM instance and execute fit
model = GaussianHMM(n_components=4, covariance_type="diag", n_iter=1000).fit(X) # Predict the optimal sequence of internal hidden state
hidden_states = model.predict(X) print("done") ############################################################################### # Print trained parameters and plotprint("Transition matrix") print(model.transmat_) print() print("Means and vars of each hidden state") for i in range(model.n_components): print("{0}th hidden state".format(i)) print("mean = ", model.means_[i]) print("var = ", np.diag(model.covars_[i])) print() fig, axs = plt.subplots(model.n_components, sharex=True, sharey=True) colours = cm.rainbow(np.linspace(0, 1, model.n_components)) for i, (ax, colour) in enumerate(zip(axs, colours)): # Use fancy indexing to plot data in each state.
mask = hidden_states == i ax.plot_date(dates[mask], close_v[mask], ".-", c=colour) ax.set_title("{0}th hidden state".format(i)) # Format the ticks. ax.xaxis.set_major_locator(YearLocator()) ax.xaxis.set_minor_locator(MonthLocator()) ax.grid(True) plt.show()
Out:
Transition matrix
[[ 9.79220773e-01 2.57382344e-15 2.72061945e-03 1.80586073e-02]
[ 1.12216188e-12 7.73561269e-01 1.85019044e-01 4.14196869e-02]
[ 3.25313504e-03 1.12692615e-01 8.83368021e-01 6.86228435e-04]
[ 1.18741799e-01 4.20310643e-01 1.18670597e-18 4.60947557e-01]]
Means and vars of each hidden state
0th hidden state
mean = [ 2.33331888e-02 4.97389989e+07]
var = [ 6.97748259e-01 2.49466578e+14]
1th hidden state
mean = [ 2.12401671e-02 8.81882861e+07]
var = [ 1.18665023e-01 5.64418451e+14]
2th hidden state
mean = [ 7.69658065e-03 5.43135922e+07]
var = [ 5.02315562e-02 1.54569357e+14]
3th hidden state
mean = [ -3.53210673e-01 1.53080943e+08]
var = [ 2.55544137e+00 5.88210257e+15]
C語言版: 一、 HTK(Hidden Markov Model Toolkit): HTK是英國劍橋大學開發的一套基於C語言的隱馬爾科夫模型工具箱,主要應用於語音識別、語音合成的研究,也被用在其餘領域,如字符識別和DNA排序等。HTK是重量級的HMM版本。 HTK主頁:http://htk.eng.cam.ac.uk/ 二、 GHMM Library: The General Hidden Markov Model library (GHMM) is a freely available LGPL-ed C library implementing efficient data structures and algorithms for basic and extended HMMs. GHMM主頁:http://www.ghmm.org/ 三、 UMDHMM(Hidden Markov Model Toolkit): Hidden Markov Model (HMM) Software: Implementation of Forward-Backward, Viterbi, and Baum-Welch algorithms. 這款屬於輕量級的HMM版本。 UMDHMM主頁:http://www.kanungo.com/software/software.html
Java版: 四、 Jahmm Java Library (general-purpose Java library): Jahmm (pronounced "jam"), is a Java implementation of Hidden Markov Model (HMM) related algorithms. It's been designed to be easy to use (e.g. simple things are simple to program) and general purpose. Jahmm主頁:http://code.google.com/p/jahmm/
Malab版: 五、 Hidden Markov Model (HMM) Toolbox for Matlab: This toolbox supports inference and learning for HMMs with discrete outputs (dhmm's), Gaussian outputs (ghmm's), or mixtures of Gaussians output (mhmm's). Matlab-HMM主頁:http://www.cs.ubc.ca/~murphyk/Software/HMM/hmm.html
Common Lisp版: 六、CL-HMM Library (HMM Library for Common Lisp): Simple Hidden Markov Model library for ANSI Common Lisp. Main structures and basic algorithms implemented. Performance speed comparable to C code. It's licensed under LGPL. CL-HMM主頁:http://www.ashrentum.net/jmcejuela/programs/cl-hmm/
Haskell版: 七、The hmm package (A Haskell library for working with Hidden Markov Models): A simple library for working with Hidden Markov Models. Should be usable even by people who are not familiar with HMMs. Includes implementations of Viterbi's algorithm and the forward algorithm. Haskell-HMM主頁:http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hmm 注:Haskell是一種純函數式編程語言,它的命名源自美國數學家Haskell Brooks Curry,他在數學邏輯方面上的工做使得函數式編程語言有了普遍的基礎。