-
Notifications
You must be signed in to change notification settings - Fork 5
Test (adaptive) Metropolis-Hastings with diagonally elongated distribution #148
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
624df6e
1743db4
2fb59e2
2201aca
1f74e61
79e9482
bb0220f
34b6ad6
c0f9de9
3547070
8abc056
5040a9e
463a8e1
c175fa2
ad199e2
9220100
69fba75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||
| // --------------------------------------------------------------------- | ||||||||||||
| // | ||||||||||||
| // Copyright (C) 2020 by the SampleFlow authors. | ||||||||||||
| // | ||||||||||||
| // This file is part of the SampleFlow library. | ||||||||||||
| // | ||||||||||||
| // The SampleFlow library is free software; you can use it, redistribute | ||||||||||||
| // it, and/or modify it under the terms of the GNU Lesser General | ||||||||||||
| // Public License as published by the Free Software Foundation; either | ||||||||||||
| // version 2.1 of the License, or (at your option) any later version. | ||||||||||||
| // The full text of the license can be found in the file LICENSE.md at | ||||||||||||
| // the top level directory of SampleFlow. | ||||||||||||
| // | ||||||||||||
| // --------------------------------------------------------------------- | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| // Test the Metropolis-Hastings producer with a diagonally elongated | ||||||||||||
| // distribution. | ||||||||||||
|
|
||||||||||||
| #include <iostream> | ||||||||||||
| #include <sampleflow/producers/metropolis_hastings.h> | ||||||||||||
| #include <sampleflow/filters/conversion.h> | ||||||||||||
| #include <sampleflow/consumers/mean_value.h> | ||||||||||||
| #include <valarray> | ||||||||||||
| #include <random> | ||||||||||||
| #include <cmath> | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| using SampleType = std::valarray<double>; | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| double log_likelihood (const SampleType &x) | ||||||||||||
| { | ||||||||||||
| std::valarray<double> mu = {0, 0}; | ||||||||||||
| std::valarray<double> cov = {{1, 1.5}, {1.5, 3}}; | ||||||||||||
| // FIXME: what's the correct way to do these matrix operations? | ||||||||||||
| return -0.5 * (ln(|cov|) + (x - mu)^T @ cov^-1 @ (x - mu) + k * ln(2 * pi)); | ||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First, the Second, just multiply it out:
Suggested change
That's not elegant, but it'll do for this test. |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| std::pair<SampleType,double> perturb (const SampleType &x) | ||||||||||||
| { | ||||||||||||
| // FIXME: how to sample MVN(x, cov)? | ||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as discussed today, you don't have to :-) |
||||||||||||
| static std::mt19937 rng; | ||||||||||||
| std::normal_distribution<double> distribution(0, 1); | ||||||||||||
| SampleType y = x; | ||||||||||||
| for (auto &el : y) | ||||||||||||
| y += distribution(rng); | ||||||||||||
| return {y, 1.0}; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| int main () | ||||||||||||
| { | ||||||||||||
| SampleFlow::Producers::MetropolisHastings<SampleType> mh_sampler; | ||||||||||||
| SampleFlow::Producers::CovarianceMatrix<SampleType> cov_matrix; | ||||||||||||
| SampleFlow::Consumers::MeanValue<SampleType> mean_value; | ||||||||||||
| cov_matrix.connect_to_producer(mh_sampler); | ||||||||||||
| mean_value.connect_to_producer(mh_sampler); | ||||||||||||
| // Sample, starting at an asymmetric point, and creating 100,000 samples | ||||||||||||
| mh_sampler.sample({5, -1}, &log_likelihood, &perturb, 100000); | ||||||||||||
| std::cout << "Mean value = " << mean_value.get() << std::endl; | ||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't compile. But you can just do
Have you checked whether this really corresponds to positive definite matrix?