|
71 | 71 |
|
72 | 72 | ℓπ = DemoProblemComponentArrays() |
73 | 73 |
|
74 | | - # Define a Hamiltonian system |
75 | | - D = length(p1) # number of parameters |
76 | | - metric = DiagEuclideanMetric(D) |
| 74 | + @testset "Test Diagonal ComponentArray metric" begin |
77 | 75 |
|
78 | | - # choose AD framework or provide a function manually |
79 | | - hamiltonian = Hamiltonian(metric, ℓπ, Val(:ForwardDiff); x=p1) |
| 76 | + # Define a Hamiltonian system |
| 77 | + M⁻¹ = ComponentArray(; μ=1.0, σ=1.0) |
| 78 | + metric = DiagEuclideanMetric(M⁻¹) |
80 | 79 |
|
81 | | - # Define a leapfrog solver, with initial step size chosen heuristically |
82 | | - initial_ϵ = find_good_stepsize(hamiltonian, p1) |
83 | | - integrator = Leapfrog(initial_ϵ) |
| 80 | + # choose AD framework or provide a function manually |
| 81 | + hamiltonian = Hamiltonian(metric, ℓπ, Val(:ForwardDiff)) |
84 | 82 |
|
85 | | - # Define an HMC sampler, with the following components |
86 | | - proposal = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) |
87 | | - adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, integrator)) |
| 83 | + # Define a leapfrog solver, with initial step size chosen heuristically |
| 84 | + initial_ϵ = find_good_stepsize(hamiltonian, p1) |
| 85 | + integrator = Leapfrog(initial_ϵ) |
88 | 86 |
|
89 | | - # -- run sampler |
90 | | - n_samples, n_adapts = 100, 50 |
91 | | - samples, stats = sample( |
92 | | - hamiltonian, |
93 | | - proposal, |
94 | | - p1, |
95 | | - n_samples, |
96 | | - adaptor, |
97 | | - n_adapts; |
98 | | - progress=false, |
99 | | - verbose=false, |
100 | | - ) |
| 87 | + # Define an HMC sampler, with the following components |
| 88 | + proposal = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) |
| 89 | + adaptor = StanHMCAdaptor( |
| 90 | + MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, integrator) |
| 91 | + ) |
101 | 92 |
|
102 | | - @test length(samples) == n_samples |
103 | | - @test length(stats) == n_samples |
104 | | - labels = ComponentArrays.labels(samples[1]) |
105 | | - @test "μ" ∈ labels |
106 | | - @test "σ" ∈ labels |
| 93 | + # -- run sampler |
| 94 | + n_samples, n_adapts = 100, 50 |
| 95 | + samples, stats = sample( |
| 96 | + hamiltonian, |
| 97 | + proposal, |
| 98 | + p1, |
| 99 | + n_samples, |
| 100 | + adaptor, |
| 101 | + n_adapts; |
| 102 | + progress=false, |
| 103 | + verbose=false, |
| 104 | + ) |
| 105 | + |
| 106 | + @test length(samples) == n_samples |
| 107 | + @test length(stats) == n_samples |
| 108 | + lab = ComponentArrays.labels(samples[1]) |
| 109 | + @test "μ" ∈ lab |
| 110 | + @test "σ" ∈ lab |
| 111 | + end |
| 112 | + |
| 113 | + @testset "Test Dense ComponentArray metric" begin |
| 114 | + |
| 115 | + # Define a Hamiltonian system |
| 116 | + ax = getaxes(p1)[1] |
| 117 | + M⁻¹ = ComponentArray([2.0 1.0; 1.0 2.0], ax, ax) |
| 118 | + metric = DenseEuclideanMetric(M⁻¹) |
| 119 | + |
| 120 | + # choose AD framework or provide a function manually |
| 121 | + hamiltonian = Hamiltonian(metric, ℓπ, Val(:ForwardDiff)) |
| 122 | + |
| 123 | + # Define a leapfrog solver, with initial step size chosen heuristically |
| 124 | + initial_ϵ = find_good_stepsize(hamiltonian, p1) |
| 125 | + integrator = Leapfrog(initial_ϵ) |
| 126 | + |
| 127 | + # Define an HMC sampler, with the following components |
| 128 | + proposal = HMCKernel(Trajectory{MultinomialTS}(integrator, GeneralisedNoUTurn())) |
| 129 | + adaptor = StanHMCAdaptor( |
| 130 | + MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, integrator) |
| 131 | + ) |
| 132 | + |
| 133 | + # -- run sampler |
| 134 | + n_samples, n_adapts = 100, 50 |
| 135 | + samples, stats = sample( |
| 136 | + hamiltonian, |
| 137 | + proposal, |
| 138 | + p1, |
| 139 | + n_samples, |
| 140 | + adaptor, |
| 141 | + n_adapts; |
| 142 | + progress=false, |
| 143 | + verbose=false, |
| 144 | + ) |
| 145 | + |
| 146 | + @test length(samples) == n_samples |
| 147 | + @test length(stats) == n_samples |
| 148 | + lab = ComponentArrays.labels(samples[1]) |
| 149 | + @test "μ" ∈ lab |
| 150 | + @test "σ" ∈ lab |
| 151 | + end |
107 | 152 | end |
108 | 153 |
|
109 | 154 | @testset "ADTypes" begin |
|
0 commit comments