马尔科夫链模型

实验代码

#股市的概率分布为[0.4, 0.3, 0.3]
import numpy as np

P = np.matrix([[0.5, 0.3, 0.2], [0.6, 0.35, 0.05], [0.5, 0.05, 0.45]])
p = np.matrix([[0.4, 0.3, 0.3]])
for i in range(1, 101):
    p = p * P
    print(f"round{i}:")
    print(p)

部分输出

round1:
[[0.53 0.24 0.23]]
round2:
[[0.524  0.2545 0.2215]]
round3:
[[0.52545 0.25735 0.2172 ]]
round4:
[[0.525735  0.2585675 0.2156975]]
round5:
[[0.52585675 0.259004   0.21513925]]
round6:
[[0.5259004  0.25916539 0.21493421]]
round7:
[[0.52591654 0.25922472 0.21485875]]
round8:
[[0.52592247 0.25924655 0.21483098]]
round9:
[[0.52592465 0.25925458 0.21482076]]
round10:
[[0.52592546 0.25925754 0.214817  ]]
round11:
[[0.52592575 0.25925863 0.21481562]]
round12:
[[0.52592586 0.25925903 0.21481511]]
round13:
[[0.5259259  0.25925917 0.21481492]]
round14:
[[0.52592592 0.25925923 0.21481485]]
round15:
[[0.52592592 0.25925925 0.21481483]]
round16:
[[0.52592592 0.25925925 0.21481482]]
round17:
[[0.52592593 0.25925926 0.21481482]]
round18:
[[0.52592593 0.25925926 0.21481482]]
round19:
[[0.52592593 0.25925926 0.21481482]]
round20:
[[0.52592593 0.25925926 0.21481481]]
round21:
[[0.52592593 0.25925926 0.21481481]]
round22:
[[0.52592593 0.25925926 0.21481481]]
round23:
[[0.52592593 0.25925926 0.21481481]]
round24:
[[0.52592593 0.25925926 0.21481481]]
round25:
[[0.52592593 0.25925926 0.21481481]]
round26:
[[0.52592593 0.25925926 0.21481481]]
round27:
[[0.52592593 0.25925926 0.21481481]]
round28:
[[0.52592593 0.25925926 0.21481481]]
round29:
[[0.52592593 0.25925926 0.21481481]]
round30:
[[0.52592593 0.25925926 0.21481481]]
round31:
[[0.52592593 0.25925926 0.21481481]]
round32:
[[0.52592593 0.25925926 0.21481481]]
round33:
[[0.52592593 0.25925926 0.21481481]]
round34:
[[0.52592593 0.25925926 0.21481481]]
round35:
[[0.52592593 0.25925926 0.21481481]]
round36:
[[0.52592593 0.25925926 0.21481481]]
round37:
[[0.52592593 0.25925926 0.21481481]]
round38:
[[0.52592593 0.25925926 0.21481481]]
round39:
[[0.52592593 0.25925926 0.21481481]]
round40:
[[0.52592593 0.25925926 0.21481481]]

实验代码

import numpy as np

P = np.matrix([[0.5, 0.3, 0.2], [0.6, 0.35, 0.05], [0.5, 0.05, 0.45]])
for i in range(1, 45):
    P = P * P
    print(f"round {i}:")
    print(P)

部分输出


round 1:
[[0.53  0.265 0.205]
 [0.535 0.305 0.16 ]
 [0.505 0.19  0.305]]
round 2:
[[0.5262   0.260225 0.213575]
 [0.527525 0.2652   0.207275]
 [0.523325 0.249725 0.22695 ]]
round 3:
[[0.52593077 0.25927708 0.21479215]
 [0.52595547 0.25936798 0.21467654]
 [0.5258784  0.25908441 0.21503719]]
round 4:
[[0.52592593 0.25925927 0.21481481]
 [0.52592594 0.2592593  0.21481477]
 [0.52592591 0.2592592  0.21481489]]
round 5:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 6:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 7:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 8:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 9:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 10:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 11:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 12:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 13:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 14:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 15:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 16:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 17:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 18:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 19:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 20:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 21:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 22:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 23:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 24:
[[0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]
 [0.52592593 0.25925926 0.21481481]]
round 25:
[[0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]]
round 26:
[[0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]]
round 27:
[[0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]]
round 28:
[[0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]]
round 29:
[[0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]
 [0.52592593 0.25925926 0.21481482]]
round 30:
[[0.52592594 0.25925927 0.21481482]
 [0.52592594 0.25925927 0.21481482]
 [0.52592594 0.25925927 0.21481482]]
round 31:
[[0.52592596 0.25925927 0.21481483]
 [0.52592596 0.25925927 0.21481483]
 [0.52592596 0.25925927 0.21481483]]
round 32:
[[0.52592599 0.25925929 0.21481484]
 [0.52592599 0.25925929 0.21481484]
 [0.52592599 0.25925929 0.21481484]]
round 33:
[[0.52592605 0.25925932 0.21481487]
 [0.52592605 0.25925932 0.21481487]
 [0.52592605 0.25925932 0.21481487]]
round 34:
[[0.52592618 0.25925938 0.21481492]
 [0.52592618 0.25925938 0.21481492]
 [0.52592618 0.25925938 0.21481492]]
round 35:
[[0.52592644 0.25925951 0.21481502]
 [0.52592644 0.25925951 0.21481502]
 [0.52592644 0.25925951 0.21481502]]
round 36:
[[0.52592695 0.25925976 0.21481523]
 [0.52592695 0.25925976 0.21481523]
 [0.52592695 0.25925976 0.21481523]]
round 37:
[[0.52592797 0.25926027 0.21481565]
 [0.52592797 0.25926027 0.21481565]
 [0.52592797 0.25926027 0.21481565]]
round 38:
[[0.52593001 0.25926127 0.21481648]
 [0.52593001 0.25926127 0.21481648]
 [0.52593001 0.25926127 0.21481648]]
round 39:
[[0.52593409 0.25926328 0.21481815]
 [0.52593409 0.25926328 0.21481815]
 [0.52593409 0.25926328 0.21481815]]
round 40:
[[0.52594225 0.25926731 0.21482148]
 [0.52594225 0.25926731 0.21482148]
 [0.52594225 0.25926731 0.21482148]]

从运行结果我们可以看出,三次之后的结果和最后的结果是一致的。

你可能感兴趣的:(numpy,python)