Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small confusing implementation on assignments in Algorithm 2.1 and Example 3.1. #43

Closed
cocomoff opened this issue Feb 6, 2021 · 2 comments

Comments

@cocomoff
Copy link

cocomoff commented Feb 6, 2021

I tried to run Example 3.1 based on codes in Sec. 2 and Sec.3 as follows:

X = Variable(:x, 2); Y = Variable(:y, 2); Z = Variable(:z, 2)

# Table of X, Y (above) and Y, Z (below) at p.45
φ1 = Factor([X, Y], FactorTable((x=0, y=0) => 0.3, (x=0, y=1) => 0.4, (x=1, y=0) => 0.2, (x=1, y=1) => 0.1))
φ2 = Factor([Y, Z], FactorTable((y=0, z=0) => 0.2, (y=0, z=1) => 0.0, (y=1, z=0) => 0.3, (y=1, z=1) => 0.5))

and I got the below result after applying Base.:* function:

using PrettyPrint
φ12 = φ1 * φ2;
pprint(φ12.table)

# output is
http://localhost:8888/lab?{
  {:y : 0, :z : 1, :x : 0} : 0.0,
  {:y : 0, :z : 2, :x : 0} : 0.0,
  {:y : 1, :z : 1, :x : 1} : 0.05,
  {:y : 1, :z : 2, :x : 0} : 0.0,
  {:y : 1, :z : 2, :x : 1} : 0.0,
  {:y : 0, :z : 1, :x : 1} : 0.0,
  {:y : 0, :z : 2, :x : 1} : 0.0,
  {:y : 1, :z : 1, :x : 0} : 0.2,
}

This result did not equal to what I expected; The result table at p.45 of X, Y, Z can be obtained by replacing 0 -> 1, 1-> 2, and 2->3 in our definition. After checking the code of assignment in Algorithm 2.1, I'm thinking that this result is from 1:v.m of

function assignments(vars::AbstractVector{Variable})
    names = [var.name for var in vars]
    return vec([Assignment(n=>v for (n,v) in zip(names, values)) for values in product((1:v.m for v in vars)...)])
end

So, some parts of examples are a bit confusing for me. I know that the value in Julia is often assumed as 1-origin, but for probabilities we often uses {0,1} and I'm sorry I have no answer about this point. However, some remarks or comments are useful to learn deeply from the textbook with Julia.

@mykelk
Copy link
Contributor

mykelk commented Feb 6, 2021

Thanks for filing this issue! To make the algorithms simpler, we assume all variables can take on values 1:n. If you want something else, then you'd have to wrap the code to do the appropriate mapping. See notes in Example 2.5.

@cocomoff
Copy link
Author

cocomoff commented Feb 7, 2021

Thanks @mykelk for your great work! I have overlooked some notes in Sec.2, but now I can confirm that the code works (maybe comments in Algorithm 2.1 rather than Example 2.5?).

@cocomoff cocomoff closed this as completed Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants