8000 Variance fixes in simulated spectra by fjebaker · Pull Request #101 · fjebaker/SpectralFitting.jl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Variance fixes in simulated spectra #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ update_model!(model, result)
To estimate the goodness of our fit, we can mimic the `goodness` command from XSPEC. This will use the [`simulate`](@ref) function to simulate spectra for a dataset (here determined by the result), and fit the model to the simulated dataset. The fit statistic for each fit is then appended to an array, which we can use to plot a histogram:

```@example walk
spread = goodness(result; N = 1000, seed = 42)
spread = goodness(result; N = 1000, seed = 42, exposure_time = data.data.spectrum.exposure_time)
histogram(spread, ylims = (0, 300), label = "Simulated")
vline!([result.χ2], label = "Best fit")
```
Expand Down
12 changes: 8 additions & 4 deletions src/simulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mutable struct SimulatedSpectrum{T,F} <: AbstractDataset
model_domain::Vector{T}
output_domain::Vector{T}
data::Vector{T}
errors::Vector{T}
variance::Vector{T}
units::Union{Nothing,SpectralUnits.RateOrCount}
transformer!!::F
seed::Int
Expand All @@ -18,7 +18,7 @@ end

function make_objective_variance(::ContiguouslyBinned, dataset::SimulatedSpectrum)
check_units_warning(dataset.units)
dataset.errors .^ 2
dataset.variance
end

make_model_domain(::ContiguouslyBinned, dataset::SimulatedSpectrum) = dataset.model_domain
Expand Down Expand Up @@ -51,13 +51,18 @@ function simulate!(
simulate_distribution = Distributions.Poisson,
rng = Random.default_rng(),
exposure_time = 1e5,
counting_variance = true,
)
config.objective .= _invoke_and_transform!(config.cache, config.model_domain, p)
for (i, m) in enumerate(config.objective)
distr = simulate_distribution(m * exposure_time)
count = rand(rng, distr)
config.objective[i] = count / exposure_time
config.variance[i] = sqrt(abs(count)) / exposure_time

# how to propagate the variance
if counting_variance
config.variance[i] = count / exposure_time^2
end
end
end

Expand All @@ -69,7 +74,6 @@ function simulate!(conf::FittingConfig; seed = abs(rand(Int)), kwargs...)
conf.model_domain,
conf.output_domain,
conf.objective,
# variance has already been sqrt'd
conf.variance,
u"counts / (s * keV)",
conf.cache.transformer!!,
Expand Down
16 changes: 8 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ end
end

@testset "fitting" verbose = true begin
include("fitting/test-fit-simple-dataset.jl")
include("fitting/test-cache.jl")
include("fitting/test-binding.jl")
include("fitting/test-results.jl")
@time include("fitting/test-fit-simple-dataset.jl")
@time include("fitting/test-cache.jl")
@time include("fitting/test-binding.jl")
@time include("fitting/test-results.jl")

@testset "powerlaws" begin
include("fitting/test-fit-powerlaw.jl")
@time include("fitting/test-fit-powerlaw.jl")
end
@testset "multifits" begin
include("fitting/test-fit-multi.jl")
include("fitting/test-fit-optim.jl")
@time include("fitting/test-fit-multi.jl")
@time include("fitting/test-fit-optim.jl")
end
if has_test_dir
@testset "sample-data" begin
include("fitting/test-sample-data.jl")
@time include("fitting/test-sample-data.jl")
end
else
@warn "Skipping dataset tests."
Expand Down
0