从过渡矩阵生成马尔可夫链 [英] Generating Markov Chain from transition matrix

查看:171
本文介绍了从过渡矩阵生成马尔可夫链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在给定转换矩阵的情况下模拟数据。我使用这个已回答的问题,那么我的数据为:

I'm trying to simulate data given a transition matrix. I made the transition matrix using this answered question, so let's say my data is:

days=['rain', 'rain', 'rain', 'clouds', 'rain', 'sun', 'clouds', 'clouds', 
  'rain', 'sun', 'rain', 'rain', 'clouds', 'clouds', 'sun', 'sun', 
  'clouds', 'clouds', 'rain', 'clouds', 'sun', 'rain', 'rain', 'sun',
  'sun', 'clouds', 'clouds', 'rain', 'rain', 'sun', 'sun', 'rain', 
  'rain', 'sun', 'clouds', 'clouds', 'sun', 'sun', 'clouds', 'rain', 
  'rain', 'rain', 'rain', 'sun', 'sun', 'sun', 'sun', 'clouds', 'sun', 
  'clouds', 'clouds', 'sun', 'clouds', 'rain', 'sun', 'sun', 'sun', 
  'clouds', 'sun', 'rain', 'sun', 'sun', 'sun', 'sun', 'clouds', 
  'rain', 'clouds', 'clouds', 'sun', 'sun', 'sun', 'sun', 'sun', 'sun', 
  'clouds', 'clouds', 'clouds', 'clouds', 'clouds', 'sun', 'rain', 
  'rain', 'rain', 'clouds', 'sun', 'clouds', 'clouds', 'clouds', 'rain', 
  'clouds', 'rain', 'sun', 'sun', 'clouds', 'sun', 'sun', 'sun', 'sun',
  'sun', 'sun', 'rain']

我使用以下方法创建转换矩阵:

I create the transition matrix using:

pd.crosstab(pd.Series(days[1:],name='Tomorrow'),
            pd.Series(days[:-1],name='Today'),normalize=1)

具有以下输出:

Today      clouds      rain       sun
Tomorrow                             
clouds    0.40625  0.230769  0.309524
rain      0.28125  0.423077  0.142857
sun       0.31250  0.346154  0.547619

现在,我想使用上面的矩阵生成输出。因此,假设我的随机起点是雨,那么输出将是(例如):

Now, I want to generate output using the matrix above. So let's say my random starting point would be 'rain', then, the output would be (for example):

[rain, rain, clouds, sun] 

不幸的是,我只能找到使用字典构建矩阵的解决方案。

Unfortunately, I can only find solutions where the matrix is made using dictionaries.

编辑:
我使用过:

I used:

pd.crosstab(pd.Series(word[:-1],name='Current'),
            pd.Series(word[1:],name='Next'),normalize=0)

我自己的矩阵:

Next    a    b      c          d         e   f   g   h
Current                             
a      0.0  0.0 0.428571    0.571429    0.0 0.0 0.0 0.0
b      0.0  0.0 0.230769    0.769231    0.0 0.0 0.0 0.0
c      0.0  0.0 0.000000    0.000000    0.0 0.0 1.0 0.0
d      0.0  0.0 0.000000    0.000000    0.0 0.0 0.0 1.0
e      1.0  0.0 0.000000    0.000000    0.0 0.0 0.0 0.0
f      0.0  1.0 0.000000    0.000000    0.0 0.0 0.0 0.0
g      0.0  0.0 0.000000    0.000000    1.0 0.0 0.0 0.0
h      0.0  0.0 0.000000    0.000000    0.0 1.0 0.0 0.0


推荐答案

以下功能应该起作用- get_next_term 在给定转换矩阵和前一项的情况下生成链中的下一项,而 make_chain 创建一个长度为 n 的链,给定一个转换矩阵和初始项。

The following functions should work - get_next_term generates the next term in the chain given a transition matrix and the preceeding term, and make_chain creates a chain of length n given a transition matrix and the initial term.

代码:

import random
def get_next_term(t_s):
    return random.choices(t_s.index, t_s)[0]

def make_chain(t_m, start_term, n):
    chain = [start_term]
    for i in range(n-1):
        chain.append(get_next_term(t_m[chain[-1]]))
    return chain

用法:

>>> make_chain(transition_mat, 'rain', 5)
['rain', 'rain', 'clouds', 'clouds', 'sun']

使用您的数据:

>>> make_chain(transition_mat2, 'a', 8)
['a', 'e', 'g', 'c', 'a', 'e', 'g', 'c']

这篇关于从过渡矩阵生成马尔可夫链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆