Skip to content

Commit 7b616ad

Browse files
authored
Add files via upload
1 parent 0f4f71a commit 7b616ad

17 files changed

+1368
-0
lines changed

Supporting algorithm 1 (VB).bas

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Attribute VB_Name = "Module1"
2+
'##############################################################################################
3+
'# John Wiley & Sons, Inc. #
4+
'# #
5+
'# Book: Markov Chains: From Theory To Implementation And Experimentation #
6+
'# Author: Dr. Paul Gagniuc #
7+
'# Data: 01/09/2016 #
8+
'# #
9+
'# Description: #
10+
'# Supporting algorithm 1. A two states Markov Chain simulator based on the proportions of #
11+
'# letters. Two letter sequences with predetermined proportions of “W” and “B” letters are #
12+
'# used for the representation of two jars. The chance of a letter chosen at random from #
13+
'# one of the two sequences is directly dictated by the proportions of “W” and “B” letters. #
14+
'##############################################################################################
15+
16+
Dim Jar(0 To 1) As String
17+
18+
Private Sub main()
19+
20+
draws = 17
21+
22+
Jar(0) = "WWBBBBBBBB"
23+
Jar(1) = "WWWWWBBBBB"
24+
25+
a = Draw(0) ' Draws start from jar "W"
26+
z = z & " Jar W[" & a & "],"
27+
28+
For i = 1 To draws
29+
30+
If a = "W" Then
31+
a = Draw(0)
32+
z = z & " Jar W[" & a & "],"
33+
Else
34+
a = Draw(1)
35+
z = z & " Jar B[" & a & "],"
36+
End If
37+
38+
MsgBox z
39+
Next i
40+
41+
End Sub
42+
43+
Function Draw(ByVal S As Integer) As String
44+
Randomize
45+
randomly_choose = Int(Rnd * Len(Jar(S)))
46+
ball = Mid(Jar(S), randomly_choose + 1, 1)
47+
Draw = ball
48+
End Function

Supporting algorithm 10 (VB).bas

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
Attribute VB_Name = "Module1"
2+
'##############################################################################################
3+
'# John Wiley & Sons, Inc. #
4+
'# #
5+
'# Book: Markov Chains: From Theory To Implementation And Experimentation #
6+
'# Author: Dr. Paul Gagniuc #
7+
'# Data: 01/09/2016 #
8+
'# #
9+
'# Description: #
10+
'# Supporting algorithm 10. Predictions based on sequences produced by n-state #
11+
'# Markov machines. This example also uses a DNA sequence as a model. However, #
12+
'# the algorithm allows for an unlimited number of letters (observations). #
13+
'# Previously, the vector – matrix multiplication cycle was declared manually #
14+
'# with a range of expressions. Here, the multiplication cycle is made #
15+
'# iteratively. For a prediction on more than 4 states, the matrix elements #
16+
'# and the number of vector components can be increased to cover a new #
17+
'# prediction requirement. Note that “ExtractProb” function is not shown. #
18+
'# However, when the above algorithm is used the “ExtractProb” function #
19+
'# must be present. #
20+
'##############################################################################################
21+
22+
Dim M(1 To 4, 1 To 4) As String
23+
24+
Private Sub main()
25+
26+
Dim v(0 To 3, 0 To 1) As Variant
27+
28+
Call ExtractProb("TACTTCGATTTAAGCGCGGCGGCCTATATTA")
29+
30+
chain = 5
31+
32+
v(0, 0) = 1
33+
v(1, 0) = 0
34+
v(2, 0) = 0
35+
v(3, 0) = 0
36+
37+
v(0, 1) = 0
38+
v(1, 1) = 0
39+
v(2, 1) = 0
40+
v(3, 1) = 0
41+
42+
For k = 1 To chain
43+
44+
For i = 0 To 3
45+
For j = 0 To 3
46+
v(i, 1) = v(i, 1) + (v(j, 0) * M(j + 1, i + 1))
47+
Next j
48+
Next i
49+
50+
For i = 0 To 3
51+
v(i, 0) = v(i, 1)
52+
v(i, 1) = 0
53+
Next i
54+
55+
A = v(0, 0)
56+
T = v(1, 0)
57+
c = v(2, 0)
58+
G = v(3, 0)
59+
60+
MsgBox "V(" & k & ")=[" & A & " | " & T & " | " & c & " | " & G & "]"
61+
62+
Next k
63+
64+
End Sub
65+
66+
67+
Function ExtractProb(ByVal s As String)
68+
69+
Ea = "A"
70+
Et = "T"
71+
Eg = "G"
72+
Ec = "C"
73+
74+
For i = 1 To 4
75+
For j = 1 To 4
76+
M(i, j) = 0
77+
Next j
78+
Next i
79+
80+
Ta = 0
81+
Tt = 0
82+
Tg = 0
83+
Tc = 0
84+
85+
For i = 2 To Len(s) - 1
86+
87+
DI1 = Mid(s, i, 1)
88+
DI2 = Mid(s, i + 1, 1)
89+
90+
If DI1 = Ea Then r = 1
91+
If DI1 = Et Then r = 2
92+
If DI1 = Eg Then r = 3
93+
If DI1 = Ec Then r = 4
94+
95+
If DI2 = Ea Then c = 1
96+
If DI2 = Et Then c = 2
97+
If DI2 = Eg Then c = 3
98+
If DI2 = Ec Then c = 4
99+
100+
M(r, c) = Val(M(r, c)) + 1
101+
102+
If DI1 = Ea Then Ta = Ta + 1
103+
If DI1 = Et Then Tt = Tt + 1
104+
If DI1 = Eg Then Tg = Tg + 1
105+
If DI1 = Ec Then Tc = Tc + 1
106+
107+
Next i
108+
109+
For i = 1 To 4
110+
For j = 1 To 4
111+
If i = 1 Then M(i, j) = Val(M(i, j)) / Ta
112+
If i = 2 Then M(i, j) = Val(M(i, j)) / Tt
113+
If i = 3 Then M(i, j) = Val(M(i, j)) / Tg
114+
If i = 4 Then M(i, j) = Val(M(i, j)) / Tc
115+
Next j
116+
Next i
117+
118+
End Function
119+

Supporting algorithm 11 (VB).bas

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Attribute VB_Name = "Module1"
2+
'##############################################################################################
3+
'# John Wiley & Sons, Inc. #
4+
'# #
5+
'# Book: Markov Chains: From Theory To Implementation And Experimentation #
6+
'# Author: Dr. Paul Gagniuc #
7+
'# Data: 01/09/2016 #
8+
'# #
9+
'# Description: #
10+
'# Supporting algorithm 11. The conversion of measurements to states. #
11+
'# A range of values is divided into 4 equal regions. Each region #
12+
'# corresponds to a state: “A”, “B”, “C”, and “D”. The numeric values #
13+
'# are associated with a representative letter based on their position #
14+
'# over the regions. Thus, the initial values are listed as letters (observations). #
15+
'##############################################################################################
16+
17+
Private Sub main()
18+
Dim Inp() As String
19+
Dim R As String
20+
21+
R = "159,82,187,194,179,115,197,102,105,104,95,126,74,143,143,127,98," & _
22+
"70,92,170,168,182,149,85,137,100,170,180,61,177,86,195,198,182,150," & _
23+
"197,103,103,186,100,96,196"
24+
25+
Inp = Split(R, ",")
26+
Lu = 200
27+
Ld = 60
28+
n = 4
29+
Pr = (Lu - Ld) / n
30+
31+
For i = 0 To UBound(Inp)
32+
s = (Inp(i) - Ld) / Pr
33+
s = Split(s, ".")(0)
34+
35+
If s = 0 Then l = "A"
36+
If s = 1 Then l = "B"
37+
If s = 2 Then l = "C"
38+
If s = 3 Then l = "D"
39+
40+
Obs = Obs & l
41+
Reg = Reg & s & ","
42+
Next i
43+
44+
MsgBox "Reg=" & Reg & vbCrLf & "Obs=" & Obs & vbCrLf
45+
End Sub

Supporting algorithm 12 (VB).bas

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Attribute VB_Name = "Module1"
2+
'##############################################################################################
3+
'# John Wiley & Sons, Inc. #
4+
'# #
5+
'# Book: Markov Chains: From Theory To Implementation And Experimentation #
6+
'# Author: Dr. Paul Gagniuc #
7+
'# Data: 01/09/2016 #
8+
'# #
9+
'# Description: #
10+
'# Supporting algorithm 12. Prediction based on a 3x3 transition matrix. #
11+
'# Known transition probability values are directly used from a transition #
12+
'# matrix for highlighting the behavior of an absorbing Markov Chain. #
13+
'##############################################################################################
14+
15+
Dim Jar(1 To 3, 1 To 3) As String
16+
17+
Private Sub main()
18+
Dim v(0 To 2, 0 To 1) As Variant
19+
20+
Jar(1, 1) = 0.33
21+
Jar(1, 2) = 0.33
22+
Jar(1, 3) = 0.33
23+
24+
Jar(2, 1) = 0.5
25+
Jar(2, 2) = 0.5
26+
Jar(2, 3) = 0
27+
28+
Jar(3, 1) = 0
29+
Jar(3, 2) = 0
30+
Jar(3, 3) = 1
31+
32+
chain = 5
33+
34+
v(0, 0) = 1
35+
v(1, 0) = 0
36+
v(2, 0) = 0
37+
38+
v(0, 1) = 0
39+
v(1, 1) = 0
40+
v(2, 1) = 0
41+
42+
For k = 1 To chain
43+
44+
For i = 0 To 2
45+
For j = 0 To 2
46+
v(i, 1) = v(i, 1) + (v(j, 0) * Jar(j + 1, i + 1))
47+
Next j
48+
Next i
49+
50+
For i = 0 To 2
51+
v(i, 0) = v(i, 1)
52+
v(i, 1) = 0
53+
Next i
54+
55+
A = v(0, 0)
56+
B = v(1, 0)
57+
C = v(2, 0)
58+
59+
MsgBox "Step(" & k & ")=[" & A & " | " & B & " | " & C & "]"
60+
61+
Next k
62+
63+
End Sub

Supporting algorithm 13 (VB).bas

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Attribute VB_Name = "Module1"
2+
'##############################################################################################
3+
'# John Wiley & Sons, Inc. #
4+
'# #
5+
'# Book: Markov Chains: From Theory To Implementation And Experimentation #
6+
'# Author: Dr. Paul Gagniuc #
7+
'# Data: 01/09/2016 #
8+
'# #
9+
'# Description: #
10+
'# Supporting algorithm 13. Prediction framework based on a 4x4 transition matrix. #
11+
'# Known transition probability values are directly used from a transition matrix #
12+
'# for highlighting the behavior of a Markov Chain. A variable number of states #
13+
'# and configurations can be tested. For instance, a diagram with three states can #
14+
'# be molded on the algorithm. However, since the framework is made for a total of #
15+
'# 4 states the following modifications are required: all Jar(n, 4) elements are #
16+
'# set to zero (Jar(1, 4)=0; Jar(2, 4)=0; Jar(3, 4)=0; and Jar(4, 4)=0). Furthermore, #
17+
'# the molding of a diagram with two states on this framework involves two #
18+
'# modifications: all Jar(n, 4) elements are set to zero and all Jar(n, 3) elements #
19+
'# are set to zero. Any type of diagram configuration can be tested by following two #
20+
'# rules: 1) the absence of an arrow is indicated by zero, and 2) any value greater #
21+
'# than zero and less than or equal to 1 indicates an arrow. #
22+
'##############################################################################################
23+
24+
Dim Jar(1 To 4, 1 To 4) As String
25+
Private Sub main()
26+
27+
Dim v(0 To 3, 0 To 1) As Variant
28+
29+
Jar(1, 1) = 1
30+
Jar(1, 2) = 0
31+
Jar(1, 3) = 0
32+
Jar(1, 4) = 0
33+
34+
Jar(2, 1) = 0.5
35+
Jar(2, 2) = 0
36+
Jar(2, 3) = 0.5
37+
Jar(2, 4) = 0
38+
39+
Jar(3, 1) = 0
40+
Jar(3, 2) = 0.5
41+
Jar(3, 3) = 0
42+
Jar(3, 4) = 0.5
43+
44+
Jar(4, 1) = 0
45+
Jar(4, 2) = 0
46+
Jar(4, 3) = 1
47+
Jar(4, 4) = 0
48+
49+
chain = 5
50+
51+
v(0, 0) = 0
52+
v(1, 0) = 0
53+
v(2, 0) = 0
54+
v(3, 0) = 1
55+
56+
v(0, 1) = 0
57+
v(1, 1) = 0
58+
v(2, 1) = 0
59+
v(3, 1) = 0
60+
61+
For k = 1 To chain
62+
For i = 0 To 3
63+
For j = 0 To 3
64+
v(i, 1) = v(i, 1) + (v(j, 0) * Jar(j + 1, i + 1))
65+
Next j
66+
Next i
67+
68+
For i = 0 To 3
69+
v(i, 0) = v(i, 1)
70+
v(i, 1) = 0
71+
Next i
72+
73+
A = v(0, 0)
74+
B = v(1, 0)
75+
C = v(2, 0)
76+
D = v(3, 0)
77+
78+
MsgBox "Step(" & k & ")=[" & A & "|" & B & "|" & C & "|" & D & "]"
79+
Next k
80+
81+
End Sub

0 commit comments

Comments
 (0)