The hidden markov model is a finite set of states, each of which is associated with a “generally multidimensional” probability distribution. Transitions among the states are governed by a set of probabilities called transition probabilities. - jedlik.phy.bme.hu
隐马尔可夫模型使用概率来根据过去发生的事件预测未来事件或状态。 状态(state)被认为是隐藏的,因为我们从不直接查看或访问。 相反,我们用每个状态的观察(observation)。 在每个状态,我们都有一个基于概率分布的与之相关的观察。
例如,关于天气,状态指的是热或冷。 在热状态下的观察是,“如果外面很热,一个人快乐的可能性为 70%,一个人悲伤的可能性为 30%。另一方面,转换(transition)是转换到不同状态的可能性。 例如,“寒冷的一天有 30% 的变化是紧随其后的炎热的一天”。
我们来设计可以预测天气的隐马尔可夫模式
天气预测 数据 Data要创建一个隐马尔可夫模型,我们需要
- 状态
- 观察分布
- 过渡分布
假设我们已经有了这些信息。 我们的模型和信息如下所示。
- 寒冷的日子用 0 编码,炎热的日子用 1 编码。
- 我们序列中的第一天有 80% 的几率感冒。
- 寒冷的一天有 30% 的机会紧随其后是炎热的一天。
- 炎热的一天有 20% 的机会紧随其后是寒冷的一天。
- 每天的温度正态分布,在寒冷的日子里,平均值和标准偏差为 0 和 5,在炎热的日子里,平均值和标准偏差为 15 和 10。
# imports
import tensorflow_probability as tfp
import tensorflow as tf
# constants
tfd = tfp.distributions # making a shortcut for this model
initial_distribution = tfd.Categorical(probs=[0.2, 0.8]) # Refer to point 2 above
transition_distribution = tfd.Categorical(probs=[[0.7, 0.3],
[0.2, 0.8]]) # refer to points 3 and 4 above
observation_distribution = tfd.Normal(loc=[0., 15.], scale=[5., 10.]) # refer to point 5 above
# the loc argument represents the mean and the scale is the standard devitation
用这个信息我们来预测每天的温度。
模型 Model用Tensorflow设计HMM模型
# make model
model = tfd.HiddenMarkovModel(
initial_distribution=initial_distribution,
transition_distribution=transition_distribution,
observation_distribution=observation_distribution,
num_steps=7) # the number of steps represents the number of days we would like to predict for
执行模式
# run our model and get our expected values mean = model.mean() # partially defined tensor # due to the way TensorFlow works on a lower level we need to evaluate part of the graph # from within a session to see the value of this tensor # in the new version of tensorflow we need to use tf.compat.v1.Session() rather than just tf.Session() with tf.compat.v1.Session() as sess: print(mean.numpy()) # this will give us the average temperatures
我们下面七天的温度预测
[11.999999 10.5 9.750001 9.375 9.1875 9.09375 9.046875]
尝试不同的参数,看看它如何影响模型!比如,如果我们提高热天的平均值,如何影响天气温度?
Conclusion我们设计的隐马尔可夫模型实际上并不是很好。 例如,这个模型在很远的未来几天内表现不佳,因为它永远不会更新其概率分布。 我们不能只用今天的天气来预测明年的天气。以后再了解HMM的不同应用,下次再见!



