Skip to content

Commit

Permalink
Add BaytesOptim
Browse files Browse the repository at this point in the history
  • Loading branch information
paschermayr committed Aug 13, 2023
1 parent ae32432 commit 78d59a2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
fail-fast: false
matrix:
version:
- '1.8'
- '1.9'
- 'nightly'
os:
Expand Down
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name = "Baytes"
uuid = "72ddfcfc-6e9d-43df-829b-7aed7c549d4f"
authors = ["Patrick Aschermayr <p.aschermayr@gmail.com>"]
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"
Expand All @@ -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"
Expand All @@ -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"]
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
112 changes: 112 additions & 0 deletions test/test-construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 78d59a2

@paschermayr
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

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/89535

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:

git tag -a v0.3.10 -m "<description of version>" 78d59a2351e54ce6a2c57b52581c119736eb8108
git push origin v0.3.10

Please sign in to comment.