algorithmsbooks / decisionmaking

Algorithms for Decision Making textbook
522 stars 54 forks source link

Example 4.2 #98

Closed AlexandrParkhomenko closed 2 years ago

AlexandrParkhomenko commented 2 years ago

Hello Mykel J. Kochenderfer, Tim A. Wheeler, Kyle H. Wray ! This is full example:

using Graphs

struct Variable
    name::Symbol
    m::Int # number of possible values
end

G = SimpleDiGraph(3)
add_edge!(G, 1, 2)
add_edge!(G, 3, 2)
vars = [Variable(:A, 2), Variable(:B, 2), Variable(:C, 2)]
D = [1 2 2 1; 1 2 2 1; 2 2 2 2]

function prior(vars, G)
    n = length(vars)
    r = [vars[i].m for i in 1:n]
    q = [prod([r[j] for j in inneighbors(G, i)]) for i in 1:n]
    return [ones(q[i], r[i]) for i in 1:n]
end

println(prior(vars, G))
[[1.0 1.0], [1.0 1.0; 1.0 1.0; 1.0 1.0; 1.0 1.0], [1.0 1.0]]

The Algorythm produces only the Ones. What am I doing wrong?

Best regards, Alexandr.

mykelk commented 2 years ago

Yep, it should produce matrices of ones. I think Example 4.2 illustrates how this matrix of ones can be added to the data matrix to obtain the posterior Dirichlet parameters.

AlexandrParkhomenko commented 2 years ago

I just need to pay more attention to the text. Thanks a lot for the book, Mykel !