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

More robust SGMCMC tests #120

Open
SamDuffield opened this issue Feb 13, 2025 · 1 comment
Open

More robust SGMCMC tests #120

SamDuffield opened this issue Feb 13, 2025 · 1 comment
Labels
tests Improvements or additions to tests

Comments

@SamDuffield
Copy link
Contributor

The error tolerances for the SGMCMC tests are quite large -> this should be reduced and robustified!

@SamDuffield SamDuffield added the tests Improvements or additions to tests label Feb 13, 2025
@KaelanDt
Copy link
Contributor

could be as simple as checking that the error on the means and covariances decreases when collecting more samples, eg for sgld:

log_posts_1, log_posts_2 = [], []
n_steps_1, n_steps_2 = 1000, 10000

for _ in range(n_steps_1):
        sgld_state = sampler.update(sgld_state, batch, inplace=False)

        all_params_1 = tree_map(
            lambda x, y: torch.cat((x, y.unsqueeze(0))), all_params_1, sgld_state.params
        )

        log_posts_1.append(sgld_state.log_posterior.item())

for _ in range(n_steps_2):
        sgld_state = sampler.update(sgld_state, batch, inplace=False)

        all_params_2 = tree_map(
            lambda x, y: torch.cat((x, y.unsqueeze(0))), all_params_2, sgld_state.params
        )

        log_posts_2.append(sgld_state.log_posterior.item())

    burnin = 1000
    all_params_flat_1 = torch.vmap(lambda x: tree_ravel(x)[0])(all_params_1)
    sampled_mean_1 = all_params_flat_1[burnin:].mean(0)
    sampled_cov_1 = torch.cov(all_params_flat_1[burnin:].T)

    all_params_flat_2 = torch.vmap(lambda x: tree_ravel(x)[0])(all_params_2)
    sampled_mean_2 = all_params_flat_2[burnin:].mean(0)
    sampled_cov_2 = torch.cov(all_params_flat_2[burnin:].T)

    assert log_posts_2[-1] > log_posts_1[-1] > log_posts_1[0]
    assert torch.allclose(sampled_mean_1, target_mean_flat, atol=1e-0, rtol=1e-1)
    assert torch.allclose(sampled_cov_1, target_cov, atol=1e-0, rtol=1e-1)
    assert torch.linalg.norm(abs(sampled_mean_2 - target_mean_flat)) < torch.linalg.norm(abs(sampled_mean_1 - target_mean_flat))
    assert torch.linalg.norm(abs(sampled_cov_2 - target_cov)) < torch.linalg.norm(abs(sampled_cov_1 - target_cov))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Improvements or additions to tests
Projects
None yet
Development

No branches or pull requests

2 participants