-
Notifications
You must be signed in to change notification settings - Fork 779
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
Hybrid Bayes Net sampling #1347
Changes from 3 commits
4fc02a6
cdf1c4e
e997828
789b5d2
c245264
2483d7c
fe394cc
417a7ce
ff8a586
0be2a67
b023523
f6a2e7c
77b4028
da86e06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,9 @@ | |
#include <gtsam/hybrid/HybridBayesNet.h> | ||
#include <gtsam/hybrid/HybridValues.h> | ||
|
||
// In Wrappers we have no access to this so have a default ready | ||
static std::mt19937_64 kRandomNumberGenerator(42); | ||
|
||
namespace gtsam { | ||
|
||
/* ************************************************************************* */ | ||
|
@@ -232,6 +235,43 @@ VectorValues HybridBayesNet::optimize(const DiscreteValues &assignment) const { | |
return gbn.optimize(); | ||
} | ||
|
||
/* ************************************************************************* */ | ||
HybridValues HybridBayesNet::sample(VectorValues given, std::mt19937_64 *rng, | ||
SharedDiagonal model) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given should be const&, and model should not be there I think (per my email to you). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. I followed the signature in the other sample methods, so maybe update there as well? Also it can't be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, now I remember! It is passed by value deliberately, to allow for updating it in-place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add that in comment so we don’t forget again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, not really :-) the argument was passed by value which makes a copy, functionally equivalent to what you changed it to. Modulo possible compiler optimizations that might do something different - no idea which one is best. |
||
DiscreteBayesNet dbn; | ||
for (size_t idx = 0; idx < size(); idx++) { | ||
if (factors_.at(idx)->isDiscrete()) { | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// If factor at `idx` is discrete-only, we add to the discrete bayes net. | ||
dbn.push_back(this->atDiscrete(idx)); | ||
} | ||
} | ||
// Sample a discrete assignment. | ||
DiscreteValues assignment = dbn.sample(); | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Select the continuous bayes net corresponding to the assignment. | ||
GaussianBayesNet gbn = this->choose(assignment); | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Sample from the gaussian bayes net. | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
VectorValues sample = gbn.sample(given, rng, model); | ||
return HybridValues(assignment, sample); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could do {assignment, sample} |
||
} | ||
|
||
/* ************************************************************************* */ | ||
HybridValues HybridBayesNet::sample(std::mt19937_64 *rng, | ||
SharedDiagonal model) const { | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
VectorValues given; | ||
return sample(given, rng, model); | ||
} | ||
|
||
/* ************************************************************************* */ | ||
HybridValues HybridBayesNet::sample(VectorValues given, | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SharedDiagonal model) const { | ||
return sample(given, &kRandomNumberGenerator, model); | ||
} | ||
|
||
/* ************************************************************************* */ | ||
HybridValues HybridBayesNet::sample(SharedDiagonal model) const { | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sample(&kRandomNumberGenerator, model); | ||
} | ||
|
||
/* ************************************************************************* */ | ||
double HybridBayesNet::error(const VectorValues &continuousValues, | ||
const DiscreteValues &discreteValues) const { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given should be HybridValues: we could condition on discrete values as well…