ABMEv_metrics.jl 3.71 KB
Newer Older
 Victor Boussange committed Jan 17, 2020 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 ``````import StatsBase:Histogram,fit,step,normalize d1(x,y) = norm(x .- y,1) d2(x,y) = norm(x .- y,2) hamming(k::Array, l::Array) = sum(k .!= l) """ H_discrete(s) Interconnectedness measure as in Nordbotten 2018 for discrete setup """ function H_discrete(s) N = length(s) H = 0 for x in s for y in s H += d2(x,y) end end return H / N^2 end """ findclusters(v::Vector,allextrema =true) Returns a tuple with the cluster mean and its associated weight # Arguments """ function findclusters(v::Vector,allextrema =true) # this function fits a histogram to some distribution and extracts its local maxima h = fit(Histogram,v) s = step(h.edges...) x = normalize(h.weights,1) dx = x[2:end] .- x[1:end-1] if allextrema sdx = dx[2:end] .* dx[1:end-1] idx = findall(i -> i < 0, sdx) else idx = findall(i -> dx[i] > 0 && dx[i+1] < 0, 1:length(dx)) end return collect(h.edges...)[idx .+ 1] .+ s/2, x[idx .+ 1] end import Statistics.var `````` Victor committed May 07, 2020 45 ``````# TODO: rename this to gamma diversity `````` Victor Boussange committed Jan 17, 2020 46 47 48 49 50 51 52 53 ``````""" var(world::Array{Agent};trait=:) If trait = 0, returns the variance of the geotrait, knowing that by default it is associated with position trait 1. If trait > 0, returns the covariance matrix, with first row geotrait and second row trait. # Arguments """ `````` Victor committed Apr 03, 2020 54 55 ``````function var(world::Array{U,1};trait=1) where U <: Union{Missing,Agent{T}} where T world = collect(skipmissing(world)) `````` Victor committed May 07, 2020 56 `````` xarray = get_x(world,trait) `````` Victor Boussange committed Jan 17, 2020 57 58 59 60 61 62 63 64 65 66 `````` return var(xarray,dims=1) end """ covgeo(world::Array{Agent,1},trait = 0) If trait = 0, returns the variance of the geotrait, knowing that by default it is associated with position trait 1. If trait > 0, returns the covariance matrix, with first row geotrait and second row trait # Arguments """ `````` Victor committed May 07, 2020 67 ``````function covgeo(world::Array{U,1},t::Number,trait = 0) where U <: Union{Missing,Agent} `````` Victor committed Apr 03, 2020 68 `````` world = collect(skipmissing(world)) `````` Victor committed May 08, 2020 69 `````` xarray = Float64.(get_geo.(world,t)) `````` Victor Boussange committed Jan 17, 2020 70 `````` if trait > 0 `````` Victor committed May 08, 2020 71 `````` xstd = reshape(Float64.(get_x.(world,trait)),size(world,1),size(world,2)) `````` Victor Boussange committed Jan 17, 2020 72 73 74 75 76 77 78 79 80 81 82 `````` xarray = hcat(xarray,xstd) end return cov(xarray) end """ function hamming(world::Array{Agent,1}) Returns a matrix H where H_ij = hamming(a_i,a_j). The hamming distance is taken through the whole history and functional space of the agents. """ `````` Victor committed Apr 14, 2020 83 ``````function hamming(world::Array{Agent,1}) where T <: Int `````` Victor Boussange committed Jan 17, 2020 84 85 86 87 88 89 90 91 92 `````` N = size(world,1) H = zeros(N,N) for (i,a) in enumerate(world) for (j,b) in enumerate(world) H[i,j] = hamming(get_xhist(a),get_xhist(b)) end end return H end `````` Victor committed May 07, 2020 93 ``````""" `````` Victor committed May 08, 2020 94 `````` get_alpha_div(world::Array{U,1},t::Number,trait=1) where U <: Union{Missing,Agent} `````` Victor committed May 07, 2020 95 ``````Mean of the local variance of `trait` per patch `````` Victor committed May 07, 2020 96 97 ``````# Arguments """ `````` Victor committed May 08, 2020 98 99 ``````function get_alpha_div(world::Array{U,1},t::Number,trait=1) where U <: Union{Missing,Agent} _xall_df = world2df(world,t,true) `````` Victor committed May 07, 2020 100 `````` xall_per_patch = groupby(_xall_df, :x1,sort=true) `````` Victor committed May 07, 2020 101 `````` if trait == 0 `````` Victor committed May 08, 2020 102 103 `````` # need to convert to Float64, otherwise infinite variance return mean([var(Float64.(xp.g)) for xp in xall_per_patch]) `````` Victor committed May 07, 2020 104 `````` else `````` Victor committed May 08, 2020 105 `````` return mean([var(Float64.(xp[:,trait+1])) for xp in xall_per_patch]) `````` Victor committed May 07, 2020 106 `````` end `````` Victor committed May 07, 2020 107 108 109 ``````end """ `````` Victor committed May 08, 2020 110 `````` get_beta_div(world::Array{U,1},t::Number,trait=1) where U <: Union{Missing,Agent} `````` Victor committed May 07, 2020 111 ``````Variance of the mean of `trait` per patch `````` Victor committed May 07, 2020 112 113 ``````# Arguments """ `````` Victor committed May 08, 2020 114 115 ``````function get_beta_div(world::Array{U,1},t::Number,trait=1) where U <: Union{Missing,Agent} _xall_df = world2df(world,t,true) `````` Victor committed May 07, 2020 116 `````` xall_per_patch = groupby(_xall_df, :x1,sort=true) `````` Victor committed May 07, 2020 117 `````` if trait == 0 `````` Victor committed May 08, 2020 118 119 `````` # need to convert to Float64, otherwise infinite variance sbar_i = [mean(Float64.(xp.g)) for xp in xall_per_patch] `````` Victor committed May 07, 2020 120 `````` else `````` Victor committed May 08, 2020 121 `````` sbar_i = [mean(Float64.(xp[:,trait+1])) for xp in xall_per_patch] `````` Victor committed May 07, 2020 122 123 `````` end return var(sbar_i) `````` Victor committed May 07, 2020 124 ``end``