-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from JamesYang007/rename_tag
Renamed rv_tag to Variable, added Normal and Bernoulli distributions and updated tests.
- Loading branch information
Showing
12 changed files
with
322 additions
and
105 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
#include <cassert> | ||
#include <random> | ||
#include <cmath> | ||
#include <numeric> | ||
|
||
namespace ppl { | ||
|
||
// TODO: change name to BernoulliDist and make class template. | ||
// bernoulli should be a function that creates this kind of object. | ||
|
||
template <typename p_type> | ||
struct Bernoulli | ||
{ | ||
using value_t = int; | ||
using dist_value_t = double; | ||
|
||
Bernoulli(p_type p) | ||
: p_{p} { assert((this -> p() >= 0) && (this -> p() <= 1)); } | ||
|
||
template <class GeneratorType> | ||
value_t sample(GeneratorType& gen) const | ||
{ | ||
std::bernoulli_distribution dist(p()); | ||
return dist(gen); | ||
} | ||
|
||
dist_value_t pdf(value_t x) const | ||
{ | ||
if (x == 1) return p(); | ||
else if (x == 0) return 1. - p(); | ||
else return 0.0; | ||
} | ||
|
||
dist_value_t log_pdf(value_t x) const | ||
{ | ||
if (x == 1) return std::log(p()); | ||
else if (x == 0) return std::log(1. - p()); | ||
else return std::numeric_limits<dist_value_t>::lowest(); | ||
} | ||
|
||
inline dist_value_t p() const { return static_cast<dist_value_t>(p_); } | ||
|
||
private: | ||
p_type p_; | ||
}; | ||
|
||
} // namespace ppl |
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,49 @@ | ||
#pragma once | ||
#include <math.h> | ||
|
||
#include <cassert> | ||
#include <cmath> | ||
#include <numeric> | ||
#include <random> | ||
|
||
namespace ppl { | ||
|
||
// TODO: change name to NormalDist and make class template. | ||
// normal should be a function that creates this kind of object. | ||
|
||
template <typename mean_type, typename var_type> | ||
struct Normal { | ||
using value_t = double; | ||
using dist_value_t = double; | ||
|
||
static_assert(std::is_convertible_v<mean_type, value_t>); | ||
static_assert(std::is_convertible_v<var_type, value_t>); | ||
|
||
Normal(mean_type mean, var_type var) | ||
: mean_{mean}, var_{var} { | ||
assert(this -> var() > 0); | ||
}; | ||
|
||
template <class GeneratorType> | ||
value_t sample(GeneratorType& gen) const { | ||
std::normal_distribution<value_t> dist(mean(), var()); | ||
return dist(gen); | ||
} | ||
|
||
dist_value_t pdf(value_t x) const { | ||
return std::exp(- 0.5 * std::pow(x - mean(), 2) / var()) / (std::sqrt(var() * 2 * M_PI)); | ||
} | ||
|
||
dist_value_t log_pdf(value_t x) const { | ||
return (-0.5 * std::pow(x - mean(), 2) / var()) - 0.5 * (std::log(var()) + std::log(2) + std::log(M_PI)); | ||
} | ||
|
||
inline value_t mean() const { return static_cast<value_t>(mean_);} | ||
inline value_t var() const { return static_cast<value_t>(var_);} | ||
|
||
private: | ||
mean_type mean_; | ||
var_type var_; | ||
}; | ||
|
||
} // namespace ppl |
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.