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

Add base class for distributions with non-conjugate priors and an example use #67

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cxx/distributions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ cc_library(
],
)

cc_library(
name = "nonconjugate",
hdrs = ["nonconjugate.hh"],
visibility = ["//:__subpackages__"],
deps = [
":base",
],
)

cc_library(
name = "normal",
srcs = ["normal.cc"],
Expand All @@ -73,6 +82,16 @@ cc_library(
"//:util_math",
],
)

cc_library(
name = "skellam",
hdrs = ["skellam.hh"],
deps = [
":nonconjugate",
"//:util_math",
],
)

cc_library(
name = "zero_mean_normal",
srcs = ["zero_mean_normal.cc"],
Expand Down Expand Up @@ -137,6 +156,17 @@ cc_test(
],
)

cc_test(
name = "skellam_test",
srcs = ["skellam_test.cc"],
deps = [
":skellam",
"@boost//:algorithm",
"@boost//:math",
"@boost//:test",
],
)

cc_test(
name = "zero_mean_normal_test",
srcs = ["zero_mean_normal_test.cc"],
Expand Down
8 changes: 8 additions & 0 deletions cxx/distributions/base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@ class Distribution {
// e^logp_score() under those hyperparameters.
virtual void transition_hyperparameters(std::mt19937* prng) = 0;

// Set the current latent values to a sample from the parameter prior.
// Only children of NonconjugateDistribution need define this.
virtual void init_theta(std::mt19937* prng) {};

// Transition the current latent values. Only children of
// NonconjugateDistribution need define this.
virtual void transition_theta(std::mt19937* prng) {};

virtual ~Distribution() = default;
};
42 changes: 42 additions & 0 deletions cxx/distributions/nonconjugate.hh
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) {
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

(this->N)++;
cumulative_logp += logp(x);
};

void unincorporate(const T& x) {
--(this->N);
cumulative_logp -= logp(x);
};

double logp_score() const {
return cumulative_logp;
}
};
73 changes: 73 additions & 0 deletions cxx/distributions/skellam.hh
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
}

};
33 changes: 33 additions & 0 deletions cxx/distributions/skellam_test.cc
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);
}