Skip to content

Commit be5fd15

Browse files
committed
Export Radiocarbon distributions to Isoplot.jl
1 parent 082a7fe commit be5fd15

5 files changed

Lines changed: 4 additions & 248 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ StatGeochemBase = "61e559cd-58b4-4257-8528-26bb26ff2b9a"
2020
[compat]
2121
DelimitedFiles = "1"
2222
Distributions = "0.15 - 0.25"
23-
Isoplot = "0.4"
23+
Isoplot = "0.5"
2424
KernelDensity = "0.4 - 0.6"
2525
LsqFit = "0.7 - 0.15"
2626
NaNStatistics = "0.6.40"

src/Chron.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ module Chron
3939
include("Utilities.jl")
4040
# Functions for propagating systematic uncertainties
4141
include("Systematic.jl")
42-
# Intcal2013 calibration curve for radiocarbion
43-
include("Intcal.jl")
4442
# Functions for estimating extrema of a finite-range distribution
4543
include("DistMetropolis.jl")
4644
# Functions for stratigraphic modelling
@@ -60,7 +58,7 @@ module Chron
6058
screen_outliers, BootstrapCrystDistributionKDE
6159

6260
# Custom distributions and related functions
63-
export strat_ll, BilinearExponential, Radiocarbon
61+
export strat_ll, BilinearExponential
6462

6563
# Dealing with systematic uncertainty
6664
export add_systematic_uncert_UPb, add_systematic_uncert_ArAr, add_systematic_uncert_UTh, SystematicUncertainty

src/Intcal.jl

Lines changed: 0 additions & 117 deletions
This file was deleted.

src/Utilities.jl

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -158,110 +158,7 @@
158158
Base.:+(d::BilinearExponential{T}, c::Real) where {T} = BilinearExponential{T}(d.A, d.loc + c, d.scl, d.shp, d.skw)
159159
Base.:*(d::BilinearExponential{T}, c::Real) where {T} = BilinearExponential{T}(d.A-log(abs(c)), d.loc * c, d.scl*abs(c), d.shp, d.skw)
160160

161-
## --- Radiocarbon distribution type
162-
163-
struct Radiocarbon{T<:Real} <: ContinuousUnivariateDistribution
164-
μ::T
165-
σ::T
166-
dist::Vector{T}
167-
ldist::Vector{T}
168-
lcdist::Vector{T}
169-
lccdist::Vector{T}
170-
end
171-
172-
function Radiocarbon::T, σ::T, ldist::Vector{T}) where {T<:Real}
173-
# Ensure normalization
174-
dist = exp.(ldist)
175-
normconst = sum(dist) * one(T)
176-
dist ./= normconst
177-
ldist .-= normconst
178-
179-
# Cumulative distributions
180-
lcdist = nanlogcumsumexp(ldist)
181-
lccdist = nanlogcumsumexp(ldist, reverse=true)
182161

183-
return Radiocarbon{T}(μ, σ, dist, ldist, lcdist, lccdist)
184-
end
185-
186-
function Radiocarbon(Age_14C::Real, Age_14C_sigma::Real, calibration::NamedTuple=intcal13)
187-
@assert calibration.Age_Calendar == 1:1:length(calibration.Age_14C)
188-
@assert step(calibration.Age_Calendar) == calibration.dt == 1
189-
190-
ldist = normlogproduct.(Age_14C, Age_14C_sigma, calibration.Age_14C, calibration.Age_sigma)
191-
192-
# Ensure normalization
193-
dist = exp.(ldist)
194-
normconst = sum(dist) * calibration.dt
195-
dist ./= normconst
196-
ldist .-= normconst
197-
198-
μ = histmean(dist, calibration.Age_Calendar)
199-
σ = histstd(dist, calibration.Age_Calendar, corrected=false)
200-
201-
# Cumulative distributions
202-
lcdist = nanlogcumsumexp(ldist)
203-
lccdist = nanlogcumsumexp(ldist, reverse=true)
204-
205-
return Radiocarbon(μ, σ, dist, ldist, lcdist, lccdist)
206-
end
207-
208-
## Conversions
209-
Base.convert(::Type{Radiocarbon{T}}, d::Radiocarbon) where {T<:Real} = Radiocarbon{T}(T(d.μ), T(d.σ), T.(d.ldist))
210-
Base.convert(::Type{Radiocarbon{T}}, d::Radiocarbon{T}) where {T<:Real} = d
211-
212-
## Parameters
213-
Distributions.params(d::Radiocarbon) = (d.dist, d.ldist)
214-
@inline Distributions.partype(d::Radiocarbon{T}) where {T<:Real} = T
215-
216-
Distributions.location(d::Radiocarbon) = d.μ
217-
Distributions.scale(d::Radiocarbon) = d.σ
218-
219-
Base.eltype(::Type{Radiocarbon{T}}) where {T} = T
220-
221-
## Evaluation
222-
@inline Distributions.pdf(d::Radiocarbon, x::Real) = exp(logpdf(d, x))
223-
@inline function Distributions.pdf(d::Radiocarbon{T}, x::Real) where {T}
224-
if firstindex(d.ldist) <= x <= lastindex(d.ldist)
225-
linterp_at_index(d.dist, x, -maxintfloat(T))
226-
else
227-
# Treat as a single Normal distrbution if outside of the calibration range
228-
pdf(Normal(d.μ, d.σ), x)
229-
end
230-
end
231-
@inline function Distributions.logpdf(d::Radiocarbon{T}, x::Real) where {T}
232-
if firstindex(d.ldist) <= x <= lastindex(d.ldist)
233-
linterp_at_index(d.ldist, x, -maxintfloat(T))
234-
else
235-
# Treat as a single Normal distrbution if outside of the calibration range
236-
logpdf(Normal(d.μ, d.σ), x)
237-
end
238-
end
239-
@inline Distributions.cdf(d::Radiocarbon, x::Real) = exp(logcdf(d, x))
240-
@inline function Distributions.logcdf(d::Radiocarbon{T}, x::Real) where {T}
241-
if firstindex(d.lcdist) <= x <= lastindex(d.lcdist)
242-
linterp_at_index(d.lcdist, x, -maxintfloat(T))
243-
else
244-
# Treat as a single Normal distrbution if outside of the calibration range
245-
logcdf(Normal(d.μ, d.σ), x)
246-
end
247-
end
248-
@inline Distributions.ccdf(d::Radiocarbon, x::Real) = exp(logccdf(d, x))
249-
@inline function Distributions.logccdf(d::Radiocarbon{T}, x::Real) where {T}
250-
if firstindex(d.lccdist) <= x <= lastindex(d.lccdist)
251-
linterp_at_index(d.lccdist, x, -maxintfloat(T))
252-
else
253-
# Treat as a single Normal distrbution if outside of the calibration range
254-
logccdf(Normal(d.μ, d.σ), x)
255-
end
256-
end
257-
258-
## Statistics
259-
Distributions.mean(d::Radiocarbon) = d.μ
260-
Distributions.var(d::Radiocarbon) = d.σ * d.σ
261-
Distributions.std(d::Radiocarbon) = d.σ
262-
Distributions.skewness(d::Radiocarbon) = histskewness(d.dist, eachindex(d.dist), corrected=false)
263-
Distributions.kurtosis(d::Radiocarbon) = histkurtosis(d.dist, eachindex(d.dist), corrected=false)
264-
265162
## --- Extend `normpdf_ll` to deal with Distributions.Normal
266163

267164
function StatGeochemBase.normpdf_ll(x::AbstractVector, ages::AbstractVector{Normal{T}}) where T

test/testUtilities.jl

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,14 @@
2626
@test skewness(d) -0.16449904171203025 atol = 1e-6
2727
@test kurtosis(d) 0.0865922917156188 atol = 1e-6
2828

29-
## --- Test Radiocarbon distribution
30-
31-
d = Radiocarbon(1000, 10, intcal13)
32-
@test d isa Radiocarbon{Float64}
33-
@test pdf(d, 950) 0.0006472245090905895
34-
@test logpdf(d, 950) -7.342817323513984
35-
@test pdf.(d, [900, 950, 1000]) [6.333126104255167e-8, 0.0006472245090905895, 2.652591331229418e-13]
36-
@test cdf.(d, [-1, 900, 950, 1000, 1e5]) [0.0, 0.000563737957020387, 0.18096988328291203, 0.18312332434946413, 1.0]
37-
@test ccdf.(d, [-1, 900, 950, 1000, 1e5]) [1.0, 0.18255964979458322, 0.0028006656465210025, 7.114355524701459e-11, 0.0]
38-
@test logcdf.(d, [-1, 900, 950, 1000, 1e5]) [-7649.088264850034, -7.480921029645386, -1.7094246522629235, -1.6975954495627632, -0.0]
39-
@test logccdf.(d, [-1, 900, 950, 1000, 1e5]) [-0.0, -1.700678311173327, -5.8778981591541335, -23.366321375298313, -8.706770436253259e7]
40-
41-
@test eltype(d) === partype(d) === Float64
42-
@test location(d) 927.2556461305643
43-
@test scale(d) 7.5077650707247
44-
45-
@test mean(d) 927.2556461305643
46-
@test var(d) 7.5077650707247^2
47-
@test std(d) 7.5077650707247
48-
@test skewness(d) -4.6327184250788696
49-
@test kurtosis(d) 67.68860469654331
50-
5129
## --- Other utility functions for log likelihoods
5230

5331
@test strat_ll([66.02,], [BilinearExponential(66.00606672179812, 0.17739474265630253, 70.57882331291309, 0.6017142541555505)]) (-3.251727600957773 -4.265260194845626)
54-
@test strat_ll([950,], [Radiocarbon(1000, 10, intcal13)]) -7.342817323513984
32+
@test strat_ll([950,], [Radiocarbon(1000, 10, calibration=intcal13)]) -7.342817323513984
5533

5634
@test strat_ll([0.0, 0.0], [Normal(0,1), Normal(0,1)]) 0
5735
@test strat_ll([0.0, 0.5], [Normal(0,1), Uniform(0,1)]) 0
5836
@test strat_ll([0.0, 0.5, 66.02], [Normal(0,1), Uniform(0,1), BilinearExponential(66.00606672179812, 0.17739474265630253, 70.57882331291309, 0.6017142541555505)]) (-3.251727600957773 -4.265260194845626)
59-
@test strat_ll([0.0, 0.5, 66.02, 900], [Normal(0,1), Uniform(0,1), BilinearExponential(66.00606672179812, 0.17739474265630253, 70.57882331291309, 0.6017142541555505), Radiocarbon(1000, 10, intcal13)]) -24.09187457017352
37+
@test strat_ll([0.0, 0.5, 66.02, 900], [Normal(0,1), Uniform(0,1), BilinearExponential(66.00606672179812, 0.17739474265630253, 70.57882331291309, 0.6017142541555505), Radiocarbon(1000, 10, calibration=intcal13)]) -24.09187457017352
6038

6139
## ---

0 commit comments

Comments
 (0)