forked from probsys/hierarchical-irm
-
Notifications
You must be signed in to change notification settings - Fork 1
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 base class for distributions with non-conjugate priors and an example use #67
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a4314e0
Add NonconjugateDistribution and Skellam classes
ThomasColthurst 5ca7684
Add skellam_test; fix build errors
ThomasColthurst c46431c
Fix test failures
ThomasColthurst fc91153
Change nonconjugate class to keep a running logp score
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,42 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <random> | ||
#include "distributions/base.hh" | ||
|
||
template <typename T> | ||
class NonconjugateDistribution : public Distribution<T> { | ||
public: | ||
// Abstract base class for Distributions that don't have conjugate priors. | ||
|
||
// The log probability of x given the current latent values. | ||
virtual double logp(const T& x) const = 0; | ||
|
||
// Sample a value from the distribution given the current latent values. | ||
virtual T sample(std::mt19937* prng) = 0; | ||
|
||
// Transition hyperparameters given the current latent values. | ||
virtual void transition_hyperparameters(std::mt19937* prng) = 0; | ||
|
||
// Set the current latent values to a sample from the parameter prior. | ||
virtual void init_theta(std::mt19937* prng) = 0; | ||
|
||
// Transition the current latent values. | ||
virtual void transition_theta(std::mt19937* prng) = 0; | ||
|
||
double cumulative_logp = 0.0; | ||
|
||
void incorporate(const T& x) { | ||
(this->N)++; | ||
cumulative_logp += logp(x); | ||
}; | ||
|
||
void unincorporate(const T& x) { | ||
--(this->N); | ||
cumulative_logp -= logp(x); | ||
}; | ||
|
||
double logp_score() const { | ||
return cumulative_logp; | ||
} | ||
}; |
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,73 @@ | ||
#pragma once | ||
|
||
#include <cmath> | ||
|
||
#include "distributions/nonconjugate.hh" | ||
#include "util_math.hh" | ||
|
||
#define MEAN_GRID { -10.0, 0.0, 10.0 } | ||
#define STDDEV_GRID { 0.1, 1.0, 10.0 } | ||
|
||
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); | ||
} | ||
|
||
class Skellam : public NonconjugateDistribution<int> { | ||
public: | ||
// Skellam distribution with log Normal hyperprior of latent rates. | ||
double mean1, mean2, stddev1, stddev2; // Hyperparameters | ||
// Skellam distribution with log-normal priors on mu1 and mu2. | ||
double mu1, mu2; // Latent values. | ||
|
||
Skellam(): mean1(0.0), mean2(0.0), stddev1(1.0), stddev2(1.0), | ||
mu1(1.0), mu2(1.0) {} | ||
|
||
double 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 sample(std::mt19937* prng) { | ||
std::poisson_distribution<int> d1(mu1); | ||
std::poisson_distribution<int> d2(mu2); | ||
return d1(*prng) - d2(*prng); | ||
} | ||
|
||
void 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 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)); | ||
} | ||
|
||
void transition_theta(std::mt19937* prng) { | ||
// TODO(thomaswc): This | ||
} | ||
|
||
}; |
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,33 @@ | ||
// Apache License, Version 2.0, refer to LICENSE.txt | ||
|
||
#define BOOST_TEST_MODULE test Skellam | ||
|
||
#include "distributions/skellam.hh" | ||
|
||
#include <boost/test/included/unit_test.hpp> | ||
|
||
namespace tt = boost::test_tools; | ||
|
||
BOOST_AUTO_TEST_CASE(simple) { | ||
Skellam sd; | ||
std::mt19937 prng; | ||
|
||
sd.init_theta(&prng); | ||
|
||
BOOST_TEST(sd.logp_score() == 0.0, tt::tolerance(1e-6)); | ||
BOOST_TEST(sd.logp(6) == -8.2461659399497425, tt::tolerance(1e-6)); | ||
|
||
sd.incorporate(5); | ||
sd.incorporate(2); | ||
BOOST_TEST(sd.N == 2); | ||
|
||
sd.unincorporate(5); | ||
sd.incorporate(7); | ||
BOOST_TEST(sd.N == 2); | ||
|
||
BOOST_TEST(sd.logp_score() == -12.676907210873877, tt::tolerance(1e-6)); | ||
BOOST_TEST(sd.logp(6) == -8.2461659399497425, tt::tolerance(1e-6)); | ||
|
||
int s = sd.sample(&prng); | ||
BOOST_TEST(s < 100.0); | ||
} |
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.
In general we'll want to override incorporate/unincorporate to maintain sufficient statistics and avoid the loop at each call to logp_score, right? Is that not possible for Skellam, or is this more efficient?
For distributions with mostly unique samples, keeping a running tally of logp_score at each call to incorporate/unincorporate could be better as it would avoid looping over all the data in logp_score. Ultimately I think we might want to leave incorporate/unincorporate/logp_score undefined in the base class and have each distribution explicitly do what's best, but I think this is fine for now.
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.
I've been told in places like https://www.johndcook.com/CompendiumOfConjugatePriors.pdf (bottom of page 2) that having fixed dimensional sufficient statistics implies the existence of a conjugate prior family. That said, I've never actually read the precise statement or proof of that claim.
I'm fine with a running tally, though. I'll upgrade the code to do that now.