-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding a life function, untested * Should be better * up * up * up * Patching the tail of the life table * up comment * oops * increment version number * add compat * remove useless functions * is this enough? * proofread * add test for all rate tables * Handle exceptions..... * docstring correction --------- Co-authored-by: rimhajal <[email protected]>
- Loading branch information
Showing
6 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
name = "RateTables" | ||
uuid = "d40fb65e-c2ee-4113-9e14-cb96ca0acb32" | ||
authors = ["Oskar Laverny <[email protected]> and contributors"] | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
|
||
[deps] | ||
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
|
||
[compat] | ||
Aqua = "0.8" | ||
CSV = "0.10" | ||
DataFrames = "1" | ||
Distributions = "0.25" | ||
RData = "1" | ||
Test = "1.6" | ||
julia = "1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
Life(brt::BasicRateTable,a,d) | ||
This function returns a random variable that correspond to an extracted Life from the `BasicRateTable` at age `a` and date `d`. | ||
This works by checking if the individual is closer to the oldest age than the last year in the ratetable, calculating at each step the time difference and the hazard values. For the younger individuals, we assume they go through the last column at the end no matter what age they are. | ||
""" | ||
struct Life<:Distributions.ContinuousUnivariateDistribution | ||
∂t::Vector{Float64} | ||
λ::Vector{Float64} | ||
function Life(brt::BasicRateTable,a,d) | ||
i, j = dty(a, brt.extrema_age...), dty(d, brt.extrema_year...) | ||
|
||
rem_a = RT_DAYS_IN_YEAR - rem(a, RT_DAYS_IN_YEAR) | ||
rem_d = RT_DAYS_IN_YEAR - rem(d, RT_DAYS_IN_YEAR) | ||
|
||
# Do we go right or below first ? | ||
# Happy birthday (below) or Happy new year (right) first ? | ||
k,l = rem_a < rem_d ? (i+1,j) : (i,j+1) | ||
|
||
# Cap obtained values to avoid going out of bounds: | ||
K,L = size(brt.values) | ||
k = min(k, K) | ||
l = min(l, L) | ||
|
||
# lengths and hazards in the first two cells: | ||
∂t = [min(rem_a,rem_d), abs(rem_a - rem_d)] | ||
λ = [brt.values[i,j], brt.values[k,l]] | ||
|
||
while (k < K) && (l < L) | ||
i,j,k,l = i+1, j+1, k+1, l+1 | ||
push!(∂t, RT_DAYS_IN_YEAR - ∂t[2], ∂t[2]) | ||
push!(λ, brt.values[i,j], brt.values[k,l]) | ||
end | ||
if (l >= L) # exit on the right => still young ! | ||
# A good approximation is to go through the last column. | ||
for m in (k+1):K | ||
push!(∂t, RT_DAYS_IN_YEAR) | ||
push!(λ, brt.values[m,end]) | ||
end | ||
end | ||
return new(∂t,λ) | ||
end | ||
end | ||
Distributions.@distr_support Life 0.0 Inf | ||
function Distributions.expectation(L::Life) | ||
S = 1.0 | ||
E = 0.0 | ||
for j in eachindex(L.∂t) | ||
if L.λ[j] > 0 | ||
S_inc = exp(-L.λ[j]*L.∂t[j]) | ||
E += S * (1 - S_inc) / L.λ[j] | ||
S *= S_inc | ||
else | ||
E += S * L.∂t[j] | ||
end | ||
end | ||
# This reminder assumes a exponential life time afer the maximum age. | ||
R = ifelse(L.λ[end] == 0.0, 0.0, S / L.λ[end]) | ||
return E + R | ||
end | ||
""" | ||
cumhazard | ||
Assuming the last box is infinitely wide, we calculate the cumulative hazard from ∂t and λ taken from the `Life` function. | ||
""" | ||
function cumhazard(L::Life, t::Real) | ||
Λ = 0.0 | ||
u = 0.0 | ||
for j in eachindex(L.∂t) | ||
u += L.∂t[j] | ||
if t > u | ||
Λ += L.λ[j]*L.∂t[j] | ||
else | ||
Λ += L.λ[j]*(t-(u-L.∂t[j])) | ||
return Λ | ||
end | ||
end | ||
# We consider that the last box is in fact infinitely wide (exponential tail) | ||
return Λ + (t-u)*L.λ[end] | ||
end | ||
Distributions.ccdf(L::Life, t::Real) = exp(-cumhazard(L::Life,t)) | ||
function Distributions.quantile(L::Life, p::Real) | ||
Λ_target = -log(1-p) | ||
Λ = 0.0 | ||
u = 0.0 | ||
for j in eachindex(L.∂t) | ||
Λ += L.λ[j]*L.∂t[j] | ||
u += L.∂t[j] | ||
if Λ_target < Λ | ||
u -= (Λ - Λ_target) / L.λ[j] | ||
return u | ||
end | ||
end | ||
return u | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6709d48
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
6709d48
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/106960
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: