LoginSignup
7
10

More than 5 years have passed since last update.

マルコフ連鎖をPythonで

Last updated at Posted at 2017-12-30

マルコフ連鎖状態遷移の
http://web.tuat.ac.jp/~s-hotta/info/slide4.pdf
の天気の確率遷移をPythonで問題を説いてみる

markovchain.py
import numpy as np
#晴の日の翌日が、晴になる確率は 0.6 曇りになる確率は 0.3 雨になる確率は 0.1
#曇の日の翌日が、晴になる確率は 0.3 曇りになる確率は 0.6 雨になる確率は 0.1
#雨の日の翌日が、晴になる確率は 0.2 曇りになる確率は 0.3 雨になる確率は 0.5
#これを行列で表すと
prob_matrix  =np.array(
[[0.6, 0.3, 0.1],
[0.3, 0.6, 0.1],
[0.2, 0.3, 0.5]])

#初期状態を、晴として、翌日の天気の確率分布を計算(10日後まで繰り返す)
state = np.array([1,0, 0])
for x in range(10):
    state = np.dot(state,prob_matrix)
    print('{0}日後'.format(x + 1))
    print state

出力結果

1日後
[ 0.6  0.3  0.1]
2日後
[ 0.47  0.39  0.14]
3日後
[ 0.427  0.417  0.156]
4日後
[ 0.4125  0.4251  0.1624]
5日後
[ 0.40751  0.42753  0.16496]
6日後
[ 0.405757  0.428259  0.165984]
7日後
[ 0.4051287  0.4284777  0.1663936]
8日後
[ 0.40489925  0.42854331  0.16655744]
9日後
[ 0.40481403  0.42856299  0.16662298]
10日後
[ 0.40478191  0.4285689   0.16664919]

ということで、途中から天気の分布は収束する。

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10