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

Revert "James.yang/nuts revamp" #14

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "lib/benchmark"]
path = lib/benchmark
url = https://github.com/google/benchmark.git
[submodule "lib/FastAD"]
path = lib/FastAD
url = https://github.com/JamesYang007/FastAD.git
44 changes: 12 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ jobs:
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-7', 'cmake', 'ninja-build', 'valgrind',
'libopenblas-dev', 'liblapack-dev',
'libarpack2-dev']
packages: ['g++-7', 'cmake', 'ninja-build', 'valgrind']
env:
- CXX_COMPILER="g++-7"
- CC_COMPILER="gcc-7"
- COMPILER="g++-7"
- CTEST_OPTIONS="-T memcheck"
- VALGRIND="ON"

Expand All @@ -32,9 +29,7 @@ jobs:
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-7', 'cmake', 'ninja-build',
'libopenblas-dev', 'liblapack-dev',
'libarpack2-dev']
packages: ['g++-7', 'cmake', 'ninja-build']
before_install:
- sudo -H pip install --upgrade requests[security]
- pip install --user cpp-coveralls
Expand All @@ -48,63 +43,48 @@ jobs:
--gcov 'gcov-7'
--gcov-options '\-lp'
env:
- CXX_COMPILER="g++-7"
- CC_COMPILER="gcc-7"
- COMPILER="g++-7"
- COVERALL="ON"
- CMAKE_OPTIONS="-DAUTOPPL_ENABLE_TEST_COVERAGE=ON"

# Linux/GCC>=5

- os: linux
compiler: gcc
env:
- CXX_COMPILER="g++-7"
- CC_COMPILER="gcc-7"
env: COMPILER=g++-7
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-7', 'ninja-build',
'libopenblas-dev', 'liblapack-dev',
'libarpack2-dev']
packages: ['g++-7', 'ninja-build']

- os: linux
compiler: gcc
env:
- CXX_COMPILER="g++-8"
- CC_COMPILER="gcc-8"
env: COMPILER=g++-8
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-8', 'ninja-build',
'libopenblas-dev', 'liblapack-dev',
'libarpack2-dev']
packages: ['g++-8', 'ninja-build']

- os: linux
compiler: gcc
env:
- CXX_COMPILER="g++-9"
- CC_COMPILER="gcc-9"
env: COMPILER=g++-9
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-9', 'ninja-build',
'libopenblas-dev', 'liblapack-dev',
'libarpack2-dev']
packages: ['g++-9', 'ninja-build']

# Build Script

script:

# set CXX to correct compiler
- if [[ "${CXX_COMPILER}" != "" ]]; then
export CXX=${CXX_COMPILER};
export CC=${CC_COMPILER};
- if [[ "${COMPILER}" != "" ]]; then
export CXX=${COMPILER};
fi

# show OS/compiler/cmake version
- uname -a
- ${CXX} --version
- ${CC} --version
- cmake --version

# setup, clean build, and test
Expand Down
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ target_include_directories(${PROJECT_NAME}
# Set C++17 standard for project target
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)

# Find Armadillo C++
find_package(Armadillo REQUIRED)

# Find FastAD
find_package(FastAD CONFIG REQUIRED)

# Configure tests
if (AUTOPPL_ENABLE_TEST)
include(CTest) # enable memcheck
Expand Down
37 changes: 20 additions & 17 deletions include/autoppl/algorithm/mh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
#include <variant>
#include <autoppl/util/traits.hpp>
#include <autoppl/variable.hpp>
#include <autoppl/algorithm/sampler_tools.hpp>

#define AUTOPPL_MH_UNKNOWN_VALUE_TYPE_ERROR \
"Unknown value type: must be convertible to util::disc_param_t " \
"such as uint64_t or util::cont_param_t such as double."

/*
* Assumptions:
* - every variable referenced in model is of type Variable<double>
*/

namespace ppl {
namespace alg {
namespace details {

/*
* Convert ValueType to either util::cont_param_t if floating point
Expand Down Expand Up @@ -108,7 +111,7 @@ inline void mh_posterior__(ModelType& model,
}

// move old value into params
using converted_value_t = value_to_param_t<value_t>;
using converted_value_t = details::value_to_param_t<value_t>;
params_it->next = static_cast<converted_value_t>(curr);
++params_it;
}
Expand All @@ -124,8 +127,8 @@ inline void mh_posterior__(ModelType& model,
using value_t = typename util::var_traits<var_t>::value_t;
if constexpr (util::param_is_base_of_v<var_t>) {
if (n_swaps) {
using converted_value_t = value_to_param_t<value_t>;
var.set_value(*std::get_if<converted_value_t>(&params_it->next));
using converted_value_t = details::value_to_param_t<value_t>;
var.set_value(std::get<converted_value_t>(params_it->next));
++params_it;
--n_swaps;
}
Expand All @@ -152,8 +155,8 @@ inline void mh_posterior__(ModelType& model,
using value_t = typename util::var_traits<var_t>::value_t;
if constexpr(util::param_is_base_of_v<var_t>) {
if (!accept) {
using converted_value_t = value_to_param_t<value_t>;
var.set_value(*std::get_if<converted_value_t>(&params_it->next));
using converted_value_t = details::value_to_param_t<value_t>;
var.set_value(std::get<converted_value_t>(params_it->next));
++params_it;
}
auto storage = var.get_storage();
Expand All @@ -167,7 +170,7 @@ inline void mh_posterior__(ModelType& model,
}
}

} // namespace alg
} // namespace details

/*
* Metropolis-Hastings algorithm to sample from posterior distribution.
Expand All @@ -190,13 +193,13 @@ inline void mh_posterior(ModelType& model,
).count()
)
{
using data_t = alg::MHData;
using data_t = details::MHData;

// set-up auxiliary tools
constexpr double initial_radius = 5.;
std::mt19937 gen(seed);
size_t n_params = 0;
double curr_log_pdf = 0.; // current log pdf
const double initial_radius = 5.; // arbitrarily chosen radius for initial sampling

// 1. initialize parameters with values in valid range
// - discrete valued params sampled uniformly within the distribution range
Expand Down Expand Up @@ -233,13 +236,13 @@ inline void mh_posterior(ModelType& model,
model.traverse(init_params);

std::vector<data_t> params(n_params); // vector of parameter-related data with candidate
alg::mh_posterior__(model,
params.begin(),
gen,
n_sample,
curr_log_pdf,
alpha,
stddev);
details::mh_posterior__(model,
params.begin(),
gen,
n_sample,
curr_log_pdf,
alpha,
stddev);
}

} // namespace ppl
Loading