Skip to content

Commit

Permalink
Merge pull request #30 from probcomp/052924-clang-format
Browse files Browse the repository at this point in the history
Update code to be clang-formatted
  • Loading branch information
emilyfertig authored May 30, 2024
2 parents 0f24c85 + 4100330 commit 5a94ae6
Show file tree
Hide file tree
Showing 24 changed files with 3,495 additions and 4,138 deletions.
2,751 changes: 1,039 additions & 1,712 deletions cxx/cxxopts.hpp

Large diffs are not rendered by default.

94 changes: 46 additions & 48 deletions cxx/distributions/adapter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,53 @@

#pragma once
#include <iostream>
#include <string>
#include <sstream>
#include <string>

#include "distributions/base.hh"

template <typename SampleType = double> class DistributionAdapter : Distribution<std::string> {
public:
// The underlying distribution that is being adapted. We own the
// underlying Distribution.
Distribution<SampleType> *d;

DistributionAdapter(Distribution<SampleType> *dd): d(dd) {};

SampleType from_string(const std::string& x) const {
SampleType s;
std::istringstream(x) >> s;
return s;
}

std::string to_string(const SampleType& s) const {
std::ostringstream os;
os << s;
return os.str();
}

void incorporate(const std::string& x) {
SampleType s = from_string(x);
d->incorporate(s);
}

void unincorporate(const std::string& x) {
SampleType s = from_string(x);
d->unincorporate(s);
}

double logp(const std::string& x) const {
SampleType s = from_string(x);
return d->logp(s);
}

double logp_score() const {
return d->logp_score();
}

std::string sample() {
SampleType s = d->sample();
return to_string(s);
}

~DistributionAdapter() {
delete d;
}
template <typename SampleType = double>
class DistributionAdapter : Distribution<std::string> {
public:
// The underlying distribution that is being adapted. We own the
// underlying Distribution.
Distribution<SampleType>* d;

DistributionAdapter(Distribution<SampleType>* dd) : d(dd) {};

SampleType from_string(const std::string& x) const {
SampleType s;
std::istringstream(x) >> s;
return s;
}

std::string to_string(const SampleType& s) const {
std::ostringstream os;
os << s;
return os.str();
}

void incorporate(const std::string& x) {
SampleType s = from_string(x);
d->incorporate(s);
}

void unincorporate(const std::string& x) {
SampleType s = from_string(x);
d->unincorporate(s);
}

double logp(const std::string& x) const {
SampleType s = from_string(x);
return d->logp(s);
}

double logp_score() const { return d->logp_score(); }

std::string sample() {
SampleType s = d->sample();
return to_string(s);
}

~DistributionAdapter() { delete d; }
};
7 changes: 4 additions & 3 deletions cxx/distributions/adapter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

#define BOOST_TEST_MODULE test Normal

#include <boost/test/included/unit_test.hpp>
#include "distributions/adapter.hh"

#include <boost/test/included/unit_test.hpp>

#include "distributions/normal.hh"
namespace tt = boost::test_tools;

BOOST_AUTO_TEST_CASE(adapt_normal)
{
BOOST_AUTO_TEST_CASE(adapt_normal) {
std::mt19937 prng;
DistributionAdapter<double> ad(new Normal(&prng));

Expand Down
64 changes: 32 additions & 32 deletions cxx/distributions/base.hh
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#pragma once

template <typename SampleType = double> class Distribution {
// Abstract base class for probability distributions in HIRM.
public:
// N is the number of incorporated observations.
int N = 0;

// Accumulate x.
virtual void incorporate(const SampleType& x) = 0;

// Undo the accumulation of x. Should only be called with x's that
// have been previously passed to incorporate().
virtual void unincorporate(const SampleType& x) = 0;

// The log probability of x according to the posterior predictive
// distribution: log P(x | incorporated_data), where P(x | data) =
// \integral_{theta} P(x | theta ) P(theta | data) dtheta
// and theta are the parameters of the distribution.
virtual double logp(const SampleType& x) const = 0;

// The log probability of the data we have accumulated so far according
// to the prior: log P(data | alpha) where alpha is the vector of
// hyperparameters of the prior and P(data)
// = \integral_{theta} P(data | theta) P(theta | alpha) dtheta.
virtual double logp_score() const = 0;

// A sample from the predictive distribution.
// TODO(thomaswc): Consider refactoring so that this takes a
// PRNG parameter.
virtual SampleType sample() = 0;

virtual ~Distribution() = default;
template <typename SampleType = double>
class Distribution {
// Abstract base class for probability distributions in HIRM.
public:
// N is the number of incorporated observations.
int N = 0;

// Accumulate x.
virtual void incorporate(const SampleType& x) = 0;

// Undo the accumulation of x. Should only be called with x's that
// have been previously passed to incorporate().
virtual void unincorporate(const SampleType& x) = 0;

// The log probability of x according to the posterior predictive
// distribution: log P(x | incorporated_data), where P(x | data) =
// \integral_{theta} P(x | theta ) P(theta | data) dtheta
// and theta are the parameters of the distribution.
virtual double logp(const SampleType& x) const = 0;

// The log probability of the data we have accumulated so far according
// to the prior: log P(data | alpha) where alpha is the vector of
// hyperparameters of the prior and P(data)
// = \integral_{theta} P(data | theta) P(theta | alpha) dtheta.
virtual double logp_score() const = 0;

// A sample from the predictive distribution.
// TODO(thomaswc): Consider refactoring so that this takes a
// PRNG parameter.
virtual SampleType sample() = 0;

virtual ~Distribution() = default;
};

79 changes: 39 additions & 40 deletions cxx/distributions/beta_bernoulli.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@

#pragma once
#include <cassert>
#include "util_math.hh"

#include "distributions/base.hh"
#include "util_math.hh"

class BetaBernoulli : public Distribution<double> {
public:
double alpha = 1; // hyperparameter
double beta = 1; // hyperparameter
int s = 0; // sum of observed values
std::mt19937 *prng;
public:
double alpha = 1; // hyperparameter
double beta = 1; // hyperparameter
int s = 0; // sum of observed values
std::mt19937* prng;

// BetaBernoulli does not take ownership of prng.
BetaBernoulli(std::mt19937 *prng) {
this->prng = prng;
}
void incorporate(const double& x){
assert(x == 0 || x == 1);
++N;
s += x;
}
void unincorporate(const double& x) {
assert(x == 0 || x ==1);
--N;
s -= x;
assert(0 <= s);
assert(0 <= N);
}
double logp(const double& x) const {
assert(x == 0 || x == 1);
double log_denom = log(N + alpha + beta);
double log_numer = x ? log(s + alpha) : log(N - s + beta);
return log_numer - log_denom;
}
double logp_score() const {
double v1 = lbeta(s + alpha, N - s + beta);
double v2 = lbeta(alpha, beta);
return v1 - v2;
}
double sample() {
double p = exp(logp(1));
std::vector<int> items {0, 1};
std::vector<double> weights {1-p, p};
int idx = choice(weights, prng);
return items[idx];
}
// BetaBernoulli does not take ownership of prng.
BetaBernoulli(std::mt19937* prng) { this->prng = prng; }
void incorporate(const double& x) {
assert(x == 0 || x == 1);
++N;
s += x;
}
void unincorporate(const double& x) {
assert(x == 0 || x == 1);
--N;
s -= x;
assert(0 <= s);
assert(0 <= N);
}
double logp(const double& x) const {
assert(x == 0 || x == 1);
double log_denom = log(N + alpha + beta);
double log_numer = x ? log(s + alpha) : log(N - s + beta);
return log_numer - log_denom;
}
double logp_score() const {
double v1 = lbeta(s + alpha, N - s + beta);
double v2 = lbeta(alpha, beta);
return v1 - v2;
}
double sample() {
double p = exp(logp(1));
std::vector<int> items{0, 1};
std::vector<double> weights{1 - p, p};
int idx = choice(weights, prng);
return items[idx];
}
};
6 changes: 3 additions & 3 deletions cxx/distributions/beta_bernoulli_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#define BOOST_TEST_MODULE test BetaBernoulli

#include <boost/test/included/unit_test.hpp>
#include "distributions/beta_bernoulli.hh"

#include <boost/test/included/unit_test.hpp>
namespace tt = boost::test_tools;

BOOST_AUTO_TEST_CASE(test_simple)
{
BOOST_AUTO_TEST_CASE(test_simple) {
std::mt19937 prng;
BetaBernoulli bb(&prng);

Expand Down
Loading

0 comments on commit 5a94ae6

Please sign in to comment.