1. Using Ms Excel or
Google Sheets
Convolution Neural Network
Hidden Layer
Search for NP problem
https://www.linkedin.com/in/a111chen @2022
2. Convolution Neural Network
Objective:
1) Find the Xn using the Kernel, Convolution, Relu and Pooling
2) Find the Wn using the Matrix, Linest or Solver Method
3) What if Xn have correlation with each other?
Why:
1) To evaluate the pattern of the data
2) To predict the outcome for the new data from existing approximate
function f(x) = W0X0 + W1X1 + W2X2 + …. WnXn, with X0 = 1 (Bias)
5. 11 3 0 1 2
0 4 2 0 1
3 2 7 1 0
4 0 4 10 1
2 5 1 3 14
Matrix Xn:
5 x 5
W1
W2
W3
W4
W5
Matrix Wn:
5 x 1
45
30
15
20
92
Matrix WnXn:
5 x 1
X =
Matrix Wn = Reverse order of TRANSPOSE(LINEST(Matrix WnXn, Matrix Xn, False, False))
1.423902
6.895467
-0.62519
1.313663
3.668504
=
W1
W2
W3
W4
W5
Find the Wn using the Linest Method
6. 11 3 0 1 2
0 4 2 0 1
3 2 7 1 0
4 0 4 10 1
2 5 1 3 14
Matrix Xn:
5 x 5
W1
W2
W3
W4
W5
Matrix Wn:
5 x 1
45
30
15
20
92
Matrix WnXn:
5 x 1
X =
Using solver
1) Matrix Wn put random number => Changing Variables
2) Predict Matrix WnXn = MMULT(Matrix Wn, Matrix Xn)
3) Error^2 = SUMXMY2(Predict Matrix WnXn,Matrix
WnXn) => Set Objective Min
W1X1
W2X2
W3X3
W4X4
W5X5
Predict Matrix WnXn :
5 x 1
1.423904
6.895465
-0.62518
1.313662
3.668504
W1
W2
W3
W4
W5
Find the Wn using the Solver Method
Helper Matrix
=
SUMXMY2
7. 0.5
0.5
0.3
0.1
0.5 0.5 0.3 0.1
Matrix A: 4 x 1 Matrix B: 1 x 4 Matrix C: 4 x 4
X
Find Matrix C
=
Matrix C = MMULT(Matrix A, Matrix B)
Matrix C = MMULT(Matrix A, TRANSPOSE(Matrix A))
?
0.25 0.25 0.15 0.05
0.25 0.25 0.15 0.05
0.15 0.15 0.09 0.03
0.05 0.05 0.03 0.01
Matrix C: 4 x 4
8. 1.00 -0.02 0.09 0.50
-0.02 1.00 -0.74 0.10
0.09 -0.74 1.00 0.54
0.50 0.10 0.54 1.00
Array D:
Correlation
W1
W2
W3
W4
Array C:
Weighted
16.54872
20.07137
15.4287
15.44397
Array B:
SD
17.865
23.2375
13.5875
17.81
Array A:
Return
Using solver Find Optimal for Weighted
1) Array C put random number => Changing Variables
2) Predict Portfolio SD = SQRT(SUMPRODUCT(MMULT(Array C,
TRANSPOSE(Array C)), MMULT(Array B, TRANSPOSE(Array B
)),Array D))
3) Predict Portfolio Return = SUMPRODUCT(Array C, Array A)
4) Risk free rate = 2.02
5) Sharpe ratio = (Predict Portfolio SD – Risk free rate) /
Predict Portfolio Return => Set Objective Max
W1
W2
W3
W4
0.3
0.5
0.2
0
=
What if Xn have correlation with each other?
9. Hidden Layer
Objective:
1. Table or Array (Table, {}) is useful for later check back the error more
faster and look organise
2. (Randombetween) for random number between top and bottom.
3. (Mod) for rotation in circle like 24hour will go back to 0,
4. For String (mid, left, right, concat, textjoin, substitute, len, find)
5. Calculate using (If, Sumifs, Countifs, Let, and, or) with criteria
6. Lookup data using (Index, Match, Vlookup , Choose , Filter , Unique)
for value between two array or table column
7. (Sequence) for generate a list of sequential numbers in an array
Why:
1. To understand and calculate the China ancient mathematic
2. From simple to complex layer using table, formula, and array
formula.
10. Step 1
Possible Simulate Predict Error^2
5 70.8% 75.0% 17.64
9 29.2% 25.0% 17.64
Step 2
Possible Simulate Predict Error^2
4 48.9% 50.0% 1.21
8 51.1% 50.0% 1.21
Step 3
Possible Simulate Predict Error^2
4 51.3% 50.0% 1.69
8 48.7% 50.0% 1.69
Step 4
Possible Simulate Predict Error^2
6 5.5% 6.3% 0.56
7 29.2% 31.3% 4.20
8 44.2% 43.8% 0.20
9 21.1% 18.8% 5.52
Total 100.0% 100.0% 10.49
少 老 Total
阴 43.8% 6.3% 50.0%
阳 31.3% 18.8% 50.0%
Total 75.0% 25.0% 100.0%
Step 1
[@Bal0] = 50 -1 = 49
[@L1] = RANDBETWEEN(1,[@Bal0])
[@Pos1] =3+MOD([@L1]-2,4)+MOD([@Bal0]-[@L1]-1,4)
Step 2
[@Bal1]=[@Bal0]-[@Pos1]
[@L2] =RANDBETWEEN(1,[@Bal1])
[@Pos2] =3+MOD([@L2]-2,4)+MOD([@Bal1]-[@L2]-1,4)
Step 3
[@Bal2]=[@Bal1]-[@Pos2]
[@L3] =RANDBETWEEN(1,[@Bal2])
[@Pos3] =3+MOD([@L3]-2,4)+MOD([@Bal2]-[@L3]-1,4)
Step 4
[@Pos4] =([@Bal2]-[@Pos3])/4
Simple : Possibility of 易经占卜
After simulate for 1000 times
Note: Mod without Zero
1) MOD(n-1,d)+1
2) If(MOD(n,d) = 0, d,
MOD(n,d))
20. Search for NP problem
Objective:
1. Declare, Read and Write for the Array, Collection and Dictionary
2. Apply at Snake and Ladder game using do loop
3. Apply at Sudoku Solver using Back tracking Search
4. Apply at Tic Tac Toe using Search from game play
5. Apply at Monte Carlo Tree Search
6. Check the accuracy between actual with prediction
Why:
1. Reduce the interaction between worksheet function and VBA sub
procedure, then the speed will be more faster
2. To searching the best possible solution for solving NP problem
21. Array
Public arrA As Variant
ReDim arrA(1 To 8, 1 To 8)
arrA = Range(“A1:H8")
For row = 1 to 8
For col = 1 to 8
arrA(row,col) = cells(row,col)
Next
Next
Cells(13, "D").Resize(8, 8) = arrA
Collection with Class and Array
Set oColl = New clsMarkovChain
oColl.Board = Board
oColl.PlayerColor = PlayerColor
oColl.Position = Position
collA.Add oColl
Public collA As Collection
Public oColl As clsMarkovChain
ReDim arrt(1 To LastCol)
For i = 1 To collA.Count
Set oColl = collA(i)
arrA(i) = oColl.Board & "," &
oColl.PlayerColor &
oColl.Position & "," & result
Next
Cells(13, “D").Resize(1, collA.Count) = arrA
Enable Mircosoft Scripting Runtime
Dim dictA As New Scripting.Dictionary
For i = 1 To LastRow
If dictA.Exists(arrA(i, Col)) = True Then
dictA(arrA(i, Col)) = dictA(arrA(i, Col)) + 1
Else
dictA.Add arrA(i,Col), 1
End If
Next
ReDim arrA(1 To dictA.Count, 1 To 2)
Row = 1
For Each key In dictA
arrA(row, 1) = key
arrA(row, 2) = dictA(key)
row = row + 1
Next
Cells(13, “D").Resize(row-1,2) = arrA
Dictionary with Array
22. Snake and Ladder
From To Up/Down
1 38 Ladder
4 14 Ladder
9 31 Ladder
16 6 Snake
21 42 Ladder
28 84 Ladder
36 44 Ladder
47 26 Snake
49 11 Snake
51 67 Ladder
56 53 Snake
64 60 Snake
71 91 Ladder
80 100 Ladder
87 24 Snake
93 73 Snake
95 75 Snake
98 78 Snake
Start 0
Last 100
r = 0
Check = False
Do
'Add dice number
Randomize
Dice = Int((6 * Rnd) + 1)
Start = Start + Dice
'If more than 100
If Start > 100 Then Start = 200 - Start
'If hit the snake and ladder
For i = 1 To 18
If SnakeLadder(i, 1) = Start Then Start = SnakeLadder(i, 2)
Next
r = r + 1
If start = last Then Check = True
Loop Until Check = True
Find how many turn to finish the game? Looping
Input Data
Dice is
random from
1 to 6
Note: Random Integer Range
Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
23. Sudoku Solver
Constraint Propagation Search (Back Tracking)
(2) Hidden Single
IF(OR(COUNTIF(INDIRECT("Row"&[@Row]&"_"),N)=1,COUN
TIF(INDIRECT("Col"&[@Col]),N)=1,COUNTIF(INDIRECT("Bo
x"&[@Box]),N)=1),"",1)
N= 1, 2,3,4,5,6,7,8,9
=IF(COUNTIFS(INDIRECT("Table1[["&[@No]&"]:["&[@No]&"
]]"),[@Sum],Table1[N],N)=1,N,"")
No = Col, Row, Box
Sum = A to I, 1 to 9, A to I
N=1,2,3,4,5,6,7,8,9
(1) Naked Single
(1) Create a new copy of values for each recursive call to
search. The alternative is to keep track of each change to
values and undo the change when we hit a dead end.
Variable ordering (which square do we try first?)
• From Top to Bottom and Left to Right (Box1)
Value ordering (which digit do we try first for the square?)
• From Left to Right (Len1)
If Box1 = 1 And Len1 = 0 Then
MsgBox "Unsolvable Sudoku", vbExclamation
End If
If Len1 <= 0 Then
Range(Cells(Row1 + 1, 11), Cells(Row1 + 9, 20)).Clear
Range(Cells(Row0 + 1, 11), Cells(Row0 + 9, 20)).Copy Range("InputCopy")
Range(Cells(Box1 + 2, 22), Cells(Box1 + 2, 23)).Clear
Cells(Box1 + 1, 24) = Cells(Box1 + 1, 24) + 1
Cells(Box1 + 2, 24) = 1
Call FillLeftNumber
Else
Range(Cells(Row1 + 1, 11), Cells(Row1 + 9, 20)).Copy Range("InputCopy")
Cells(Box1 + 2, 24) = Cells(Box1 + 2, 24) + 1
Call FillLeftNumber
End If
(3) Fill Only One Possible
24. Tic Tac Toe
Create a Winner Determiner Function Search from game play
(1) Check Row, Column, Diagonal got
Win, Loss and Draw
(2) Check the number of empty
space for Row, Column and Diagonal
1. Collect the game play dataset in (Board,
Policy, Result)= Markov decision process
2. Check the empty space or legal move
got Player 1 Win, Player -1 Win and
Draw (for End game)
3. For the current state, check the win
possibility from the game play dataset
for Player 1 win, Player -1 win or Draw
for each policy (w/n) (for Middle game)
4. Calculate the win rate = (2)Empty space
+ (3)Dataset
5. Select the highest win rate, if got more
than one highest win rate, then
randomly select the highest win rate
1 2 3
Row 0 0 0
Col 0 0 0
Dia 0 0
Matrix to check empty space,
Win, Loss and Draw
25. Monte Carlo Tree Search (Simulation and Rollup)
1. Calculate the UCT
UCT(row, col) = w/n + C * Sqr(Log(N) / n), with C=Sqr(2)
1.1 If have prediction, suggestion PUCT
PUCT(row, col) = w/n + (PredictValue – w/n)/n + C *
PredictPolicy * Sqr(N) / (1 + n),
with C = Log((N+19652+1)/19652) + 2.5
2. Convert to List for Legal Move and UCT
i = 1
For row = 1 To 8
For col = 1 To 8
If LegalMove(row, col) <> "" Then
LegalMoveA(i) = LegalMove(row, col)
UCT(i) = UCT(row, col)
i = i + 1
End If
Next
Next
4. Extract the Max Value of UCT for the Legal Move
For i = 1 To col
If GetMaxValue <= UCT(i) Then GetMaxValue = UCT(i)
Next
j = 1
For i = 1 To col
If UCT(i) = GetMaxValue Then
LegalMoveA(j) = LegalMoveA(i)
j = j + 1
End If
Next
6. Random Select if Max Value of UCT is more than One
If j > 2 Then
Randomize
j = Int(1 + Rnd * (j - 1))
Position = LegalMoveA(j)
End If
3. Only One Legal Move
If i = 2 Then Position = LegalMoveA(1)
5. Only One Max Value of UCT
If j = 2 Then Position = LegalMoveA(1)
Predict from
Convolution Neural
Network or other
Evaluation
26. Observation Actual Training Predicted Classification
1 1 1 TP
2 0 0 TN
3 1 1 TP
4 1 0 FN
5 1 1 TP
6 1 0 FN
7 0 0 TN
8 0 0 TN
9 0 0 TN
10 0 1 FP
TP 3
FP 1
FN 2
TN 4
Precision (P) TP/(TP – FP) 0.75
Recall (R) TP/(TP – FN) 0.6
Accuracy (TP+TN)/(TP+FP+FN+TN) 0.7
F1 score 2*P*R/(P+R) 0.666666667
False positive rate (FPR) FP/(FP+TN) 0.2
True positive rate (TPR) TP/(TP+FN) 0.6
Check the accuracy
Root Mean Squared Error (RMSE).
=SQRT(SUMXMY2(Actual Training, Predicted)/No of Observation)
= 0.547722558
No d/dx equal
1 c 0
2 x^n n * x^(n-1)
3 a^x a^x * In a
4 a^u a^u * u' * In a
5 c * u c * u'
6 u * v u' * v + u * v'
7 u / v (v * u' - u * v') / v^2
8 f(g[u]) f'(g[u]) * g'[u] * u'
9 f(g[x]) f'(g[x]) * g'[x]
10 f(x)^ n n * f(x) ^ (n-1) * f'(x)
11 log a
(u)
u' / (u * In a)
12 In (u) u'/u
13 x^x x^x * (In x + 1)
c = constant
u = function
u' = d/dx [function]
v = function
v' = d/dx [function]
chain rule
In (y) = 1/y, if In (x) = 1/x
Logarithmic ,
Step 1: Both side "In",
Step 2: follow u * v,
Step 3: multiply y to the d/dx
The derivative of a function
27. An idea wants to be shared. And,
in the sharing, it becomes more
complex, more interesting, and
more likely to work for more
people.
—adrienne maree brown, writer
and activist