Skip to content

Commit 82fa8d8

Browse files
committed
Add resamplingprobability function
1 parent 79c3638 commit 82fa8d8

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/utilities/Resampling.jl

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@
10771077
@batch for g eachindex(spatialscaleᵣ)
10781078
for h eachindex(agescale)
10791079
kᵢ = 0.0
1080-
@inbounds for j 1:N
1080+
@inbounds @simd ivdep for j 1:N
10811081
kᵢⱼ = 1.0 / ((spatialdistᵣ[j]/spatialscaleᵣ[g])^lp + 1.0) + 1.0 / ((abs(age[i] - age[j])/agescale[h])^lp + 1.0)
10821082
kᵢ += kᵢⱼ
10831083
end
@@ -1175,18 +1175,12 @@
11751175
k = Array{Float64}(undef, N)
11761176
p = Progress(N÷10, desc="Calculating weights: ")
11771177
@inbounds @batch for i 1:N
1178-
if isnan(x[i])
1179-
# If there is missing data, set k=inf for weight=0
1180-
k[i] = Inf
1181-
else
1182-
# Otherwise, calculate weight
1183-
kᵢ = 0.0
1184-
@turbo for j 1:N
1185-
kᵢⱼ = 1.0 / ( (abs(x[i] - x[j])/xscale)^lp + 1.0)
1186-
kᵢ += kᵢⱼ
1187-
end
1188-
k[i] = kᵢ
1178+
kᵢ = 0.0
1179+
@turbo for j 1:N
1180+
kᵢⱼ = 1.0 / ( (abs(x[i] - x[j])/xscale)^lp + 1.0)
1181+
kᵢ += kᵢⱼ
11891182
end
1183+
k[i] = kᵢ
11901184
(i % 10 == 0) && next!(p)
11911185
end
11921186
return k
@@ -1196,15 +1190,33 @@
11961190

11971191
"""
11981192
```julia
1199-
k = invweight_age(age::AbstractArray; lp::Number=2, agescale::Number=38.0)
1193+
k = invweight_age(age::AbstractArray;
1194+
\tlp::Number=2,
1195+
\tagescale::Number=38.0
1196+
)
12001197
```
12011198
12021199
Find the inverse weights `k` (proportional to temporal sample density) for
12031200
a set of geological samples with specified `age` (of crystallization, deposition, etc.).
12041201
"""
1205-
function invweight_age(age::AbstractArray; lp::Number=2, agescale::Number=38.0)
1206-
return invweight(age, agescale, lp=lp)
1207-
end
1202+
invweight_age(age::AbstractArray; lp::Number=2, agescale::Number=38.0) = invweight(age, agescale; lp)
12081203
export invweight_age
12091204

1205+
1206+
"""
1207+
```julia
1208+
resamplingprobability(k; median_probability::Number=1/6)
1209+
```
1210+
1211+
Calculate scaled resampling probabilities `p` given a vector of inverse weights `k`
1212+
and a desired median resampling probability (1/6 by default).
1213+
"""
1214+
function resamplingprobability(k; median_probability::Number=1/6)
1215+
median_k = 1/median_probability - 1
1216+
f = median_k / nanmedian(filter(isfinite, k))
1217+
p = @. 1 / ((k * f) + 1)
1218+
return p
1219+
end
1220+
export resamplingprobability
1221+
12101222
## --- End of file

test/testResampling.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
@test invweight_location(lat, lon) [2.3478642777118957, 2.950680392065609, 3.2200556525889006, 3.348975353894235, 3.4103053158720016, 3.4297605864726126, 3.413776296513463, 3.3555688532471923, 3.2289225181937002, 2.9601916238626513, 2.3566734930529947, Inf]
104104
@test invweight_age(age) [10.744908100409873, 10.808982898265619, 10.859335221424754, 10.895581458515855, 10.91744238026025, 10.924748324734335, 10.91744238026025, 10.895581458515855, 10.859335221424754, 10.808982898265619, 10.744908100409873, Inf]
105105

106+
@test resamplingprobability(invweight(lat, lon, age)) [0.17709468716990867, 0.16997032242046156, 0.1667541264064975, 0.16513990970091127, 0.16433863731114293, 0.1640825308282905, 0.16430537469842987, 0.16507611788372856, 0.16666666666666666, 0.16987285789479709, 0.17699668840192856, 0.0]
107+
@test resamplingprobability(invweight_location(lat, lon)) [0.2157181923483138, 0.1795609083825818, 0.16704894086177344, 0.1616579035210186, 0.15921356093554181, 0.158453529370978, 0.1590774311384848, 0.16139152082816496, 0.16666666666666666, 0.17908729309480173, 0.21508527491483678, 0.0]
108+
@test resamplingprobability(invweight_age(age)) [0.16814313324769609, 0.1673131616109112, 0.16666666666666666, 0.16620436987522427, 0.16592678603901323, 0.16583422381194865, 0.16592678603901323, 0.16620436987522427, 0.16666666666666666, 0.1673131616109112, 0.16814313324769609, 0.0]
106109

107110
## --- bin_bsr
108111

0 commit comments

Comments
 (0)