kthohr / mcmc Goto Github PK
View Code? Open in Web Editor NEWA C++ library of Markov Chain Monte Carlo (MCMC) methods
Home Page: https://mcmclib.readthedocs.io/en/latest/
License: Apache License 2.0
A C++ library of Markov Chain Monte Carlo (MCMC) methods
Home Page: https://mcmclib.readthedocs.io/en/latest/
License: Apache License 2.0
see https://brew.sh
I have successfully installed mcmc and to run the examples, I copy the file from tests/examples into the main mcmc folder and then compile and build with, for example, g++ hmc_normal.cpp -o hmc_normal -std=c++11 -Iinclude
Then I always get errors related to the call to the specific mcmc type header. In this case:
/tmp/ccanKULh.o: In function `main':
hmc_normal.cpp:(.text+0xc30): undefined reference to `mcmc::hmc(arma::Col<double> const&, arma::Mat<double>&, std::function<double (arma::Col<double> const&, arma::Col<double>*, void*)>, void*, mcmc::algo_settings_t&)'
/tmp/ccanKULh.o: In function `void arma::blas::gemv<double>(char const*, int const*, int const*, double const*, double const*, int const*, double const*, int const*, double const*, double*, int const*)':
hmc_normal.cpp:(.text._ZN4arma4blas4gemvIdEEvPKcPKiS5_PKT_S8_S5_S8_S5_S8_PS6_S5_[_ZN4arma4blas4gemvIdEEvPKcPKiS5_PKT_S8_S5_S8_S5_S8_PS6_S5_]+0x53): undefined reference to `dgemv_'
/tmp/ccanKULh.o: In function `void arma::blas::gemm<double>(char const*, char const*, int const*, int const*, int const*, double const*, double const*, int const*, double const*, int const*, double const*, double*, int const*)':
hmc_normal.cpp:(.text._ZN4arma4blas4gemmIdEEvPKcS3_PKiS5_S5_PKT_S8_S5_S8_S5_S8_PS6_S5_[_ZN4arma4blas4gemmIdEEvPKcS3_PKiS5_S5_PKT_S8_S5_S8_S5_S8_PS6_S5_]+0x5f): undefined reference to `dgemm_'
/tmp/ccanKULh.o: In function `void arma::blas::syrk<double>(char const*, char const*, int const*, int const*, double const*, double const*, int const*, double const*, double*, int const*)':
hmc_normal.cpp:(.text._ZN4arma4blas4syrkIdEEvPKcS3_PKiS5_PKT_S8_S5_S8_PS6_S5_[_ZN4arma4blas4syrkIdEEvPKcS3_PKiS5_PKT_S8_S5_S8_PS6_S5_]+0x52): undefined reference to `dsyrk_'
/tmp/ccanKULh.o: In function `double arma::blas::dot<double>(unsigned long long, double const*, double const*)':
hmc_normal.cpp:(.text._ZN4arma4blas3dotIdEET_yPKS2_S4_[_ZN4arma4blas3dotIdEET_yPKS2_S4_]+0x4c): undefined reference to `ddot_'
collect2: error: ld returned 1 exit status
There is a call to mcmc:hmc(***) in the main function of that file, but with a different signature. Similar issues arise with all other examples. All the errors also mention "arma" so I assume this might have to do with linking with armadillo library.
Please assist. Is this to do with linking the headers, or with the namespace resolution? Can anyone assist with this please?
Line 136 in de.cpp causes arma to throw a dimension mismatch exception (at least if n_vals is more than 1).
The bug is fixed if I change line 134 to
arma::rowvec prop_rand = arma::randu(1, n_vals)(2par_b) - par_b; // generate a vector of U[-b,b] RVs
I put the sample code into mcmc_sample.cpp and tried to compile it with the following command:
g++ -std=c++11 -Wall -fPIC -O3 -march=native -ffp-contract=fast -flto -DARMA_NO_DEBUG -I./include -lmcmc mcmc_sample.cpp
but kept getting the following error:
/tmp/ccpHtc3q.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup+0x5cc): undefined reference to `mcmc::rwmh(arma::Col<double> const&, arma::Mat<double>&, std::function<double (arma::Col<double> const&, void*)>, void*)'
collect2: error: ld returned 1 exit status
Following is the info about my ubuntu linux and g++.
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
What is the minimum version of g++ required by this project? Thanks!
After running make, I met an error that the version of Eigen is too low
In file included from ./include/BaseMatrixOps/include/BaseMatrixOps.hpp:26,
from ./include/misc/mcmc_options.hpp:193,
from ./include/mcmc.hpp:24,
from src/aees.cpp:25:
./include/BaseMatrixOps/include/misc/bmo_options.hpp:27:10: error: #error Eigen must be version 3.4.0 or above
27 | #error Eigen must be version 3.4.0 or above
But my Eigen is 3.4.0:
❯ pkg-config --modversion eigen3
3.4.0
how to solve this problem?
Thanks!
Hello, I'd like to ask if there is a similar MCMC library in C language?
Hi,
Thanks for the library, its incredibly useful!
I've some doubts about what to pass as my energy functional. I have an energy function with respect to some P, say; lets call it f(P) . For my application f(P) = 0 should correspond to high probability samples, while higher energies should correspond to low probability ones. So I use e^( -1 * f(P) ) as my density function. Since f(p) can lie in [0,infinity), its not technically a density function.
To use your HMC API, my log posterior kernel returns the log of this density, i.e, -1 * f(P) , and the corresponding gradient with respect to P. The HMC run is initialized with some some initial values for P, and the function that computes the log density, receives proposals for P.
I sample a 100k samples, which is a lot for my case; I divide the samples into 100 bins of equally spaced energy values, and count the number of samples in each bin. Then i plot these counts as a histogram against the energy values. X-axis is the energy values, and Y-axis is the count in each bin of energy values. This histogram should represent the "learned" target distribution.
Since my kernel is e^(-1 * f(p) ), I should get a monotonically decreasing curve as the energy values increase. That is, the histogram should look like the curve of e^(-x). However, I get a bell-curve like a gaussian, with a sharp peak in the middle. But , the sampled values are as I expect them to be.
On the other hand, if I return negative log likelihood from my kernel and adjust the gradients accordingly, and repeat the above process, the histogram I plot looks like the curve of e^( - x) , just as expected. However, in this case the sampled samples don't look right.
Could you please clarify what's happening here, and what should the function return for HMC?
Thanks!
Hi,
I've been trying to follow your instructions for adding mcmc to an R package to use the samplers you've written.
I already have a package that uses Eigen and CppAD to generate loglikes and gradients. I can't get mcmc to play with that package.
So to see where I was going wrong I'vee tried starting a new package, that just uses Rcpp, I followed your instructions of using the header only version and adding the following at the stop of my cpp file:
#define MCMC_USE_RCPP_EIGEN
#include "mcmc.hpp"
When I try to build a it I get the following error:
#error MCMClib: you must enable the armadillo or eigen wrappers
When I add #define MCMC_ENABLE_EIGEN_WRAPPERS
I get the following error:
#error Eigen must be version 3.4.0 or above
Anyway, any help would be greatly appreciated as I'm very keen to use your samplers, rather than having to write them out myself.
Kind regards,
Skip.
When trying to install the header-only version, I get this error:
mtoussai@riley2:~/git/mcmc> ./configure --header-only-version
sed: can't read /home/mtoussai/git/mcmc/src/mcmc_algos.cpp: No such file or directory
Hello,
I have been inspired by your approach to the MCMC samplers. However the GPL license prevents me from utilizing the library or parts of it in any other library/application which has a freer license than GPL such as MIT or BSD. I just wanted to ask if this is intentional or whether you would be okay with a freer license.
When using multiple runs of the same MCMC algorithm (and settings) with bounds, sometimes it will return a 0 acceptance rate and fail, while other runs will work as expected. Our hopeful use case would be to provide R wrappers to your fast MCMC samplers with custom bounded density functions, but the instability has us concerned.
To simplify the issue and provide a hopefully reproducible example, take the HMC example with Eigen in the package documentation. If bounds are added between 1.55 and 50 for both parameters, it fails >10% of the time. But when I increase the lower bound to 1.65, the algorithm almost always fails. Does this have to do with the step-size? Or am I missing something?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.