-
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?
Test (adaptive) Metropolis-Hastings with diagonally elongated distribution #148
Conversation
double log_likelihood (const SampleType &x) | ||
{ | ||
std::valarray<double> mu = {0, 0}; | ||
std::valarray<double> cov = {{1, 1.5}, {1.5, 3}}; |
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
double mu[2] = {0,0};
double cov[2][2] = {{1,1.5}, {1.5,3}};
Have you checked whether this really corresponds to positive definite matrix?
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)); |
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.
First, the log_likelihood
doesn't have to be normalized, so you can remove the ln(|cov|)
factor along with the ln(2*pi)
one.
Second, just multiply it out:
return -0.5 * (ln(|cov|) + (x - mu)^T @ cov^-1 @ (x - mu) + k * ln(2 * pi)); | |
return -0.5 * ( (x[0]-mu[0])*cov[0][0]*(x[0]-mu[0]) + | |
(x[0]-mu[0])*cov[0][1]*(x[1]-mu[1]) + | |
(x[1]-mu[1])*cov[1][0]*(x[0]-mu[0]) + | |
(x[1]-mu[1])*cov[1][1]*(x[1]-mu[1]) + |
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)? |
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.
as discussed today, you don't have to :-)
|
@bangerth PTAL. Note that I replaced the Cholesky implementation with something much simpler (took better advantage of the fact that I'm assuming A is 2x2 matrix). I think the covariance is correct but I'm not positive -- recall that we took an elongated distribution and "rotated" it by a kind of arbitrary factor. Appreciate any insight here. |
Can you compute what the correct covariance matrix is from the one you put in, and compare to what you get out? |
@bangerth Yes, good idea. My results are close to the results of the test:
|
@bangerth And, when in increase the number of samples in the test by a factor of ten, the test output gets a lot closer to these values. |
OK, so you think this is good to merge? |
WIP. @bangerth could you help me out with the
#FIXME
s here?