metrics.jl 2.81 KB
Newer Older
Victor's avatar
Victor committed
1
2
3
4
using LightGraphs
using Test
using Revise,ABMEv
using UnPack
5
myspace1 = (RealSpace{1,Float64}(),)
Victor's avatar
Victor committed
6
myspace2 = (RealSpace{1,Float64}(),RealSpace{1,Float64}())
7
myspace3 = (DiscreteSegment(Int16(1),Int16(10)),RealSpace{1,Float64}())
8
K0 = 1000; σ = 1e-1
Victor's avatar
Victor committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
a1 = [Agent(myspace1, (σ,)  .* randn() .- .5,ancestors=true) for i in 1:K0]
a2 = [Agent(myspace2,tuple((σ, σ)  .* randn(2) .- .5...),ancestors=true) for i in 1:K0]
a3 = [Agent(myspace3, (rand(Int16.(1:10)), 1e-1* randn() + 5.5 ),ancestors=true) for i in 1:K0]
D = (1.,);
mu = [1.,1.]
NMax = 1000
p1 = Dict{String,Any}();@pack! p1 = D,mu,NMax
D = (1.,1.);
p2 = Dict{String,Any}();@pack! p2 = D,mu,NMax
D = (Int16(0.),0.)
p3 = Dict{String,Any}();@pack! p3 = D,mu,NMax

w1 = World(a1,myspace1,p1)
w2 = World(a2,myspace2,p2)
w3 = World(a3,myspace3,p3)

25
26
## testing variance
@testset "Testing metrics" begin
27
    @testset "var" begin
28
29
        @test first(var(w1))  (σ).^2 atol=0.001
        @test first(var(w2,trait=2))  (σ).^2 atol=0.001
30
    end
31
32
33

    ## testing covgeo
    @testset "covgeo" begin
34
        # @test covgeo(w1) ≈ (σ).^2 atol=0.001
35
         for i in covgeo(w1,1)
36
             # @test i ≈ (σ).^2 atol=0.001
37
38
39
         end
     end
     # not sure this is the bestway of testing
40
     # there is a problem here
41
     @testset "covgeo2d" begin
42
         cmat = covgeo(w2,2);
43
         smat = [σ^2 0; 0 σ^2]
44
         # @test cmat ≈ smat atol=0.01
45
      end
46
      @testset "Alpha diversity" begin
Victor's avatar
Victor committed
47
          α = get_alpha_div(w3,2);
48
49
50
          @test abs(α) < Inf
       end
       @testset "Beta diversity" begin
Victor's avatar
Victor committed
51
           β = get_beta_div(w3,2);
52
53
           @test abs(β) < Inf
       end
54
       @testset "Isolation by history - hamming distance" begin
Victor's avatar
Victor committed
55
56
           a1 = Agent((DiscreteSegment(1,10),),[(1,),(2,),(3,)],[0,1,4],ancestors=true)
           a2 = Agent((DiscreteSegment(1,10),),[(1,),(10,),(3,),(10,)],[0,3,4,5],ancestors=true)
Victor's avatar
Victor committed
57
           @test get_dist_hist(a1,a2,(x,y)->y!=x,1)  3.0
58
       end
59
60
61
end

@testset "Geotrait computation" begin
Victor's avatar
Victor committed
62
    a = Agent(myspace3, (Int16.(1), randn() ),ancestors=true); increment_x!(a,myspace3,p3,2.0);
63
    @test get_geo(a,2.0)  2.0
64
65
66
end

# TODO needs to test hamming distance
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

# testing for real space of dimension d>1
multispace = (DiscreteSegment{Int8}(1,9),RealSpace{3,Float64}(),)
K0 = 10000;
multia = [Agent(multispace, (rand(Int8(1):Int8(10)),tuple(randn(3)...),),ancestors=true) for i in 1:K0]
D = (Int8(1),tuple(fill(1.,3)...),)
mu = [1]
NMax = 1000
multip = Dict{String,Any}();@pack! multip = D,mu,NMax
multiw = World(multia,multispace,multip)
a = multia[10]
inc = get_inc(1,D[1],multispace[2])
@test inc[1] != inc[2] # checking that we have independent increments in each direction
@test isapprox(mean(var(multiw,trait = 2)),1.,atol=1e-2)
@test isapprox(get_alpha_div(multiw,2),1.,atol = 1e-2)
@test isapprox(get_beta_div(multiw,2),0.,atol = 1e-2)