metrics.jl 4.18 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
g = SimpleGraph(1000,4000)
Victor's avatar
Victor committed
9
myspace4 = (RealSpace{1,Float64}(),GraphSpace(g),)
10

11
K0 = 1000; σ = 1e-1
Victor's avatar
Victor committed
12
13
14
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]
Victor's avatar
Victor committed
15
a4 = [Agent(myspace4, (1.,rand(Int64(1):Int64(1000)),),ancestors=true) for i in 1:K0]
16

Victor's avatar
Victor committed
17
18
19
20
21
22
23
24
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
Victor's avatar
Victor committed
25
D = (0.,0.)
26
p4 = Dict{String,Any}();@pack! p4 = D,mu,NMax
Victor's avatar
Victor committed
27
28
29
30

w1 = World(a1,myspace1,p1)
w2 = World(a2,myspace2,p2)
w3 = World(a3,myspace3,p3)
31
w4 = World(a4,myspace4,p4)
Victor's avatar
Victor committed
32

33
34
## testing variance
@testset "Testing metrics" begin
35
    @testset "var" begin
36
37
        @test first(var(w1))  (σ).^2 atol=0.001
        @test first(var(w2,trait=2))  (σ).^2 atol=0.001
38
        @test first(var(w4,trait=1)) < Inf
Victor's avatar
Victor committed
39
40
        @test first(var(w4,trait=2)) < Inf

41

42
    end
43
44
45

    ## testing covgeo
    @testset "covgeo" begin
46
        # @test covgeo(w1) ≈ (σ).^2 atol=0.001
47
         for i in covgeo(w1,1)
48
             # @test i ≈ (σ).^2 atol=0.001
49
50
51
         end
     end
     # not sure this is the bestway of testing
52
     # there is a problem here
53
     @testset "covgeo2d" begin
54
         cmat = covgeo(w2,2);
55
         smat = [σ^2 0; 0 σ^2]
56
         # @test cmat ≈ smat atol=0.01
57
      end
58
      @testset "Alpha diversity" begin
Victor's avatar
Victor committed
59
          α = get_alpha_div(w3,2);
60
          @test abs(α) < Inf
61
62
          α = get_alpha_div(w4,1);
          @test abs(α) < Inf
63
64
       end
       @testset "Beta diversity" begin
Victor's avatar
Victor committed
65
           β = get_beta_div(w3,2);
66
           @test abs(β) < Inf
67
68
           β = get_beta_div(w4,1);
           @test abs(β) < Inf
69
       end
70
       @testset "Isolation by history - hamming distance" begin
Victor's avatar
Victor committed
71
72
           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
73
           @test get_dist_hist(a1,a2,(x,y)->y!=x,1)  3.0
74
       end
75
76
77
end

@testset "Geotrait computation" begin
Victor's avatar
Victor committed
78
    a = Agent(myspace3, (Int16.(1), randn() ),ancestors=true); increment_x!(a,myspace3,p3,2.0);
79
    @test get_geo(a,2.0)  2.0
80
81
82
end

# TODO needs to test hamming distance
83

Victor's avatar
Victor committed
84
## testing for real space of dimension d>1
85
86
multispace = (DiscreteSegment{Int8}(1,9),RealSpace{3,Float64}(),)
K0 = 10000;
Victor's avatar
Victor committed
87
multia = [Agent(multispace, (rand(Int8(1):Int8(10)),tuple( randn(3) ...),),ancestors=true) for i in 1:K0]
88
89
90
91
92
93
94
95
96
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)
Victor's avatar
Victor committed
97
@test prod([isapprox(i,0.,atol=1e-1) for i in mean(multiw,trait = 2)])
98
99
@test isapprox(get_alpha_div(multiw,2),1.,atol = 1e-2)
@test isapprox(get_beta_div(multiw,2),0.,atol = 1e-2)
Victor's avatar
Victor committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

## some test to write metrics functions
# world = multiw;trait=2
# g = groupby(a->a[1],agents(world))
# v = [var(World(subw,space(world),parameters(world)),trait=trait) for subw in values(g)]
# h = vcat(v...)
# mean(mean(h,dims=1))
# mean(h)
# mean([mean(var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false)) for subw in values(g)])
#
# [var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false) for subw in values(g)]
# subw = collect(values(g))[1]
# var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false)
# var(World(subw,space(world),parameters(world)),trait=trait)
# mean(World(subw,space(world),parameters(world)),trait=1)
#
# m = [mean(World(subw,space(world),parameters(world)),trait=trait) for subw in values(g)]
# h=vcat(m...)
# var(h,dims=1,corrected=false)