Skip to content

Commit

Permalink
Move utility functions out of StratMetropolis.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Jul 2, 2024
1 parent ec24946 commit a816d4f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/Chron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ module Chron
using Distributions
using QuadGK

# Custom objects for holding Chron age data
include("Objects.jl")
# General utility functions
include("Utilities.jl")
# Functions for propagating systematic uncertainties
include("Systematic.jl")
# Custom objects for holding Chron age data
include("Objects.jl")
# Intcal2013 calibration curve for radiocarbion
include("Intcal.jl")
# Functions for estimating extrema of a finite-range distribution
Expand Down
2 changes: 1 addition & 1 deletion src/Objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
Name::NTuple{N, String}
Height::Vector{Float64}
Height_sigma::Vector{Float64}
Age::Vector{Distribution{Univariate, Continuous}}
Age_Distribution::Vector{<:Union{<:Distribution{Univariate, Continuous}}}
Age_Sidedness::Vector{Float64}
Chronometer::NTuple{N, Symbol}
Age_Unit::String
Expand Down
27 changes: 0 additions & 27 deletions src/StratMetropolis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,33 +433,6 @@

## --- # Internals of the Markov chain

# Use dispatch to let us reduce duplication
strat_ll(x, ages::AbstractVector{<:Radiocarbon}) = interpolate_ll(x, ages)
strat_ll(x, ages::AbstractVector{<:Normal}) = normpdf_ll(x, ages)
strat_ll(x::Real, age::Distribution) = logpdf(age, x)
function strat_ll(x, ages)
ll = zero(float(eltype(x)))
@inbounds for i in eachindex(x, ages)
ll += logpdf(ages[i], x[i])
end
return ll
end

adjust!(ages, chronometer, systematic::Nothing) = ages
function adjust!(ages, chronometer, systematic::SystematicUncertainty)
systUPb = randn()*systematic.UPb
systArAr = randn()*systematic.ArAr
@assert eachindex(ages)==eachindex(chronometer)
@inbounds for i eachindex(ages)
if chronometer[i] === :UPb
ages[i] += systUPb
elseif chronometer[i] === :ArAr
ages[i] += systArAr
end
end
return ages
end

function stratmetropolis(Height, Height_sigma, model_heights::AbstractRange, Age_Sidedness, ages, model_ages, proposal_sigma, burnin::Integer, nsteps::Integer, sieve::Integer, Chronometer=nothing, systematic=nothing)
resolution = step(model_heights)
npoints = length(model_heights)
Expand Down
19 changes: 18 additions & 1 deletion src/Systematic.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
## --- Dealing with systematic uncertainty
## --- Adjust distributions for systematic uncertainty

adjust!(ages, chronometer, systematic::Nothing) = ages
function adjust!(ages, chronometer, systematic::SystematicUncertainty)
systUPb = randn()*systematic.UPb
systArAr = randn()*systematic.ArAr
@assert eachindex(ages)==eachindex(chronometer)
@inbounds for i eachindex(ages)
if chronometer[i] === :UPb
ages[i] += systUPb
elseif chronometer[i] === :ArAr
ages[i] += systArAr
end
end
return ages
end

## --- Add systematic uncertainty when given an already-generated age or age-depth distribution

function add_systematic_uncert_UPb(agedistmyr::Vector{<:AbstractFloat})
λ238 = val(λ238U) # Jaffey decay constant, 1/Myr
Expand Down
17 changes: 14 additions & 3 deletions src/Utilities.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## --- Dealing with arbitrary distributions


## --- "bilinear exponential" distribution type

"""
Expand Down Expand Up @@ -280,4 +277,18 @@
return ll
end

## --- log likelihood functions allowing for arbitrary Distributions

# Use dispatch to let us reduce duplication
strat_ll(x, ages::AbstractVector{<:Radiocarbon}) = interpolate_ll(x, ages)
strat_ll(x, ages::AbstractVector{<:Normal}) = normpdf_ll(x, ages)
strat_ll(x::Real, age::Distribution) = logpdf(age, x)
function strat_ll(x, ages)
ll = zero(float(eltype(x)))
@inbounds for i in eachindex(x, ages)
ll += logpdf(ages[i], x[i])
end
return ll
end

## --- End of File

0 comments on commit a816d4f

Please # to comment.