Commit f2532696 authored by Victor's avatar Victor
Browse files

updated metrics so that pairwise history distance can take any distance metrics

parent 8e4a55fe
......@@ -22,7 +22,7 @@ module ABMEv
export H_discrete,findclusters,var,covgeo,hamming,get_beta_div, get_alpha_div,
get_dist_hist,get_pairwise_average_isolation,
get_local_pairwise_average_isolation,
isnotequal_dist,squared_dist
isnotequal_dist,squared_dist,get_xhist_mat
export update_afterbirth_std!,update_afterdeath_std!
export generalised_gaussian,gaussian,ma,geomsmooth,arithsmooth,eth_grad_std,
DiversityFunction,geomsmooth2D,arithsmooth2D,interpolate_df,groupby
......
......@@ -125,7 +125,7 @@ end
"""
function get_xhist_mat(world,trait=1,time = 0)
returns `xhist,ttot`, where `xhist` is a matrix with dimension `lenght(world)` x `length(thist)`,
returns `xhist,ttot`, where `xhist` is a matrix with dimension `lenght(world)` x `length(thist)+1`,
which consists of geographical history of ancestors at every time step.
If `time` is specified and is higher that the highest time step in world,
then the last column of xhist corresponds to actual position of agents
......@@ -136,21 +136,31 @@ function get_xhist_mat(world,trait=1,time = 0)
ttot = sort!(unique(thist))
xhist = zeros(length(world),length(ttot))
for (i,a) in enumerate(world)
_thist = get_thist(a)
_thist = get_thist(a)
# Here we check that there is no redundancy of thist because of casting errors
# In the future, we should remove this check, as the type of time has been set to Float64
# Hence there should be no more problems of this type
ttrue = _thist[2:end] .- _thist[1:end-1] .> 0
if count(ttrue) < length(_thist) - 1
_tt = vcat(ttrue,true)
_thist = _thist[_tt] # we drop the position that was occupied for dt = .0
_xhist = get_xhist(a,trait)[_tt]
else
_xhist = get_xhist(a,trait)
k = 0
_l = length(_thist)
for j in 1:(_l - 1)
xhist[i,j+k] = _xhist[j]
while _thist[j+1] > ttot[j+k+1]
k+=1
xhist[i,j+k] = _xhist[j]
if j+k == length(ttot)
break
end
end
end
xhist[i,_l+k:end] .= _xhist[end]
end
k = 0
_l = length(_thist)
for j in 1:(_l - 1)
xhist[i,j+k] = _xhist[j]
while _thist[j+1] > ttot[j+k+1]
k+=1
xhist[i,j+k] = _xhist[j]
if j+k == length(ttot)
break
end
end
end
xhist[i,_l+k:end] .= _xhist[end]
end
tt = 0
time > ttot[end] ? ttot = vcat(ttot,time) : tt = 1
......@@ -167,39 +177,8 @@ Returns the integral of the distance `dist` through time of `trait` between `a1.
```
"""
function get_dist_hist(a1,a2,dist,trait=1,time = 0)
thist = vcat(get_thist(a1),get_thist(a2))
ttot = sort!(unique(thist))
xhist = zeros(2,length(ttot))
for (i,a) in enumerate([a1,a2])
_thist = get_thist(a)
# Here we check that there is no redundancy of thist because of casting errors
# In the future, we should remove this check, as the type of time has been set to Float64
# Hence there should be no more problems of this type
ttrue = _thist[2:end] .- _thist[1:end-1] .> 0
if count(ttrue) < length(_thist) - 1
_tt = vcat(ttrue,true)
_thist = _thist[_tt] # we drop the position that was occupied for dt = .0
_xhist = get_xhist(a,trait)[_tt]
else
_xhist = get_xhist(a,trait)
end
k = 0
_l = length(_thist)
for j in 1:(_l - 1)
xhist[i,j+k] = _xhist[j]
while _thist[j+1] > ttot[j+k+1]
k+=1
xhist[i,j+k] = _xhist[j]
if j+k == length(ttot)
break
end
end
end
xhist[i,_l+k:end] .= _xhist[end]
end
tt = 0
time > ttot[end] ? ttot = vcat(ttot,time) : tt = 1
return sum(dist.(xhist[1,1:end-tt],xhist[2,1:end-tt])[:] .* (ttot[2:end] .- ttot[1:end-1]))
xhist,ttot = get_xhist_mat([a1,a2],trait,time)
return sum(dist.(xhist[1,:],xhist[2,:]) .* (ttot[2:end] .- ttot[1:end-1]))
end
......
......@@ -33,10 +33,10 @@ p = Dict("mu" => [1. 1.],"D" => [0. 0.], "nodes" =>10 )
β = get_beta_div(agentsd,1.0,2);
@test abs(β) < Inf
end
@testset "Isolation by history hamming distance" begin
@testset "Isolation by history - hamming distance" begin
a1 = Agent{StdAgent,Float64}([1 2 3],[0,1,4],0.,1.)
a2 = Agent{StdAgent,Float64}([1 10 3 10],[0,3,4,5],0.,1.)
@test get_hamming_dist_hist(a1,a2,1) 3.0
@test get_dist_hist(a1,a2,isnotequal_dist,1) 3.0
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment