forked from probsys/hierarchical-irm
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add transition_theta to outer inference code #71
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c1f67f5
Add Skellam to HIRM distributions, plus add transition_theta calls to…
ThomasColthurst cdd541a
Fix more build errors
ThomasColthurst 81c0874
Fix test
ThomasColthurst a8dbac5
Fix merge conflicts
ThomasColthurst 6c4cca6
Add skellam.cc to avoid double definitions of lognormal_logp.
ThomasColthurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "distributions/skellam.hh" | ||
|
||
#include <cassert> | ||
#include <cmath> | ||
#include "util_math.hh" | ||
|
||
double lognormal_logp(double x, double mean, double stddev) { | ||
double y = (std::log(x) - mean) / stddev; | ||
return - y*y / 2.0 | ||
- std::log(x * stddev) - 0.5 * std::log(2.0 * std::numbers::pi); | ||
} | ||
|
||
double Skellam::logp(const int&x) const { | ||
return -mu1 - mu2 + (x / 2.0) * std::log(mu1 / mu2) | ||
// TODO(thomaswc): Replace this with something more numerically stable. | ||
+ std::log(std::cyl_bessel_i(x, 2.0 * std::sqrt(mu1 * mu2))); | ||
} | ||
|
||
int Skellam::sample(std::mt19937* prng) { | ||
std::poisson_distribution<int> d1(mu1); | ||
std::poisson_distribution<int> d2(mu2); | ||
return d1(*prng) - d2(*prng); | ||
} | ||
|
||
void Skellam::transition_hyperparameters(std::mt19937* prng) { | ||
std::vector<double> logps; | ||
std::vector<std::tuple<double, double, double, double>> hypers; | ||
for (double tmean1 : MEAN_GRID) { | ||
for (double tstddev1 : STDDEV_GRID) { | ||
for (double tmean2 : MEAN_GRID) { | ||
for (double tstddev2 : STDDEV_GRID) { | ||
double lp = lognormal_logp(mu1, tmean1, tstddev1) | ||
+ lognormal_logp(mu2, tmean2, tstddev2); | ||
logps.push_back(lp); | ||
hypers.push_back( | ||
std::make_tuple(tmean1, tstddev1, tmean2, tstddev2)); | ||
} | ||
} | ||
} | ||
} | ||
int i = sample_from_logps(logps, prng); | ||
mean1 = std::get<0>(hypers[i]); | ||
stddev1 = std::get<1>(hypers[i]); | ||
mean2 = std::get<2>(hypers[i]); | ||
stddev2 = std::get<3>(hypers[i]); | ||
} | ||
|
||
void Skellam::init_theta(std::mt19937* prng) { | ||
std::normal_distribution<double> d1(mean1, stddev1); | ||
std::normal_distribution<double> d2(mean2, stddev2); | ||
mu1 = std::exp(d1(*prng)); | ||
mu2 = std::exp(d2(*prng)); | ||
} | ||
|
||
std::vector<double> Skellam::store_latents() const { | ||
std::vector<double> v; | ||
v.push_back(mu1); | ||
v.push_back(mu2); | ||
return v; | ||
} | ||
|
||
void Skellam::set_latents(const std::vector<double>& v) { | ||
assert(v.size() == 2); | ||
mu1 = v[0]; | ||
mu2 = v[1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Out of curiosity, is there a need for Skellam to be initialized randomly vs. statically like the other distributions? Or do we see other distributions initializing their parameters from their hyperprior? Mainly asking since it seems a little odd that Skellam's path is different from other distributions.
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.
Yeah, all the NonconjugateDistributions will need random initializations. The reason is that the NonconjugateDistributions can't efficiently marginalize over the latent parameters, so instead they store current values of their latents, which evolve whenever transition_theta is called. And I believe that that overall learning procedure works better when the latents are randomly initialized, but I guess I could be wrong about that. I personally always use random initialization when doing Metropolis-Hastings, but I guess some people always use the origin or the mean of the sampling distribution or something like that.
Anyway, we could definitely raise this issue on the slack channel if you like, but the above is my current understanding. Oh, and I guess another thing is that the GenDB doc says that for "distributions that explicitly represent their latents", "the code to initialize the model state will need to include code for sampling initial parameters from the parameter prior". So this is that.