-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add rng parameter, fixes #44 * Remove now-unused method
- Loading branch information
Showing
6 changed files
with
78 additions
and
28 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,30 +1,31 @@ | ||
## Optimization ======================================= | ||
|
||
function perturb(p, Cp) | ||
function perturb(rng::AbstractRNG, p, Cp) | ||
d = MvNormal(mean(p), 1.1Cp + 1e-12I) | ||
Particles(length(p[1]), d) | ||
Particles(rng, length(p[1]), d) | ||
end | ||
|
||
|
||
|
||
""" | ||
res = optimize(f,p,τ=1,iters=10000) | ||
res = optimize([rng::AbstractRNG,] f,p,τ=1,iters=10000) | ||
Find the minimum of Function `f`, starting with initial distribution described by `p::Vector{Particles}`. `τ` is the initial temperature. | ||
""" | ||
function optimize(f,p,τ=1; τi=1.005, iters=10000, tol=1e-8) | ||
function optimize(rng::AbstractRNG,f,p,τ=1; τi=1.005, iters=10000, tol=1e-8) | ||
p = deepcopy(p) | ||
N = length(p[1]) | ||
we = zeros(N) | ||
for i = 1:iters | ||
y = -(f(p).particles) | ||
we .= exp.(τ.*y) | ||
j = sample(1:N, ProbabilityWeights(we), N, ordered=true) | ||
j = sample(rng,1:N, ProbabilityWeights(we), N, ordered=true) | ||
foreach(x->(x.particles .= x.particles[j]), p); # @test length(unique(p[1].particles)) == length(unique(j)) | ||
Cp = cov(p) | ||
tr(Cp) < tol && (@info "Converged at iteration $i"; return p) | ||
p = perturb(p, Cp) | ||
p = perturb(rng, p, Cp) | ||
τ *= τi | ||
end | ||
p | ||
end | ||
optimize(f,p,τ=1; kwargs...) = optimize(Random.GLOBAL_RNG,f,p,τ; kwargs...) |
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
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 |
---|---|---|
@@ -1,13 +1,17 @@ | ||
""" | ||
systematic_sample(N, d=Normal(0,1); permute=true) | ||
systematic_sample([rng::AbstractRNG,] N, d=Normal(0,1); permute=true) | ||
returns a `Vector` of length `N` sampled systematically from the distribution `d`. If `permute=false`, this vector will be sorted. | ||
""" | ||
function systematic_sample(N, d=Normal(0,1); permute=true) | ||
function systematic_sample(rng::AbstractRNG, N, d=Normal(0,1); permute=true) | ||
e = 0.5/N # rand()/N | ||
y = e:1/N:1 | ||
o = map(y) do y | ||
quantile(d,y) | ||
end | ||
permute && permute!(o, randperm(N)) | ||
permute && permute!(o, randperm(rng, N)) | ||
o | ||
end | ||
function systematic_sample(N, d=Normal(0,1); kwargs...) | ||
return systematic_sample(Random.GLOBAL_RNG, N, d; kwargs...) | ||
end |
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