-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
258 additions
and
99 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using PDMPFlux | ||
|
||
# using Test | ||
using Zygote, Random, Plots | ||
|
||
N_sk = 10_000 # number of skeleton points | ||
N = 10_000 # number of samples | ||
|
||
function runtest(N_sk::Int, N::Int, dim::Int=2) | ||
function U_banana(x::Vector) | ||
mean_x2 = (x[1]^2 - 1) | ||
return -(- x[1]^2 + -(x[2] - mean_x2)^2) / 2 | ||
end | ||
|
||
∇U(x::Vector) = gradient(U_banana, x)[1] | ||
seed = 8 | ||
key = MersenneTwister(seed) | ||
xinit = ones(dim) | ||
vinit = ones(dim) | ||
grid_size = 0 # constant bounds | ||
|
||
sampler = ZigZag(dim, ∇U, grid_size=grid_size) | ||
out = sample_skeleton(sampler, N_sk, xinit, vinit, seed=seed, verbose = true) | ||
samples = sample_from_skeleton(sampler, N, out) | ||
|
||
return out, samples | ||
end | ||
|
||
out, samples = runtest(N_sk, N) | ||
anim_traj(out, 1000; T_start=100, plot_start=100, filename="ZigZag_Banana2D.gif") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using PDMPFlux | ||
|
||
# using Test | ||
using Zygote, Random, Plots | ||
|
||
N_sk = 10_000 # number of skeleton points | ||
N = 10_000 # number of samples | ||
|
||
function runtest(N_sk::Int, N::Int, dim::Int=3) | ||
function U_banana(x::Vector) | ||
mean_x2 = (x[1]^2 - 1) | ||
return -(- x[1]^2 + -(x[2] - mean_x2)^2 - sum(x[3:end].^2)) / 2 | ||
end | ||
|
||
∇U(x::Vector) = gradient(U_banana, x)[1] | ||
seed = 8 | ||
key = MersenneTwister(seed) | ||
xinit = ones(dim) | ||
vinit = ones(dim) | ||
grid_size = 0 # constant bounds | ||
|
||
sampler = ZigZag(dim, ∇U, grid_size=grid_size) | ||
out = sample_skeleton(sampler, N_sk, xinit, vinit, seed=seed, verbose = true) | ||
samples = sample_from_skeleton(sampler, N, out) | ||
|
||
return out, samples | ||
end | ||
|
||
out, samples = runtest(N_sk, N) | ||
anim_traj(out, 1000; T_start=100, filename="ZigZag_Banana3D.gif", plot_type="3D") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using PDMPFlux | ||
|
||
using Random, Distributions, Plots, LaTeXStrings, ForwardDiff, LinearAlgebra | ||
|
||
""" | ||
Funnel distribution for testing. Returns energy and sample functions. | ||
For reference, see Neal, R. M. (2003). Slice sampling. The Annals of Statistics, 31(3), 705–767. | ||
""" | ||
function funnel(d::Int=10, σ::Float64=3.0, clip_y::Int=11) | ||
|
||
function neg_energy(x::Vector) | ||
v = x[1] | ||
log_density_v = logpdf(Normal(0.0, 3.0), v) | ||
variance_other = exp(v) | ||
other_dim = d - 1 | ||
cov_other = I * variance_other | ||
mean_other = zeros(other_dim) | ||
log_density_other = logpdf(MvNormal(mean_other, cov_other), x[2:end]) | ||
return - log_density_v - log_density_other | ||
end | ||
|
||
function sample_data(n_samples::Int) | ||
# sample from Nd funnel distribution | ||
y = clamp.(σ * randn(n_samples, 1), -clip_y, clip_y) | ||
x = randn(n_samples, d - 1) .* exp.(-y / 2) | ||
return hcat(.- y, x) | ||
end | ||
|
||
return neg_energy, sample_data | ||
end | ||
|
||
function plot_funnel(d::Int=10, n_samples::Int=10000) | ||
_, sample_data = funnel(d) | ||
data = sample_data(n_samples) | ||
|
||
# 最初の2次元を抽出(yとx1) | ||
y = data[:, 1] | ||
x1 = data[:, 2] | ||
|
||
# 散布図をプロット | ||
scatter(y, x1, alpha=0.5, markersize=1, xlabel=L"y", ylabel=L"x_1", | ||
title="Funnel Distribution (First Two Dimensions' Ground Truth)", grid=true, legend=false, color="#78C2AD") | ||
|
||
# xlim と ylim を追加 | ||
xlims!(-8, 8) # x軸の範囲を -8 から 8 に設定 | ||
ylims!(-7, 7) # y軸の範囲を -7 から 7 に設定 | ||
end | ||
plot_funnel() | ||
|
||
function run_ZigZag_on_funnel(N_sk::Int=100_000, N::Int=100_000; d::Int=10) | ||
U, _ = funnel(d) | ||
∇U(x::Vector{Float64}) = ForwardDiff.gradient(U, x) | ||
xinit = ones(d) | ||
vinit = ones(d) | ||
seed = 2024 | ||
grid_size = 0 # constant bounds | ||
sampler = ZigZag(d, ∇U, grid_size=grid_size) | ||
out = sample_skeleton(sampler, N_sk, xinit, vinit, seed=seed, verbose = true) | ||
samples = sample_from_skeleton(sampler, N, out) | ||
return out, samples | ||
end | ||
output, samples = run_ZigZag_on_funnel(d=2) | ||
|
||
# jointplot(samples) | ||
# plot_traj(output, 10000) | ||
# plot_traj(output, 1000, plot_type="3D") | ||
|
||
anim_traj(output, 1000; plot_start=100, filename="ZigZag_Funnel2D.gif") | ||
# anim_traj(output, 1000; filename="ZigZag_Funnel2D.gif") | ||
|
||
diagnostic(output) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
8724cc4
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.
@JuliaRegistrator register()
8724cc4
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.
Registration pull request created: JuliaRegistries/General/117869
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:
Also, note the warning: Version 0.2.1 skips over 0.2.0
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.