Constructing a truth table (CPT) efficiently
I am working on an assignment on Probabilistic Reasoning for an AI intro course. I am having a bit of (programming) trouble with constructing the conditional probability tables (cpt) in a Bayesian Network.
I want to construct the cpt for a BayesNode
object which has its set of parents
. The cpt is actually a truth table, assigning true/false values to every one of the parents, and for each entry in that table, a probability should be computed.
This is what I have so far:
abstract class BayesNode<T> {
ArrayList<BayesNode> parents;
Map<Map<BayesNode,Boolean>, Probability> cpt = new HashMap<>();
}
void constructCpt() {
constructEntries(new HashMap<>(), new LinkedList<>(this.parents));
}
private void constructEntries
(Map<BayesNode, Boolean> tableEntry, Queue<BayesNode> left){
if (left.size() == 0){
cpt.put(new HashMap<>(tableEntry), computeConditionalProbability(tableEntry));
}
else{
BayesNode nextParent = left.poll();
tableEntry.put(nextParent,true);
constructEntries(tableEntry, new LinkedList<>(left));
tableEntry.put(nextParent, false);
constructEntries(tableEntry, new LinkedList<>(left));
}
}
cpt
is the table itself, with its key being another HashMap
which represents the entry in the table, and its value being a probability for that entry.
The inner map has the BayesNode
as keys, and booleans as values.
While it seems to work so far, I have a feeling I got it wrong somehow, and perhaps there is a more elegant way of iterating the parents of the node in order to construct the table. Especially, I worry that the repeated creation of LinkedList
is just overflowing the heap unnecessarily.
Thanks in advance!
java artificial-intelligence bayesian-networks
add a comment |
I am working on an assignment on Probabilistic Reasoning for an AI intro course. I am having a bit of (programming) trouble with constructing the conditional probability tables (cpt) in a Bayesian Network.
I want to construct the cpt for a BayesNode
object which has its set of parents
. The cpt is actually a truth table, assigning true/false values to every one of the parents, and for each entry in that table, a probability should be computed.
This is what I have so far:
abstract class BayesNode<T> {
ArrayList<BayesNode> parents;
Map<Map<BayesNode,Boolean>, Probability> cpt = new HashMap<>();
}
void constructCpt() {
constructEntries(new HashMap<>(), new LinkedList<>(this.parents));
}
private void constructEntries
(Map<BayesNode, Boolean> tableEntry, Queue<BayesNode> left){
if (left.size() == 0){
cpt.put(new HashMap<>(tableEntry), computeConditionalProbability(tableEntry));
}
else{
BayesNode nextParent = left.poll();
tableEntry.put(nextParent,true);
constructEntries(tableEntry, new LinkedList<>(left));
tableEntry.put(nextParent, false);
constructEntries(tableEntry, new LinkedList<>(left));
}
}
cpt
is the table itself, with its key being another HashMap
which represents the entry in the table, and its value being a probability for that entry.
The inner map has the BayesNode
as keys, and booleans as values.
While it seems to work so far, I have a feeling I got it wrong somehow, and perhaps there is a more elegant way of iterating the parents of the node in order to construct the table. Especially, I worry that the repeated creation of LinkedList
is just overflowing the heap unnecessarily.
Thanks in advance!
java artificial-intelligence bayesian-networks
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Dec 27 '18 at 23:24
"I have a feeling I got it wrong somehow" suggests that you haven't built and executed a test plan. Most of all you haven't demonstrated the operation and raised specific worries. This is to say, you do not yet have a Stack Overflow type of question. No input, no output, no tracing or debugging results ... SO is not for nebulous design help or code reviews.
– Prune
Dec 27 '18 at 23:26
Please check "Which site?" for general issues and "Code Review or not?"
– Prune
Dec 27 '18 at 23:26
add a comment |
I am working on an assignment on Probabilistic Reasoning for an AI intro course. I am having a bit of (programming) trouble with constructing the conditional probability tables (cpt) in a Bayesian Network.
I want to construct the cpt for a BayesNode
object which has its set of parents
. The cpt is actually a truth table, assigning true/false values to every one of the parents, and for each entry in that table, a probability should be computed.
This is what I have so far:
abstract class BayesNode<T> {
ArrayList<BayesNode> parents;
Map<Map<BayesNode,Boolean>, Probability> cpt = new HashMap<>();
}
void constructCpt() {
constructEntries(new HashMap<>(), new LinkedList<>(this.parents));
}
private void constructEntries
(Map<BayesNode, Boolean> tableEntry, Queue<BayesNode> left){
if (left.size() == 0){
cpt.put(new HashMap<>(tableEntry), computeConditionalProbability(tableEntry));
}
else{
BayesNode nextParent = left.poll();
tableEntry.put(nextParent,true);
constructEntries(tableEntry, new LinkedList<>(left));
tableEntry.put(nextParent, false);
constructEntries(tableEntry, new LinkedList<>(left));
}
}
cpt
is the table itself, with its key being another HashMap
which represents the entry in the table, and its value being a probability for that entry.
The inner map has the BayesNode
as keys, and booleans as values.
While it seems to work so far, I have a feeling I got it wrong somehow, and perhaps there is a more elegant way of iterating the parents of the node in order to construct the table. Especially, I worry that the repeated creation of LinkedList
is just overflowing the heap unnecessarily.
Thanks in advance!
java artificial-intelligence bayesian-networks
I am working on an assignment on Probabilistic Reasoning for an AI intro course. I am having a bit of (programming) trouble with constructing the conditional probability tables (cpt) in a Bayesian Network.
I want to construct the cpt for a BayesNode
object which has its set of parents
. The cpt is actually a truth table, assigning true/false values to every one of the parents, and for each entry in that table, a probability should be computed.
This is what I have so far:
abstract class BayesNode<T> {
ArrayList<BayesNode> parents;
Map<Map<BayesNode,Boolean>, Probability> cpt = new HashMap<>();
}
void constructCpt() {
constructEntries(new HashMap<>(), new LinkedList<>(this.parents));
}
private void constructEntries
(Map<BayesNode, Boolean> tableEntry, Queue<BayesNode> left){
if (left.size() == 0){
cpt.put(new HashMap<>(tableEntry), computeConditionalProbability(tableEntry));
}
else{
BayesNode nextParent = left.poll();
tableEntry.put(nextParent,true);
constructEntries(tableEntry, new LinkedList<>(left));
tableEntry.put(nextParent, false);
constructEntries(tableEntry, new LinkedList<>(left));
}
}
cpt
is the table itself, with its key being another HashMap
which represents the entry in the table, and its value being a probability for that entry.
The inner map has the BayesNode
as keys, and booleans as values.
While it seems to work so far, I have a feeling I got it wrong somehow, and perhaps there is a more elegant way of iterating the parents of the node in order to construct the table. Especially, I worry that the repeated creation of LinkedList
is just overflowing the heap unnecessarily.
Thanks in advance!
java artificial-intelligence bayesian-networks
java artificial-intelligence bayesian-networks
edited Dec 27 '18 at 23:04
asked Dec 27 '18 at 21:53
tfreifeld
52
52
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Dec 27 '18 at 23:24
"I have a feeling I got it wrong somehow" suggests that you haven't built and executed a test plan. Most of all you haven't demonstrated the operation and raised specific worries. This is to say, you do not yet have a Stack Overflow type of question. No input, no output, no tracing or debugging results ... SO is not for nebulous design help or code reviews.
– Prune
Dec 27 '18 at 23:26
Please check "Which site?" for general issues and "Code Review or not?"
– Prune
Dec 27 '18 at 23:26
add a comment |
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Dec 27 '18 at 23:24
"I have a feeling I got it wrong somehow" suggests that you haven't built and executed a test plan. Most of all you haven't demonstrated the operation and raised specific worries. This is to say, you do not yet have a Stack Overflow type of question. No input, no output, no tracing or debugging results ... SO is not for nebulous design help or code reviews.
– Prune
Dec 27 '18 at 23:26
Please check "Which site?" for general issues and "Code Review or not?"
– Prune
Dec 27 '18 at 23:26
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Dec 27 '18 at 23:24
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Dec 27 '18 at 23:24
"I have a feeling I got it wrong somehow" suggests that you haven't built and executed a test plan. Most of all you haven't demonstrated the operation and raised specific worries. This is to say, you do not yet have a Stack Overflow type of question. No input, no output, no tracing or debugging results ... SO is not for nebulous design help or code reviews.
– Prune
Dec 27 '18 at 23:26
"I have a feeling I got it wrong somehow" suggests that you haven't built and executed a test plan. Most of all you haven't demonstrated the operation and raised specific worries. This is to say, you do not yet have a Stack Overflow type of question. No input, no output, no tracing or debugging results ... SO is not for nebulous design help or code reviews.
– Prune
Dec 27 '18 at 23:26
Please check "Which site?" for general issues and "Code Review or not?"
– Prune
Dec 27 '18 at 23:26
Please check "Which site?" for general issues and "Code Review or not?"
– Prune
Dec 27 '18 at 23:26
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951235%2fconstructing-a-truth-table-cpt-efficiently%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951235%2fconstructing-a-truth-table-cpt-efficiently%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Dec 27 '18 at 23:24
"I have a feeling I got it wrong somehow" suggests that you haven't built and executed a test plan. Most of all you haven't demonstrated the operation and raised specific worries. This is to say, you do not yet have a Stack Overflow type of question. No input, no output, no tracing or debugging results ... SO is not for nebulous design help or code reviews.
– Prune
Dec 27 '18 at 23:26
Please check "Which site?" for general issues and "Code Review or not?"
– Prune
Dec 27 '18 at 23:26