The idea can also be applied to year vectors, but given the range of values they have, I think it is preferable to carry out translations to bring all the values between 1 and max(yera)-min(year).
Another thing to do in preparation, which perhaps could help, is to work on integers for data that are integers (since they then have to be used as indexes).
As soon as I have time Iβll try to draft a more complete script, if itβs not clear what Iβm trying to say.
Integerify
table = (
ages = Int.(trunc.(table.ages .* 365.241)),
years = Int.(trunc.(table.years .* 365.241)),
sexes = table.sexes,
values = table.values
)
data=(T=Int.(data.T),age=Int.(data.age),sex=data.sex,status=data.status,year=Int.(data.year)
)
function Ξ(data, table, prec=1)
# Initialize vectors:
grid = unique(sort([(1:prec:(maximum(data.T)+1)); data.T; [maximum(data.T)+1]]))
num_excess = zeros(grid[end])
num_pop = zeros(grid[end])
num_variance = zeros(grid[end])
den = zeros(grid[end])
d=Int(maximum(data.age)+maximum(grid))
idxa=fill(lastindex(table.ages),d)
s=1
for i in 1:table.ages[end]-1
if i >= table.ages[s+1]
s+=1
end
idxa[i]=s
end
#table.years.-table.years[1]
#data.year.-table.years[1]
# Loop over individuals
for i in 1:length(data.age)
Tα΅’ = searchsortedlast(grid, data.T[i]) # index of the time of event (death or censored) in the grid
wβ = 1.0
sΞβ = 0.0
for j in 1:Tα΅’
#Ξ»β = Ξ»(table, data.age[i] + grid[j], data.year[i] + grid[j], data.sex[i])
a= idxa[data.age[i] + grid[j]]
y=searchsortedlast(table.years, data.year[i] + grid[j])
g = data.sex[i]==:male ? 1 : 2
Ξ»β = table.values[a,y,g]
Ξβ = Ξ»β * (grid[j+1]-grid[j]) # Ξ»β * βt
sΞβ += Ξβ
wβ = exp(sΞβ)
num_pop[j] += Ξβ * wβ
den[j] += wβ
end
num_excess[Tα΅’] += wβ * data.status[i]
num_variance[Tα΅’] += wβ^2 * data.status[i]
end
βΞβ = (num_excess - num_pop) ./ den
βΟβ = num_variance ./ den.^2
return grid, βΞβ, βΟβ
end
julia> using BenchmarkTools
julia> @btime Ξ(data, table)
191.024 ms (59 allocations: 1.40 MiB)
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 β¦ 8140, 8141, 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149], [0.002894868611732328, 0.0018966845282910414, 0.003079845040555203, 0.003427879530253135, 0.002763112499157984, 0.002771085990264049, 0.0015876394324403375, 0.004837336707644467, 0.003830746874088555, 0.0024714042541990793 β¦ -0.00045476929384445137, -0.00045480172457826204, -0.00045483414608312107, -0.000542010162781309, -0.000542010162781309, -0.000542010162781309, -0.000542010162781309, -0.000542010162781309, -0.000542010162781309, NaN], [5.049982192832294e-7, 3.388282760556044e-7, 5.387125920341493e-7, 5.990895522239727e-7, 4.886001588297959e-7, 4.912342704415929e-7, 2.9113476790888077e-7, 8.468796858477259e-7, 6.779046685495679e-7, 4.4688733619822384e-7 β¦ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, NaN])
adoro questo tipo di grafici!!!
julia> lineplot(grid,cumprod(1 .- βΞβ))
ββββββββββββββββββββββββββββββββββββββββββ
1.2 ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β£Έβ β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β£Ώβ β β β β β β β β β β β β β β β β β β
ββ‘β β β β β β β β β β β β β β β β β β β β β£Ώβ β β β β β β β β β β β β β β β β β β
ββ‘β β β β β β β β β β β β β β β β β β β β’Έβ’Ήβ β β β β β β β β β β β β β β β β β β
ββ‘β β β β β β β β β β β β β β β β β β β β’Έβ’Έβ β β β β β β β β β β β β β β β β β β
ββ’³β β β β β β β β β β β β β β β β β β β β’Έβ’Έβ β β β β β β β β β β β β β β β β β β
ββ β‘β β β β β β β β β β β β β β β β β β β’Έβ’Έβ β β β β β β β β β β β β β β β β β β
ββ β’³β β β β β β β β β β β β β β β β β β β‘β’Έβ β’°β’»β β β β β β β β β β β β β β β β
ββ β β£β β β β β β β β β β β β β β β β β β‘β’Έβ β‘β’Έβ β β β β β β β β β β β β β β β
ββ β β β’¦β‘β β β β β β β β β β β β β β β β‘β’Έβ‘Όβ β’Έβ β β β β β β β β β β β β β β β
ββ β β β β ³β£β β β β β β β β β β β β β β’°β β’Έβ‘β β’Έβ β β β β β β β β β β β β β β β
ββ β β β β β β β ¦β’€β£β£β’β‘β β β β£β£ β‘΄β β β β β β’Έβ£β£€β β’β£β£β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β ⠹⑦⠢⠢⒲⣠β β β β β
0.3 ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββββββββββββββββββββββββββββββββββββββββββ
β 0β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β 9 000β