Skip to content

Commit ba47050

Browse files
committed
Improve parameter fit performance
1 parent 70ef2ee commit ba47050

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

docs/src/parameter_fitting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function dL(z, prob::CosmologyProblem, Ωm0, h; Ωr0 = 5e-5)
8181
M.m.Ω₀ => Ωm0,
8282
M.Λ.Ω₀ => 1 - Ωr0 - Ωm0,
8383
M.g.h => h, M.r.T₀ => 0.0 # TODO: don't set
84-
))
84+
), build_initializeprob = false) # bypass unnecessary initialization for performance
8585
sol = solve(prob)
8686
return dL(z, sol)
8787
end
@@ -112,7 +112,7 @@ end
112112
113113
# TODO: speed up: https://discourse.julialang.org/t/modelingtoolkit-odesystem-in-turing/115700/
114114
sn = supernova(data, prob) # condition model on data
115-
chain = sample(sn, NUTS(), MCMCSerial(), 200, 1)
115+
chain = sample(sn, NUTS(; init_ϵ = 0.0125), MCMCSerial(), 2000, 1)
116116
```
117117
As we see above, the MCMC `chain` displays a summary with information about the fitted parameters, including their posterior means and standard deviations.
118118
We can also plot the chains:

src/components/metric.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ function metric(; name = :g, kwargs...)
99
defs = [
1010
H₀ => H100 * h
1111
]
12-
initialization_eqs = [
13-
O^0)(a ~ D(a) * t)
14-
]
1512
guesses = [
1613
a => 1e-5
1714
]
@@ -24,5 +21,5 @@ function metric(; name = :g, kwargs...)
2421
H ~ E * H₀
2522
g₁₁ ~ a^2 * (-1 - 2*ϵ*Ψ)
2623
g₂₂ ~ a^2 * (+1 - 2*ϵ*Φ)
27-
], t, vars, pars; defaults = defs, initialization_eqs, guesses, name, description, kwargs...)
24+
], t, vars, pars; defaults = defs, guesses, name, description, kwargs...)
2825
end

src/models.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ function ΛCDM(;
7474
if all.(have(species, :Ω₀)) && startswith(ModelingToolkit.description(G), "General relativity")
7575
push!(defs, species[end].Ω₀ => 1 - sum(s.Ω₀ for s in species[begin:end-1]))
7676
end
77+
initialization_eqs = [
78+
O^0)(g.a ~ D(g.a) * t)
79+
]
7780

7881
description = "Standard cosmological constant and cold dark matter cosmological model"
79-
connections = ODESystem([eqs0; eqs1], t, vars, [pars; k]; defaults = defs, name, description)
82+
connections = ODESystem([eqs0; eqs1], t, vars, [pars; k]; defaults = defs, initialization_eqs, name, description)
8083
components = filter(!isnothing, [g; G; species; I])
8184
M = compose(connections, components...)
8285
return complete(M; flatten = false)
@@ -118,6 +121,7 @@ function RMΛ(;
118121
] .|> O^1)
119122
defs = [
120123
g.Ψ => 20 // 15,
124+
g.a => (r.Ω₀) * t
121125
]
122126
connections = ODESystem([eqs0; eqs1], t, [], [k]; defaults = defs, name)
123127
M = compose(connections, g, G, species...)

src/solve.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ end
161161
Return an updated `CosmologyProblem` where parameters in `prob` are updated to values specified in `pars`.
162162
Parameters that are not specified in `pars` keep their values from `prob`.
163163
"""
164-
function remake(prob::CosmologyProblem, pars::Dict; bg = true, th = true, pt = true, shoot = true)
164+
function remake(prob::CosmologyProblem, pars::Dict; bg = true, th = true, pt = true, shoot = true, kwargs...)
165165
pars_full = merge(prob.pars, pars) # save full dictionary for constructor
166166
vars, pars = split_vars_pars(prob.M, pars)
167-
bg = bg && !isnothing(prob.bg) ? remake(prob.bg; u0 = vars, p = pars) : nothing
168-
th = th && !isnothing(prob.th) ? remake(prob.th; u0 = vars, p = pars) : nothing
169-
pt = pt && !isnothing(prob.pt) ? remake(prob.pt; u0 = vars, p = pars) : nothing
167+
bg = bg && !isnothing(prob.bg) ? remake(prob.bg; u0 = vars, p = pars, kwargs...) : nothing
168+
th = th && !isnothing(prob.th) ? remake(prob.th; u0 = vars, p = pars, kwargs...) : nothing
169+
pt = pt && !isnothing(prob.pt) ? remake(prob.pt; u0 = vars, p = pars, kwargs...) : nothing
170170
shoot_pars = shoot ? prob.shoot : keys(Dict())
171171
shoot_conditions = shoot ? prob.conditions : []
172172
return CosmologyProblem(prob.M, bg, th, pt, pars_full, shoot_pars, shoot_conditions, prob.spline_thermo)

0 commit comments

Comments
 (0)