algorithmsbooks / decisionmaking

Algorithms for Decision Making textbook
522 stars 54 forks source link

Example 4.1 #97

Closed AlexandrParkhomenko closed 2 years ago

AlexandrParkhomenko commented 2 years ago

Hello Mykel J. Kochenderfer!

There is another questions:

It work:

function sub2ind(siz, x)
    k = vcat(1, cumprod(siz[1:end-1]))
    return dot(k, x .- 1) + 1
end
function statistics(vars, G, D::Matrix{Int})
    n = size(G)[1] #// size(D, 1) # why do we limit the size to D?
    r = [vars[i].m for i in 1:n] #// in old version was right
    q = [prod([r[j] for j in inneighbors(G, i)]) for i in 1:n]
    M = [zeros(q[i], r[i]) for i in 1:n]
    for o in eachcol(D)
        for i in 1:n
            k = o[i]
            parents = inneighbors(G, i)
            j = 1
            if !isempty(parents)
                j = sub2ind(r[parents], o[parents])
            end
            M[i][j, k] += 1.0
        end
    end
    return M
end
mykelk commented 2 years ago

To make the code consistent with the text, we updated Alg 2.1 to use r as the field name.

D is a matrix whose rows correspond to the number of variables. Hence, it should match the number of variables in G.

AlexandrParkhomenko commented 2 years ago

I missunderstand this things:

discrete variables that we denote as X1:n. Our data D = {o1, . . . , om}

What is the dimension of matrix D?

mykelk commented 2 years ago

If you have n variables and m data points, then D will be n x m.

AlexandrParkhomenko commented 2 years ago

I've got it. Thank you, Mykel J. Kochenderfer.