From 78d59a2351e54ce6a2c57b52581c119736eb8108 Mon Sep 17 00:00:00 2001
From: Patrick Aschermayr
Date: Sun, 13 Aug 2023 11:48:33 +0200
Subject: [PATCH] Add BaytesOptim
---
.github/workflows/CI.yml | 1 -
Project.toml | 8 ++-
test/runtests.jl | 4 ++
test/test-construction.jl | 112 ++++++++++++++++++++++++++++++++++++++
4 files changed, 122 insertions(+), 3 deletions(-)
diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml
index d51cf28..03b611a 100644
--- a/.github/workflows/CI.yml
+++ b/.github/workflows/CI.yml
@@ -18,7 +18,6 @@ jobs:
fail-fast: false
matrix:
version:
- - '1.8'
- '1.9'
- 'nightly'
os:
diff --git a/Project.toml b/Project.toml
index 69df793..bdeaf82 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,13 +1,14 @@
name = "Baytes"
uuid = "72ddfcfc-6e9d-43df-829b-7aed7c549d4f"
authors = ["Patrick Aschermayr "]
-version = "0.3.9"
+version = "0.3.10"
[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
BaytesCore = "e5a8efeb-54cd-4583-bf9c-87b09970f7ba"
BaytesFilters = "26b78818-2f47-47c8-af4b-89b2c3dade3f"
BaytesMCMC = "8e6e46a0-107e-4f93-8296-97663f3953ce"
+BaytesOptim = "c78c806d-0713-4f4e-8b87-f7023233ea83"
BaytesPMCMC = "c4a8eb41-45be-4f57-9046-b06303d6bef8"
BaytesSMC = "2428e3a5-deaa-4b4d-8ec5-54cd60d7566c"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
@@ -26,6 +27,7 @@ ArgCheck = "2"
BaytesCore = "0.2"
BaytesFilters = "0.3"
BaytesMCMC = "0.3"
+BaytesOptim = "0.1"
BaytesPMCMC = "0.3"
BaytesSMC = "0.3"
DocStringExtensions = "0.8, 0.9"
@@ -38,8 +40,10 @@ SimpleUnPack = "1"
julia = "^1.8"
[extras]
+NLSolversBase = "d41bc354-129a-5804-8e4c-c37616107c6c"
+Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[targets]
-test = ["Test", "Distributions"]
+test = ["Test", "Distributions", "NLSolversBase", "Optim"]
diff --git a/test/runtests.jl b/test/runtests.jl
index 6cce5b7..16f9a72 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -10,7 +10,11 @@ using Distributions
using
ModelWrappers,
Baytes
+using BaytesOptim
+
+#include("D:/OneDrive/1_Life/1_Git/0_Dev/Julia/modules/Baytes.jl/src/Baytes.jl")
#using .Baytes
+
############################################################################################
# Include Files
include("testhelper/TestHelper.jl")
diff --git a/test/test-construction.jl b/test/test-construction.jl
index 978a7e1..1e97a4f 100644
--- a/test/test-construction.jl
+++ b/test/test-construction.jl
@@ -274,6 +274,118 @@ end
end
end
+############################################################################################
+#tempermethod = tempermethods[1]
+#iter = length(objectives)
+using Optim, NLSolversBase
+@testset "Sampling, BaytesOptim" begin
+ for tempermethod in tempermethods
+ for iter in eachindex(objectives)
+ #println(tempermethod, " ", iter)
+ sampledefault = SampleDefault(;
+ dataformat=Batch(),
+ tempering=deepcopy(tempermethod), #IterationTempering(Float64, UpdateFalse(), 1.0, 1000),
+ chains=4,
+ iterations=100,
+ burnin=max(1, Int64(floor(10/10))),
+ thinning = 1,
+ safeoutput=false,
+ printoutput=false,
+ printdefault=PrintDefault(),
+ report=ProgressReport(;
+ bar=false,
+ log=SilentLog()
+ ),
+ )
+ temperupdate = sampledefault.tempering.adaption
+ _obj = deepcopy(objectives[iter])
+ _flattentype = _obj.model.info.reconstruct.default.output
+## Optimization
+ def = OptimDefault(;
+ kernel = (;
+ magnitude_penalty = 1e-4,
+ iterations = 1000
+ )
+ )
+ opt = Optimizer(
+ _rng,
+ OptimLBFG,
+ _obj,
+ def,
+ )
+## Test Constructor
+ mcmc = MCMC(NUTS,(:σ,); stepsize = ConfigStepsize(;stepsizeadaption = UpdateFalse()))
+ _oc = OptimConstructor(OptimLBFG, :μ,
+ OptimDefault(;
+ kernel = (;
+ iterations = 123)
+ )
+ )
+ Optimizer(OptimLBFG, :μ)
+ trace, algorithms = sample(_rng, _obj.model, _obj.data, _oc; default = deepcopy(sampledefault))
+
+## Inference Section
+ transform = Baytes.TraceTransform(trace, _obj.model)
+ postmean = trace_to_posteriormean(trace, transform)
+
+ post3D = trace_to_3DArray(trace, transform)
+ post3Dᵤ = trace_to_3DArrayᵤ(trace, transform)
+ @test size(post3D) == size(post3Dᵤ)
+ post2D = trace_to_2DArray(trace, transform)
+ post2Dᵤ = trace_to_2DArrayᵤ(trace, transform)
+ @test size(post2D) == size(post2Dᵤ)
+##
+ #Check trace transforms
+ g_vals = get_chainvals(trace, transform)
+ m_vals = merge_chainvals(trace, transform)
+ f_vals = flatten_chainvals(trace, transform)
+ @test sum( map(val -> length(val), g_vals) ) == length(m_vals) == sum( map(val -> length(val), f_vals) )
+
+ #Check printing commands
+ printchainsummary(trace, transform, Val(:text))
+ printchainsummary(_obj.model, trace, transform, Val(:text))
+#=
+ ## SMC
+ ibis = SMCConstructor(_oc, SMCDefault(jitterthreshold=0.99, resamplingthreshold=1.0))
+ trace, algorithms = sample(_rng, _obj.model, _obj.data, ibis; default = deepcopy(sampledefault))
+ ## If single mcmc kernel assigned, can capture previous results
+ @test isa(trace.summary.info.captured, UpdateFalse)
+ ## Continue sampling
+ newdat = randn(_rng, length(_obj.data)+100)
+ trace2, algorithms2 = sample!(100, _rng, _obj.model, newdat, trace, algorithms)
+ @test isa(trace2.summary.info.captured, UpdateFalse)
+=#
+## Combinations
+ trace, algorithms = sample(_rng, _obj.model, _obj.data, mcmc, _oc; default = deepcopy(sampledefault))
+ transform = Baytes.TraceTransform(trace, _obj.model)
+ printchainsummary(trace, transform, Val(:text))
+ m_vals = merge_chainvals(trace, transform)
+ ## Continue sampling
+ newdat = randn(_rng, length(_obj.data)+100)
+ trace2, algorithms2 = sample!(100, _rng, _obj.model, newdat, trace, algorithms)
+ end
+ end
+end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
############################################################################################
#Utility
@testset "Utility, maxiterations" begin