-
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.
- Loading branch information
Showing
10 changed files
with
203 additions
and
86 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,58 @@ | ||
#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(static_cast<value_t>(var_) > 0); | ||
}; | ||
|
||
template <class GeneratorType> | ||
value_t sample(GeneratorType& gen) const { | ||
value_t mean, var; | ||
mean = static_cast<value_t>(mean_); | ||
var = static_cast<value_t>(var_); | ||
|
||
std::normal_distribution<value_t> dist(mean, var); | ||
return dist(gen); | ||
} | ||
|
||
dist_value_t pdf(value_t x) const { | ||
value_t mean, var; | ||
mean = static_cast<value_t>(mean_); | ||
var = static_cast<value_t>(var_); | ||
|
||
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 { | ||
value_t mean, var; | ||
mean = static_cast<value_t>(mean_); | ||
var = static_cast<value_t>(var_); | ||
|
||
return (-0.5 * std::pow(x - mean, 2) / var) - 0.5 * (std::log(var) + std::log(2) + std::log(M_PI)); | ||
} | ||
|
||
private: | ||
mean_type mean_; | ||
var_type var_; | ||
}; | ||
|
||
} // namespace ppl |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <autoppl/distribution/normal.hpp> | ||
#include <autoppl/expression/variable.hpp> | ||
|
||
#include <cmath> | ||
#include <array> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace ppl { | ||
|
||
struct normal_dist_fixture : ::testing::Test { | ||
protected: | ||
Variable<double> mu {0.}; | ||
Variable<double> sigma {1.}; | ||
Normal<double, double> dist1 = Normal(0., 1.); | ||
Normal<Variable<double>, Variable<double> > dist2 = Normal(mu, sigma); | ||
}; | ||
|
||
TEST_F(normal_dist_fixture, simple_gaussian) { | ||
EXPECT_DOUBLE_EQ(dist1.pdf(0.0), 0.3989422804014327); | ||
EXPECT_DOUBLE_EQ(dist1.pdf(-0.5), 0.3520653267642995); | ||
EXPECT_DOUBLE_EQ(dist1.pdf(4), 0.00013383022576488537); | ||
|
||
EXPECT_DOUBLE_EQ(dist1.log_pdf(0.0), std::log(dist1.pdf(0.0))); | ||
EXPECT_DOUBLE_EQ(dist1.log_pdf(-0.5), std::log(dist1.pdf(-0.5))); | ||
EXPECT_DOUBLE_EQ(dist1.log_pdf(4), std::log(dist1.pdf(4))); | ||
} | ||
|
||
} // 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,41 @@ | ||
#include <autoppl/distribution/uniform.hpp> | ||
#include <autoppl/expression/variable.hpp> | ||
|
||
#include <cmath> | ||
#include <array> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace ppl { | ||
|
||
struct uniform_dist_fixture : ::testing::Test { | ||
protected: | ||
Variable<double> x {0.5}; | ||
Variable<double> y {0.1}; | ||
Uniform<double, double> dist1 = Uniform(0., 1.); | ||
Uniform<double, Variable<double> > dist2 = Uniform(0., x); | ||
Uniform<Variable<double>, Variable<double> > dist3 = Uniform(y, x); | ||
}; | ||
|
||
TEST_F(uniform_dist_fixture, simple_uniform) { | ||
EXPECT_DOUBLE_EQ(dist1.pdf(1.1), 0.0); | ||
|
||
EXPECT_DOUBLE_EQ(dist2.pdf(1.0), 0.0); | ||
EXPECT_DOUBLE_EQ(dist2.pdf(0.25), 2.0); | ||
|
||
EXPECT_DOUBLE_EQ(dist3.pdf(-0.1), 0.0); | ||
EXPECT_DOUBLE_EQ(dist3.pdf(0.25), 2.5); | ||
} | ||
|
||
TEST_F(uniform_dist_fixture, uniform_sampling) { | ||
std::random_device rd{}; | ||
std::mt19937 gen{rd()}; | ||
|
||
for (int i = 0; i < 100; i++) { | ||
double sample = dist1.sample(gen); | ||
EXPECT_GT(sample, 0.0); | ||
EXPECT_LT(sample, 1.0); | ||
} | ||
} | ||
|
||
} // ppl |
Oops, something went wrong.