-
Notifications
You must be signed in to change notification settings - Fork 9
/
EffectiveNumbers.jl
165 lines (131 loc) · 4.98 KB
/
EffectiveNumbers.jl
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# SPDX-License-Identifier: BSD-2-Clause
"""
powermean
# Calculates the weighted powermean of a series of numbers
Calculates *order*th power mean of *values*, weighted by
*weights*. By default, *weights* are equal and *order*
is 1, so this is just the arithmetic mean.
# Arguments:
- `values`: values for which to calculate mean
- `order[s]`: order[s] of power mean
- `weights`: weights of elements, normalised to 1 inside function
# Returns:
- weighted power mean(s)
"""
function powermean end
function powermean(values::V1, order::R = 1,
weights::V2 = fill!(similar(values), 1)) where
{R <: Real, FP <: AbstractFloat,
V1 <: AbstractVector{FP}, V2 <: AbstractVector{FP}}
length(values) == length(weights) ||
throw(DimensionMismatch("powermean: Weight and value vectors must be the same length"))
# Check whether all weights are zero in group.
# In that case we want to return a NaN
if iszero(weights)
return convert(FP, NaN)
end
if isinf(order)
if order > 0 # +Inf -> Maximum
s = zero(FP)
for i in eachindex(values, weights)
@inbounds if (weights[i] > eps(FP)) & (values[i] > s)
s = values[i]
end
end
return s
else # -Inf -> Minimum
s = convert(FP, Inf)
for i in eachindex(values, weights)
@inbounds if (weights[i] > eps(FP)) & (values[i] < s)
s = values[i]
end
end
return s
end
else
if order ≈ zero(order)
s = zero(FP)
for i in eachindex(values, weights)
@inbounds if weights[i] > eps(FP)
s += weights[i] * log(values[i])
end
end
return exp(s / sum(weights))
else
s = zero(FP)
for i in eachindex(values, weights)
@inbounds if weights[i] > eps(FP)
s += weights[i] * values[i]^order
end
end
return (s / sum(weights))^(one(FP) / order)
end
end
end
# This is the next most common case - a vector of orders
function powermean(values::V1,
orders::VR,
weights::V2 = fill!(similar(values), 1.0)) where
{R <: Real, VR <: AbstractVector{R},
FP <: AbstractFloat, V1 <: AbstractVector{FP},
V2 <: AbstractVector{FP}}
return map(order -> powermean(values, order, weights), orders)
end
# This is the next most simple case - matrices with subcommunities, and an order or orders
function powermean(values::M1, orders,
weights::M2 = fill!(similar(values), 1)) where
{FP <: AbstractFloat, M1 <: AbstractMatrix{FP},
M2 <: AbstractMatrix{FP}}
size(values) == size(weights) ||
throw(DimensionMismatch("powermean: Weight and value matrixes " *
"must be the same size"))
@views map(col -> powermean(values[:, col], orders,
weights[:, col]), 1:size(values, 2))
end
"""
qD
Calculates Hill / naive-similarity diversity of order(s) *qs* of a
population with given relative proportions.
# Arguments:
- `proportions`: relative proportions of different types in population
- `qs`: single number or vector of orders of diversity measurement
# Returns:
- Diversity of order qs (single number or vector of diversities)
"""
function qD end
function qD(asm::AbstractAssemblage, qs)
hassimilarity(asm) &&
error("function cannot run with $(typeof(gettypes(asm))) types as contains similarity")
countsubcommunities(asm) == 1 ||
throw(DimensionMismatch("Can only calculate diversity of a single community"))
return powermean(getabundance(asm), qs .- 1, getabundance(asm))[1] .^ -1
end
function qD(proportions::AbstractVector{<:Real}, qs)
return qD(Metacommunity(proportions), qs)
end
"""
qDZ
Calculates Leinster-Cobbold / similarity-sensitive diversity of >= 1
order(s) *qs* of a population with given relative *proportions*, and
similarity matrix *Z*.
# Arguments:
- `proportions`: relative proportions of different types in a population
- `qs`: single number or vector of orders of diversity measurement
- `Z`: similarity matrix
# Returns:
- Diversity of order qs (single number or vector of diversities)
"""
function qDZ end
function qDZ(asm::AbstractAssemblage, qs)
countsubcommunities(asm) == 1 ||
throw(DimensionMismatch("Can only calculate diversity of a single community"))
return powermean(getordinariness!(asm), qs .- 1, getabundance(asm))[1] .^ -1
end
function qDZ(proportions::AbstractVector{<:Real}, qs,
sim::AbstractTypes = UniqueTypes(size(proportions, 1)))
return qDZ(Metacommunity(proportions, sim), qs)
end
function qDZ(proportions::AbstractVector{<:Real}, qs,
Z::AbstractMatrix{<:AbstractFloat})
return qDZ(Metacommunity(proportions, GeneralTypes(Z)), qs)
end